AgentApply.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace app\api\validate;
  3. use think\Validate;
  4. use app\common\model\commission\Apply as ApplyModel;
  5. use app\common\model\commission\Identity as IdentityModel;
  6. use app\common\Enum\AgentType;
  7. class AgentApply extends Validate
  8. {
  9. /**
  10. * 验证规则
  11. */
  12. protected $rule = [
  13. 'apply_type' => 'require|in:personal,company',
  14. 'agent_identity_id' => 'require|integer|gt:0',
  15. 'province_id' => 'integer|gt:0',
  16. 'city_id' => 'integer|gt:0',
  17. 'district_id' => 'integer|gt:0',
  18. 'province_name' => 'max:50',
  19. 'city_name' => 'max:50',
  20. 'district_name' => 'max:50',
  21. // 个人申请字段
  22. 'real_name' => 'length:2,30',
  23. '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]$/',
  24. 'mobile' => 'regex:/^1\d{10}$/',
  25. 'id_card_front' => 'max:255',
  26. 'id_card_back' => 'max:255',
  27. // 企业申请字段
  28. 'company_name' => 'length:2,100',
  29. 'legal_person' => 'length:2,30',
  30. 'legal_mobile' => 'regex:/^1\d{10}$/',
  31. '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]$/',
  32. 'legal_id_front' => 'max:255',
  33. 'legal_id_back' => 'max:255',
  34. 'business_license' => 'max:255',
  35. ];
  36. /**
  37. * 提示消息
  38. */
  39. protected $message = [
  40. 'apply_type.require' => '申请类型不能为空',
  41. 'apply_type.in' => '申请类型必须是personal或company',
  42. 'agent_identity_id.require' => '请选择代理商身份',
  43. 'agent_identity_id.integer' => '代理商身份ID格式错误',
  44. 'agent_identity_id.gt' => '代理商身份ID必须大于0',
  45. 'province_id.integer' => '省份ID格式错误',
  46. 'province_id.gt' => '省份ID必须大于0',
  47. 'city_id.integer' => '城市ID格式错误',
  48. 'city_id.gt' => '城市ID必须大于0',
  49. 'district_id.integer' => '区域ID格式错误',
  50. 'district_id.gt' => '区域ID必须大于0',
  51. 'province_name.max' => '省份名称最多50个字符',
  52. 'city_name.max' => '城市名称最多50个字符',
  53. 'district_name.max' => '区域名称最多50个字符',
  54. // 个人申请消息
  55. 'real_name.length' => '真实姓名长度必须在2-30个字符之间',
  56. 'id_card.regex' => '身份证号格式不正确',
  57. 'mobile.regex' => '手机号格式不正确',
  58. 'id_card_front.max' => '身份证正面图片路径过长',
  59. 'id_card_back.max' => '身份证反面图片路径过长',
  60. // 企业申请消息
  61. 'company_name.length' => '企业名称长度必须在2-100个字符之间',
  62. 'legal_person.length' => '法人姓名长度必须在2-30个字符之间',
  63. 'legal_mobile.regex' => '法人手机号格式不正确',
  64. 'legal_id_card.regex' => '法人身份证号格式不正确',
  65. 'legal_id_front.max' => '法人身份证正面图片路径过长',
  66. 'legal_id_back.max' => '法人身份证反面图片路径过长',
  67. 'business_license.max' => '营业执照图片路径过长',
  68. ];
  69. /**
  70. * 验证场景
  71. */
  72. protected $scene = [
  73. 'apply' => ['apply_type', 'agent_identity_id']
  74. ];
  75. /**
  76. * 自定义验证代理商申请数据
  77. * @param array $data
  78. * @return bool
  79. */
  80. public function sceneApply()
  81. {
  82. // 基础字段验证
  83. $this->rule['agent_identity_id'] = $this->rule['agent_identity_id'] . '|checkIdentityExists';
  84. $this->rule['apply_type'] = $this->rule['apply_type'] . '|checkRequiredFieldsByType';
  85. return $this;
  86. }
  87. /**
  88. * 检查代理商身份是否存在且启用
  89. * @param mixed $value
  90. * @param mixed $rule
  91. * @param array $data
  92. * @return bool|string
  93. */
  94. protected function checkIdentityExists($value, $rule, $data)
  95. {
  96. $identity = IdentityModel::where('id', $value)
  97. ->where('status', IdentityModel::STATUS_ENABLED)
  98. ->find();
  99. if (!$identity) {
  100. return '选择的代理商身份不存在或已禁用';
  101. }
  102. // 根据代理商身份类型检查地区字段
  103. $requiredFields = AgentType::getRequiredAreaFields($identity->agent_type);
  104. if (!empty($requiredFields)) {
  105. // 检查必需的地区字段
  106. foreach ($requiredFields as $field) {
  107. if (empty($data[$field])) {
  108. $fieldTexts = [
  109. 'province_id' => '省份',
  110. 'city_id' => '城市',
  111. 'district_id' => '区域'
  112. ];
  113. return $fieldTexts[$field] . 'ID不能为空';
  114. }
  115. }
  116. // 验证地区ID的有效性
  117. $areaCheck = $this->validateAreaIds($data, $requiredFields);
  118. if ($areaCheck !== true) {
  119. return $areaCheck;
  120. }
  121. }
  122. return true;
  123. }
  124. /**
  125. * 根据申请类型检查必需字段
  126. * @param mixed $value
  127. * @param mixed $rule
  128. * @param array $data
  129. * @return bool|string
  130. */
  131. protected function checkRequiredFieldsByType($value, $rule, $data)
  132. {
  133. if ($value == ApplyModel::APPLY_TYPE_PERSONAL) {
  134. // 个人申请必需字段
  135. $requiredFields = [
  136. 'real_name' => '真实姓名',
  137. 'id_card' => '身份证号',
  138. 'mobile' => '手机号',
  139. 'id_card_front' => '身份证正面照片',
  140. 'id_card_back' => '身份证反面照片'
  141. ];
  142. } else {
  143. // 企业申请必需字段
  144. $requiredFields = [
  145. 'company_name' => '企业名称',
  146. 'legal_person' => '法人姓名',
  147. 'legal_mobile' => '法人手机号',
  148. 'legal_id_card' => '法人身份证号',
  149. 'legal_id_front' => '法人身份证正面照片',
  150. 'legal_id_back' => '法人身份证反面照片',
  151. 'business_license' => '营业执照照片'
  152. ];
  153. }
  154. foreach ($requiredFields as $field => $fieldName) {
  155. if (!isset($data[$field]) || $data[$field] === '') {
  156. return $fieldName . '不能为空';
  157. }
  158. }
  159. return true;
  160. }
  161. /**
  162. * 验证地区ID的有效性
  163. * @param array $data
  164. * @param array $requiredFields 需要验证的字段
  165. * @return bool|string
  166. */
  167. protected function validateAreaIds($data, $requiredFields = ['province_id', 'city_id', 'district_id'])
  168. {
  169. $areaModel = new \app\common\model\Area();
  170. // 验证省份
  171. if (in_array('province_id', $requiredFields) && !empty($data['province_id'])) {
  172. $province = $areaModel->where('id', $data['province_id'])->where('level', 1)->find();
  173. if (!$province) {
  174. return '选择的省份不存在';
  175. }
  176. }
  177. // 验证城市
  178. if (in_array('city_id', $requiredFields) && !empty($data['city_id'])) {
  179. $city = $areaModel->where('id', $data['city_id'])->where('level', 2)->find();
  180. if (!$city) {
  181. return '选择的城市不存在';
  182. }
  183. // 验证城市是否属于选择的省份
  184. if (!empty($data['province_id']) && $city->pid != $data['province_id']) {
  185. return '选择的城市不属于该省份';
  186. }
  187. }
  188. // 验证区域
  189. if (in_array('district_id', $requiredFields) && !empty($data['district_id'])) {
  190. $district = $areaModel->where('id', $data['district_id'])->where('level', 3)->find();
  191. if (!$district) {
  192. return '选择的区域不存在';
  193. }
  194. // 验证区域是否属于选择的城市
  195. if (!empty($data['city_id']) && $district->pid != $data['city_id']) {
  196. return '选择的区域不属于该城市';
  197. }
  198. }
  199. return true;
  200. }
  201. /**
  202. * 获取代理商身份是否需要地区信息
  203. * @param int $identityId
  204. * @return array
  205. */
  206. public static function getAreaRequirement($identityId)
  207. {
  208. $identity = IdentityModel::where('id', $identityId)
  209. ->where('status', IdentityModel::STATUS_ENABLED)
  210. ->find();
  211. if (!$identity) {
  212. return [
  213. 'required' => false,
  214. 'fields' => [],
  215. 'agent_type' => '',
  216. 'agent_type_text' => '',
  217. 'message' => '代理商身份不存在或已禁用'
  218. ];
  219. }
  220. $requirementInfo = AgentType::getAreaRequirementInfo($identity->agent_type);
  221. return [
  222. 'required' => $requirementInfo['required'],
  223. 'fields' => $requirementInfo['fields'],
  224. 'agent_type' => $identity->agent_type,
  225. 'agent_type_text' => AgentType::getTypeText($identity->agent_type),
  226. 'message' => $requirementInfo['message']
  227. ];
  228. }
  229. }