DivisionController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\api\controller\v1\user;
  12. use app\Request;
  13. use app\services\agent\DivisionAgentApplyServices;
  14. use app\services\agent\DivisionServices;
  15. use app\services\other\AgreementServices;
  16. use app\services\user\UserServices;
  17. use crmeb\services\CacheService;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\DbException;
  20. use think\db\exception\ModelNotFoundException;
  21. class DivisionController
  22. {
  23. protected $services;
  24. /**
  25. * DivisionController constructor.
  26. * @param DivisionAgentApplyServices $services
  27. */
  28. public function __construct(DivisionAgentApplyServices $services)
  29. {
  30. $this->services = $services;
  31. }
  32. /**
  33. * 申请代理商
  34. * @param Request $request
  35. * @param $id
  36. * @return mixed
  37. */
  38. public function applyAgent(Request $request, $id)
  39. {
  40. $data = $request->postMore([
  41. ['uid', 0],
  42. ['agent_name', ''],
  43. ['name', ''],
  44. ['phone', 0],
  45. ['code', 0],
  46. ['division_invite', 0],
  47. ['images', []]
  48. ]);
  49. $verifyCode = CacheService::get('code_' . $data['phone']);
  50. if ($verifyCode != $data['code']) return app('json')->fail('验证码错误');
  51. if ($data['division_invite'] == 0) return app('json')->fail('请填写邀请码');
  52. $this->services->applyAgent($data, $id);
  53. return app('json')->success('提交成功');
  54. }
  55. /**
  56. * 申请详情
  57. * @param Request $request
  58. * @return mixed
  59. * @throws DataNotFoundException
  60. * @throws DbException
  61. * @throws ModelNotFoundException
  62. */
  63. public function applyInfo(Request $request)
  64. {
  65. $uid = $request->uid();
  66. $data = $this->services->applyInfo($uid);
  67. return app('json')->success($data);
  68. }
  69. /**
  70. * 移动端获取规则
  71. * @param AgreementServices $agreementServices
  72. * @return mixed
  73. * @throws DataNotFoundException
  74. * @throws DbException
  75. * @throws ModelNotFoundException
  76. */
  77. public function getAgentAgreement(AgreementServices $agreementServices)
  78. {
  79. $data = $agreementServices->getAgreementBytype(2);
  80. return app('json')->success($data);
  81. }
  82. /**
  83. * 员工列表
  84. * @param Request $request
  85. * @return mixed
  86. * @throws DataNotFoundException
  87. * @throws DbException
  88. * @throws ModelNotFoundException
  89. */
  90. public function getStaffList(Request $request)
  91. {
  92. $where = $request->postMore([
  93. ['keyword', ''],
  94. ['sort', ''],
  95. ]);
  96. $where['agent_id'] = $request->uid();
  97. return app('json')->success($this->services->getStaffList($request->isRoutine(), $where));
  98. }
  99. /**
  100. * 设置员工比例
  101. * @param Request $request
  102. * @return mixed
  103. */
  104. public function setStaffPercent(Request $request)
  105. {
  106. [$agentPercent, $uid] = $request->postMore([
  107. ['agent_percent', ''],
  108. ['uid', 0],
  109. ], true);
  110. $agentId = $request->uid();
  111. if (!$uid) return app('json')->fail('参数错误');
  112. /** @var UserServices $userService */
  113. $userService = app()->make(UserServices::class);
  114. $upPercent = $userService->value(['uid' => $agentId], 'division_percent');
  115. if ($agentPercent >= $upPercent) return app('json')->fail('比例不能大于您的比例');
  116. $userService->update(['uid' => $uid, 'agent_id' => $agentId], ['division_percent' => $agentPercent]);
  117. return app('json')->success('设置成功');
  118. }
  119. /**
  120. * 删除员工
  121. * @param Request $request
  122. * @param $uid
  123. * @return mixed
  124. */
  125. public function delStaff(Request $request, $uid)
  126. {
  127. if (!$uid) return app('json')->fail('参数错误');
  128. $agentId = $request->uid();
  129. /** @var UserServices $userService */
  130. $userService = app()->make(UserServices::class);
  131. $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]);
  132. return app('json')->success('删除成功');
  133. }
  134. /**
  135. * 绑定员工方法
  136. * @param Request $request
  137. * @return \think\Response
  138. * @throws DataNotFoundException
  139. * @throws DbException
  140. * @throws ModelNotFoundException
  141. * @author 吴汐
  142. * @email 442384644@qq.com
  143. * @date 2024/2/2
  144. */
  145. public function agentSpread(Request $request)
  146. {
  147. [$agentId, $agentCode] = $request->postMore([
  148. ['agent_id', 0],
  149. ['agent_code', 0],
  150. ], true);
  151. $res = app()->make(DivisionServices::class)->agentSpreadStaff($request->uid(), (int)$agentId, (int)$agentCode);
  152. if ($res) {
  153. return app('json')->success($res);
  154. } else {
  155. return app('json')->fail('无操作');
  156. }
  157. }
  158. }