DivisionController.php 4.7 KB

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