Commission.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace app\api\controller\commission;
  3. use app\common\Service\commission\Agent as AgentService;
  4. use app\common\model\commission\Agent as AgentModel;
  5. use app\common\Service\ShopConfigService;
  6. use app\api\controller\Base;
  7. class Commission extends Base
  8. {
  9. protected $noNeedLogin = ['config'];
  10. protected $noNeedRight = ['*'];
  11. protected AgentService $service;
  12. public function _initialize()
  13. {
  14. parent::_initialize();
  15. $on = ShopConfigService::getConfigField('shop.commission.level');
  16. if (!$on) {
  17. $this->error('分销中心已关闭,该功能暂不可用', null, 101);
  18. }
  19. $user = auth_user();
  20. // 检查分销商状态
  21. $this->service = new AgentService($user);
  22. if ($this->service->agent && $this->service->agent->status === AgentModel::AGENT_STATUS_FORBIDDEN) {
  23. $this->error('账户已被禁用,该功能暂不可用', null, 102);
  24. }
  25. }
  26. /**
  27. * 获取分销配置
  28. *
  29. * @return void
  30. */
  31. public function config()
  32. {
  33. try {
  34. // 获取分销配置
  35. $commissionConfig = ShopConfigService::getConfigs('shop.commission');
  36. // 处理become_agent字段(如果是JSON字符串需要解析)
  37. if (isset($commissionConfig['become_agent']) && is_string($commissionConfig['become_agent'])) {
  38. $commissionConfig['become_agent'] = json_decode($commissionConfig['become_agent'], true) ?: ['type' => 'apply', 'value' => ''];
  39. }
  40. // 构建返回的配置数据
  41. $config = [
  42. // 基础配置
  43. 'level' => intval($commissionConfig['level'] ?? 2),
  44. 'self_buy' => intval($commissionConfig['self_buy'] ?? 0),
  45. 'invite_lock' => $commissionConfig['invite_lock'] ?? 'share',
  46. 'agent_check' => intval($commissionConfig['agent_check'] ?? 0),
  47. 'upgrade_jump' => intval($commissionConfig['upgrade_jump'] ?? 1),
  48. 'upgrade_check' => intval($commissionConfig['upgrade_check'] ?? 0),
  49. // 成为分销商条件
  50. 'become_agent' => $commissionConfig['become_agent'] ?? ['type' => 'apply', 'value' => ''],
  51. 'need_form' => intval($commissionConfig['need_form'] ?? 0),
  52. 'background_image' => !empty($commissionConfig['background_image']) ? cdnurl($commissionConfig['background_image'], true) : '',
  53. 'show_protocol' => intval($commissionConfig['show_protocol'] ?? 1),
  54. 'apply_protocol' => $commissionConfig['apply_protocol'] ?? '申请协议',
  55. // 结算配置
  56. 'reward_type' => $commissionConfig['reward_type'] ?? 'pay_price',
  57. 'reward_event' => $commissionConfig['reward_event'] ?? 'paid',
  58. 'refund_commission_reward' => intval($commissionConfig['refund_commission_reward'] ?? 1),
  59. 'refund_commission_order' => intval($commissionConfig['refund_commission_order'] ?? 1),
  60. // 分享配置
  61. 'share_title' => $commissionConfig['share_title'] ?? '我发现了一个好商品',
  62. 'share_description' => $commissionConfig['share_description'] ?? '快来看看这个优质商品,价格优惠,质量保证!',
  63. 'share_image' => !empty($commissionConfig['share_image']) ? cdnurl($commissionConfig['share_image'], true) : '',
  64. ];
  65. $this->success('获取成功', $config);
  66. } catch (\Exception $e) {
  67. $this->error('获取配置失败: ' . $e->getMessage());
  68. }
  69. }
  70. }