WechatUser.php 4.7 KB

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