|
@@ -0,0 +1,259 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\api\validate;
|
|
|
+
|
|
|
+use think\Validate;
|
|
|
+use app\common\model\commission\Apply as ApplyModel;
|
|
|
+use app\common\model\commission\Identity as IdentityModel;
|
|
|
+use app\common\Enum\AgentType;
|
|
|
+
|
|
|
+class AgentApply extends Validate
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 验证规则
|
|
|
+ */
|
|
|
+ protected $rule = [
|
|
|
+ 'apply_type' => 'require|in:personal,company',
|
|
|
+ 'agent_identity_id' => 'require|integer|gt:0',
|
|
|
+ 'province_id' => 'integer|gt:0',
|
|
|
+ 'city_id' => 'integer|gt:0',
|
|
|
+ 'district_id' => 'integer|gt:0',
|
|
|
+ 'province_name' => 'max:50',
|
|
|
+ 'city_name' => 'max:50',
|
|
|
+ 'district_name' => 'max:50',
|
|
|
+
|
|
|
+ // 个人申请字段
|
|
|
+ 'real_name' => 'length:2,30',
|
|
|
+ 'id_card' => 'regex:/^[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]$/',
|
|
|
+ 'mobile' => 'regex:/^1\d{10}$/',
|
|
|
+ 'id_card_front' => 'max:255',
|
|
|
+ 'id_card_back' => 'max:255',
|
|
|
+
|
|
|
+ // 企业申请字段
|
|
|
+ 'company_name' => 'length:2,100',
|
|
|
+ 'legal_person' => 'length:2,30',
|
|
|
+ 'legal_mobile' => 'regex:/^1\d{10}$/',
|
|
|
+ 'legal_id_card' => 'regex:/^[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]$/',
|
|
|
+ 'legal_id_front' => 'max:255',
|
|
|
+ 'legal_id_back' => 'max:255',
|
|
|
+ 'business_license' => 'max:255',
|
|
|
+ ];
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 提示消息
|
|
|
+ */
|
|
|
+ protected $message = [
|
|
|
+ 'apply_type.require' => '申请类型不能为空',
|
|
|
+ 'apply_type.in' => '申请类型必须是personal或company',
|
|
|
+ 'agent_identity_id.require' => '请选择代理商身份',
|
|
|
+ 'agent_identity_id.integer' => '代理商身份ID格式错误',
|
|
|
+ 'agent_identity_id.gt' => '代理商身份ID必须大于0',
|
|
|
+ 'province_id.integer' => '省份ID格式错误',
|
|
|
+ 'province_id.gt' => '省份ID必须大于0',
|
|
|
+ 'city_id.integer' => '城市ID格式错误',
|
|
|
+ 'city_id.gt' => '城市ID必须大于0',
|
|
|
+ 'district_id.integer' => '区域ID格式错误',
|
|
|
+ 'district_id.gt' => '区域ID必须大于0',
|
|
|
+ 'province_name.max' => '省份名称最多50个字符',
|
|
|
+ 'city_name.max' => '城市名称最多50个字符',
|
|
|
+ 'district_name.max' => '区域名称最多50个字符',
|
|
|
+
|
|
|
+ // 个人申请消息
|
|
|
+ 'real_name.length' => '真实姓名长度必须在2-30个字符之间',
|
|
|
+ 'id_card.regex' => '身份证号格式不正确',
|
|
|
+ 'mobile.regex' => '手机号格式不正确',
|
|
|
+ 'id_card_front.max' => '身份证正面图片路径过长',
|
|
|
+ 'id_card_back.max' => '身份证反面图片路径过长',
|
|
|
+
|
|
|
+ // 企业申请消息
|
|
|
+ 'company_name.length' => '企业名称长度必须在2-100个字符之间',
|
|
|
+ 'legal_person.length' => '法人姓名长度必须在2-30个字符之间',
|
|
|
+ 'legal_mobile.regex' => '法人手机号格式不正确',
|
|
|
+ 'legal_id_card.regex' => '法人身份证号格式不正确',
|
|
|
+ 'legal_id_front.max' => '法人身份证正面图片路径过长',
|
|
|
+ 'legal_id_back.max' => '法人身份证反面图片路径过长',
|
|
|
+ 'business_license.max' => '营业执照图片路径过长',
|
|
|
+ ];
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证场景
|
|
|
+ */
|
|
|
+ protected $scene = [
|
|
|
+ 'apply' => ['apply_type', 'agent_identity_id']
|
|
|
+ ];
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 自定义验证代理商申请数据
|
|
|
+ * @param array $data
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function sceneApply()
|
|
|
+ {
|
|
|
+ // 基础字段验证
|
|
|
+ $this->rule['agent_identity_id'] = $this->rule['agent_identity_id'] . '|checkIdentityExists';
|
|
|
+ $this->rule['apply_type'] = $this->rule['apply_type'] . '|checkRequiredFieldsByType';
|
|
|
+
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查代理商身份是否存在且启用
|
|
|
+ * @param mixed $value
|
|
|
+ * @param mixed $rule
|
|
|
+ * @param array $data
|
|
|
+ * @return bool|string
|
|
|
+ */
|
|
|
+ protected function checkIdentityExists($value, $rule, $data)
|
|
|
+ {
|
|
|
+ $identity = IdentityModel::where('id', $value)
|
|
|
+ ->where('status', IdentityModel::STATUS_ENABLED)
|
|
|
+ ->find();
|
|
|
+ if (!$identity) {
|
|
|
+ return '选择的代理商身份不存在或已禁用';
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据代理商身份类型检查地区字段
|
|
|
+ $requiredFields = AgentType::getRequiredAreaFields($identity->agent_type);
|
|
|
+
|
|
|
+ if (!empty($requiredFields)) {
|
|
|
+ // 检查必需的地区字段
|
|
|
+ foreach ($requiredFields as $field) {
|
|
|
+ if (empty($data[$field])) {
|
|
|
+ $fieldTexts = [
|
|
|
+ 'province_id' => '省份',
|
|
|
+ 'city_id' => '城市',
|
|
|
+ 'district_id' => '区域'
|
|
|
+ ];
|
|
|
+ return $fieldTexts[$field] . 'ID不能为空';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证地区ID的有效性
|
|
|
+ $areaCheck = $this->validateAreaIds($data, $requiredFields);
|
|
|
+ if ($areaCheck !== true) {
|
|
|
+ return $areaCheck;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据申请类型检查必需字段
|
|
|
+ * @param mixed $value
|
|
|
+ * @param mixed $rule
|
|
|
+ * @param array $data
|
|
|
+ * @return bool|string
|
|
|
+ */
|
|
|
+ protected function checkRequiredFieldsByType($value, $rule, $data)
|
|
|
+ {
|
|
|
+ if ($value == ApplyModel::APPLY_TYPE_PERSONAL) {
|
|
|
+ // 个人申请必需字段
|
|
|
+ $requiredFields = [
|
|
|
+ 'real_name' => '真实姓名',
|
|
|
+ 'id_card' => '身份证号',
|
|
|
+ 'mobile' => '手机号',
|
|
|
+ 'id_card_front' => '身份证正面照片',
|
|
|
+ 'id_card_back' => '身份证反面照片'
|
|
|
+ ];
|
|
|
+ } else {
|
|
|
+ // 企业申请必需字段
|
|
|
+ $requiredFields = [
|
|
|
+ 'company_name' => '企业名称',
|
|
|
+ 'legal_person' => '法人姓名',
|
|
|
+ 'legal_mobile' => '法人手机号',
|
|
|
+ 'legal_id_card' => '法人身份证号',
|
|
|
+ 'legal_id_front' => '法人身份证正面照片',
|
|
|
+ 'legal_id_back' => '法人身份证反面照片',
|
|
|
+ 'business_license' => '营业执照照片'
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach ($requiredFields as $field => $fieldName) {
|
|
|
+ if (!isset($data[$field]) || $data[$field] === '') {
|
|
|
+ return $fieldName . '不能为空';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证地区ID的有效性
|
|
|
+ * @param array $data
|
|
|
+ * @param array $requiredFields 需要验证的字段
|
|
|
+ * @return bool|string
|
|
|
+ */
|
|
|
+ protected function validateAreaIds($data, $requiredFields = ['province_id', 'city_id', 'district_id'])
|
|
|
+ {
|
|
|
+ $areaModel = new \app\common\model\Area();
|
|
|
+
|
|
|
+ // 验证省份
|
|
|
+ if (in_array('province_id', $requiredFields) && !empty($data['province_id'])) {
|
|
|
+ $province = $areaModel->where('id', $data['province_id'])->where('level', 1)->find();
|
|
|
+ if (!$province) {
|
|
|
+ return '选择的省份不存在';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证城市
|
|
|
+ if (in_array('city_id', $requiredFields) && !empty($data['city_id'])) {
|
|
|
+ $city = $areaModel->where('id', $data['city_id'])->where('level', 2)->find();
|
|
|
+ if (!$city) {
|
|
|
+ return '选择的城市不存在';
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证城市是否属于选择的省份
|
|
|
+ if (!empty($data['province_id']) && $city->pid != $data['province_id']) {
|
|
|
+ return '选择的城市不属于该省份';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证区域
|
|
|
+ if (in_array('district_id', $requiredFields) && !empty($data['district_id'])) {
|
|
|
+ $district = $areaModel->where('id', $data['district_id'])->where('level', 3)->find();
|
|
|
+ if (!$district) {
|
|
|
+ return '选择的区域不存在';
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证区域是否属于选择的城市
|
|
|
+ if (!empty($data['city_id']) && $district->pid != $data['city_id']) {
|
|
|
+ return '选择的区域不属于该城市';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取代理商身份是否需要地区信息
|
|
|
+ * @param int $identityId
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public static function getAreaRequirement($identityId)
|
|
|
+ {
|
|
|
+ $identity = IdentityModel::where('id', $identityId)
|
|
|
+ ->where('status', IdentityModel::STATUS_ENABLED)
|
|
|
+ ->find();
|
|
|
+
|
|
|
+ if (!$identity) {
|
|
|
+ return [
|
|
|
+ 'required' => false,
|
|
|
+ 'fields' => [],
|
|
|
+ 'agent_type' => '',
|
|
|
+ 'agent_type_text' => '',
|
|
|
+ 'message' => '代理商身份不存在或已禁用'
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ $requirementInfo = AgentType::getAreaRequirementInfo($identity->agent_type);
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'required' => $requirementInfo['required'],
|
|
|
+ 'fields' => $requirementInfo['fields'],
|
|
|
+ 'agent_type' => $identity->agent_type,
|
|
|
+ 'agent_type_text' => AgentType::getTypeText($identity->agent_type),
|
|
|
+ 'message' => $requirementInfo['message']
|
|
|
+ ];
|
|
|
+ }
|
|
|
+}
|