Agent.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. $identityData = $this->buildAgentIdentityData($data);
  58. $data['agent_identity_title'] = $identityData['title'];
  59. $data['manage_area'] = $identityData['area_info'];
  60. $data['manage_area_text'] = $identityData['area_text'];
  61. // 获取用户基本信息和佣金数据
  62. $user = $this->service->user;
  63. if ($user) {
  64. $data['user_info'] = [
  65. 'nickname' => $user->nickname,
  66. 'avatar' => cdnurl($user->avatar), // 转换为完整CDN URL
  67. 'username' => $user->username,
  68. ];
  69. }
  70. $data['commission'] = $this->service->user->commission ?? 0;
  71. // 获取代理商申请信息
  72. $applyInfo = ApplyModel::where('user_id', $this->service->user->id)
  73. ->order('id desc')
  74. ->find();
  75. $data['apply_info'] = $applyInfo;
  76. }
  77. $this->success('分销商信息', $data);
  78. }
  79. /**
  80. * 构造代理商身份数据(包括标题和区域信息)
  81. * @param array $agentData 代理商数据
  82. * @return array
  83. */
  84. private function buildAgentIdentityData($agentData)
  85. {
  86. $result = [
  87. 'title' => '普通代理商',
  88. 'area_info' => null,
  89. 'area_text' => '-'
  90. ];
  91. switch ($agentData['agent_type']) {
  92. case AgentType::NORMAL:
  93. $result['title'] = '普通代理商';
  94. break;
  95. case AgentType::PROVINCE:
  96. // 省级代理商:山东省代理商
  97. if (!empty($agentData['manage_province_id'])) {
  98. $provinceInfo = \app\common\model\Area::where('id', $agentData['manage_province_id'])->find();
  99. if ($provinceInfo) {
  100. $result['title'] = $provinceInfo['name'] . '代理商';
  101. $result['area_info'] = [
  102. 'province_name' => $provinceInfo['name'],
  103. 'province_id' => $agentData['manage_province_id']
  104. ];
  105. $result['area_text'] = $provinceInfo['name'];
  106. } else {
  107. $result['title'] = '省级代理商';
  108. }
  109. } else {
  110. $result['title'] = '省级代理商';
  111. }
  112. break;
  113. case AgentType::CITY:
  114. // 市级代理商:临沂市代理商
  115. $areaInfo = [];
  116. $areaText = [];
  117. // 获取省份信息
  118. if (!empty($agentData['manage_province_id'])) {
  119. $provinceInfo = \app\common\model\Area::where('id', $agentData['manage_province_id'])->find();
  120. if ($provinceInfo) {
  121. $areaInfo['province_name'] = $provinceInfo['name'];
  122. $areaInfo['province_id'] = $agentData['manage_province_id'];
  123. $areaText[] = $provinceInfo['name'];
  124. }
  125. }
  126. // 获取城市信息
  127. if (!empty($agentData['manage_city_id'])) {
  128. $cityInfo = \app\common\model\Area::where('id', $agentData['manage_city_id'])->find();
  129. if ($cityInfo) {
  130. $result['title'] = $cityInfo['name'] . '代理商';
  131. $areaInfo['city_name'] = $cityInfo['name'];
  132. $areaInfo['city_id'] = $agentData['manage_city_id'];
  133. $areaText[] = $cityInfo['name'];
  134. } else {
  135. $result['title'] = '市级代理商';
  136. }
  137. } else {
  138. $result['title'] = '市级代理商';
  139. }
  140. if (!empty($areaInfo)) {
  141. $result['area_info'] = $areaInfo;
  142. $result['area_text'] = implode('-', $areaText);
  143. }
  144. break;
  145. case AgentType::DISTRICT:
  146. // 区级代理商:兰山区代理商
  147. $areaInfo = [];
  148. $areaText = [];
  149. // 获取省份信息
  150. if (!empty($agentData['manage_province_id'])) {
  151. $provinceInfo = \app\common\model\Area::where('id', $agentData['manage_province_id'])->find();
  152. if ($provinceInfo) {
  153. $areaInfo['province_name'] = $provinceInfo['name'];
  154. $areaInfo['province_id'] = $agentData['manage_province_id'];
  155. $areaText[] = $provinceInfo['name'];
  156. }
  157. }
  158. // 获取城市信息
  159. if (!empty($agentData['manage_city_id'])) {
  160. $cityInfo = \app\common\model\Area::where('id', $agentData['manage_city_id'])->find();
  161. if ($cityInfo) {
  162. $areaInfo['city_name'] = $cityInfo['name'];
  163. $areaInfo['city_id'] = $agentData['manage_city_id'];
  164. $areaText[] = $cityInfo['name'];
  165. }
  166. }
  167. // 获取区域信息
  168. if (!empty($agentData['manage_district_id'])) {
  169. $districtInfo = \app\common\model\Area::where('id', $agentData['manage_district_id'])->find();
  170. if ($districtInfo) {
  171. $result['title'] = $districtInfo['name'] . '代理商';
  172. $areaInfo['district_name'] = $districtInfo['name'];
  173. $areaInfo['district_id'] = $agentData['manage_district_id'];
  174. $areaText[] = $districtInfo['name'];
  175. } else {
  176. $result['title'] = '区域代理商';
  177. }
  178. } else {
  179. $result['title'] = '区域代理商';
  180. }
  181. if (!empty($areaInfo)) {
  182. $result['area_info'] = $areaInfo;
  183. $result['area_text'] = implode('-', $areaText);
  184. }
  185. break;
  186. }
  187. return $result;
  188. }
  189. // 我的团队
  190. public function team()
  191. {
  192. $agentId = $this->service->user->id;
  193. $data = UserModel::where('parent_user_id', $agentId)
  194. ->where('status', 'normal')
  195. ->with(['agent' => function ($query) {
  196. return $query->with('level_info');
  197. }])
  198. ->paginate($this->request->param('list_rows', 8));
  199. $this->success("", $data);
  200. }
  201. // 佣金转余额/提现
  202. public function transfer()
  203. {
  204. $amount = $this->request->param('amount');
  205. if ($amount <= 0) {
  206. $this->error('请输入正确的金额');
  207. }
  208. $agent = $this->service->agent;
  209. if (!$agent) {
  210. $this->error('您还不是分销商');
  211. }
  212. // 使用BcMath工具类检查可提现余额(累计收益 - 已提现金额)
  213. $totalIncome = $agent->total_income ?? '0.00';
  214. $withdrawnAmount = $agent->withdrawn_amount ?? '0.00';
  215. $requestAmount = BcMath::format($amount);
  216. $availableBalance = BcMath::sub($totalIncome, $withdrawnAmount);
  217. // 使用BcMath比较函数检查余额是否足够
  218. if (BcMath::comp($requestAmount, $availableBalance) > 0) {
  219. $this->error('提现金额超过可用余额,可用余额:' . $availableBalance . '元');
  220. }
  221. Db::transaction(function () use ($amount, $agent) {
  222. $user = auth_user();
  223. // 转账到用户余额
  224. Wallet::change($user, 'money', $amount, 'commission_withdraw');
  225. // 更新代理商的已提现金额
  226. AgentModel::where('user_id', $user->id)->setInc('withdrawn_amount', $amount);
  227. });
  228. $this->success('提现成功');
  229. }
  230. }