123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace app\admin\controller\commission;
- use app\common\controller\Backend;
- use app\common\Service\ShopConfigService;
- use app\common\Enum\CommissionConfigEnum;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- /**
- * 分销配置管理
- *
- * @icon fa fa-cogs
- */
- class Config extends Backend
- {
- /**
- * Config模型对象
- *
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- }
- /**
- * 查看分销配置
- */
- public function index()
- {
- // 获取分销配置
- $commissionConfig = ShopConfigService::getConfigs('shop.commission');
-
- // 合并默认配置
- $defaultConfig = CommissionConfigEnum::getDefaultConfig();
- $commissionConfig = array_merge($defaultConfig, $commissionConfig ?: []);
-
- // 传递枚举数据给视图
- $this->view->assign([
- 'commissionConfig' => $commissionConfig,
- 'levelList' => CommissionConfigEnum::$levelList,
- 'switchList' => CommissionConfigEnum::$switchList,
- 'checkList' => CommissionConfigEnum::$checkList,
- 'allowList' => CommissionConfigEnum::$allowList,
- 'upgradeCheckList' => CommissionConfigEnum::$upgradeCheckList,
- 'needFormList' => CommissionConfigEnum::$needFormList,
- 'showProtocolList' => CommissionConfigEnum::$showProtocolList,
- 'inviteLockList' => CommissionConfigEnum::$inviteLockList,
- 'becomeAgentList' => CommissionConfigEnum::$becomeAgentList,
- 'rewardTypeList' => CommissionConfigEnum::$rewardTypeList,
- 'rewardEventList' => CommissionConfigEnum::$rewardEventList,
- 'refundCommissionRewardList' => CommissionConfigEnum::$refundCommissionRewardList,
- 'refundCommissionOrderList' => CommissionConfigEnum::$refundCommissionOrderList
- ]);
-
- return $this->view->fetch();
- }
-
- /**
- * 编辑配置
- */
- public function edit($ids = null)
- {
- if ($this->request->isPost()) {
- $this->token();
- $params = $this->request->post("row/a", [], 'trim');
-
- if ($params) {
- try {
- // 验证配置参数
- $this->validateConfigParams($params);
-
- // 特殊处理某些配置项
- if (isset($params['become_agent']) && is_array($params['become_agent'])) {
- $params['become_agent'] = json_encode($params['become_agent'], JSON_UNESCAPED_UNICODE);
- }
- // 更新配置
- ShopConfigService::setConfigs('shop.commission', $params);
- $this->success('配置保存成功');
- } catch (ValidateException $e) {
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- $this->error($e->getMessage());
- }
- //catch (\Exception $e) {
- // $this->error($e->getMessage());
- // }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- }
-
- /**
- * 验证配置参数
- */
- private function validateConfigParams($params)
- {
- // 验证分销层级
- if (isset($params['level']) && !CommissionConfigEnum::isValidLevel($params['level'])) {
- throw new ValidateException('分销层级参数无效');
- }
-
- // 验证开关类型参数
- $switchFields = ['self_buy', 'agent_check', 'upgrade_jump', 'upgrade_check', 'need_form', 'show_protocol'];
- foreach ($switchFields as $field) {
- if (isset($params[$field]) && !CommissionConfigEnum::isValidSwitch($params[$field])) {
- throw new ValidateException("{$field}参数无效");
- }
- }
-
- // 验证锁定下级条件
- if (isset($params['invite_lock']) && !CommissionConfigEnum::isValidInviteLock($params['invite_lock'])) {
- throw new ValidateException('锁定下级条件参数无效');
- }
-
- // 验证成为分销商条件
- if (isset($params['become_agent']) && is_array($params['become_agent'])) {
- if (isset($params['become_agent']['type']) && !CommissionConfigEnum::isValidBecomeAgent($params['become_agent']['type'])) {
- throw new ValidateException('成为分销商条件参数无效');
- }
- }
-
- // 验证商品结算方式
- if (isset($params['reward_type']) && !CommissionConfigEnum::isValidRewardType($params['reward_type'])) {
- throw new ValidateException('商品结算方式参数无效');
- }
-
- // 验证佣金结算方式
- if (isset($params['reward_event']) && !CommissionConfigEnum::isValidRewardEvent($params['reward_event'])) {
- throw new ValidateException('佣金结算方式参数无效');
- }
-
- // 验证退款扣除设置
- $refundFields = ['refund_commission_reward', 'refund_commission_order'];
- foreach ($refundFields as $field) {
- if (isset($params[$field]) && !CommissionConfigEnum::isValidSwitch($params[$field])) {
- throw new ValidateException("{$field}参数无效");
- }
- }
- }
- }
|