where('status', 'in', ['pending', 'approved']) ->find(); if ($existApply) { if ($existApply->status == 'approved') { throw new Exception('您已经是代理商,无需重复申请'); } else { throw new Exception('您已有待审核的申请,请等待审核结果'); } } // 验证身份是否存在 $identity = IdentityModel::where('id', $data['agent_identity_id']) ->where('status', IdentityModel::STATUS_ENABLED) ->find(); if (!$identity) { throw new Exception('选择的代理商身份不存在或已禁用'); } // 验证地区信息 $this->validateArea($data); // 根据申请类型验证必要字段 $this->validateApplyData($data); return Db::transaction(function () use ($userId, $data, $identity) { // 创建申请记录 $apply = new ApplyModel(); $apply->user_id = $userId; $apply->apply_type = $data['apply_type']; $apply->agent_identity_id = $data['agent_identity_id']; $apply->agent_type = $identity->agent_type; // 从身份配置获取代理商类型 $apply->status = ApplyModel::STATUS_PENDING; // 设置初始状态为待审核 // 地区信息 - 根据代理商类型设置需要的地区字段 $requiredFields = AgentType::getRequiredAreaFields($identity->agent_type); if (in_array('province_id', $requiredFields)) { $apply->province_id = $data['province_id'] ?? null; $apply->province_name = $data['province_name'] ?? ''; } if (in_array('city_id', $requiredFields)) { $apply->city_id = $data['city_id'] ?? null; $apply->city_name = $data['city_name'] ?? ''; } if (in_array('district_id', $requiredFields)) { $apply->district_id = $data['district_id'] ?? null; $apply->district_name = $data['district_name'] ?? ''; } // 根据申请类型填充对应字段 if ($data['apply_type'] == ApplyModel::APPLY_TYPE_PERSONAL) { $apply->real_name = $data['real_name']; $apply->id_card = $data['id_card']; $apply->id_card_front = $data['id_card_front']; $apply->id_card_back = $data['id_card_back']; $apply->mobile = $data['mobile']; } else { $apply->company_name = $data['company_name']; $apply->legal_person = $data['legal_person']; $apply->legal_mobile = $data['legal_mobile']; $apply->legal_id_card = $data['legal_id_card']; $apply->legal_id_front = $data['legal_id_front']; $apply->legal_id_back = $data['legal_id_back']; $apply->business_license = $data['business_license']; } $apply->save(); // 检查是否需要审核 $needCheck = ShopConfigService::getConfigField('shop.commission.agent_apply_check'); if (!$needCheck) { // 不需要审核,直接通过 $this->approveApply($apply); } return $apply; }); } /** * 验证地区信息 */ private function validateArea($data) { // 获取代理商身份信息 $identity = IdentityModel::where('id', $data['agent_identity_id']) ->where('status', IdentityModel::STATUS_ENABLED) ->find(); if (!$identity) { throw new Exception('代理商身份不存在'); } // 根据代理商类型获取需要的地区字段 $requiredFields = AgentType::getRequiredAreaFields($identity->agent_type); if (empty($requiredFields)) { // 普通代理商不需要地区信息 return; } // 验证必需的地区字段 foreach ($requiredFields as $field) { if (empty($data[$field])) { $fieldTexts = [ 'province_id' => '省份', 'city_id' => '城市', 'district_id' => '区域' ]; throw new Exception($fieldTexts[$field] . '不能为空'); } } // 验证地区ID的有效性和层级关系 $this->validateAreaIds($data, $requiredFields); } /** * 验证地区ID的有效性和层级关系 */ private function validateAreaIds($data, $requiredFields) { // 验证省份 if (in_array('province_id', $requiredFields) && !empty($data['province_id'])) { $province = Area::where('id', $data['province_id'])->where('level', 1)->find(); if (!$province) { throw new Exception('选择的省份不存在'); } } // 验证城市 if (in_array('city_id', $requiredFields) && !empty($data['city_id'])) { $city = Area::where('id', $data['city_id'])->where('level', 2)->find(); if (!$city) { throw new Exception('选择的城市不存在'); } // 验证城市是否属于选择的省份 if (!empty($data['province_id']) && $city->pid != $data['province_id']) { throw new Exception('选择的城市不属于该省份'); } } // 验证区域 if (in_array('district_id', $requiredFields) && !empty($data['district_id'])) { $district = Area::where('id', $data['district_id'])->where('level', 3)->find(); if (!$district) { throw new Exception('选择的区域不存在'); } // 验证区域是否属于选择的城市 if (!empty($data['city_id']) && $district->pid != $data['city_id']) { throw new Exception('选择的区域不属于该城市'); } } } /** * 验证申请数据 */ private function validateApplyData($data) { if ($data['apply_type'] == ApplyModel::APPLY_TYPE_PERSONAL) { $requiredFields = ['real_name', 'id_card', 'id_card_front', 'id_card_back', 'mobile']; foreach ($requiredFields as $field) { if (empty($data[$field])) { throw new Exception('个人申请信息不完整'); } } // 验证身份证格式 if (!$this->validateIdCard($data['id_card'])) { throw new Exception('身份证号格式不正确'); } } else { $requiredFields = ['company_name', 'legal_person', 'legal_mobile', 'legal_id_card', 'legal_id_front', 'legal_id_back', 'business_license']; foreach ($requiredFields as $field) { if (empty($data[$field])) { throw new Exception('企业申请信息不完整'); } } // 验证法人身份证格式 if (!$this->validateIdCard($data['legal_id_card'])) { throw new Exception('法人身份证号格式不正确'); } } } /** * 验证身份证号格式 */ private function validateIdCard($idCard) { return preg_match('/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/', $idCard); } /** * 审核通过申请 */ public function approveApply(ApplyModel $apply, $adminId = null) { return Db::transaction(function () use ($apply, $adminId) { // 更新申请状态 $apply->status = ApplyModel::STATUS_APPROVED; $apply->admin_id = $adminId; $apply->audit_time = time(); $apply->save(); // 创建代理商记录 $this->createAgentFromApply($apply); return $apply; }); } /** * 审核拒绝申请 */ public function rejectApply(ApplyModel $apply, $reason, $adminId = null) { $apply->status = ApplyModel::STATUS_REJECTED; $apply->reject_reason = $reason; $apply->admin_id = $adminId; $apply->audit_time = time(); return $apply->save(); } /** * 根据申请信息创建代理商 */ private function createAgentFromApply(ApplyModel $apply) { // 检查用户是否已经是分销商 $existAgent = AgentModel::where('user_id', $apply->user_id)->find(); if ($existAgent) { return $existAgent; } // 获取身份信息 $identity = IdentityModel::where('id', $apply->agent_identity_id)->find(); // 创建分销商记录 $agent = new AgentModel(); $agent->user_id = $apply->user_id; $agent->level = 1; // 默认等级 $agent->agent_type = $apply->agent_type; $agent->status = AgentModel::AGENT_STATUS_NORMAL; $agent->become_time = time(); // 如果是区域代理商(省级、市级、区域级),设置管辖区域 if (AgentType::isRegionalAgent($apply->agent_type)) { // 根据代理商类型设置对应的管辖区域 if ($apply->agent_type == AgentType::PROVINCE && !empty($apply->province_id)) { $agent->manage_province_id = $apply->province_id; } elseif ($apply->agent_type == AgentType::CITY) { if (!empty($apply->province_id)) { $agent->manage_province_id = $apply->province_id; } if (!empty($apply->city_id)) { $agent->manage_city_id = $apply->city_id; } } elseif ($apply->agent_type == AgentType::DISTRICT) { if (!empty($apply->province_id)) { $agent->manage_province_id = $apply->province_id; } if (!empty($apply->city_id)) { $agent->manage_city_id = $apply->city_id; } if (!empty($apply->district_id)) { $agent->manage_district_id = $apply->district_id; } } } $agent->save(); return $agent; } /** * 获取用户的申请记录 */ public function getUserApply($userId) { return ApplyModel::where('user_id', $userId) ->with(['identity', 'province', 'city', 'district']) ->order('id desc') ->find(); } }