AgentManage.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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\controller\admin\v1\agent;
  12. use app\controller\admin\AuthController;
  13. use app\jobs\user\UserFriendsJob;
  14. use app\jobs\user\UserSpreadJob;
  15. use app\services\agent\AgentLevelServices;
  16. use app\services\agent\AgentManageServices;
  17. use app\services\other\AgreementServices;
  18. use app\services\user\UserServices;
  19. use think\facade\App;
  20. /**
  21. * 分销商管理控制器
  22. * Class AgentManage
  23. * @package app\controller\admin\v1\agent
  24. */
  25. class AgentManage extends AuthController
  26. {
  27. /**
  28. * AgentManage constructor.
  29. * @param App $app
  30. * @param AgentManageServices $services
  31. */
  32. public function __construct(App $app, AgentManageServices $services)
  33. {
  34. parent::__construct($app);
  35. $this->services = $services;
  36. }
  37. /**
  38. * 分销管理列表
  39. * @return mixed
  40. */
  41. public function index()
  42. {
  43. $where = $this->request->getMore([
  44. ['nickname', ''],
  45. ['data', ''],
  46. ]);
  47. return $this->success($this->services->agentSystemPage($where));
  48. }
  49. /**
  50. * 分销头部统计
  51. * @return mixed
  52. */
  53. public function get_badge()
  54. {
  55. $where = $this->request->getMore([
  56. ['data', ''],
  57. ['nickname', ''],
  58. ]);
  59. return $this->success(['res' => $this->services->getSpreadBadge($where)]);
  60. }
  61. /**
  62. * 推广人列表
  63. * @return mixed
  64. */
  65. public function get_stair_list()
  66. {
  67. $where = $this->request->getMore([
  68. ['uid', 0],
  69. ['data', ''],
  70. ['nickname', ''],
  71. ['type', '']
  72. ]);
  73. return $this->success($this->services->getStairList($where));
  74. }
  75. /**
  76. * 推广人列表头部统计
  77. * @return mixed
  78. */
  79. public function get_stair_badge()
  80. {
  81. $where = $this->request->getMore([
  82. ['uid', ''],
  83. ['data', ''],
  84. ['nickname', ''],
  85. ['type', ''],
  86. ]);
  87. return $this->success(['res' => $this->services->getSairBadge($where)]);
  88. }
  89. /**
  90. * 统计推广订单列表
  91. * @return mixed
  92. * @throws \think\db\exception\DataNotFoundException
  93. * @throws \think\db\exception\DbException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. */
  96. public function get_stair_order_list()
  97. {
  98. $where = $this->request->getMore([
  99. ['uid', 0],
  100. ['data', ''],
  101. ['order_id', ''],
  102. ['type', ''],
  103. ['nickname', '']
  104. ]);
  105. return $this->success($this->services->getStairOrderList((int)$where['uid'], $where));
  106. }
  107. /**
  108. * 统计推广订单头部统计
  109. * @return mixed
  110. */
  111. public function get_stair_order_badge()
  112. {
  113. return $this->success();
  114. }
  115. /**
  116. * 查看公众号推广二维码
  117. * @param int $uid
  118. * @return json
  119. * */
  120. public function look_code($uid = '', $action = '')
  121. {
  122. if (!$uid || !$action) return $this->fail('缺少参数');
  123. try {
  124. if (method_exists($this, $action)) {
  125. $res = $this->$action($uid);
  126. if ($res)
  127. return $this->success($res);
  128. else
  129. return $this->fail(isset($res['msg']) ? $res['msg'] : '获取失败,请稍后再试!');
  130. } else
  131. return $this->fail('暂无此方法');
  132. } catch (\Exception $e) {
  133. return $this->fail('获取推广二维码失败,请检查您的微信配置', ['line' => $e->getLine(), 'messag' => $e->getMessage()]);
  134. }
  135. }
  136. /**
  137. * 获取公众号二维码
  138. * */
  139. public function wechant_code($uid)
  140. {
  141. $qr_code = $this->services->wechatCode((int)$uid);
  142. if (isset($qr_code['url']))
  143. return ['code_src' => $qr_code['url']];
  144. else
  145. return $this->fail('获取失败,请稍后再试!');
  146. }
  147. /**
  148. * 查看小程序推广二维码
  149. * @param string $uid
  150. */
  151. public function look_xcx_code($uid = '')
  152. {
  153. if (!strlen(trim($uid))) {
  154. return $this->fail('缺少参数');
  155. }
  156. return $this->success($this->services->lookXcxCode((int)$uid));
  157. }
  158. /**
  159. * 查看H5推广二维码
  160. * @param string $uid
  161. * @return mixed|string
  162. */
  163. public function look_h5_code($uid = '')
  164. {
  165. if (!strlen(trim($uid))) return $this->fail('缺少参数');
  166. return $this->success($this->services->lookH5Code((int)$uid));
  167. }
  168. /**
  169. * 解除单个用户的推广权限
  170. * @param int $uid
  171. * */
  172. public function delete_spread($uid)
  173. {
  174. if (!$uid) $this->fail('缺少参数');
  175. return $this->success($this->services->delSpread((int)$uid) ? '解除成功' : '解除失败');
  176. }
  177. /**
  178. * 修改上级推广人
  179. * @param UserServices $services
  180. * @return mixed
  181. */
  182. public function editSpread(UserServices $services)
  183. {
  184. [$uid, $spreadUid] = $this->request->postMore([
  185. [['uid', 'd'], 0],
  186. [['spread_uid', 'd'], 0],
  187. ], true);
  188. if (!$uid || !$spreadUid) {
  189. return $this->fail('缺少参数');
  190. }
  191. if ($uid == $spreadUid) {
  192. return $this->fail('上级推广人不能为自己');
  193. }
  194. $userInfo = $services->get($uid);
  195. if (!$userInfo) {
  196. return $this->fail('用户不存在');
  197. }
  198. if (!$services->count(['uid' => $spreadUid])) {
  199. return $this->fail('上级用户不存在');
  200. }
  201. if ($userInfo->spread_uid == $spreadUid) {
  202. return $this->fail('当前推广人已经是所选人');
  203. }
  204. $spreadInfo = $services->get($spreadUid);
  205. if ($spreadInfo->spread_uid == $uid) {
  206. return $this->fail('上级推广人不能为自己下级');
  207. }
  208. $userInfo->spread_uid = $spreadUid;
  209. $userInfo->spread_time = time();
  210. $userInfo->save();
  211. //记录推广绑定关系
  212. UserSpreadJob::dispatch([(int)$uid, (int)$spreadUid]);
  213. //记录好友关系
  214. UserFriendsJob::dispatch([(int)$uid, (int)$spreadUid]);
  215. return $this->success('修改成功');
  216. }
  217. /**
  218. * 取消推广员推广资格
  219. * @param $uid
  220. * @return mixed
  221. */
  222. public function delete_system_spread($uid)
  223. {
  224. if (!$uid) $this->fail('缺少参数');
  225. return $this->success($this->services->delSystemSpread((int)$uid) ? '取消成功' : '取消失败');
  226. }
  227. /**
  228. * 获取赠送分销等级表单
  229. * @param $uid
  230. * @return mixed
  231. */
  232. public function getLevelForm(AgentLevelServices $services, $uid)
  233. {
  234. if (!$uid) $this->fail('缺少参数');
  235. return $this->success($services->levelForm((int)$uid));
  236. }
  237. /**
  238. * 赠送分销等级
  239. * @param AgentLevelServices $services
  240. * @return mixed
  241. * @throws \think\db\exception\DataNotFoundException
  242. * @throws \think\db\exception\DbException
  243. * @throws \think\db\exception\ModelNotFoundException
  244. */
  245. public function giveAgentLevel(AgentLevelServices $services)
  246. {
  247. [$uid, $id] = $this->request->postMore([
  248. [['uid', 'd'], 0],
  249. [['id', 'd'], 0],
  250. ], true);
  251. if (!$uid || !$id) {
  252. return $this->fail('缺少参数');
  253. }
  254. return $this->success($services->givelevel((int)$uid, (int)$id) ? '赠送成功' : '赠送失败');
  255. }
  256. /**
  257. * 保存分销说明
  258. * @param $id
  259. * @param AgreementServices $agreementServices
  260. * @return mixed
  261. */
  262. public function setAgentAgreement($id, AgreementServices $agreementServices)
  263. {
  264. $data = $this->request->postMore([
  265. ['type', 2],
  266. ['title', ""],
  267. ['content', ''],
  268. ['status', ''],
  269. ]);
  270. $data['title'] = $data['title'] ?: '分销说明';
  271. return $this->success($agreementServices->saveAgreement($data, (int)$id));
  272. }
  273. /**
  274. * 获取分销说明
  275. * @param AgreementServices $agreementServices
  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 getAgentAgreement(AgreementServices $agreementServices)
  282. {
  283. $list = $agreementServices->getAgreementBytype(2);
  284. if($list && isset($list['title'])) $list['title'] = $list['title'] ?: '分销说明';
  285. return $this->success($list);
  286. }
  287. }