DivisionController.php 4.0 KB

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