AgentManage.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. //TODO 废弃
  73. /**
  74. * 推广人列表头部统计
  75. * @return mixed
  76. */
  77. public function get_stair_badge()
  78. {
  79. $where = $this->request->getMore([
  80. ['uid', ''],
  81. ['data', ''],
  82. ['nickname', ''],
  83. ['type', ''],
  84. ]);
  85. return app('json')->success(['res' => $this->services->getSairBadge($where)]);
  86. }
  87. /**
  88. * 统计推广订单列表
  89. * @return mixed
  90. * @throws \think\db\exception\DataNotFoundException
  91. * @throws \think\db\exception\DbException
  92. * @throws \think\db\exception\ModelNotFoundException
  93. */
  94. public function get_stair_order_list()
  95. {
  96. $where = $this->request->getMore([
  97. ['uid', 0],
  98. ['data', ''],
  99. ['order_id', ''],
  100. ['type', ''],
  101. ]);
  102. return app('json')->success($this->services->getStairOrderList((int)$where['uid'], $where));
  103. }
  104. /**
  105. * 查看公众号推广二维码
  106. * @param int $uid
  107. * @return json
  108. * */
  109. public function look_code($uid = '', $action = '')
  110. {
  111. if (!$uid || !$action) return app('json')->fail('缺少参数');
  112. try {
  113. if (method_exists($this, $action)) {
  114. $res = $this->$action($uid);
  115. if ($res)
  116. return app('json')->success($res);
  117. else
  118. return app('json')->fail($res['msg'] ?? '获取失败,请稍后再试!');
  119. } else
  120. return app('json')->fail('暂无此方法');
  121. } catch (\Exception $e) {
  122. return app('json')->fail('获取推广二维码失败,请检查您的微信配置', ['line' => $e->getLine(), 'messag' => $e->getMessage()]);
  123. }
  124. }
  125. /**
  126. * 获取公众号二维码
  127. * */
  128. public function wechant_code($uid)
  129. {
  130. $qr_code = $this->services->wechatCode((int)$uid);
  131. if (isset($qr_code['url']))
  132. return ['code_src' => $qr_code['url']];
  133. else
  134. return app('json')->fail('获取失败,请稍后再试!');
  135. }
  136. /**
  137. * TODO 查看小程序推广二维码
  138. * @param string $uid
  139. */
  140. public function look_xcx_code($uid = '')
  141. {
  142. if (!strlen(trim($uid))) {
  143. return app('json')->fail('缺少参数');
  144. }
  145. return app('json')->success($this->services->lookXcxCode((int)$uid));
  146. }
  147. /**
  148. * 查看H5推广二维码
  149. * @param string $uid
  150. * @return mixed|string
  151. */
  152. public function look_h5_code($uid = '')
  153. {
  154. if (!strlen(trim($uid))) return app('json')->fail('缺少参数');
  155. return app('json')->success($this->services->lookH5Code((int)$uid));
  156. }
  157. /**
  158. * 解除单个用户的推广权限
  159. * @param int $uid
  160. * */
  161. public function delete_spread($uid)
  162. {
  163. if (!$uid) app('json')->fail('缺少参数');
  164. return app('json')->success($this->services->delSpread((int)$uid) ? '解除成功' : '解除失败');
  165. }
  166. /**
  167. * 修改上级推广人
  168. * @param UserServices $services
  169. * @return mixed
  170. */
  171. public function editSpread(UserServices $services)
  172. {
  173. [$uid, $spreadUid] = $this->request->postMore([
  174. [['uid', 'd'], 0],
  175. [['spread_uid', 'd'], 0],
  176. ], true);
  177. if (!$uid || !$spreadUid) {
  178. return app('json')->fail('缺少参数');
  179. }
  180. if ($uid == $spreadUid) {
  181. return app('json')->fail('上级推广人不能为自己');
  182. }
  183. $userInfo = $services->get($uid);
  184. if (!$userInfo) {
  185. return app('json')->fail('用户不存在');
  186. }
  187. if (!$services->count(['uid' => $spreadUid])) {
  188. return app('json')->fail('上级用户不存在');
  189. }
  190. if ($userInfo->spread_uid == $spreadUid) {
  191. return app('json')->fail('当前推广人已经是所选人');
  192. }
  193. $spreadInfo = $services->get($spreadUid);
  194. if ($spreadInfo->spread_uid == $uid) {
  195. return app('json')->fail('上级推广人不能为自己下级');
  196. }
  197. //之前的上级减少推广人数
  198. if ($userInfo->spread_uid) {
  199. $oldSpread = $services->get($userInfo->spread_uid);
  200. $oldSpread->spread_count = $oldSpread->spread_count - 1;
  201. $oldSpread->save();
  202. }
  203. $spreadInfo->spread_count = $spreadInfo->spread_count + 1;
  204. $spreadInfo->save();
  205. $userInfo->spread_uid = $spreadUid;
  206. $userInfo->spread_time = time();
  207. $userInfo->save();
  208. return app('json')->success('修改成功');
  209. }
  210. /**
  211. * 取消推广员推广资格
  212. * @param $uid
  213. * @return mixed
  214. */
  215. public function delete_system_spread($uid)
  216. {
  217. if (!$uid) app('json')->fail('缺少参数');
  218. return app('json')->success($this->services->delSystemSpread((int)$uid) ? '取消成功' : '取消失败');
  219. }
  220. /**
  221. * 获取赠送分销等级表单
  222. * @param AgentLevelServices $services
  223. * @param $uid
  224. * @return mixed
  225. * @throws \FormBuilder\Exception\FormBuilderException
  226. * @throws \think\db\exception\DataNotFoundException
  227. * @throws \think\db\exception\DbException
  228. * @throws \think\db\exception\ModelNotFoundException
  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. }