AgentManage.php 12 KB

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