WechatUser.php 13 KB

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