DivisionController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace app\api\controller\v1\user;
  3. use app\Request;
  4. use app\services\agent\DivisionAgentApplyServices;
  5. use app\services\other\AgreementServices;
  6. use app\services\user\UserServices;
  7. use crmeb\services\CacheService;
  8. use think\db\exception\DataNotFoundException;
  9. use think\db\exception\DbException;
  10. use think\db\exception\ModelNotFoundException;
  11. class DivisionController
  12. {
  13. protected $services;
  14. /**
  15. * DivisionController constructor.
  16. * @param DivisionAgentApplyServices $services
  17. */
  18. public function __construct(DivisionAgentApplyServices $services)
  19. {
  20. $this->services = $services;
  21. }
  22. /**
  23. * 申请代理商
  24. * @param Request $request
  25. * @param $id
  26. * @return mixed
  27. */
  28. public function applyAgent(Request $request, $id)
  29. {
  30. $data = $request->postMore([
  31. ['uid', 0],
  32. ['agent_name', ''],
  33. ['name', ''],
  34. ['phone', 0],
  35. ['code', 0],
  36. ['division_invite', 0],
  37. ['images', []]
  38. ]);
  39. $verifyCode = CacheService::get('code_' . $data['phone']);
  40. if ($verifyCode != $data['code']) return app('json')->fail(410010);
  41. if ($data['division_invite'] == 0) return app('json')->fail(500028);
  42. $this->services->applyAgent($data, $id);
  43. return app('json')->success(100017);
  44. }
  45. /**
  46. * 申请详情
  47. * @param Request $request
  48. * @return mixed
  49. * @throws DataNotFoundException
  50. * @throws DbException
  51. * @throws ModelNotFoundException
  52. */
  53. public function applyInfo(Request $request)
  54. {
  55. $uid = $request->uid();
  56. $data = $this->services->applyInfo($uid);
  57. return app('json')->success($data);
  58. }
  59. /**
  60. * 移动端获取规则
  61. * @param AgreementServices $agreementServices
  62. * @return mixed
  63. * @throws DataNotFoundException
  64. * @throws DbException
  65. * @throws ModelNotFoundException
  66. */
  67. public function getAgentAgreement(AgreementServices $agreementServices)
  68. {
  69. $data = $agreementServices->getAgreementBytype(2);
  70. return app('json')->success($data);
  71. }
  72. /**
  73. * 员工列表
  74. * @param Request $request
  75. * @return mixed
  76. * @throws DataNotFoundException
  77. * @throws DbException
  78. * @throws ModelNotFoundException
  79. */
  80. public function getStaffList(Request $request)
  81. {
  82. $where = $request->postMore([
  83. ['keyword', ''],
  84. ['sort', ''],
  85. ]);
  86. $where['agent_id'] = $request->uid();
  87. return app('json')->success($this->services->getStaffList($request->user(), $where));
  88. }
  89. /**
  90. * 设置员工比例
  91. * @param Request $request
  92. * @return mixed
  93. */
  94. public function setStaffPercent(Request $request)
  95. {
  96. [$agentPercent, $uid] = $request->postMore([
  97. ['agent_percent', ''],
  98. ['uid', 0],
  99. ], true);
  100. $agentId = $request->uid();
  101. if (!$uid) return app('json')->fail(100100);
  102. /** @var UserServices $userService */
  103. $userService = app()->make(UserServices::class);
  104. $upPercent = $userService->value(['uid' => $agentId], 'division_percent');
  105. if ($agentPercent >= $upPercent) return app('json')->fail(410164);
  106. $userService->update(['uid' => $uid, 'agent_id' => $agentId], ['division_percent' => $agentPercent]);
  107. return app('json')->success(100014);
  108. }
  109. /**
  110. * 删除员工
  111. * @param Request $request
  112. * @param $uid
  113. * @return mixed
  114. */
  115. public function delStaff(Request $request, $uid)
  116. {
  117. if (!$uid) return app('json')->fail(100100);
  118. $agentId = $request->uid();
  119. /** @var UserServices $userService */
  120. $userService = app()->make(UserServices::class);
  121. $userService->update(['uid' => $uid, 'agent_id' => $agentId], ['division_percent' => 0, 'agent_id' => 0, 'division_id' => 0, 'staff_id' => 0, 'division_type' => 0, 'is_staff' => 0]);
  122. return app('json')->success(100002);
  123. }
  124. }