AgentApply.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace app\api\controller\commission;
  3. use app\common\Service\Commission\AgentApply as AgentApplyService;
  4. use app\common\model\commission\Apply as ApplyModel;
  5. use think\Exception;
  6. use app\api\controller\Base;
  7. class AgentApply extends Base
  8. {
  9. protected $noNeedLogin = ['identities', 'areas'];
  10. protected $noNeedRight = ['*'];
  11. /**
  12. * 获取代理商身份列表
  13. */
  14. public function identities()
  15. {
  16. try {
  17. $service = new AgentApplyService();
  18. $identities = $service->getIdentityList();
  19. $this->success('获取成功', $identities);
  20. } catch (Exception $e) {
  21. $this->error($e->getMessage());
  22. }
  23. }
  24. /**
  25. * 提交代理商申请
  26. */
  27. public function apply()
  28. {
  29. try {
  30. $user = auth_user();
  31. $data = $this->request->param();
  32. // 验证必要参数
  33. $this->validateApplyParams($data);
  34. $service = new AgentApplyService();
  35. $apply = $service->submitApply($user->id, $data);
  36. if ($apply->status == ApplyModel::STATUS_APPROVED) {
  37. $this->success('申请提交成功,您已成为代理商!', $apply);
  38. } else {
  39. $this->success('申请提交成功,请等待审核!', $apply);
  40. }
  41. } catch (Exception $e) {
  42. $this->error($e->getMessage());
  43. }
  44. }
  45. /**
  46. * 获取申请状态
  47. */
  48. public function status()
  49. {
  50. try {
  51. $user = auth_user();
  52. $service = new AgentApplyService();
  53. $apply = $service->getUserApply($user->id);
  54. if (!$apply) {
  55. $this->success('未找到申请记录', null);
  56. }
  57. $this->success('获取成功', $apply);
  58. } catch (Exception $e) {
  59. $this->error($e->getMessage());
  60. }
  61. }
  62. /**
  63. * 验证申请参数
  64. */
  65. private function validateApplyParams($data)
  66. {
  67. $requiredFields = ['apply_type', 'agent_identity_id', 'province_id', 'city_id', 'district_id'];
  68. foreach ($requiredFields as $field) {
  69. if (!isset($data[$field]) || $data[$field] === '') {
  70. throw new Exception('参数' . $field . '不能为空');
  71. }
  72. }
  73. if (!in_array($data['apply_type'], [ApplyModel::APPLY_TYPE_PERSONAL, ApplyModel::APPLY_TYPE_COMPANY])) {
  74. throw new Exception('申请类型不正确');
  75. }
  76. }
  77. }