Agent.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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\commission\Reward as RewardModel;
  7. use app\common\model\commission\Apply as ApplyModel;
  8. use app\common\model\Goods as GoodsModel;
  9. use app\common\Service\Wallet;
  10. use app\common\Enum\AgentType;
  11. use app\common\library\BcMath;
  12. class Agent extends Commission
  13. {
  14. protected $noNeedLogin = [];
  15. protected $noNeedRight = ['*'];
  16. // 分销商详情
  17. public function index()
  18. {
  19. $status = $this->service->getAgentStatus(true);
  20. $condition = [
  21. 'type' => '',
  22. 'value' => ''
  23. ];
  24. switch ($status) {
  25. case AgentModel::AGENT_STATUS_NULL:
  26. $condition = $this->service->config->getBecomeAgentEvent();
  27. if ($condition['type'] === 'goods') {
  28. $condition['value'] = GoodsModel::show()->whereIn('id', $condition['value'])->select();
  29. }
  30. $this->error('', $condition, 100);
  31. break;
  32. case AgentModel::AGENT_STATUS_NEEDINFO:
  33. $this->error('待完善信息,请补充您的资料后提交审核', $condition, 103);
  34. break;
  35. case AgentModel::AGENT_STATUS_PENDING:
  36. $this->error('正在审核中,请耐心等候结果', $condition, 104);
  37. break;
  38. case AgentModel::AGENT_STATUS_REJECT:
  39. $agentFormStatus = $this->service->config->isAgentApplyForm();
  40. if ($agentFormStatus) {
  41. $this->error('抱歉!您的申请信息未通过,请尝试修改后重新提交', $condition, 105);
  42. } else {
  43. $this->error('抱歉!您的申请未通过,请尝试重新申请', $condition, 106);
  44. }
  45. break;
  46. case AgentModel::AGENT_STATUS_FREEZE:
  47. $this->error('抱歉!您的账户已被冻结,如有疑问请联系客服', $condition, 107);
  48. break;
  49. }
  50. $data = $this->service->agent;
  51. // 增强代理商数据信息
  52. if ($data) {
  53. $data = $data->toArray();
  54. // 添加代理商类型描述(使用枚举类)
  55. $data['agent_type_text'] = AgentType::getTypeText($data['agent_type']);
  56. // 如果是区域代理商,添加管辖区域信息
  57. if (AgentType::isRegionalAgent($data['agent_type'])) {
  58. $areaInfo = [];
  59. // 获取省份信息
  60. if (!empty($data['manage_province_id'])) {
  61. $provinceInfo = \app\common\model\Area::where('id', $data['manage_province_id'])->find();
  62. if ($provinceInfo) {
  63. $areaInfo['province_name'] = $provinceInfo['name'];
  64. $areaInfo['province_id'] = $data['manage_province_id'];
  65. }
  66. }
  67. // 获取城市信息
  68. if (!empty($data['manage_city_id'])) {
  69. $cityInfo = \app\common\model\Area::where('id', $data['manage_city_id'])->find();
  70. if ($cityInfo) {
  71. $areaInfo['city_name'] = $cityInfo['name'];
  72. $areaInfo['city_id'] = $data['manage_city_id'];
  73. }
  74. }
  75. // 获取区域信息
  76. if (!empty($data['manage_district_id'])) {
  77. $districtInfo = \app\common\model\Area::where('id', $data['manage_district_id'])->find();
  78. if ($districtInfo) {
  79. $areaInfo['district_name'] = $districtInfo['name'];
  80. $areaInfo['district_id'] = $data['manage_district_id'];
  81. }
  82. }
  83. $data['manage_area'] = $areaInfo;
  84. // 生成管辖区域描述文本
  85. $areaText = [];
  86. if (isset($areaInfo['province_name'])) $areaText[] = $areaInfo['province_name'];
  87. if (isset($areaInfo['city_name'])) $areaText[] = $areaInfo['city_name'];
  88. if (isset($areaInfo['district_name'])) $areaText[] = $areaInfo['district_name'];
  89. $data['manage_area_text'] = implode('-', $areaText);
  90. } else {
  91. $data['manage_area'] = null;
  92. $data['manage_area_text'] = '-';
  93. }
  94. // 获取用户基本信息和佣金数据
  95. $user = $this->service->user;
  96. if ($user) {
  97. $data['user_info'] = [
  98. 'nickname' => $user->nickname,
  99. 'avatar' => cdnurl($user->avatar), // 转换为完整CDN URL
  100. 'username' => $user->username,
  101. ];
  102. }
  103. // 获取代理商申请信息
  104. $applyInfo = ApplyModel::where('user_id', $this->service->user->id)
  105. ->with(['identity'])
  106. ->order('id desc')
  107. ->find();
  108. $data['apply_info'] = $applyInfo;
  109. }
  110. $this->success('分销商信息', $data);
  111. }
  112. // 我的团队
  113. public function team()
  114. {
  115. $agentId = $this->service->user->id;
  116. $data = UserModel::where('parent_user_id', $agentId)
  117. ->where('status', 'normal')
  118. ->with(['agent' => function ($query) {
  119. return $query->with('level_info');
  120. }])
  121. ->paginate($this->request->param('list_rows', 8));
  122. $this->success("", $data);
  123. }
  124. // 佣金转余额/提现
  125. public function transfer()
  126. {
  127. $amount = $this->request->param('amount');
  128. if ($amount <= 0) {
  129. $this->error('请输入正确的金额');
  130. }
  131. $agent = $this->service->agent;
  132. if (!$agent) {
  133. $this->error('您还不是分销商');
  134. }
  135. // 使用BcMath工具类检查可提现余额(累计收益 - 已提现金额)
  136. $totalIncome = $agent->total_income ?? '0.00';
  137. $withdrawnAmount = $agent->withdrawn_amount ?? '0.00';
  138. $requestAmount = BcMath::format($amount);
  139. $availableBalance = BcMath::sub($totalIncome, $withdrawnAmount);
  140. // 使用BcMath比较函数检查余额是否足够
  141. if (BcMath::comp($requestAmount, $availableBalance) > 0) {
  142. $this->error('提现金额超过可用余额,可用余额:' . $availableBalance . '元');
  143. }
  144. Db::transaction(function () use ($amount, $agent) {
  145. $user = auth_user();
  146. // 转账到用户余额
  147. Wallet::change($user, 'money', $amount, 'commission_withdraw');
  148. // 更新代理商的已提现金额
  149. AgentModel::where('user_id', $user->id)->setInc('withdrawn_amount', $amount);
  150. });
  151. $this->success('提现成功');
  152. }
  153. }