123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?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;
- 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']);
-
- // 如果是区域代理商,添加管辖区域信息
- if (AgentType::isRegionalAgent($data['agent_type'])) {
- $areaInfo = [];
-
- // 获取省份信息
- if (!empty($data['manage_province_id'])) {
- $provinceInfo = \app\common\model\Area::where('id', $data['manage_province_id'])->find();
- if ($provinceInfo) {
- $areaInfo['province_name'] = $provinceInfo['name'];
- $areaInfo['province_id'] = $data['manage_province_id'];
- }
- }
-
- // 获取城市信息
- if (!empty($data['manage_city_id'])) {
- $cityInfo = \app\common\model\Area::where('id', $data['manage_city_id'])->find();
- if ($cityInfo) {
- $areaInfo['city_name'] = $cityInfo['name'];
- $areaInfo['city_id'] = $data['manage_city_id'];
- }
- }
-
- // 获取区域信息
- if (!empty($data['manage_district_id'])) {
- $districtInfo = \app\common\model\Area::where('id', $data['manage_district_id'])->find();
- if ($districtInfo) {
- $areaInfo['district_name'] = $districtInfo['name'];
- $areaInfo['district_id'] = $data['manage_district_id'];
- }
- }
-
- $data['manage_area'] = $areaInfo;
-
- // 生成管辖区域描述文本
- $areaText = [];
- if (isset($areaInfo['province_name'])) $areaText[] = $areaInfo['province_name'];
- if (isset($areaInfo['city_name'])) $areaText[] = $areaInfo['city_name'];
- if (isset($areaInfo['district_name'])) $areaText[] = $areaInfo['district_name'];
- $data['manage_area_text'] = implode('-', $areaText);
- } else {
- $data['manage_area'] = null;
- $data['manage_area_text'] = '-';
- }
-
- // 获取用户基本信息和佣金数据
- $user = $this->service->user;
- if ($user) {
- $data['user_info'] = [
- 'nickname' => $user->nickname,
- 'avatar' => cdnurl($user->avatar), // 转换为完整CDN URL
- 'username' => $user->username,
- ];
- }
-
- // 获取代理商申请信息
- $applyInfo = ApplyModel::where('user_id', $this->service->user->id)
- ->with(['identity'])
- ->order('id desc')
- ->find();
-
- if ($applyInfo) {
- $data['apply_info'] = [
- 'id' => $applyInfo->id,
- 'apply_type' => $applyInfo->apply_type,
- 'apply_type_text' => $applyInfo->apply_type_text,
- 'status' => $applyInfo->status,
- 'status_text' => $applyInfo->status_text,
- 'identity_name' => $applyInfo->identity->name ?? '',
- 'area_text' => implode('-', array_filter([
- $applyInfo->province_name,
- $applyInfo->city_name,
- $applyInfo->district_name
- ])),
- 'createtime' => $applyInfo->createtime ? date('Y-m-d H:i:s', $applyInfo->createtime) : '',
- 'audit_time' => $applyInfo->audit_time ? date('Y-m-d H:i:s', $applyInfo->audit_time) : '',
- ];
- } else {
- $data['apply_info'] = null;
- }
- }
- $this->success('分销商信息', $data);
- }
- // 我的团队
- 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('提现成功');
- }
-
-
-
- }
|