AgentApply.php 10 KB

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