AgentManage.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. namespace app\adminapi\controller\v1\agent;
  3. use app\adminapi\controller\AuthController;
  4. use Exception;
  5. use think\db\exception\DataNotFoundException;
  6. use think\db\exception\DbException;
  7. use think\db\exception\ModelNotFoundException;
  8. use app\models\user\{
  9. User, UserBill
  10. };
  11. use app\models\routine\{
  12. RoutineCode, RoutineQrcode
  13. };
  14. use app\models\store\StoreOrder;
  15. use app\models\system\SystemAttachment;
  16. use app\models\wechat\WechatUser as UserModel;
  17. use crmeb\services\{
  18. QrcodeService, UploadService, UtilService as Util
  19. };
  20. /**
  21. * 分销商管理控制器
  22. * Class AgentManage
  23. * @package app\adminapi\controller\v1\agent
  24. */
  25. class AgentManage extends AuthController
  26. {
  27. /**
  28. * 分销管理列表
  29. * @return mixed
  30. * @throws Exception
  31. */
  32. public function index()
  33. {
  34. $where = Util::getMore([
  35. ['nickname', ''],
  36. ['data', ''],
  37. ['excel', ''],
  38. ['page', 1],
  39. ['limit', 20],
  40. ]);
  41. $where['mer_id'] = $this->merId;
  42. return $this->success(UserModel::agentSystemPage($where));
  43. }
  44. /**
  45. * 分销头部统计
  46. * @return mixed
  47. * @throws Exception
  48. */
  49. public function get_badge()
  50. {
  51. $where = Util::getMore([
  52. ['data', ''],
  53. ['nickname', ''],
  54. ]);
  55. $where['mer_id'] = $this->merId;
  56. $res = UserModel::getSpreadBadge($where);
  57. return $this->success(compact('res'));
  58. }
  59. /**
  60. * 推广人列表
  61. * @return mixed
  62. * @throws Exception
  63. */
  64. public function get_stair_list()
  65. {
  66. $where = Util::getMore([
  67. ['uid', 0],
  68. ['data', ''],
  69. ['nickname', ''],
  70. ['type', ''],
  71. ['page', 1],
  72. ['limit', 20],
  73. ]);
  74. if (!$where['uid'] || !User::merSet($this->merId)->where('uid', $where['uid'])->find()) {
  75. return $this->fail('查看的用户不存在');
  76. }
  77. return $this->success(UserModel::getStairList($where));
  78. }
  79. /**
  80. * 推广人列表头部统计
  81. * @return mixed
  82. * @throws Exception
  83. */
  84. public function get_stair_badge()
  85. {
  86. $where = Util::getMore([
  87. ['uid', ''],
  88. ['data', ''],
  89. ['nickname', ''],
  90. ['type', ''],
  91. ]);
  92. if (!$where['uid'] || !User::merSet($this->merId)->where('uid', $where['uid'])->find()) {
  93. return $this->fail('查看的用户不存在');
  94. }
  95. $res = UserModel::getSairBadge($where);
  96. return $this->success(compact('res'));
  97. }
  98. /**
  99. * 统计推广订单列表
  100. * @return mixed
  101. * @throws Exception
  102. */
  103. public function get_stair_order_list()
  104. {
  105. $where = Util::getMore([
  106. ['uid', 0],
  107. ['data', ''],
  108. ['order_id', ''],
  109. ['type', ''],
  110. ['page', 1],
  111. ['limit', 20],
  112. ]);
  113. if (!$where['uid'] || !User::merSet($this->merId)->where('uid', $where['uid'])->find()) {
  114. return $this->fail('查看的用户不存在');
  115. }
  116. return $this->success(UserModel::getStairOrderList($where));
  117. }
  118. /**
  119. * 统计推广订单头部统计
  120. * @return mixed
  121. * @throws Exception
  122. */
  123. public function get_stair_order_badge()
  124. {
  125. $where = Util::getMore([
  126. ['uid', ''],
  127. ['data', ''],
  128. ['order_id', ''],
  129. ['type', ''],
  130. ]);
  131. if (!$where['uid'] || !User::merSet($this->merId)->where('uid', $where['uid'])->find()) {
  132. return $this->fail('查看的用户不存在');
  133. }
  134. return $this->success(UserModel::getStairOrderBadge($where));
  135. }
  136. /**
  137. * 二级推荐人页面
  138. * @param string $uid
  139. * @return mixed
  140. * @throws DataNotFoundException
  141. * @throws DbException
  142. * @throws ModelNotFoundException
  143. */
  144. public function stair_two($uid = '')
  145. {
  146. if ($uid == '') return $this->fail('参数错误');
  147. if (!User::merSet($this->merId)->where('uid', $uid)->find()) {
  148. return $this->fail('查看的用户不存在');
  149. }
  150. $spread_uid = User::where('spread_uid', $uid)->column('uid', 'uid');
  151. if (count($spread_uid))
  152. $spread_uid_two = User::where('spread_uid', 'in', $spread_uid)->column('uid', 'uid');
  153. else
  154. $spread_uid_two = [0];
  155. $list = User::alias('u')
  156. ->where('u.uid', 'in', $spread_uid_two)
  157. ->field('u.avatar,u.nickname,u.now_money,u.spread_time,u.uid')
  158. ->where('u.status', 1)
  159. ->order('u.add_time DESC')
  160. ->select()
  161. ->toArray();
  162. foreach ($list as $key => $value) $list[$key]['orderCount'] = StoreOrder::getOrderCount($value['uid']) ?: 0;
  163. return $this->success(compact('list'));
  164. }
  165. /**
  166. * 批量清除推广权限
  167. * @return mixed
  168. * @throws Exception
  169. */
  170. public function delete_promoter()
  171. {
  172. list($uids) = Util::postMore([
  173. ['uids', []]
  174. ], $this->request, true);
  175. if (!count($uids)) return $this->fail('请选择需要解除推广权限的用户!');
  176. User::beginTrans();
  177. try {
  178. if (User::merSet($this->merId)->where('uid', 'in', $uids)->update(['is_promoter' => 0])) {
  179. User::commitTrans();
  180. return $this->success('解除成功');
  181. } else {
  182. User::rollbackTrans();
  183. return $this->fail('解除失败');
  184. }
  185. } catch (\PDOException $e) {
  186. User::rollbackTrans();
  187. return $this->fail('数据库操作错误', ['line' => $e->getLine(), 'message' => $e->getMessage()]);
  188. } catch (Exception $e) {
  189. User::rollbackTrans();
  190. return $this->fail('系统错误', ['line' => $e->getLine(), 'message' => $e->getMessage()]);
  191. }
  192. }
  193. /**
  194. * 查看公众号推广二维码
  195. * @param string $uid
  196. * @param string $action
  197. * @return mixed
  198. * @throws DataNotFoundException
  199. * @throws DbException
  200. * @throws ModelNotFoundException
  201. */
  202. public function look_code($uid = '', $action = '')
  203. {
  204. if (!$uid || !$action) return $this->fail('缺少参数');
  205. if (!User::merSet($this->merId)->where('uid', $uid)->find()) {
  206. return $this->fail('查看的用户不存在');
  207. }
  208. try {
  209. if (method_exists($this, $action)) {
  210. $res = $this->$action($uid);
  211. if ($res)
  212. return $this->success($res);
  213. else
  214. return $this->fail(isset($res['msg']) ? $res['msg'] : '获取失败,请稍后再试!');
  215. } else
  216. return $this->fail('暂无此方法');
  217. } catch (Exception $e) {
  218. return $this->fail('获取推广二维码失败,请检查您的微信配置', ['line' => $e->getLine(), 'messag' => $e->getMessage()]);
  219. }
  220. }
  221. /**
  222. * 获取小程序二维码
  223. * @param $uid
  224. * @return mixed
  225. * @throws DataNotFoundException
  226. * @throws ModelNotFoundException
  227. * @throws \think\Exception
  228. * @throws DbException
  229. */
  230. public function routine_code($uid)
  231. {
  232. $userInfo = User::getUserInfos($uid, $this->merId);
  233. if (!$userInfo) {
  234. return $this->fail('查看的用户不存在');
  235. }
  236. $name = $userInfo['uid'] . '_' . $userInfo['is_promoter'] . '_user.jpg';
  237. $imageInfo = SystemAttachment::getInfo($name, 'name');
  238. if (!$imageInfo) {
  239. $res = RoutineCode::getShareCode($uid, 'spread', '', '', $this->merId);
  240. if (!$res) throw new \think\Exception('二维码生成失败');
  241. $upload = UploadService::init(null, $this->merId);
  242. if ($upload->to('routine/spread/code')->stream($res['res'], $name) === false) {
  243. return $upload->getError();
  244. }
  245. $imageInfo = $upload->getUploadInfo();
  246. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time']);
  247. RoutineQrcode::setRoutineQrcodeFind($res['id'], ['status' => 1, 'time' => time(), 'qrcode_url' => $imageInfo['dir']]);
  248. $urlCode = $imageInfo['dir'];
  249. } else $urlCode = $imageInfo['att_dir'];
  250. return ['code_src' => $urlCode];
  251. }
  252. /**
  253. * 获取公众号二维码
  254. * @param $uid
  255. * @return mixed
  256. * @throws DataNotFoundException
  257. * @throws DbException
  258. * @throws ModelNotFoundException
  259. * @throws \think\Exception
  260. */
  261. public function wechant_code($uid)
  262. {
  263. if (!$uid || !User::merSet($this->merId)->where('uid', $uid)->find()) {
  264. return $this->fail('查看的用户不存在');
  265. }
  266. $qr_code = QrcodeService::getTemporaryQrcode('spread', $uid);
  267. if (isset($qr_code['url']))
  268. return ['code_src' => $qr_code['url']];
  269. else
  270. throw new \think\Exception('获取失败,请稍后再试!');
  271. }
  272. /**
  273. * TODO 查看小程序推广二维码
  274. * @param string $uid
  275. * @return string
  276. * @throws DataNotFoundException
  277. * @throws DbException
  278. * @throws ModelNotFoundException
  279. */
  280. public function look_xcx_code($uid = '')
  281. {
  282. if (!$uid || !User::merSet($this->merId)->where('uid', $uid)->find()) {
  283. return $this->fail('查看的用户不存在');
  284. }
  285. if (!strlen(trim($uid))) return $this->fail('缺少参数');
  286. try {
  287. $userInfo = User::getUserInfos($uid);
  288. $name = $userInfo['uid'] . '_' . $userInfo['is_promoter'] . '_user.jpg';
  289. $imageInfo = SystemAttachment::getInfo($name, 'name');
  290. if (!$imageInfo) {
  291. $res = RoutineCode::getShareCode($uid, 'spread', '', '', $this->merId);
  292. if (!$res) return $this->fail('二维码生成失败');
  293. $upload = UploadService::init(null, $this->merId);
  294. if ($upload->to('routine/spread/code')->stream($res['res'], $name) === false) {
  295. return $upload->getError();
  296. }
  297. $imageInfo = $upload->getUploadInfo();
  298. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time']);
  299. RoutineQrcode::setRoutineQrcodeFind($res['id'], ['status' => 1, 'time' => time(), 'qrcode_url' => $imageInfo['dir']]);
  300. $urlCode = $imageInfo['dir'];
  301. } else $urlCode = $imageInfo['att_dir'];
  302. return $this->success(['code_src' => $urlCode]);
  303. } catch (Exception $e) {
  304. return $this->fail('查看推广二维码失败!', ['line' => $e->getLine(), 'meassge' => $e->getMessage()]);
  305. }
  306. }
  307. /**
  308. * 查看H5推广二维码
  309. * @param string $uid
  310. * @return mixed|string
  311. * @throws DataNotFoundException
  312. * @throws DbException
  313. * @throws ModelNotFoundException
  314. */
  315. public function look_h5_code($uid = '')
  316. {
  317. if (!$uid || !User::merSet($this->merId)->where('uid', $uid)->find()) {
  318. return $this->fail('查看的用户不存在');
  319. }
  320. if (!strlen(trim($uid))) return $this->fail('缺少参数');
  321. try {
  322. $userInfo = User::getUserInfos($uid);
  323. $name = $userInfo['uid'] . '_h5_' . $userInfo['is_promoter'] . '_user.jpg';
  324. $imageInfo = SystemAttachment::getInfo($name, 'name');
  325. if (!$imageInfo) {
  326. $urlCode = QrcodeService::getWechatQrcodePath($uid . '_h5_' . $userInfo['is_promoter'] . '_user.jpg', '');
  327. } else $urlCode = $imageInfo['att_dir'];
  328. return $this->success(['code_src' => $urlCode]);
  329. } catch (Exception $e) {
  330. return $this->fail('查看推广二维码失败!', ['line' => $e->getLine(), 'meassge' => $e->getMessage()]);
  331. }
  332. }
  333. /**
  334. * 解除单个用户的推广权限
  335. * @param int $uid
  336. * @return
  337. * @throws DataNotFoundException
  338. * @throws DbException
  339. * @throws ModelNotFoundException
  340. */
  341. public function delete_spread($uid)
  342. {
  343. if (!$uid || !User::merSet($this->merId)->where('uid', $uid)->find()) {
  344. return $this->fail('查看的用户不存在');
  345. }
  346. $res = User::where('uid', $uid)->update(['spread_uid' => 0]);
  347. if ($res !== false)
  348. return $this->success('解除成功');
  349. else
  350. return $this->fail('解除失败');
  351. }
  352. /**
  353. * 清除推广人
  354. * @param int $uid
  355. * @return
  356. * @throws DataNotFoundException
  357. * @throws DbException
  358. * @throws ModelNotFoundException
  359. */
  360. public function empty_spread($uid = 0)
  361. {
  362. if (!$uid) return $this->fail('缺少参数');
  363. if (!$uid || !User::merSet($this->merId)->where('uid', $uid)->find()) {
  364. return $this->fail('查看的用户不存在');
  365. }
  366. $res = true;
  367. $spread_uid = User::where('spread_uid', $uid)->column('uid', 'uid');
  368. if (count($spread_uid)) $res = $res && false !== User::where('spread_uid', 'in', $spread_uid)->update(['spread_uid' => 0]);
  369. $res = $res && false !== User::where('spread_uid', $uid)->update(['spread_uid' => 0]);
  370. if ($res)
  371. return $this->success('清除成功');
  372. else
  373. return $this->fail('清除失败');
  374. }
  375. /**
  376. * 个人资金详情页面
  377. * @param string $uid
  378. * @return mixed
  379. * @throws DataNotFoundException
  380. * @throws DbException
  381. * @throws ModelNotFoundException
  382. */
  383. public function now_money($uid = '')
  384. {
  385. if ($uid == '') return $this->fail('参数错误');
  386. if (!$uid || !User::merSet($this->merId)->where('uid', $uid)->find()) {
  387. return $this->fail('查看的用户不存在');
  388. }
  389. $list = UserBill::where('uid', $uid)->where('category', 'now_money')
  390. ->field('mark,pm,number,add_time')
  391. ->where('status', 1)->order('add_time DESC')->select()->toArray();
  392. foreach ($list as &$v) {
  393. $v['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
  394. }
  395. return $this->success($list);
  396. }
  397. }