AgentApply.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. // 增强申请数据信息
  58. $data = $apply->toArray();
  59. $this->success('获取成功', $data);
  60. } catch (Exception $e) {
  61. $this->error($e->getMessage());
  62. }
  63. }
  64. /**
  65. * 验证申请参数
  66. */
  67. private function validateApplyParams($data)
  68. {
  69. $requiredFields = ['apply_type', 'agent_identity_id', 'province_id', 'city_id', 'district_id'];
  70. foreach ($requiredFields as $field) {
  71. if (!isset($data[$field]) || $data[$field] === '') {
  72. throw new Exception('参数' . $field . '不能为空');
  73. }
  74. }
  75. if (!in_array($data['apply_type'], [ApplyModel::APPLY_TYPE_PERSONAL, ApplyModel::APPLY_TYPE_COMPANY])) {
  76. throw new Exception('申请类型不正确');
  77. }
  78. }
  79. }