WechatUser.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\controller\admin\wechat;
  12. use crmeb\basic\BaseController;
  13. use app\common\repositories\wechat\WechatUserRepository;
  14. use crmeb\services\WechatUserGroupService;
  15. use crmeb\services\WechatUserTagService;
  16. use FormBuilder\Exception\FormBuilderException;
  17. use think\App;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\DbException;
  20. use think\db\exception\ModelNotFoundException;
  21. /**
  22. * Class WechatUser
  23. * @package app\controller\admin\wechat
  24. * @author xaboy
  25. * @day 2020-04-29
  26. */
  27. class WechatUser extends BaseController
  28. {
  29. /**
  30. * @var WechatUserRepository
  31. */
  32. protected $repository;
  33. /**
  34. * WechatUser constructor.
  35. * @param App $app
  36. * @param WechatUserRepository $repository
  37. */
  38. public function __construct(App $app, WechatUserRepository $repository)
  39. {
  40. parent::__construct($app);
  41. $this->repository = $repository;
  42. }
  43. /**
  44. * @return mixed
  45. * @throws DataNotFoundException
  46. * @throws DbException
  47. * @throws ModelNotFoundException
  48. * @author xaboy
  49. * @day 2020-04-29
  50. */
  51. public function lst()
  52. {
  53. $where = $this->request->params([
  54. ['page', 1],
  55. ['limit', 20],
  56. ['nickname', ''],
  57. ['tagid_list', ''],
  58. ['groupid', '-1'],
  59. ['sex', ''],
  60. ['export', ''],
  61. ['subscribe', '']
  62. ]);
  63. $where['tagid_list'] = implode(',', array_filter(array_unique(explode(',', $where['tagid_list']))));
  64. [$page, $limit] = $this->getPage();
  65. return app('json')->success($this->repository->getList($where, $page, $limit));
  66. }
  67. /**
  68. * @param $id
  69. * @param WechatUserTagService $service
  70. * @return mixed
  71. * @throws DataNotFoundException
  72. * @throws DbException
  73. * @throws ModelNotFoundException
  74. * @author xaboy
  75. * @day 2020-04-29
  76. */
  77. public function syncTag($id, WechatUserTagService $service)
  78. {
  79. $user = $this->repository->get($id);
  80. if (!$user)
  81. return app('json')->fail('数据不存在');
  82. $tag = $service->userTags($user->openid);
  83. if ($tag['tagid_list']) $data['tagid_list'] = implode(',', $tag['tagid_list']);
  84. else $data['tagid_list'] = '';
  85. $user->save($tag);
  86. return app('json')->success('同步成功');
  87. }
  88. /**
  89. * @param $id
  90. * @return mixed
  91. * @throws DataNotFoundException
  92. * @throws DbException
  93. * @throws FormBuilderException
  94. * @throws ModelNotFoundException
  95. * @author xaboy
  96. * @day 2020-04-29
  97. */
  98. public function tagForm($id)
  99. {
  100. if (!$this->repository->exists($id))
  101. return app('json')->fail('数据不存在');
  102. return app('json')->success(formToData($this->repository->updateUserTagForm($id)));
  103. }
  104. /**
  105. * @param $id
  106. * @return mixed
  107. * @throws DataNotFoundException
  108. * @throws DbException
  109. * @throws ModelNotFoundException
  110. * @author xaboy
  111. * @day 2020-04-29
  112. */
  113. public function tag($id)
  114. {
  115. if (!$this->repository->exists($id))
  116. return app('json')->fail('数据不存在');
  117. $tags = explode(',', $this->request->param('tag_id'));
  118. $this->repository->updateTag($id, $tags);
  119. return app('json')->success('操作成功');
  120. }
  121. /**
  122. * @param $id
  123. * @return mixed
  124. * @throws DataNotFoundException
  125. * @throws DbException
  126. * @throws FormBuilderException
  127. * @throws ModelNotFoundException
  128. * @author xaboy
  129. * @day 2020-04-29
  130. */
  131. public function groupForm($id)
  132. {
  133. if (!$this->repository->exists($id))
  134. return app('json')->fail('数据不存在');
  135. return app('json')->success(formToData($this->repository->updateUserGroupForm($id)));
  136. }
  137. /**
  138. * @param $id
  139. * @return mixed
  140. * @throws DataNotFoundException
  141. * @throws DbException
  142. * @throws ModelNotFoundException
  143. * @author xaboy
  144. * @day 2020-04-29
  145. */
  146. public function group($id)
  147. {
  148. if (!$this->repository->exists($id))
  149. return app('json')->fail('数据不存在');
  150. $groupId = $this->request->param('group_id');
  151. $this->repository->updateGroup($id, $groupId);
  152. return app('json')->success('操作成功');
  153. }
  154. /**
  155. * @param WechatUserTagService $wechatUserTagService
  156. * @param WechatUserGroupService $wechatUserGroupService
  157. * @return mixed
  158. * @author xaboy
  159. * @day 2020-04-29
  160. */
  161. public function tagGroup(WechatUserTagService $wechatUserTagService, WechatUserGroupService $wechatUserGroupService)
  162. {
  163. $groupList = $wechatUserGroupService->lst();
  164. $tagList = $wechatUserTagService->lst();
  165. return app('json')->success(compact('groupList', 'tagList'));
  166. }
  167. }