AgentManage.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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. */
  38. public function index()
  39. {
  40. $where = $this->request->getMore([
  41. ['nickname', ''],
  42. ['data', ''],
  43. ]);
  44. return app('json')->success($this->services->agentSystemPage($where));
  45. }
  46. /**
  47. * 分销头部统计
  48. * @return mixed
  49. */
  50. public function get_badge()
  51. {
  52. $where = $this->request->getMore([
  53. ['data', ''],
  54. ['nickname', ''],
  55. ]);
  56. return app('json')->success(['res' => $this->services->getSpreadBadge($where)]);
  57. }
  58. /**
  59. * 推广人列表
  60. * @return mixed
  61. */
  62. public function get_stair_list()
  63. {
  64. $where = $this->request->getMore([
  65. ['uid', 0],
  66. ['data', ''],
  67. ['nickname', ''],
  68. ['type', '']
  69. ]);
  70. return app('json')->success($this->services->getStairList($where));
  71. }
  72. /**
  73. * 推广人列表头部统计
  74. * @return mixed
  75. */
  76. public function get_stair_badge()
  77. {
  78. $where = $this->request->getMore([
  79. ['uid', ''],
  80. ['data', ''],
  81. ['nickname', ''],
  82. ['type', ''],
  83. ]);
  84. return app('json')->success(['res' => $this->services->getSairBadge($where)]);
  85. }
  86. /**
  87. * 统计推广订单列表
  88. * @return mixed
  89. * @throws \think\db\exception\DataNotFoundException
  90. * @throws \think\db\exception\DbException
  91. * @throws \think\db\exception\ModelNotFoundException
  92. */
  93. public function get_stair_order_list()
  94. {
  95. $where = $this->request->getMore([
  96. ['uid', 0],
  97. ['data', ''],
  98. ['order_id', ''],
  99. ['type', ''],
  100. ]);
  101. return app('json')->success($this->services->getStairOrderList((int)$where['uid'], $where));
  102. }
  103. /**
  104. * 统计推广订单头部统计
  105. * @return mixed
  106. */
  107. public function get_stair_order_badge()
  108. {
  109. $where = $this->request->getMore([
  110. ['uid', ''],
  111. ['data', ''],
  112. ['order_id', ''],
  113. ['type', ''],
  114. ]);
  115. return app('json')->success($this->services->getStairOrderBadge($where));
  116. }
  117. /**
  118. * 查看公众号推广二维码
  119. * @param int $uid
  120. * @return json
  121. * */
  122. public function look_code($uid = '', $action = '')
  123. {
  124. if (!$uid || !$action) return app('json')->fail('缺少参数');
  125. try {
  126. if (method_exists($this, $action)) {
  127. $res = $this->$action($uid);
  128. if ($res)
  129. return app('json')->success($res);
  130. else
  131. return app('json')->fail(isset($res['msg']) ? $res['msg'] : '获取失败,请稍后再试!');
  132. } else
  133. return app('json')->fail('暂无此方法');
  134. } catch (\Exception $e) {
  135. return app('json')->fail('获取推广二维码失败,请检查您的微信配置', ['line' => $e->getLine(), 'messag' => $e->getMessage()]);
  136. }
  137. }
  138. /**
  139. * 获取公众号二维码
  140. * */
  141. public function wechant_code($uid)
  142. {
  143. $qr_code = $this->services->wechatCode((int)$uid);
  144. if (isset($qr_code['url']))
  145. return ['code_src' => $qr_code['url']];
  146. else
  147. return app('json')->fail('获取失败,请稍后再试!');
  148. }
  149. /**
  150. * TODO 查看小程序推广二维码
  151. * @param string $uid
  152. */
  153. public function look_xcx_code($uid = '')
  154. {
  155. if (!strlen(trim($uid))) {
  156. return app('json')->fail('缺少参数');
  157. }
  158. return app('json')->success($this->services->lookXcxCode((int)$uid));
  159. }
  160. /**
  161. * 查看H5推广二维码
  162. * @param string $uid
  163. * @return mixed|string
  164. */
  165. public function look_h5_code($uid = '')
  166. {
  167. if (!strlen(trim($uid))) return app('json')->fail('缺少参数');
  168. return app('json')->success($this->services->lookH5Code((int)$uid));
  169. }
  170. /**
  171. * 解除单个用户的推广权限
  172. * @param int $uid
  173. * */
  174. public function delete_spread($uid)
  175. {
  176. if (!$uid) app('json')->fail('缺少参数');
  177. return app('json')->success($this->services->delSpread((int)$uid) ? '解除成功' : '解除失败');
  178. }
  179. /**
  180. * 修改上级推广人
  181. * @param UserServices $services
  182. * @return mixed
  183. */
  184. public function editSpread(UserServices $services)
  185. {
  186. [$uid, $spreadUid] = $this->request->postMore([
  187. [['uid', 'd'], 0],
  188. [['spread_uid', 'd'], 0],
  189. ], true);
  190. if (!$uid || !$spreadUid) {
  191. return app('json')->fail('缺少参数');
  192. }
  193. if ($uid == $spreadUid) {
  194. return app('json')->fail('上级推广人不能为自己');
  195. }
  196. $userInfo = $services->get($uid);
  197. if (!$userInfo) {
  198. return app('json')->fail('用户不存在');
  199. }
  200. if (!$services->count(['uid' => $spreadUid])) {
  201. return app('json')->fail('上级用户不存在');
  202. }
  203. if ($userInfo->spread_uid == $spreadUid) {
  204. return app('json')->fail('当前推广人已经是所选人');
  205. }
  206. $spreadInfo = $services->get($spreadUid);
  207. if($spreadInfo->spread_uid == $uid) {
  208. return app('json')->fail('上级推广人不能为自己下级');
  209. }
  210. $userInfo->spread_uid = $spreadUid;
  211. $userInfo->spread_time = time();
  212. $userInfo->save();
  213. return app('json')->success('修改成功');
  214. }
  215. /**
  216. * 取消推广员推广资格
  217. * @param $uid
  218. * @return mixed
  219. */
  220. public function delete_system_spread($uid)
  221. {
  222. if (!$uid) app('json')->fail('缺少参数');
  223. return app('json')->success($this->services->delSystemSpread((int)$uid) ? '取消成功' : '取消失败');
  224. }
  225. /**
  226. * 获取赠送分销等级表单
  227. * @param $uid
  228. * @return mixed
  229. */
  230. public function getLevelForm(AgentLevelServices $services, $uid)
  231. {
  232. if (!$uid) app('json')->fail('缺少参数');
  233. return app('json')->success($services->levelForm((int)$uid));
  234. }
  235. /**
  236. * 赠送分销等级
  237. * @param AgentLevelServices $services
  238. * @return mixed
  239. * @throws \think\db\exception\DataNotFoundException
  240. * @throws \think\db\exception\DbException
  241. * @throws \think\db\exception\ModelNotFoundException
  242. */
  243. public function giveAgentLevel(AgentLevelServices $services)
  244. {
  245. [$uid, $id] = $this->request->postMore([
  246. [['uid', 'd'], 0],
  247. [['id', 'd'], 0],
  248. ], true);
  249. if (!$uid || !$id) {
  250. app('json')->fail('缺少参数');
  251. }
  252. return app('json')->success($services->givelevel((int)$uid, (int)$id) ? '赠送成功' : '赠送失败');
  253. }
  254. }