AgentApply.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace app\common\Service\commission;
  3. use app\common\model\commission\Apply as ApplyModel;
  4. use app\common\model\commission\Identity as IdentityModel;
  5. use app\common\model\commission\Agent as AgentModel;
  6. use app\common\model\Area;
  7. use app\common\Service\ShopConfigService;
  8. use think\Db;
  9. use think\Exception;
  10. class AgentApply
  11. {
  12. /**
  13. * 获取可用的代理商身份列表
  14. */
  15. public function getIdentityList()
  16. {
  17. return IdentityModel::getEnabledList();
  18. }
  19. /**
  20. * 提交代理商申请
  21. */
  22. public function submitApply($userId, $data)
  23. {
  24. // 验证用户是否已经有申请记录
  25. $existApply = ApplyModel::where('user_id', $userId)
  26. ->where('status', 'in', ['pending', 'approved'])
  27. ->find();
  28. if ($existApply) {
  29. if ($existApply->status == 'approved') {
  30. throw new Exception('您已经是代理商,无需重复申请');
  31. } else {
  32. throw new Exception('您已有待审核的申请,请等待审核结果');
  33. }
  34. }
  35. // 验证身份是否存在
  36. $identity = IdentityModel::where('id', $data['agent_identity_id'])
  37. ->where('status', IdentityModel::STATUS_ENABLED)
  38. ->find();
  39. if (!$identity) {
  40. throw new Exception('选择的代理商身份不存在或已禁用');
  41. }
  42. // 验证地区信息
  43. $this->validateArea($data);
  44. // 根据申请类型验证必要字段
  45. $this->validateApplyData($data);
  46. return Db::transaction(function () use ($userId, $data, $identity) {
  47. // 创建申请记录
  48. $apply = new ApplyModel();
  49. $apply->user_id = $userId;
  50. $apply->apply_type = $data['apply_type'];
  51. $apply->agent_identity_id = $data['agent_identity_id'];
  52. $apply->agent_type = $identity->agent_type; // 从身份配置获取代理商类型
  53. // 地区信息
  54. $apply->province_id = $data['province_id'];
  55. $apply->city_id = $data['city_id'];
  56. $apply->district_id = $data['district_id'];
  57. $apply->province_name = $data['province_name'] ?? '';
  58. $apply->city_name = $data['city_name'] ?? '';
  59. $apply->district_name = $data['district_name'] ?? '';
  60. // 根据申请类型填充对应字段
  61. if ($data['apply_type'] == ApplyModel::APPLY_TYPE_PERSONAL) {
  62. $apply->real_name = $data['real_name'];
  63. $apply->id_card = $data['id_card'];
  64. $apply->id_card_front = $data['id_card_front'];
  65. $apply->id_card_back = $data['id_card_back'];
  66. $apply->mobile = $data['mobile'];
  67. } else {
  68. $apply->company_name = $data['company_name'];
  69. $apply->legal_person = $data['legal_person'];
  70. $apply->legal_mobile = $data['legal_mobile'];
  71. $apply->legal_id_card = $data['legal_id_card'];
  72. $apply->legal_id_front = $data['legal_id_front'];
  73. $apply->legal_id_back = $data['legal_id_back'];
  74. $apply->business_license = $data['business_license'];
  75. }
  76. $apply->save();
  77. // 检查是否需要审核
  78. $needCheck = ShopConfigService::getConfigField('shop.commission.agent_apply_check');
  79. if (!$needCheck) {
  80. // 不需要审核,直接通过
  81. $this->approveApply($apply);
  82. }
  83. return $apply;
  84. });
  85. }
  86. /**
  87. * 验证地区信息
  88. */
  89. private function validateArea($data)
  90. {
  91. $requiredFields = ['province_id', 'city_id', 'district_id'];
  92. foreach ($requiredFields as $field) {
  93. if (empty($data[$field])) {
  94. throw new Exception('请选择完整的省市区信息');
  95. }
  96. }
  97. // 验证地区ID是否存在
  98. $province = Area::where('id', $data['province_id'])->where('level', 1)->find();
  99. $city = Area::where('id', $data['city_id'])->where('level', 2)->find();
  100. $district = Area::where('id', $data['district_id'])->where('level', 3)->find();
  101. if (!$province || !$city || !$district) {
  102. throw new Exception('选择的地区信息不正确');
  103. }
  104. }
  105. /**
  106. * 验证申请数据
  107. */
  108. private function validateApplyData($data)
  109. {
  110. if ($data['apply_type'] == ApplyModel::APPLY_TYPE_PERSONAL) {
  111. $requiredFields = ['real_name', 'id_card', 'id_card_front', 'id_card_back', 'mobile'];
  112. foreach ($requiredFields as $field) {
  113. if (empty($data[$field])) {
  114. throw new Exception('个人申请信息不完整');
  115. }
  116. }
  117. // 验证身份证格式
  118. if (!$this->validateIdCard($data['id_card'])) {
  119. throw new Exception('身份证号格式不正确');
  120. }
  121. } else {
  122. $requiredFields = ['company_name', 'legal_person', 'legal_mobile', 'legal_id_card', 'legal_id_front', 'legal_id_back', 'business_license'];
  123. foreach ($requiredFields as $field) {
  124. if (empty($data[$field])) {
  125. throw new Exception('企业申请信息不完整');
  126. }
  127. }
  128. // 验证法人身份证格式
  129. if (!$this->validateIdCard($data['legal_id_card'])) {
  130. throw new Exception('法人身份证号格式不正确');
  131. }
  132. }
  133. }
  134. /**
  135. * 验证身份证号格式
  136. */
  137. private function validateIdCard($idCard)
  138. {
  139. 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);
  140. }
  141. /**
  142. * 审核通过申请
  143. */
  144. public function approveApply(ApplyModel $apply, $adminId = null)
  145. {
  146. return Db::transaction(function () use ($apply, $adminId) {
  147. // 更新申请状态
  148. $apply->status = ApplyModel::STATUS_APPROVED;
  149. $apply->admin_id = $adminId;
  150. $apply->audit_time = time();
  151. $apply->save();
  152. // 创建代理商记录
  153. $this->createAgentFromApply($apply);
  154. return $apply;
  155. });
  156. }
  157. /**
  158. * 审核拒绝申请
  159. */
  160. public function rejectApply(ApplyModel $apply, $reason, $adminId = null)
  161. {
  162. $apply->status = ApplyModel::STATUS_REJECTED;
  163. $apply->reject_reason = $reason;
  164. $apply->admin_id = $adminId;
  165. $apply->audit_time = time();
  166. return $apply->save();
  167. }
  168. /**
  169. * 根据申请信息创建代理商
  170. */
  171. private function createAgentFromApply(ApplyModel $apply)
  172. {
  173. // 检查用户是否已经是分销商
  174. $existAgent = AgentModel::where('user_id', $apply->user_id)->find();
  175. if ($existAgent) {
  176. return $existAgent;
  177. }
  178. // 获取身份信息
  179. $identity = IdentityModel::where('id', $apply->agent_identity_id)->find();
  180. // 创建分销商记录
  181. $agent = new AgentModel();
  182. $agent->user_id = $apply->user_id;
  183. $agent->level = 1; // 默认等级
  184. $agent->agent_type = $apply->agent_type;
  185. $agent->status = AgentModel::AGENT_STATUS_NORMAL;
  186. $agent->become_time = time();
  187. // 如果是区域代理商,设置管辖区域
  188. if ($apply->agent_type == 'regional') {
  189. $agent->manage_province_id = $apply->province_id;
  190. $agent->manage_city_id = $apply->city_id;
  191. $agent->manage_district_id = $apply->district_id;
  192. }
  193. $agent->save();
  194. return $agent;
  195. }
  196. /**
  197. * 获取用户的申请记录
  198. */
  199. public function getUserApply($userId)
  200. {
  201. return ApplyModel::where('user_id', $userId)
  202. ->with(['identity', 'province', 'city', 'district'])
  203. ->order('id desc')
  204. ->find();
  205. }
  206. }