Agent.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace addons\shopro\controller\commission;
  3. use think\Db;
  4. use app\admin\model\shopro\user\User as UserModel;
  5. use app\admin\model\shopro\commission\Agent as AgentModel;
  6. use app\admin\model\shopro\goods\Goods as GoodsModel;
  7. use addons\shopro\service\Wallet;
  8. class Agent extends Commission
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = ['*'];
  12. // 分销商详情
  13. public function index()
  14. {
  15. $status = $this->service->getAgentStatus(true);
  16. $condition = [
  17. 'type' => '',
  18. 'value' => ''
  19. ];
  20. switch ($status) {
  21. case AgentModel::AGENT_STATUS_NULL:
  22. $condition = $this->service->config->getBecomeAgentEvent();
  23. if ($condition['type'] === 'goods') {
  24. $condition['value'] = GoodsModel::show()->whereIn('id', $condition['value'])->select();
  25. }
  26. $this->error('', $condition, 100);
  27. break;
  28. case AgentModel::AGENT_STATUS_NEEDINFO:
  29. $this->error('待完善信息,请补充您的资料后提交审核', $condition, 103);
  30. break;
  31. case AgentModel::AGENT_STATUS_PENDING:
  32. $this->error('正在审核中,请耐心等候结果', $condition, 104);
  33. break;
  34. case AgentModel::AGENT_STATUS_REJECT:
  35. $agentFormStatus = $this->service->config->isAgentApplyForm();
  36. if ($agentFormStatus) {
  37. $this->error('抱歉!您的申请信息未通过,请尝试修改后重新提交', $condition, 105);
  38. } else {
  39. $this->error('抱歉!您的申请未通过,请尝试重新申请', $condition, 106);
  40. }
  41. break;
  42. case AgentModel::AGENT_STATUS_FREEZE:
  43. $this->error('抱歉!您的账户已被冻结,如有疑问请联系客服', $condition, 107);
  44. break;
  45. }
  46. $data = $this->service->agent;
  47. $this->success('分销商信息', $data);
  48. }
  49. // 分销商完善个人信息
  50. public function form()
  51. {
  52. if (!$this->service->config->isAgentApplyForm()) {
  53. $this->error('未开启分销商申请');
  54. }
  55. $agentForm = $this->service->config->getAgentForm();
  56. $protocol = $this->service->config->getApplyProtocol();
  57. $applyInfo = $this->service->agent->apply_info ?? [];
  58. $config = [
  59. 'form' => $agentForm['content'],
  60. 'status' => $this->service->getAgentStatus(true),
  61. 'background' => $agentForm['background_image'],
  62. 'protocol' => $protocol,
  63. 'applyInfo' => $applyInfo
  64. ];
  65. $this->success("", $config);
  66. }
  67. // 申请分销商/完善资料
  68. public function apply()
  69. {
  70. if (!$this->service->config->isAgentApplyForm()) {
  71. $this->error('未开启分销商申请');
  72. }
  73. $status = $this->service->getAgentStatus(true);
  74. if (!in_array($status, [AgentModel::AGENT_STATUS_NEEDINFO, AgentModel::AGENT_STATUS_REJECT, AgentModel::AGENT_STATUS_NORMAL])) {
  75. $this->error('您暂时不能申请');
  76. }
  77. Db::transaction(function () use ($status) {
  78. $data = $this->request->param('data/a');
  79. // 过滤无效表单字段数据
  80. $config = $this->service->config->getAgentForm();
  81. $form = (array)$config['content'];
  82. $data = array_column($data, 'value', 'name');
  83. foreach ($form as &$item) {
  84. if (!empty($data[$item['name']])) {
  85. $item['value'] = $data[$item['name']];
  86. } else {
  87. $this->error($item['name'] . '不能为空');
  88. }
  89. }
  90. if ($status === AgentModel::AGENT_STATUS_NEEDINFO) {
  91. $this->service->createNewAgent('', $form);
  92. } else {
  93. // 重置为审核中
  94. if ($status === AgentModel::AGENT_STATUS_REJECT) {
  95. $this->service->agent->status = AgentModel::AGENT_STATUS_PENDING;
  96. }
  97. // 更新分销商信息
  98. $this->service->agent->apply_info = $form;
  99. $this->service->agent->save();
  100. }
  101. });
  102. $this->success('提交成功');
  103. }
  104. // 我的团队
  105. public function team()
  106. {
  107. $agentId = $this->service->user->id;
  108. $data = UserModel::where('parent_user_id', $agentId)
  109. ->where('status', 'normal')
  110. ->with(['agent' => function ($query) {
  111. return $query->with('level_info');
  112. }])
  113. ->paginate($this->request->param('list_rows', 8));
  114. $this->success("", $data);
  115. }
  116. // 佣金转余额
  117. public function transfer()
  118. {
  119. $amount = $this->request->param('amount');
  120. if ($amount <= 0) {
  121. $this->error('请输入正确的金额');
  122. }
  123. Db::transaction(function () use ($amount) {
  124. $user = auth_user();
  125. Wallet::change($user, 'commission', -$amount, 'transfer_to_money');
  126. Wallet::change($user, 'money', $amount, 'transfer_by_commission');
  127. });
  128. $this->success('');
  129. }
  130. }