Pārlūkot izejas kodu

fix:代理商身份

super-yimizi 14 stundas atpakaļ
vecāks
revīzija
c2d1fde40b

+ 27 - 24
application/api/controller/commission/AgentApply.php

@@ -4,11 +4,12 @@ namespace app\api\controller\commission;
 
 use app\common\Service\Commission\AgentApply as AgentApplyService;
 use app\common\model\commission\Apply as ApplyModel;
+use app\api\validate\AgentApply as AgentApplyValidate;
 use think\Exception;
 use app\api\controller\Base;
 class AgentApply extends Base
 {
-    protected $noNeedLogin = ['identities', 'areas'];
+    protected $noNeedLogin = ['identities', 'areas', 'checkAreaRequirement'];
     protected $noNeedRight = ['*'];
 
     /**
@@ -26,6 +27,25 @@ class AgentApply extends Base
     }
 
     /**
+     * 检查代理商身份是否需要地区信息
+     */
+    public function checkAreaRequirement()
+    {
+        try {
+            $identityId = $this->request->param('agent_identity_id');
+            
+            if (empty($identityId)) {
+                $this->error('代理商身份ID不能为空');
+            }
+            
+            $requirement = AgentApplyValidate::getAreaRequirement($identityId);
+            $this->success('获取成功', $requirement);
+        } catch (Exception $e) {
+            $this->error($e->getMessage());
+        }
+    }
+
+    /**
      * 提交代理商申请
      */
     public function apply()
@@ -34,12 +54,15 @@ class AgentApply extends Base
             $user = auth_user();
             $data = $this->request->param();
             
-            // 验证必要参数
-            $this->validateApplyParams($data);
-            
+            // 使用验证器验证参数
+            $validate = new AgentApplyValidate();
+            if (!$validate->scene('apply')->check($data)) {
+                $this->error($validate->getError());
+            }            
             $service = new AgentApplyService();
             $apply = $service->submitApply($user->id, $data);
             
+            
             if ($apply->status == ApplyModel::STATUS_APPROVED) {
                 $this->success('申请提交成功,您已成为代理商!', $apply);
             } else {
@@ -66,30 +89,10 @@ class AgentApply extends Base
             
             // 增强申请数据信息
             $data = $apply->toArray();
-            
-          
-            
             $this->success('获取成功', $data);
         } catch (Exception $e) {
             $this->error($e->getMessage());
         }
     }
 
-    /**
-     * 验证申请参数
-     */
-    private function validateApplyParams($data)
-    {
-        $requiredFields = ['apply_type', 'agent_identity_id', 'province_id', 'city_id', 'district_id'];
-        
-        foreach ($requiredFields as $field) {
-            if (!isset($data[$field]) || $data[$field] === '') {
-                throw new Exception('参数' . $field . '不能为空');
-            }
-        }
-        
-        if (!in_array($data['apply_type'], [ApplyModel::APPLY_TYPE_PERSONAL, ApplyModel::APPLY_TYPE_COMPANY])) {
-            throw new Exception('申请类型不正确');
-        }
-    }
 }

+ 259 - 0
application/api/validate/AgentApply.php

@@ -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']
+        ];
+    }
+}

+ 63 - 0
application/common/Enum/AgentType.php

@@ -56,4 +56,67 @@ class AgentType
     {
         return [self::PROVINCE, self::CITY, self::DISTRICT];
     }
+
+    /**
+     * 获取指定代理商类型需要的地区字段
+     * @param string $type 代理商类型
+     * @return array 需要的地区字段数组
+     */
+    public static function getRequiredAreaFields($type)
+    {
+        switch ($type) {
+            case self::NORMAL:
+                return []; // 普通代理商不需要地区信息
+            case self::PROVINCE:
+                return ['province_id']; // 省级代理商需要省ID
+            case self::CITY:
+                return ['province_id', 'city_id']; // 市级代理商需要省和市ID
+            case self::DISTRICT:
+                return ['province_id', 'city_id', 'district_id']; // 区级代理商需要省市区ID
+            default:
+                return [];
+        }
+    }
+
+    /**
+     * 检查代理商类型是否需要指定的地区字段
+     * @param string $type 代理商类型
+     * @param string $field 地区字段名
+     * @return bool
+     */
+    public static function needsAreaField($type, $field)
+    {
+        $requiredFields = self::getRequiredAreaFields($type);
+        return in_array($field, $requiredFields);
+    }
+
+    /**
+     * 获取代理商类型的地区需求描述
+     * @param string $type 代理商类型
+     * @return array
+     */
+    public static function getAreaRequirementInfo($type)
+    {
+        $requiredFields = self::getRequiredAreaFields($type);
+        $typeText = self::getTypeText($type);
+        
+        if (empty($requiredFields)) {
+            return [
+                'required' => false,
+                'fields' => [],
+                'message' => $typeText . '无需选择管辖区域'
+            ];
+        }
+
+        $fieldTexts = [];
+        if (in_array('province_id', $requiredFields)) $fieldTexts[] = '省份';
+        if (in_array('city_id', $requiredFields)) $fieldTexts[] = '城市';
+        if (in_array('district_id', $requiredFields)) $fieldTexts[] = '区域';
+
+        return [
+            'required' => true,
+            'fields' => $requiredFields,
+            'message' => $typeText . '需要选择' . implode('、', $fieldTexts)
+        ];
+    }
 }

