Agent.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace app\api\controller\commission;
  3. use think\Db;
  4. use app\common\model\User as UserModel;
  5. use app\common\model\commission\Agent as AgentModel;
  6. use app\common\model\Goods as GoodsModel;
  7. use app\common\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. $config = [
  58. 'form' => $agentForm['content'],
  59. 'status' => $this->service->getAgentStatus(true),
  60. 'background' => $agentForm['background_image'],
  61. 'protocol' => $protocol
  62. ];
  63. $this->success("", $config);
  64. }
  65. // 申请分销商/完善资料
  66. public function apply()
  67. {
  68. if (!$this->service->config->isAgentApplyForm()) {
  69. $this->error('未开启分销商申请');
  70. }
  71. $status = $this->service->getAgentStatus(true);
  72. if (!in_array($status, [AgentModel::AGENT_STATUS_NEEDINFO, AgentModel::AGENT_STATUS_REJECT, AgentModel::AGENT_STATUS_NORMAL])) {
  73. $this->error('您暂时不能申请');
  74. }
  75. Db::transaction(function () use ($status) {
  76. $data = $this->request->param('data/a');
  77. // 过滤无效表单字段数据
  78. $config = $this->service->config->getAgentForm();
  79. $form = (array)$config['content'];
  80. $data = array_column($data, 'value', 'name');
  81. foreach ($form as &$item) {
  82. if (!empty($data[$item['name']])) {
  83. $item['value'] = $data[$item['name']];
  84. } else {
  85. $this->error($item['name'] . '不能为空');
  86. }
  87. }
  88. if ($status === AgentModel::AGENT_STATUS_NEEDINFO) {
  89. $this->service->createNewAgent('');
  90. } else {
  91. // 重置为审核中
  92. if ($status === AgentModel::AGENT_STATUS_REJECT) {
  93. $this->service->agent->status = AgentModel::AGENT_STATUS_PENDING;
  94. }
  95. // 保存分销商状态更新
  96. $this->service->agent->save();
  97. }
  98. });
  99. $this->success('提交成功');
  100. }
  101. // 我的团队
  102. public function team()
  103. {
  104. $agentId = $this->service->user->id;
  105. $data = UserModel::where('parent_user_id', $agentId)
  106. ->where('status', 'normal')
  107. ->with(['agent' => function ($query) {
  108. return $query->with('level_info');
  109. }])
  110. ->paginate($this->request->param('list_rows', 8));
  111. $this->success("", $data);
  112. }
  113. // 佣金转余额
  114. public function transfer()
  115. {
  116. $amount = $this->request->param('amount');
  117. if ($amount <= 0) {
  118. $this->error('请输入正确的金额');
  119. }
  120. Db::transaction(function () use ($amount) {
  121. $user = auth_user();
  122. Wallet::change($user, 'commission', -$amount, 'transfer_to_money');
  123. Wallet::change($user, 'money', $amount, 'transfer_by_commission');
  124. });
  125. $this->success('');
  126. }
  127. }