WechatUser.php 4.8 KB

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