Config.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\admin\controller\commission;
  3. use app\common\controller\Backend;
  4. use app\common\Service\ShopConfigService;
  5. use app\common\Enum\CommissionConfigEnum;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 分销配置管理
  10. *
  11. * @icon fa fa-cogs
  12. */
  13. class Config extends Backend
  14. {
  15. /**
  16. * Config模型对象
  17. *
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. }
  24. /**
  25. * 查看分销配置
  26. */
  27. public function index()
  28. {
  29. // 获取分销配置
  30. $commissionConfig = ShopConfigService::getConfigs('shop.commission');
  31. // 合并默认配置
  32. $defaultConfig = CommissionConfigEnum::getDefaultConfig();
  33. $commissionConfig = array_merge($defaultConfig, $commissionConfig ?: []);
  34. // 传递枚举数据给视图
  35. $this->view->assign([
  36. 'commissionConfig' => $commissionConfig,
  37. 'levelList' => CommissionConfigEnum::$levelList,
  38. 'switchList' => CommissionConfigEnum::$switchList,
  39. 'checkList' => CommissionConfigEnum::$checkList,
  40. 'allowList' => CommissionConfigEnum::$allowList,
  41. 'upgradeCheckList' => CommissionConfigEnum::$upgradeCheckList,
  42. 'needFormList' => CommissionConfigEnum::$needFormList,
  43. 'showProtocolList' => CommissionConfigEnum::$showProtocolList,
  44. 'inviteLockList' => CommissionConfigEnum::$inviteLockList,
  45. 'becomeAgentList' => CommissionConfigEnum::$becomeAgentList,
  46. 'rewardTypeList' => CommissionConfigEnum::$rewardTypeList,
  47. 'rewardEventList' => CommissionConfigEnum::$rewardEventList,
  48. 'refundCommissionRewardList' => CommissionConfigEnum::$refundCommissionRewardList,
  49. 'refundCommissionOrderList' => CommissionConfigEnum::$refundCommissionOrderList
  50. ]);
  51. return $this->view->fetch();
  52. }
  53. /**
  54. * 编辑配置
  55. */
  56. public function edit($ids = null)
  57. {
  58. if ($this->request->isPost()) {
  59. $this->token();
  60. $params = $this->request->post("row/a", [], 'trim');
  61. if ($params) {
  62. try {
  63. // 验证配置参数
  64. $this->validateConfigParams($params);
  65. // 特殊处理某些配置项
  66. if (isset($params['become_agent']) && is_array($params['become_agent'])) {
  67. $params['become_agent'] = json_encode($params['become_agent'], JSON_UNESCAPED_UNICODE);
  68. }
  69. // 更新配置
  70. ShopConfigService::setConfigs('shop.commission', $params);
  71. $this->success('配置保存成功');
  72. } catch (ValidateException $e) {
  73. $this->error($e->getMessage());
  74. } catch (PDOException $e) {
  75. $this->error($e->getMessage());
  76. }
  77. //catch (\Exception $e) {
  78. // $this->error($e->getMessage());
  79. // }
  80. }
  81. $this->error(__('Parameter %s can not be empty', ''));
  82. }
  83. }
  84. /**
  85. * 验证配置参数
  86. */
  87. private function validateConfigParams($params)
  88. {
  89. // 验证分销层级
  90. if (isset($params['level']) && !CommissionConfigEnum::isValidLevel($params['level'])) {
  91. throw new ValidateException('分销层级参数无效');
  92. }
  93. // 验证开关类型参数
  94. $switchFields = ['self_buy', 'agent_check', 'upgrade_jump', 'upgrade_check', 'need_form', 'show_protocol'];
  95. foreach ($switchFields as $field) {
  96. if (isset($params[$field]) && !CommissionConfigEnum::isValidSwitch($params[$field])) {
  97. throw new ValidateException("{$field}参数无效");
  98. }
  99. }
  100. // 验证锁定下级条件
  101. if (isset($params['invite_lock']) && !CommissionConfigEnum::isValidInviteLock($params['invite_lock'])) {
  102. throw new ValidateException('锁定下级条件参数无效');
  103. }
  104. // 验证成为分销商条件
  105. if (isset($params['become_agent']) && is_array($params['become_agent'])) {
  106. if (isset($params['become_agent']['type']) && !CommissionConfigEnum::isValidBecomeAgent($params['become_agent']['type'])) {
  107. throw new ValidateException('成为分销商条件参数无效');
  108. }
  109. }
  110. // 验证商品结算方式
  111. if (isset($params['reward_type']) && !CommissionConfigEnum::isValidRewardType($params['reward_type'])) {
  112. throw new ValidateException('商品结算方式参数无效');
  113. }
  114. // 验证佣金结算方式
  115. if (isset($params['reward_event']) && !CommissionConfigEnum::isValidRewardEvent($params['reward_event'])) {
  116. throw new ValidateException('佣金结算方式参数无效');
  117. }
  118. // 验证退款扣除设置
  119. $refundFields = ['refund_commission_reward', 'refund_commission_order'];
  120. foreach ($refundFields as $field) {
  121. if (isset($params[$field]) && !CommissionConfigEnum::isValidSwitch($params[$field])) {
  122. throw new ValidateException("{$field}参数无效");
  123. }
  124. }
  125. }
  126. }