DistributionConfigLogic.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\logic\distribution;
  20. use app\common\logic\BaseLogic;
  21. use app\common\model\DistributionConfig;
  22. use app\common\enum\DistributionConfigEnum;
  23. use app\common\service\FileService;
  24. /**
  25. * 分销配置逻辑层
  26. * Class DistributionConfigLogic
  27. * @package app\adminapi\logic\distribution
  28. */
  29. class DistributionConfigLogic extends BaseLogic
  30. {
  31. /**
  32. * @notes 获取分销配置
  33. * @return array
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\DbException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. * @author Tab
  38. * @date 2021/7/22 9:38
  39. */
  40. public static function getConfig()
  41. {
  42. $dbConfig = DistributionConfig::column('value', 'key');
  43. $config['switch'] = $dbConfig['switch'] ?? DistributionConfigEnum::DEFAULT_SWITCH;
  44. $config['level'] = $dbConfig['level'] ?? DistributionConfigEnum::DEFAULT_LEVEL;
  45. $config['self'] = $dbConfig['self'] ?? DistributionConfigEnum::DEFAULT_SELF;
  46. $config['open'] = $dbConfig['open'] ?? DistributionConfigEnum::DEFAULT_OPEN;
  47. $config['apply_image'] = $dbConfig['apply_image'] ?? DistributionConfigEnum::DEFAULT_APPLY_IMAGE;
  48. $config['poster'] = $dbConfig['poster'] ?? DistributionConfigEnum::DEFAULT_POSTER;
  49. $config['protocol_show'] = $dbConfig['protocol_show'] ?? DistributionConfigEnum::DEFAULT_PROTOCOL_SHOW;
  50. $config['protocol_content'] = $dbConfig['protocol_content'] ?? DistributionConfigEnum::DEFAULT_PROTOCOL_CONTENT;
  51. $config['cal_method'] = $dbConfig['cal_method'] ?? DistributionConfigEnum::DEFAULT_CAL_METHOD;
  52. $config['settlement_timing'] = $dbConfig['settlement_timing'] ?? DistributionConfigEnum::DEFAULT_SETTLEMENT_TIMING;
  53. $config['settlement_time'] = $dbConfig['settlement_time'] ?? DistributionConfigEnum::DEFAULT_SETTLEMENT_TIME;
  54. $config['apply_image'] = FileService::getFileUrl($config['apply_image']);
  55. $config['poster'] = FileService::getFileUrl($config['poster']);
  56. // 商品详情页是否显示佣金 0-不显示 1-显示
  57. if (!isset($dbConfig['is_show_earnings'])) {
  58. $config['is_show_earnings'] = 1;
  59. } else if(empty((int)$dbConfig['is_show_earnings'])) {
  60. $config['is_show_earnings'] = 0;
  61. } else {
  62. $config['is_show_earnings'] = 1;
  63. }
  64. // 详情页佣金可见用户 0-全部用户 1-分销商
  65. if (!isset($dbConfig['show_earnings_scope'])) {
  66. $config['show_earnings_scope'] = 0;
  67. } else if(empty((int)$dbConfig['show_earnings_scope'])) {
  68. $config['show_earnings_scope'] = 0;
  69. } else {
  70. $config['show_earnings_scope'] = 1;
  71. }
  72. $config = self::stringToInteger($config);
  73. return $config;
  74. }
  75. /**
  76. * @notes 分销配置
  77. * @param $params
  78. * @author Tab
  79. * @date 2021/7/22 9:38
  80. */
  81. public static function setConfig($params)
  82. {
  83. $allowFields = ['switch', 'level', 'self','open', 'apply_image', 'protocol_show', 'protocol_content', 'binding_condition', 'cal_method','settlement_timing', 'settlement_time', 'is_show_earnings', 'show_earnings_scope'];
  84. foreach($params as $k => $v) {
  85. // 判断是否在允许修改的字段中
  86. if(!in_array($k, $allowFields, true)) {
  87. continue;
  88. }
  89. if ($k == 'apply_image' || $k == 'poster') {
  90. $v = empty($v) ? '' : FileService::setFileUrl($v);
  91. }
  92. $item = DistributionConfig::where('key', $k)->findOrEmpty();
  93. if($item->isEmpty()) {
  94. // 没有则创建
  95. DistributionConfig::create(['key' => $k, 'value' => $v]);
  96. continue;
  97. }
  98. // 有则更新
  99. $item->value = $v;
  100. $item->save();
  101. }
  102. }
  103. /**
  104. * @notes 字符串数字 转 纯数字
  105. */
  106. public static function stringToInteger($config) {
  107. foreach($config as $key => $value) {
  108. if (in_array($key, [ 'apply_image', 'poster', 'protocol_content' ])) {
  109. continue;
  110. }
  111. $config[$key] = (int)$value;
  112. }
  113. return $config;
  114. }
  115. }