WechatUser.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. namespace app\admin\controller\wechat;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\user\User;
  5. use app\models\user\UserBill;
  6. use think\Collection;
  7. use think\facade\Route as Url;
  8. use app\admin\model\wechat\WechatUser as UserModel;
  9. use crmeb\services\{FormBuilder as Form, JsonService, UtilService as Util, WechatService};
  10. /**
  11. * 管理员操作记录表控制器
  12. * Class WechatUser
  13. * @package app\admin\controller\wechat
  14. */
  15. class WechatUser extends AuthController
  16. {
  17. /**
  18. * 显示操作记录
  19. */
  20. public function index()
  21. {
  22. $where = Util::getMore([
  23. ['nickname', ''],
  24. ['data', ''],
  25. ['tagid_list', ''],
  26. ['groupid', '-1'],
  27. ['sex', ''],
  28. ['export', ''],
  29. ['stair', ''],
  30. ['second', ''],
  31. ['order_stair', ''],
  32. ['order_second', ''],
  33. ['subscribe', ''],
  34. ['now_money', ''],
  35. ['is_promoter', ''],
  36. ], $this->request);
  37. $tagidList = explode(',', $where['tagid_list']);
  38. foreach ($tagidList as $k => $v) {
  39. if (!$v) {
  40. unset($tagidList[$k]);
  41. }
  42. }
  43. $tagidList = array_unique($tagidList);
  44. $where['tagid_list'] = implode(',', $tagidList);
  45. try {
  46. $groupList = UserModel::getUserGroup();
  47. $tagList = UserModel::getUserTag();
  48. } catch (\Exception $e) {
  49. $groupList = [];
  50. $tagList = [];
  51. }
  52. $this->assign([
  53. 'where' => $where,
  54. 'groupList' => $groupList,
  55. 'tagList' => $tagList
  56. ]);
  57. $limitTimeList = [
  58. 'today' => implode(' - ', [date('Y/m/d'), date('Y/m/d', strtotime('+1 day'))]),
  59. 'week' => implode(' - ', [
  60. date('Y/m/d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600)),
  61. date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600))
  62. ]),
  63. 'month' => implode(' - ', [date('Y/m') . '/01', date('Y/m') . '/' . date('t')]),
  64. 'quarter' => implode(' - ', [
  65. date('Y') . '/' . (ceil((date('n')) / 3) * 3 - 3 + 1) . '/01',
  66. date('Y') . '/' . (ceil((date('n')) / 3) * 3) . '/' . date('t', mktime(0, 0, 0, (ceil((date('n')) / 3) * 3), 1, date('Y')))
  67. ]),
  68. 'year' => implode(' - ', [
  69. date('Y') . '/01/01', date('Y/m/d', strtotime(date('Y') . '/01/01 + 1year -1 day'))
  70. ])
  71. ];
  72. $uidAll = UserModel::getAll($where);
  73. $this->assign(compact('limitTimeList', 'uidAll'));
  74. $this->assign(UserModel::systemPage($where));
  75. return $this->fetch();
  76. }
  77. public function edit_user_tag($openid)
  78. {
  79. if (!$openid) return JsonService::fail('参数错误!');
  80. $list = Collection::make(UserModel::getUserTag())->each(function ($item) {
  81. return ['value' => $item['id'], 'label' => $item['name']];
  82. });
  83. $tagList = UserModel::where('openid', $openid)->value('tagid_list');
  84. $tagList = explode(',', $tagList) ?: [];
  85. $f = [Form::select('tag_id', '用户标签', $tagList)->setOptions($list->toArray())->multiple(1)];
  86. $form = Form::make_post_form('标签名称', $f, Url::buildUrl('update_user_tag', compact('openid')));
  87. $this->assign(compact('form'));
  88. return $this->fetch('public/form-builder');
  89. }
  90. public function update_user_tag($openid)
  91. {
  92. if (!$openid) return JsonService::fail('参数错误!');
  93. $tagId = request()->post('tag_id/a', []);
  94. if (!$tagId) return JsonService::fail('请选择用户标签!');
  95. $tagList = explode(',', UserModel::where('openid', $openid)->value('tagid_list')) ?: [];
  96. UserModel::edit(['tagid_list' => $tagId], $openid, 'openid');
  97. if (!$tagId[0]) unset($tagId[0]);
  98. UserModel::edit(['tagid_list' => $tagId], $openid, 'openid');
  99. try {
  100. foreach ($tagList as $tag) {
  101. if ($tag) WechatService::userTagService()->batchUntagUsers([$openid], $tag);
  102. }
  103. foreach ($tagId as $tag) {
  104. WechatService::userTagService()->batchTagUsers([$openid], $tag);
  105. }
  106. } catch (\Exception $e) {
  107. UserModel::rollbackTrans();
  108. return JsonService::fail($e->getMessage());
  109. }
  110. UserModel::commitTrans();
  111. return JsonService::successful('修改成功!');
  112. }
  113. public function edit_user_group($openid)
  114. {
  115. if (!$openid) return JsonService::fail('参数错误!');
  116. $list = Collection::make(UserModel::getUserGroup())->each(function ($item) {
  117. return ['value' => $item['id'], 'label' => $item['name']];
  118. });
  119. $groupId = UserModel::where('openid', $openid)->value('groupid');
  120. $f = [Form::select('group_id', '用户分组', (string)$groupId)->setOptions($list->toArray())];
  121. $form = Form::make_post_form('用户分组', $f, Url::buildUrl('update_user_group', compact('openid')));
  122. $this->assign(compact('form'));
  123. return $this->fetch('public/form-builder');
  124. }
  125. public function update_user_group($openid)
  126. {
  127. if (!$openid) return JsonService::fail('参数错误!');
  128. $groupId = request()->post('group_id');
  129. // if(!$groupId) return JsonService::fail('请选择用户分组!');
  130. UserModel::beginTrans();
  131. UserModel::edit(['groupid' => $groupId], $openid, 'openid');
  132. try {
  133. WechatService::userGroupService()->moveUser($openid, $groupId);
  134. } catch (\Exception $e) {
  135. UserModel::rollbackTrans();
  136. return JsonService::fail($e->getMessage());
  137. }
  138. UserModel::commitTrans();
  139. return JsonService::successful('修改成功!');
  140. }
  141. /**
  142. * 用户标签列表
  143. */
  144. public function tag($refresh = 0)
  145. {
  146. $list = [];
  147. if ($refresh == 1) {
  148. UserModel::clearUserTag();
  149. $this->redirect(Url::buildUrl('tag')->suffix(false)->build());
  150. }
  151. try {
  152. $list = UserModel::getUserTag();
  153. } catch (\Exception $e) {
  154. }
  155. $this->assign(compact('list'));
  156. return $this->fetch();
  157. }
  158. /**
  159. * 添加标签
  160. * @return mixed
  161. */
  162. public function create_tag()
  163. {
  164. $f = [Form::input('name', '标签名称')];
  165. $form = Form::make_post_form('标签名称', $f, Url::buildUrl('save_tag'));
  166. $this->assign(compact('form'));
  167. return $this->fetch('public/form-builder');
  168. }
  169. /**
  170. * 添加
  171. */
  172. public function save_tag()
  173. {
  174. $tagName = request()->post('name');
  175. if (!$tagName) return JsonService::fail('请输入标签名称!');
  176. try {
  177. WechatService::userTagService()->create($tagName);
  178. } catch (\Exception $e) {
  179. return JsonService::fail($e->getMessage());
  180. }
  181. UserModel::clearUserTag();
  182. return JsonService::successful('添加标签成功!');
  183. }
  184. /**
  185. * 修改标签
  186. * @param $id
  187. * @return mixed
  188. */
  189. public function edit_tag($id)
  190. {
  191. $f = [Form::input('name', '标签名称')];
  192. $form = Form::make_post_form('标签名称', $f, Url::buildUrl('update_tag', ['id' => $id]));
  193. $this->assign(compact('form'));
  194. return $this->fetch('public/form-builder');
  195. }
  196. /**
  197. * 修改标签
  198. * @param $id
  199. */
  200. public function update_tag($id)
  201. {
  202. $tagName = request()->post('name');
  203. if (!$tagName) return JsonService::fail('请输入标签名称!');
  204. try {
  205. WechatService::userTagService()->update($id, $tagName);
  206. } catch (\Exception $e) {
  207. return JsonService::fail($e->getMessage());
  208. }
  209. UserModel::clearUserTag();
  210. return JsonService::successful('修改标签成功!');
  211. }
  212. /**
  213. * 删除标签
  214. * @param $id
  215. * @return \think\response\Json
  216. */
  217. public function delete_tag($id)
  218. {
  219. try {
  220. WechatService::userTagService()->delete($id);
  221. } catch (\Exception $e) {
  222. return JsonService::fail($e->getMessage());
  223. }
  224. UserModel::clearUserTag();
  225. return JsonService::successful('删除标签成功!');
  226. }
  227. /**
  228. * 用户分组列表
  229. */
  230. public function group($refresh = 0)
  231. {
  232. $list = [];
  233. try {
  234. if ($refresh == 1) {
  235. UserModel::clearUserGroup();
  236. $this->redirect(Url::buildUrl('group')->suffix(false)->build());
  237. }
  238. $list = UserModel::getUserGroup();
  239. } catch (\Exception $e) {
  240. }
  241. $this->assign(compact('list'));
  242. return $this->fetch();
  243. }
  244. /**
  245. * 添加分组
  246. * @return mixed
  247. */
  248. public function create_group()
  249. {
  250. $f = [Form::input('name', '分组名称')];
  251. $form = Form::make_post_form('标签名称', $f, Url::buildUrl('save_group'));
  252. $this->assign(compact('form'));
  253. return $this->fetch('public/form-builder');
  254. }
  255. /**
  256. * 添加
  257. */
  258. public function save_group()
  259. {
  260. $tagName = request()->post('name');
  261. if (!$tagName) return JsonService::fail('请输入分组名称!');
  262. try {
  263. WechatService::userGroupService()->create($tagName);
  264. } catch (\Exception $e) {
  265. return JsonService::fail($e->getMessage());
  266. }
  267. UserModel::clearUserGroup();
  268. return JsonService::successful('添加分组成功!');
  269. }
  270. /**
  271. * 修改分组
  272. * @param $id
  273. * @return mixed
  274. */
  275. public function edit_group($id)
  276. {
  277. $f = [Form::input('name', '分组名称')];
  278. $form = Form::make_post_form('标签名称', $f, Url::buildUrl('update_group', compact('id')));
  279. $this->assign(compact('form'));
  280. return $this->fetch('public/form-builder');
  281. }
  282. /**
  283. * 修改分组
  284. * @param $id
  285. */
  286. public function update_group($id)
  287. {
  288. $tagName = request()->post('name');
  289. if (!$tagName) return JsonService::fail('请输入分组名称!');
  290. try {
  291. WechatService::userGroupService()->update($id, $tagName);
  292. } catch (\Exception $e) {
  293. return JsonService::fail($e->getMessage());
  294. }
  295. UserModel::clearUserGroup();
  296. return JsonService::successful('修改分组成功!');
  297. }
  298. /**
  299. * 删除分组
  300. * @param $id
  301. * @return \think\response\Json
  302. */
  303. public function delete_group($id)
  304. {
  305. try {
  306. WechatService::userTagService()->delete($id);
  307. } catch (\Exception $e) {
  308. return JsonService::fail($e->getMessage());
  309. }
  310. UserModel::clearUserGroup();
  311. return JsonService::successful('删除分组成功!');
  312. }
  313. public function synchro_tag($openid)
  314. {
  315. if (!$openid) return JsonService::fail('参数错误!');
  316. $data = array();
  317. if (UserModel::be($openid, 'openid')) {
  318. try {
  319. $tag = WechatService::userTagService()->userTags($openid)->toArray();
  320. } catch (\Exception $e) {
  321. return JsonService::fail($e->getMessage());
  322. }
  323. if ($tag['tagid_list']) $data['tagid_list'] = implode(',', $tag['tagid_list']);
  324. else $data['tagid_list'] = '';
  325. $res = UserModel::edit($data, $openid, 'openid');
  326. if ($res) return JsonService::successful('同步成功');
  327. else return JsonService::fail('同步失败!');
  328. } else return JsonService::fail('参数错误!');
  329. }
  330. /**
  331. * 一级推荐人页面
  332. * @return mixed
  333. */
  334. public function stair($uid = '')
  335. {
  336. if ($uid == '') return $this->failed('参数错误');
  337. $list = User::alias('u')
  338. ->where('u.spread_uid', $uid)
  339. ->field('u.avatar,u.nickname,u.now_money,u.add_time,u.uid')
  340. ->where('u.status', 1)
  341. ->order('u.add_time DESC')
  342. ->select()
  343. ->toArray();
  344. foreach ($list as $key => $value) $list[$key]['orderCount'] = StoreOrder::getOrderCount($value['uid']);
  345. $this->assign('list', $list);
  346. return $this->fetch();
  347. }
  348. /**
  349. * 个人资金详情页面
  350. * @return mixed
  351. */
  352. public function now_money($uid = '')
  353. {
  354. if ($uid == '') return $this->failed('参数错误');
  355. $list = UserBill::where('uid', $uid)->where('category', 'now_money')
  356. ->field('mark,pm,number,add_time')
  357. ->where('status', 1)->order('add_time DESC')->select()->toArray();
  358. foreach ($list as &$v) {
  359. $v['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
  360. }
  361. $this->assign('list', $list);
  362. return $this->fetch();
  363. }
  364. }