AgentManage.php 12 KB

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