+ 98 - 22
application/common/Service/Commission/AgentApply.php

@@ -60,14 +60,25 @@ class AgentApply
             $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; // 设置初始状态为待审核
             
-            // 地区信息
-            $apply->province_id = $data['province_id'];
-            $apply->city_id = $data['city_id'];
-            $apply->district_id = $data['district_id'];
-            $apply->province_name = $data['province_name'] ?? '';
-            $apply->city_name = $data['city_name'] ?? '';
-            $apply->district_name = $data['district_name'] ?? '';
+            // 地区信息 - 根据代理商类型设置需要的地区字段
+            $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) {
@@ -95,7 +106,6 @@ class AgentApply
                 // 不需要审核,直接通过
                 $this->approveApply($apply);
             }
-
             return $apply;
         });
     }
@@ -105,20 +115,75 @@ class AgentApply
      */
     private function validateArea($data)
     {
-        $requiredFields = ['province_id', 'city_id', 'district_id'];
+        // 获取代理商身份信息
+        $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])) {
-                throw new Exception('请选择完整的省市区信息');
+                $fieldTexts = [
+                    'province_id' => '省份',
+                    'city_id' => '城市', 
+                    'district_id' => '区域'
+                ];
+                throw new Exception($fieldTexts[$field] . '不能为空');
             }
         }
 
-        // 验证地区ID是否存在
-        $province = Area::where('id', $data['province_id'])->where('level', 1)->find();
-        $city = Area::where('id', $data['city_id'])->where('level', 2)->find();
-        $district = Area::where('id', $data['district_id'])->where('level', 3)->find();
+        // 验证地区ID的有效性和层级关系
+        $this->validateAreaIds($data, $requiredFields);
+    }
 
-        if (!$province || !$city || !$district) {
-            throw new Exception('选择的地区信息不正确');
+    /**
+     * 验证地区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('选择的区域不属于该城市');
+            }
         }
     }
 
@@ -218,15 +283,25 @@ class AgentApply
         // 如果是区域代理商(省级、市级、区域级),设置管辖区域
         if (AgentType::isRegionalAgent($apply->agent_type)) {
             // 根据代理商类型设置对应的管辖区域
-            if ($apply->agent_type == AgentType::PROVINCE) {
+            if ($apply->agent_type == AgentType::PROVINCE && !empty($apply->province_id)) {
                 $agent->manage_province_id = $apply->province_id;
             } elseif ($apply->agent_type == AgentType::CITY) {
-                $agent->manage_province_id = $apply->province_id;
-                $agent->manage_city_id = $apply->city_id;
+                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) {
-                $agent->manage_province_id = $apply->province_id;
-                $agent->manage_city_id = $apply->city_id;
-                $agent->manage_district_id = $apply->district_id;
+                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;
+                }
             }
         }
 
@@ -246,3 +321,4 @@ class AgentApply
                          ->find();
     }
 }
+

+ 9 - 0
application/common/model/commission/Apply.php

@@ -11,6 +11,15 @@ class Apply extends Model
 {
     protected $name = 'shop_commission_apply';
     
+    // 添加默认字段值,防止字段不存在错误
+    protected $field = [
+        'id', 'user_id', 'apply_type', 'agent_identity_id', 'agent_type',
+        'province_id', 'city_id', 'district_id', 'province_name', 'city_name', 'district_name',
+        'real_name', 'id_card', 'id_card_front', 'id_card_back', 'mobile',
+        'company_name', 'legal_person', 'legal_mobile', 'legal_id_card', 'legal_id_front', 'legal_id_back', 'business_license',
+        'status', 'reject_reason', 'admin_id', 'audit_time', 'createtime', 'updatetime'
+    ];
+    
     // 开启自动写入时间戳字段
     protected $autoWriteTimestamp = 'int';