AgentManage.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\agent;
  12. use app\adminapi\controller\AuthController;
  13. use app\services\agent\AgentLevelServices;
  14. use app\services\agent\AgentManageServices;
  15. use app\services\user\UserServices;
  16. use think\facade\App;
  17. /**
  18. * 分销商管理控制器
  19. * Class AgentManage
  20. * @package app\adminapi\controller\v1\agent
  21. */
  22. class AgentManage extends AuthController
  23. {
  24. /**
  25. * AgentManage constructor.
  26. * @param App $app
  27. * @param AgentManageServices $services
  28. */
  29. public function __construct(App $app, AgentManageServices $services)
  30. {
  31. parent::__construct($app);
  32. $this->services = $services;
  33. }
  34. /**
  35. * 分销管理列表
  36. * @return mixed
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function index()
  42. {
  43. $where = $this->request->getMore([
  44. ['nickname', ''],
  45. ['data', ''],
  46. ]);
  47. return app('json')->success($this->services->agentSystemPage($where));
  48. }
  49. /**
  50. * 分销头部统计
  51. * @return mixed
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\DbException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. */
  56. public function get_badge()
  57. {
  58. $where = $this->request->getMore([
  59. ['data', '', '', 'time'],
  60. ['nickname', ''],
  61. ]);
  62. return app('json')->success(['res' => $this->services->getSpreadBadge($where)]);
  63. }
  64. /**
  65. * 推广人列表
  66. * @return mixed
  67. */
  68. public function get_stair_list()
  69. {
  70. $where = $this->request->getMore([
  71. ['uid', 0],
  72. ['data', ''],
  73. ['nickname', ''],
  74. ['type', '']
  75. ]);
  76. return app('json')->success($this->services->getStairList($where));
  77. }
  78. /**
  79. * 推广人列表头部统计
  80. * @return mixed
  81. */
  82. public function get_stair_badge()
  83. {
  84. $where = $this->request->getMore([
  85. ['uid', ''],
  86. ['data', ''],
  87. ['nickname', ''],
  88. ['type', ''],
  89. ]);
  90. return app('json')->success(['res' => $this->services->getSairBadge($where)]);
  91. }
  92. /**
  93. * 统计推广订单列表
  94. * @return mixed
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\DbException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. */
  99. public function get_stair_order_list()
  100. {
  101. $where = $this->request->getMore([
  102. ['uid', 0],
  103. ['data', ''],
  104. ['order_id', ''],
  105. ['type', ''],
  106. ]);
  107. return app('json')->success($this->services->getStairOrderList((int)$where['uid'], $where));
  108. }
  109. /**
  110. * 查看公众号推广二维码
  111. * @param string $uid
  112. * @param string $action
  113. * @return mixed
  114. */
  115. public function look_code($uid = '', $action = '')
  116. {
  117. if (!$uid || !$action) return app('json')->fail(100100);
  118. try {
  119. if (method_exists($this, $action)) {
  120. $res = $this->$action($uid);
  121. if ($res)
  122. return app('json')->success($res);
  123. else
  124. return app('json')->fail(100016);
  125. } else
  126. return app('json')->fail(100029);
  127. } catch (\Exception $e) {
  128. return app('json')->fail(400212, ['line' => $e->getLine(), 'messag' => $e->getMessage()]);
  129. }
  130. }
  131. /**
  132. * 获取公众号二维码
  133. * @param $uid
  134. * @return array
  135. */
  136. public function wechant_code($uid)
  137. {
  138. $qr_code = $this->services->wechatCode((int)$uid);
  139. if (isset($qr_code['url']))
  140. return ['code_src' => $qr_code['url']];
  141. else
  142. return app('json')->fail(100016);
  143. }
  144. /**
  145. * 查看小程序推广二维码
  146. * @param string $uid
  147. */
  148. public function look_xcx_code($uid = '')
  149. {
  150. if (!strlen(trim($uid))) {
  151. return app('json')->fail(100100);
  152. }
  153. return app('json')->success($this->services->lookXcxCode((int)$uid));
  154. }
  155. /**
  156. * 查看H5推广二维码
  157. * @param string $uid
  158. * @return mixed|string
  159. */
  160. public function look_h5_code($uid = '')
  161. {
  162. if (!strlen(trim($uid))) return app('json')->fail(100100);
  163. return app('json')->success($this->services->lookH5Code((int)$uid));
  164. }
  165. /**
  166. * 解除单个用户的推广权限
  167. * @param $uid
  168. * @return mixed
  169. */
  170. public function delete_spread($uid)
  171. {
  172. if (!$uid) app('json')->fail(100100);
  173. return app('json')->success($this->services->delSpread((int)$uid) ? 100014 : 100015);
  174. }
  175. /**
  176. * 修改上级推广人
  177. * @param UserServices $services
  178. * @return mixed
  179. */
  180. public function editSpread(UserServices $services)
  181. {
  182. [$uid, $spreadUid] = $this->request->postMore([
  183. [['uid', 'd'], 0],
  184. [['spread_uid', 'd'], 0],
  185. ], true);
  186. if (!$uid || !$spreadUid) {
  187. return app('json')->fail(100100);
  188. }
  189. if ($uid == $spreadUid) {
  190. return app('json')->fail(400213);
  191. }
  192. $userInfo = $services->get($uid);
  193. if (!$userInfo) {
  194. return app('json')->fail(400214);
  195. }
  196. if (!$services->count(['uid' => $spreadUid])) {
  197. return app('json')->fail(400215);
  198. }
  199. if ($userInfo->spread_uid == $spreadUid) {
  200. return app('json')->fail(400216);
  201. }
  202. $spreadInfo = $services->get($spreadUid);
  203. if ($spreadInfo->spread_uid == $uid) {
  204. return app('json')->fail(400217);
  205. }
  206. //之前的上级减少推广人数
  207. if ($userInfo->spread_uid) {
  208. $oldSpread = $services->get($userInfo->spread_uid);
  209. $oldSpread->spread_count = $oldSpread->spread_count - 1;
  210. $oldSpread->save();
  211. }
  212. $spreadInfo->spread_count = $spreadInfo->spread_count + 1;
  213. $spreadInfo->save();
  214. $userInfo->spread_uid = $spreadUid;
  215. $userInfo->spread_time = time();
  216. $userInfo->division_id = $spreadInfo->division_id;
  217. $userInfo->agent_id = $spreadInfo->agent_id;
  218. $userInfo->staff_id = $spreadInfo->staff_id;
  219. $userInfo->save();
  220. return app('json')->success(100001);
  221. }
  222. /**
  223. * 取消推广员推广资格
  224. * @param $uid
  225. * @return mixed
  226. */
  227. public function delete_system_spread($uid)
  228. {
  229. if (!$uid) app('json')->fail(100100);
  230. return app('json')->success($this->services->delSystemSpread((int)$uid) ? 100019 : 100020);
  231. }
  232. /**
  233. * 获取赠送分销等级表单
  234. * @param AgentLevelServices $services
  235. * @param $uid
  236. * @return mixed
  237. * @throws \FormBuilder\Exception\FormBuilderException
  238. * @throws \think\db\exception\DataNotFoundException
  239. * @throws \think\db\exception\DbException
  240. * @throws \think\db\exception\ModelNotFoundException
  241. */
  242. public function getLevelForm(AgentLevelServices $services, $uid)
  243. {
  244. if (!$uid) app('json')->fail(100100);
  245. return app('json')->success($services->levelForm((int)$uid));
  246. }
  247. /**
  248. * 赠送分销等级
  249. * @param AgentLevelServices $services
  250. * @return mixed
  251. * @throws \think\db\exception\DataNotFoundException
  252. * @throws \think\db\exception\DbException
  253. * @throws \think\db\exception\ModelNotFoundException
  254. */
  255. public function giveAgentLevel(AgentLevelServices $services)
  256. {
  257. [$uid, $id] = $this->request->postMore([
  258. [['uid', 'd'], 0],
  259. [['id', 'd'], 0],
  260. ], true);
  261. if (!$uid || !$id) {
  262. return app('json')->fail(100100);
  263. }
  264. return app('json')->success($services->givelevel((int)$uid, (int)$id) ? 400218 : 400219);
  265. }
  266. }