AgentManage.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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\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. * @var AgentManageServices
  26. */
  27. protected $services;
  28. /**
  29. * AgentManage constructor.
  30. * @param App $app
  31. * @param AgentManageServices $services
  32. */
  33. public function __construct(App $app, AgentManageServices $services)
  34. {
  35. parent::__construct($app);
  36. $this->services = $services;
  37. }
  38. /**
  39. * 分销管理列表
  40. * @return mixed
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. */
  45. public function index()
  46. {
  47. // 获取请求参数:昵称、日期范围
  48. $where = $this->request->getMore([
  49. ['nickname', ''],
  50. ['data', ''],
  51. ]);
  52. // 调用服务层获取分销商列表
  53. return app('json')->success($this->services->agentSystemPage($where));
  54. }
  55. /**
  56. * 分销头部统计
  57. * @return mixed
  58. * @throws \think\db\exception\DataNotFoundException
  59. * @throws \think\db\exception\DbException
  60. * @throws \think\db\exception\ModelNotFoundException
  61. */
  62. public function get_badge()
  63. {
  64. // 获取请求参数
  65. $where = $this->request->getMore([
  66. ['data', '', '', 'time'],
  67. ['nickname', ''],
  68. ]);
  69. // 返回分销统计数据
  70. return app('json')->success(['res' => $this->services->getSpreadBadge($where)]);
  71. }
  72. /**
  73. * 推广人列表
  74. * @return mixed
  75. */
  76. public function get_stair_list()
  77. {
  78. // 获取请求参数:用户ID、日期范围、昵称、类型(一级/二级)
  79. $where = $this->request->getMore([
  80. ['uid', 0],
  81. ['data', ''],
  82. ['nickname', ''],
  83. ['type', '']
  84. ]);
  85. // 调用服务层获取推广人列表
  86. return app('json')->success($this->services->getStairList($where));
  87. }
  88. /**
  89. * 推广人列表头部统计
  90. * @return mixed
  91. */
  92. public function get_stair_badge()
  93. {
  94. // 获取请求参数
  95. $where = $this->request->getMore([
  96. ['uid', ''],
  97. ['data', ''],
  98. ['nickname', ''],
  99. ['type', ''],
  100. ]);
  101. // 返回推广人统计数据
  102. return app('json')->success(['res' => $this->services->getSairBadge($where)]);
  103. }
  104. /**
  105. * 统计推广订单列表
  106. * @return mixed
  107. * @throws \think\db\exception\DataNotFoundException
  108. * @throws \think\db\exception\DbException
  109. * @throws \think\db\exception\ModelNotFoundException
  110. */
  111. public function get_stair_order_list()
  112. {
  113. // 获取请求参数
  114. $where = $this->request->getMore([
  115. ['uid', 0],
  116. ['data', ''],
  117. ['order_id', ''],
  118. ['type', ''],
  119. ]);
  120. // 调用服务层获取推广订单列表
  121. return app('json')->success($this->services->getStairOrderList((int)$where['uid'], $where));
  122. }
  123. /**
  124. * 查看公众号推广二维码
  125. * @param string $uid
  126. * @param string $action
  127. * @return mixed
  128. */
  129. public function look_code($uid = '', $action = '')
  130. {
  131. if (!$uid || !$action) return app('json')->fail('参数错误');
  132. try {
  133. // 动态调用方法
  134. if (method_exists($this, $action)) {
  135. $res = $this->$action($uid);
  136. if ($res)
  137. return app('json')->success($res);
  138. else
  139. return app('json')->fail('获取失败');
  140. } else
  141. return app('json')->fail('暂无此方法');
  142. } catch (\Exception $e) {
  143. return app('json')->fail('获取推广二维码失败,请检查您的微信配置', ['line' => $e->getLine(), 'messag' => $e->getMessage()]);
  144. }
  145. }
  146. /**
  147. * 获取公众号二维码
  148. * @param $uid
  149. * @return array
  150. */
  151. public function wechant_code($uid)
  152. {
  153. // 调用服务层生成公众号二维码
  154. $qr_code = $this->services->wechatCode((int)$uid);
  155. if (isset($qr_code['url']))
  156. return ['code_src' => $qr_code['url']];
  157. else
  158. return app('json')->fail('获取失败');
  159. }
  160. /**
  161. * 查看小程序推广二维码
  162. * @param string $uid
  163. */
  164. public function look_xcx_code($uid = '')
  165. {
  166. if (!strlen(trim($uid))) {
  167. return app('json')->fail('参数错误');
  168. }
  169. // 调用服务层生成小程序二维码
  170. return app('json')->success($this->services->lookXcxCode((int)$uid));
  171. }
  172. /**
  173. * 查看H5推广二维码
  174. * @param string $uid
  175. * @return mixed|string
  176. */
  177. public function look_h5_code($uid = '')
  178. {
  179. if (!strlen(trim($uid))) return app('json')->fail('参数错误');
  180. // 调用服务层生成H5二维码
  181. return app('json')->success($this->services->lookH5Code((int)$uid));
  182. }
  183. /**
  184. * 解除单个用户的推广权限
  185. * @param $uid
  186. * @return mixed
  187. */
  188. public function delete_spread($uid)
  189. {
  190. if (!$uid) app('json')->fail('参数错误');
  191. // 调用服务层解除推广权限
  192. return app('json')->success($this->services->delSpread((int)$uid) ? '设置成功' : '设置失败');
  193. }
  194. /**
  195. * 修改上级推广人
  196. * @param UserServices $services
  197. * @return mixed
  198. */
  199. public function editSpread(UserServices $services)
  200. {
  201. // 获取请求参数:用户ID、上级推广人ID
  202. [$uid, $spreadUid] = $this->request->postMore([
  203. [['uid', 'd'], 0],
  204. [['spread_uid', 'd'], 0],
  205. ], true);
  206. if (!$uid || !$spreadUid) {
  207. return app('json')->fail('参数错误');
  208. }
  209. if ($uid == $spreadUid) {
  210. return app('json')->fail('上级推广人不能为自己');
  211. }
  212. // 获取用户信息
  213. $userInfo = $services->get($uid);
  214. if (!$userInfo) {
  215. return app('json')->fail('用户不存在');
  216. }
  217. // 验证上级用户是否存在
  218. if (!$services->count(['uid' => $spreadUid])) {
  219. return app('json')->fail('上级用户不存在');
  220. }
  221. if ($userInfo->spread_uid == $spreadUid) {
  222. return app('json')->fail('当前推广人已经是所选人');
  223. }
  224. $spreadInfo = $services->get($spreadUid);
  225. if ($spreadInfo->spread_uid == $uid) {
  226. return app('json')->fail('上级推广人不能为自己下级');
  227. }
  228. //之前的上级减少推广人数
  229. if ($userInfo->spread_uid) {
  230. $oldSpread = $services->get($userInfo->spread_uid);
  231. $oldSpread->spread_count = $oldSpread->spread_count - 1;
  232. $oldSpread->save();
  233. }
  234. // 新上级增加推广人数
  235. $spreadInfo->spread_count = $spreadInfo->spread_count + 1;
  236. $spreadInfo->save();
  237. // 更新用户推广关系
  238. $userInfo->spread_uid = $spreadUid;
  239. $userInfo->spread_time = time();
  240. $userInfo->division_id = $spreadInfo->division_id;
  241. $userInfo->agent_id = $spreadInfo->agent_id;
  242. $userInfo->staff_id = $spreadInfo->staff_id;
  243. $userInfo->save();
  244. return app('json')->success('修改成功');
  245. }
  246. /**
  247. * 取消推广员推广资格
  248. * @param $uid
  249. * @return mixed
  250. */
  251. public function delete_system_spread($uid)
  252. {
  253. if (!$uid) app('json')->fail('参数错误');
  254. // 调用服务层取消推广资格
  255. return app('json')->success($this->services->delSystemSpread((int)$uid) ? '取消成功' : '取消失败');
  256. }
  257. /**
  258. * 获取赠送分销等级表单
  259. * @param AgentLevelServices $services
  260. * @param $uid
  261. * @return mixed
  262. * @throws \FormBuilder\Exception\FormBuilderException
  263. * @throws \think\db\exception\DataNotFoundException
  264. * @throws \think\db\exception\DbException
  265. * @throws \think\db\exception\ModelNotFoundException
  266. */
  267. public function getLevelForm(AgentLevelServices $services, $uid)
  268. {
  269. if (!$uid) app('json')->fail('参数错误');
  270. // 调用AgentLevelServices获取表单
  271. return app('json')->success($services->levelForm((int)$uid));
  272. }
  273. /**
  274. * 赠送分销等级
  275. * @param AgentLevelServices $services
  276. * @return mixed
  277. * @throws \think\db\exception\DataNotFoundException
  278. * @throws \think\db\exception\DbException
  279. * @throws \think\db\exception\ModelNotFoundException
  280. */
  281. public function giveAgentLevel(AgentLevelServices $services)
  282. {
  283. // 获取参数:用户ID、等级ID
  284. [$uid, $id] = $this->request->postMore([
  285. [['uid', 'd'], 0],
  286. [['id', 'd'], 0],
  287. ], true);
  288. if (!$uid || !$id) {
  289. return app('json')->fail('参数错误');
  290. }
  291. // 调用AgentLevelServices赠送等级
  292. return app('json')->success($services->givelevel((int)$uid, (int)$id) ? '赠送成功' : '赠送失败');
  293. }
  294. }