123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- <?php
- namespace app\api\controller\commission;
- use think\Db;
- use app\common\model\User as UserModel;
- use app\common\model\commission\Agent as AgentModel;
- use app\common\model\commission\Reward as RewardModel;
- use app\common\model\commission\Apply as ApplyModel;
- use app\common\model\Goods as GoodsModel;
- use app\common\Service\Wallet;
- use app\common\Enum\AgentType;
- use app\common\library\BcMath;
- use app\common\Enum\PageTypeEnum;
- use app\common\Service\Third\Wechat\Wechat as WechatService;
- use app\common\Enum\ChannelEnum;
- use app\common\Enum\ShareEnum;
- use app\common\Service\ShopConfigService;
- class Agent extends Commission
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = ['*'];
- // 分销商详情
- public function index()
- {
- $status = $this->service->getAgentStatus(true);
- $condition = [
- 'type' => '',
- 'value' => ''
- ];
- switch ($status) {
- case AgentModel::AGENT_STATUS_NULL:
- $condition = $this->service->config->getBecomeAgentEvent();
- if ($condition['type'] === 'goods') {
- $condition['value'] = GoodsModel::show()->whereIn('id', $condition['value'])->select();
- }
- $this->error('', $condition, 100);
- break;
- case AgentModel::AGENT_STATUS_NEEDINFO:
- $this->error('待完善信息,请补充您的资料后提交审核', $condition, 103);
- break;
- case AgentModel::AGENT_STATUS_PENDING:
- $this->error('正在审核中,请耐心等候结果', $condition, 104);
- break;
- case AgentModel::AGENT_STATUS_REJECT:
- $agentFormStatus = $this->service->config->isAgentApplyForm();
- if ($agentFormStatus) {
- $this->error('抱歉!您的申请信息未通过,请尝试修改后重新提交', $condition, 105);
- } else {
- $this->error('抱歉!您的申请未通过,请尝试重新申请', $condition, 106);
- }
- break;
- case AgentModel::AGENT_STATUS_FREEZE:
- $this->error('抱歉!您的账户已被冻结,如有疑问请联系客服', $condition, 107);
- break;
- }
- $data = $this->service->agent;
-
- // 增强代理商数据信息
- if ($data) {
- $data = $data->toArray();
-
- // 添加代理商类型描述(使用枚举类)
- $data['agent_type_text'] = AgentType::getTypeText($data['agent_type']);
-
- // 构造代理商身份描述和区域信息
- $identityData = $this->buildAgentIdentityData($data);
- $data['agent_identity_title'] = $identityData['title'];
- $data['manage_area'] = $identityData['area_info'];
- $data['manage_area_text'] = $identityData['area_text'];
-
- // 获取用户基本信息和佣金数据
- $user = $this->service->user;
- if ($user) {
- $data['user_info'] = [
- 'nickname' => $user->nickname,
- 'avatar' => cdnurl($user->avatar), // 转换为完整CDN URL
- 'username' => $user->username,
- ];
- }
- $data['commission'] = $this->service->user->commission ?? 0;
- // 获取代理商申请信息
- $applyInfo = ApplyModel::where('user_id', $this->service->user->id)
- ->order('id desc')
- ->find();
- $data['apply_info'] = $applyInfo;
-
- }
- $this->success('分销商信息', $data);
- }
- /**
- * 构造代理商身份数据(包括标题和区域信息)
- * @param array $agentData 代理商数据
- * @return array
- */
- private function buildAgentIdentityData($agentData)
- {
- $result = [
- 'title' => '普通代理商',
- 'area_info' => null,
- 'area_text' => '-'
- ];
- switch ($agentData['agent_type']) {
- case AgentType::NORMAL:
- $result['title'] = '普通代理商';
- break;
-
- case AgentType::PROVINCE:
- // 省级代理商:山东省代理商
- if (!empty($agentData['manage_province_id'])) {
- $provinceInfo = \app\common\model\Area::where('id', $agentData['manage_province_id'])->find();
- if ($provinceInfo) {
- $result['title'] = $provinceInfo['name'] . '代理商';
- $result['area_info'] = [
- 'province_name' => $provinceInfo['name'],
- 'province_id' => $agentData['manage_province_id']
- ];
- $result['area_text'] = $provinceInfo['name'];
- } else {
- $result['title'] = '省级代理商';
- }
- } else {
- $result['title'] = '省级代理商';
- }
- break;
-
- case AgentType::CITY:
- // 市级代理商:临沂市代理商
- $areaInfo = [];
- $areaText = [];
-
- // 获取省份信息
- if (!empty($agentData['manage_province_id'])) {
- $provinceInfo = \app\common\model\Area::where('id', $agentData['manage_province_id'])->find();
- if ($provinceInfo) {
- $areaInfo['province_name'] = $provinceInfo['name'];
- $areaInfo['province_id'] = $agentData['manage_province_id'];
- $areaText[] = $provinceInfo['name'];
- }
- }
-
- // 获取城市信息
- if (!empty($agentData['manage_city_id'])) {
- $cityInfo = \app\common\model\Area::where('id', $agentData['manage_city_id'])->find();
- if ($cityInfo) {
- $result['title'] = $cityInfo['name'] . '代理商';
- $areaInfo['city_name'] = $cityInfo['name'];
- $areaInfo['city_id'] = $agentData['manage_city_id'];
- $areaText[] = $cityInfo['name'];
- } else {
- $result['title'] = '市级代理商';
- }
- } else {
- $result['title'] = '市级代理商';
- }
-
- if (!empty($areaInfo)) {
- $result['area_info'] = $areaInfo;
- $result['area_text'] = implode('-', $areaText);
- }
- break;
-
- case AgentType::DISTRICT:
- // 区级代理商:兰山区代理商
- $areaInfo = [];
- $areaText = [];
-
- // 获取省份信息
- if (!empty($agentData['manage_province_id'])) {
- $provinceInfo = \app\common\model\Area::where('id', $agentData['manage_province_id'])->find();
- if ($provinceInfo) {
- $areaInfo['province_name'] = $provinceInfo['name'];
- $areaInfo['province_id'] = $agentData['manage_province_id'];
- $areaText[] = $provinceInfo['name'];
- }
- }
-
- // 获取城市信息
- if (!empty($agentData['manage_city_id'])) {
- $cityInfo = \app\common\model\Area::where('id', $agentData['manage_city_id'])->find();
- if ($cityInfo) {
- $areaInfo['city_name'] = $cityInfo['name'];
- $areaInfo['city_id'] = $agentData['manage_city_id'];
- $areaText[] = $cityInfo['name'];
- }
- }
-
- // 获取区域信息
- if (!empty($agentData['manage_district_id'])) {
- $districtInfo = \app\common\model\Area::where('id', $agentData['manage_district_id'])->find();
- if ($districtInfo) {
- $result['title'] = $districtInfo['name'] . '代理商';
- $areaInfo['district_name'] = $districtInfo['name'];
- $areaInfo['district_id'] = $agentData['manage_district_id'];
- $areaText[] = $districtInfo['name'];
- } else {
- $result['title'] = '区域代理商';
- }
- } else {
- $result['title'] = '区域代理商';
- }
-
- if (!empty($areaInfo)) {
- $result['area_info'] = $areaInfo;
- $result['area_text'] = implode('-', $areaText);
- }
- break;
- }
- return $result;
- }
- // 我的团队
- public function team()
- {
- $agentId = $this->service->user->id;
- $data = UserModel::where('parent_user_id', $agentId)
- ->where('status', 'normal')
- ->with(['agent' => function ($query) {
- return $query->with('level_info');
- }])
- ->paginate($this->request->param('list_rows', 8));
- $this->success("", $data);
- }
- // 佣金转余额/提现
- public function transfer()
- {
- $amount = $this->request->param('amount');
- if ($amount <= 0) {
- $this->error('请输入正确的金额');
- }
-
- $agent = $this->service->agent;
- if (!$agent) {
- $this->error('您还不是分销商');
- }
-
- // 使用BcMath工具类检查可提现余额(累计收益 - 已提现金额)
- $totalIncome = $agent->total_income ?? '0.00';
- $withdrawnAmount = $agent->withdrawn_amount ?? '0.00';
- $requestAmount = BcMath::format($amount);
-
- $availableBalance = BcMath::sub($totalIncome, $withdrawnAmount);
-
- // 使用BcMath比较函数检查余额是否足够
- if (BcMath::comp($requestAmount, $availableBalance) > 0) {
- $this->error('提现金额超过可用余额,可用余额:' . $availableBalance . '元');
- }
-
- Db::transaction(function () use ($amount, $agent) {
- $user = auth_user();
-
- // 转账到用户余额
- Wallet::change($user, 'money', $amount, 'commission_withdraw');
-
- // 更新代理商的已提现金额
- AgentModel::where('user_id', $user->id)->setInc('withdrawn_amount', $amount);
- });
-
- $this->success('提现成功');
- }
- /**
- * 生成分销海报微信小程序码
- */
- public function posterQrcode()
- {
- // 验证当前用户是否为分销商
- $agent = $this->service->agent;
- if (!$agent) {
- $this->error('您还不是分销商');
- }
-
- // 获取当前用户ID作为分享者
- $shareUserId = $this->service->user->id;
-
- // 从请求头获取platform参数
- $requestPlatform = $this->request->header('platform', '');
- if (!$requestPlatform) {
- $this->error('缺少platform参数');
- }
-
- // 将ChannelEnum映射到ShareEnum平台常量
- $shareEnumPlatform = $this->mapChannelToSharePlatform($requestPlatform);
-
- // 使用ShareEnum获取平台ID (1=H5,2=微信公众号网页,3=微信小程序,4=App)
- $platformId = ShareEnum::getPlatformId($shareEnumPlatform);
- $fromPlatformId = $platformId; // 来源平台同当前平台
- $commissionConfig = ShopConfigService::getConfigs('shop.commission');
-
- // try {
- // 初始化微信服务 - 固定使用微信小程序平台生成小程序码
- $wechatPlatform = ChannelEnum::CHANNEL_WECHAT_MINI_PROGRAM;
- $payload = [];
- $wechat = new WechatService($wechatPlatform, $payload);
- $mp = $wechat->getApp();
-
- // 构造spm参数: shareId.page.query.platform.from
- // shareId: 分享者用户ID
- // page: 页面类型 (分销海报页面 = 6)
- // query: 代理商ID (使用分享者ID)
- // platform: 从请求头获取的平台ID
- // from: 来源平台ID
- $spmParams = sprintf('%s.%s.%s.%s.%s',
- $shareUserId,
- PageTypeEnum::AGENT_POSTER,
- $shareUserId,
- $platformId, // 使用映射后的平台ID
- $fromPlatformId // 来源平台ID
- );
-
- // 固定跳转到分销海报页面,携带smp参数
- $page = 'pages/me/edit'; // 分销海报页面路径
- $scene = 'spm=' . $spmParams;
-
- // 生成小程序码
- $content = $mp->app_code->getUnlimit($scene, [
- 'page' => $page,
- 'is_hyaline' => false,
- 'env_version' => 'trial',
- 'check_path' => false
- ]);
-
- if ($content instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
- // 将小程序码转换为base64
- $qrcodeBase64 = base64_encode($content->getBody());
-
- // 构建返回数据
- $result = [
- 'qrcode_base64' => 'data:image/png;base64,' . $qrcodeBase64,
- 'invite_code' =>$agent->invite_code? $agent->invite_code : '',
- 'poster_background' => !empty($commissionConfig['poster_background_image']) ? cdnurl($commissionConfig['poster_background_image'], true) : '',
- ];
-
- $this->success('生成成功', $result);
- } else {
- // 小程序码获取失败
- $msg = $content['errcode'] ?? '-';
- $msg .= $content['errmsg'] ?? '';
- $this->error('小程序码生成失败:' . $msg);
- }
-
- // } catch (\Exception $e) {
- // $this->error('小程序码生成失败:' . $e->getMessage());
- // }
- }
- /**
- * 将ChannelEnum的平台常量映射到ShareEnum的平台常量
- * @param string $channelPlatform ChannelEnum平台常量
- * @return string ShareEnum平台常量
- */
- private function mapChannelToSharePlatform($channelPlatform)
- {
- $channelToShareMap = [
- ChannelEnum::CHANNEL_H5 => ShareEnum::PLATFORM_H5,
- ChannelEnum::CHANNEL_WECHAT_OFFICIAL_ACCOUNT => ShareEnum::PLATFORM_WECHAT_OFFICIAL_ACCOUNT,
- ChannelEnum::CHANNEL_WECHAT_MINI_PROGRAM => ShareEnum::PLATFORM_WECHAT_MINI_PROGRAM,
- ChannelEnum::CHANNEL_IOS_APP => ShareEnum::PLATFORM_APP,
- ChannelEnum::CHANNEL_ANDROID_APP => ShareEnum::PLATFORM_APP,
- ];
- return $channelToShareMap[$channelPlatform] ?? ShareEnum::PLATFORM_WECHAT_MINI_PROGRAM; // 默认微信小程序
- }
-
-
- }
|