AgentApply.php 8.2 KB

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