WechatUser.php 11 KB

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