Agent.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. use app\common\Enum\PageTypeEnum;
  13. use app\common\Service\Third\Wechat\Wechat as WechatService;
  14. use app\common\Enum\ChannelEnum;
  15. use app\common\Enum\ShareEnum;
  16. use app\common\Service\ShopConfigService;
  17. class Agent extends Commission
  18. {
  19. protected $noNeedLogin = [];
  20. protected $noNeedRight = ['*'];
  21. // 分销商详情
  22. public function index()
  23. {
  24. $status = $this->service->getAgentStatus(true);
  25. $condition = [
  26. 'type' => '',
  27. 'value' => ''
  28. ];
  29. switch ($status) {
  30. case AgentModel::AGENT_STATUS_NULL:
  31. $condition = $this->service->config->getBecomeAgentEvent();
  32. if ($condition['type'] === 'goods') {
  33. $condition['value'] = GoodsModel::show()->whereIn('id', $condition['value'])->select();
  34. }
  35. $this->error('', $condition, 100);
  36. break;
  37. case AgentModel::AGENT_STATUS_NEEDINFO:
  38. $this->error('待完善信息,请补充您的资料后提交审核', $condition, 103);
  39. break;
  40. case AgentModel::AGENT_STATUS_PENDING:
  41. $this->error('正在审核中,请耐心等候结果', $condition, 104);
  42. break;
  43. case AgentModel::AGENT_STATUS_REJECT:
  44. $agentFormStatus = $this->service->config->isAgentApplyForm();
  45. if ($agentFormStatus) {
  46. $this->error('抱歉!您的申请信息未通过,请尝试修改后重新提交', $condition, 105);
  47. } else {
  48. $this->error('抱歉!您的申请未通过,请尝试重新申请', $condition, 106);
  49. }
  50. break;
  51. case AgentModel::AGENT_STATUS_FREEZE:
  52. $this->error('抱歉!您的账户已被冻结,如有疑问请联系客服', $condition, 107);
  53. break;
  54. }
  55. $data = $this->service->agent;
  56. // 增强代理商数据信息
  57. if ($data) {
  58. $data = $data->toArray();
  59. // 添加代理商类型描述(使用枚举类)
  60. $data['agent_type_text'] = AgentType::getTypeText($data['agent_type']);
  61. // 构造代理商身份描述和区域信息
  62. $identityData = $this->buildAgentIdentityData($data);
  63. $data['agent_identity_title'] = $identityData['title'];
  64. $data['manage_area'] = $identityData['area_info'];
  65. $data['manage_area_text'] = $identityData['area_text'];
  66. // 获取用户基本信息和佣金数据
  67. $user = $this->service->user;
  68. if ($user) {
  69. $data['user_info'] = [
  70. 'nickname' => $user->nickname,
  71. 'avatar' => cdnurl($user->avatar), // 转换为完整CDN URL
  72. 'username' => $user->username,
  73. ];
  74. }
  75. $data['commission'] = $this->service->user->commission ?? 0;
  76. // 获取代理商申请信息
  77. $applyInfo = ApplyModel::where('user_id', $this->service->user->id)
  78. ->order('id desc')
  79. ->find();
  80. $data['apply_info'] = $applyInfo;
  81. }
  82. $this->success('分销商信息', $data);
  83. }
  84. /**
  85. * 构造代理商身份数据(包括标题和区域信息)
  86. * @param array $agentData 代理商数据
  87. * @return array
  88. */
  89. private function buildAgentIdentityData($agentData)
  90. {
  91. $result = [
  92. 'title' => '普通代理商',
  93. 'area_info' => null,
  94. 'area_text' => '-'
  95. ];
  96. switch ($agentData['agent_type']) {
  97. case AgentType::NORMAL:
  98. $result['title'] = '普通代理商';
  99. break;
  100. case AgentType::PROVINCE:
  101. // 省级代理商:山东省代理商
  102. if (!empty($agentData['manage_province_id'])) {
  103. $provinceInfo = \app\common\model\Area::where('id', $agentData['manage_province_id'])->find();
  104. if ($provinceInfo) {
  105. $result['title'] = $provinceInfo['name'] . '代理商';
  106. $result['area_info'] = [
  107. 'province_name' => $provinceInfo['name'],
  108. 'province_id' => $agentData['manage_province_id']
  109. ];
  110. $result['area_text'] = $provinceInfo['name'];
  111. } else {
  112. $result['title'] = '省级代理商';
  113. }
  114. } else {
  115. $result['title'] = '省级代理商';
  116. }
  117. break;
  118. case AgentType::CITY:
  119. // 市级代理商:临沂市代理商
  120. $areaInfo = [];
  121. $areaText = [];
  122. // 获取省份信息
  123. if (!empty($agentData['manage_province_id'])) {
  124. $provinceInfo = \app\common\model\Area::where('id', $agentData['manage_province_id'])->find();
  125. if ($provinceInfo) {
  126. $areaInfo['province_name'] = $provinceInfo['name'];
  127. $areaInfo['province_id'] = $agentData['manage_province_id'];
  128. $areaText[] = $provinceInfo['name'];
  129. }
  130. }
  131. // 获取城市信息
  132. if (!empty($agentData['manage_city_id'])) {
  133. $cityInfo = \app\common\model\Area::where('id', $agentData['manage_city_id'])->find();
  134. if ($cityInfo) {
  135. $result['title'] = $cityInfo['name'] . '代理商';
  136. $areaInfo['city_name'] = $cityInfo['name'];
  137. $areaInfo['city_id'] = $agentData['manage_city_id'];
  138. $areaText[] = $cityInfo['name'];
  139. } else {
  140. $result['title'] = '市级代理商';
  141. }
  142. } else {
  143. $result['title'] = '市级代理商';
  144. }
  145. if (!empty($areaInfo)) {
  146. $result['area_info'] = $areaInfo;
  147. $result['area_text'] = implode('-', $areaText);
  148. }
  149. break;
  150. case AgentType::DISTRICT:
  151. // 区级代理商:兰山区代理商
  152. $areaInfo = [];
  153. $areaText = [];
  154. // 获取省份信息
  155. if (!empty($agentData['manage_province_id'])) {
  156. $provinceInfo = \app\common\model\Area::where('id', $agentData['manage_province_id'])->find();
  157. if ($provinceInfo) {
  158. $areaInfo['province_name'] = $provinceInfo['name'];
  159. $areaInfo['province_id'] = $agentData['manage_province_id'];
  160. $areaText[] = $provinceInfo['name'];
  161. }
  162. }
  163. // 获取城市信息
  164. if (!empty($agentData['manage_city_id'])) {
  165. $cityInfo = \app\common\model\Area::where('id', $agentData['manage_city_id'])->find();
  166. if ($cityInfo) {
  167. $areaInfo['city_name'] = $cityInfo['name'];
  168. $areaInfo['city_id'] = $agentData['manage_city_id'];
  169. $areaText[] = $cityInfo['name'];
  170. }
  171. }
  172. // 获取区域信息
  173. if (!empty($agentData['manage_district_id'])) {
  174. $districtInfo = \app\common\model\Area::where('id', $agentData['manage_district_id'])->find();
  175. if ($districtInfo) {
  176. $result['title'] = $districtInfo['name'] . '代理商';
  177. $areaInfo['district_name'] = $districtInfo['name'];
  178. $areaInfo['district_id'] = $agentData['manage_district_id'];
  179. $areaText[] = $districtInfo['name'];
  180. } else {
  181. $result['title'] = '区域代理商';
  182. }
  183. } else {
  184. $result['title'] = '区域代理商';
  185. }
  186. if (!empty($areaInfo)) {
  187. $result['area_info'] = $areaInfo;
  188. $result['area_text'] = implode('-', $areaText);
  189. }
  190. break;
  191. }
  192. return $result;
  193. }
  194. // 我的团队
  195. public function team()
  196. {
  197. $agentId = $this->service->user->id;
  198. $data = UserModel::where('parent_user_id', $agentId)
  199. ->where('status', 'normal')
  200. ->with(['agent' => function ($query) {
  201. return $query->with('level_info');
  202. }])
  203. ->paginate($this->request->param('list_rows', 8));
  204. $this->success("", $data);
  205. }
  206. // 佣金转余额/提现
  207. public function transfer()
  208. {
  209. $amount = $this->request->param('amount');
  210. if ($amount <= 0) {
  211. $this->error('请输入正确的金额');
  212. }
  213. $agent = $this->service->agent;
  214. if (!$agent) {
  215. $this->error('您还不是分销商');
  216. }
  217. // 使用BcMath工具类检查可提现余额(累计收益 - 已提现金额)
  218. $totalIncome = $agent->total_income ?? '0.00';
  219. $withdrawnAmount = $agent->withdrawn_amount ?? '0.00';
  220. $requestAmount = BcMath::format($amount);
  221. $availableBalance = BcMath::sub($totalIncome, $withdrawnAmount);
  222. // 使用BcMath比较函数检查余额是否足够
  223. if (BcMath::comp($requestAmount, $availableBalance) > 0) {
  224. $this->error('提现金额超过可用余额,可用余额:' . $availableBalance . '元');
  225. }
  226. Db::transaction(function () use ($amount, $agent) {
  227. $user = auth_user();
  228. // 转账到用户余额
  229. Wallet::change($user, 'money', $amount, 'commission_withdraw');
  230. // 更新代理商的已提现金额
  231. AgentModel::where('user_id', $user->id)->setInc('withdrawn_amount', $amount);
  232. });
  233. $this->success('提现成功');
  234. }
  235. /**
  236. * 生成分销海报微信小程序码
  237. */
  238. public function posterQrcode()
  239. {
  240. // 验证当前用户是否为分销商
  241. $agent = $this->service->agent;
  242. if (!$agent) {
  243. $this->error('您还不是分销商');
  244. }
  245. // 获取当前用户ID作为分享者
  246. $shareUserId = $this->service->user->id;
  247. // 从请求头获取platform参数
  248. $requestPlatform = $this->request->header('platform', '');
  249. if (!$requestPlatform) {
  250. $this->error('缺少platform参数');
  251. }
  252. // 将ChannelEnum映射到ShareEnum平台常量
  253. $shareEnumPlatform = $this->mapChannelToSharePlatform($requestPlatform);
  254. // 使用ShareEnum获取平台ID (1=H5,2=微信公众号网页,3=微信小程序,4=App)
  255. $platformId = ShareEnum::getPlatformId($shareEnumPlatform);
  256. $fromPlatformId = $platformId; // 来源平台同当前平台
  257. $commissionConfig = ShopConfigService::getConfigs('shop.commission');
  258. // try {
  259. // 初始化微信服务 - 固定使用微信小程序平台生成小程序码
  260. $wechatPlatform = ChannelEnum::CHANNEL_WECHAT_MINI_PROGRAM;
  261. $payload = [];
  262. $wechat = new WechatService($wechatPlatform, $payload);
  263. $mp = $wechat->getApp();
  264. // 构造spm参数: shareId.page.query.platform.from
  265. // shareId: 分享者用户ID
  266. // page: 页面类型 (分销海报页面 = 6)
  267. // query: 代理商ID (使用分享者ID)
  268. // platform: 从请求头获取的平台ID
  269. // from: 来源平台ID
  270. $spmParams = sprintf('%s.%s.%s.%s.%s',
  271. $shareUserId,
  272. PageTypeEnum::AGENT_POSTER,
  273. $shareUserId,
  274. $platformId, // 使用映射后的平台ID
  275. $fromPlatformId // 来源平台ID
  276. );
  277. // 固定跳转到分销海报页面,携带smp参数
  278. $page = 'pages/me/edit'; // 分销海报页面路径
  279. $scene = 'spm=' . $spmParams;
  280. // 生成小程序码
  281. $content = $mp->app_code->getUnlimit($scene, [
  282. 'page' => $page,
  283. 'is_hyaline' => false,
  284. 'env_version' => 'trial',
  285. 'check_path' => false
  286. ]);
  287. if ($content instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  288. // 将小程序码转换为base64
  289. $qrcodeBase64 = base64_encode($content->getBody());
  290. // 构建返回数据
  291. $result = [
  292. 'qrcode_base64' => 'data:image/png;base64,' . $qrcodeBase64,
  293. 'invite_code' =>$agent->invite_code? $agent->invite_code : '',
  294. 'poster_background' => !empty($commissionConfig['poster_background_image']) ? cdnurl($commissionConfig['poster_background_image'], true) : '',
  295. ];
  296. $this->success('生成成功', $result);
  297. } else {
  298. // 小程序码获取失败
  299. $msg = $content['errcode'] ?? '-';
  300. $msg .= $content['errmsg'] ?? '';
  301. $this->error('小程序码生成失败:' . $msg);
  302. }
  303. // } catch (\Exception $e) {
  304. // $this->error('小程序码生成失败:' . $e->getMessage());
  305. // }
  306. }
  307. /**
  308. * 将ChannelEnum的平台常量映射到ShareEnum的平台常量
  309. * @param string $channelPlatform ChannelEnum平台常量
  310. * @return string ShareEnum平台常量
  311. */
  312. private function mapChannelToSharePlatform($channelPlatform)
  313. {
  314. $channelToShareMap = [
  315. ChannelEnum::CHANNEL_H5 => ShareEnum::PLATFORM_H5,
  316. ChannelEnum::CHANNEL_WECHAT_OFFICIAL_ACCOUNT => ShareEnum::PLATFORM_WECHAT_OFFICIAL_ACCOUNT,
  317. ChannelEnum::CHANNEL_WECHAT_MINI_PROGRAM => ShareEnum::PLATFORM_WECHAT_MINI_PROGRAM,
  318. ChannelEnum::CHANNEL_IOS_APP => ShareEnum::PLATFORM_APP,
  319. ChannelEnum::CHANNEL_ANDROID_APP => ShareEnum::PLATFORM_APP,
  320. ];
  321. return $channelToShareMap[$channelPlatform] ?? ShareEnum::PLATFORM_WECHAT_MINI_PROGRAM; // 默认微信小程序
  322. }
  323. }