Agent.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. if ($applyInfo) {
  109. $data['apply_info'] = [
  110. 'id' => $applyInfo->id,
  111. 'apply_type' => $applyInfo->apply_type,
  112. 'apply_type_text' => $applyInfo->apply_type_text,
  113. 'status' => $applyInfo->status,
  114. 'status_text' => $applyInfo->status_text,
  115. 'identity_name' => $applyInfo->identity->name ?? '',
  116. 'area_text' => implode('-', array_filter([
  117. $applyInfo->province_name,
  118. $applyInfo->city_name,
  119. $applyInfo->district_name
  120. ])),
  121. 'createtime' => $applyInfo->createtime ? date('Y-m-d H:i:s', $applyInfo->createtime) : '',
  122. 'audit_time' => $applyInfo->audit_time ? date('Y-m-d H:i:s', $applyInfo->audit_time) : '',
  123. ];
  124. } else {
  125. $data['apply_info'] = null;
  126. }
  127. }
  128. $this->success('分销商信息', $data);
  129. }
  130. // 我的团队
  131. public function team()
  132. {
  133. $agentId = $this->service->user->id;
  134. $data = UserModel::where('parent_user_id', $agentId)
  135. ->where('status', 'normal')
  136. ->with(['agent' => function ($query) {
  137. return $query->with('level_info');
  138. }])
  139. ->paginate($this->request->param('list_rows', 8));
  140. $this->success("", $data);
  141. }
  142. // 佣金转余额/提现
  143. public function transfer()
  144. {
  145. $amount = $this->request->param('amount');
  146. if ($amount <= 0) {
  147. $this->error('请输入正确的金额');
  148. }
  149. $agent = $this->service->agent;
  150. if (!$agent) {
  151. $this->error('您还不是分销商');
  152. }
  153. // 使用BcMath工具类检查可提现余额(累计收益 - 已提现金额)
  154. $totalIncome = $agent->total_income ?? '0.00';
  155. $withdrawnAmount = $agent->withdrawn_amount ?? '0.00';
  156. $requestAmount = BcMath::format($amount);
  157. $availableBalance = BcMath::sub($totalIncome, $withdrawnAmount);
  158. // 使用BcMath比较函数检查余额是否足够
  159. if (BcMath::comp($requestAmount, $availableBalance) > 0) {
  160. $this->error('提现金额超过可用余额,可用余额:' . $availableBalance . '元');
  161. }
  162. Db::transaction(function () use ($amount, $agent) {
  163. $user = auth_user();
  164. // 转账到用户余额
  165. Wallet::change($user, 'money', $amount, 'commission_withdraw');
  166. // 更新代理商的已提现金额
  167. AgentModel::where('user_id', $user->id)->setInc('withdrawn_amount', $amount);
  168. });
  169. $this->success('提现成功');
  170. }
  171. }