WechatUserRepository.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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\common\repositories\wechat;
  12. use app\common\dao\wechat\WechatUserDao;
  13. use app\common\repositories\article\ArticleRepository;
  14. use app\common\repositories\BaseRepository;
  15. use app\common\repositories\user\UserRepository;
  16. use crmeb\jobs\SendNewsJob;
  17. use crmeb\services\WechatUserGroupService;
  18. use crmeb\services\WechatUserTagService;
  19. use FormBuilder\Exception\FormBuilderException;
  20. use FormBuilder\Factory\Elm;
  21. use FormBuilder\Form;
  22. use think\db\exception\DataNotFoundException;
  23. use think\db\exception\DbException;
  24. use think\db\exception\ModelNotFoundException;
  25. use think\facade\Db;
  26. use think\facade\Queue;
  27. use think\facade\Route;
  28. /**
  29. * Class WechatUserRepository
  30. * @package app\common\repositories\wechat
  31. * @author xaboy
  32. * @day 2020-04-28
  33. * @mixin WechatUserDao
  34. */
  35. class WechatUserRepository extends BaseRepository
  36. {
  37. /**
  38. * WechatUserRepository constructor.
  39. * @param WechatUserDao $dao
  40. */
  41. public function __construct(WechatUserDao $dao)
  42. {
  43. $this->dao = $dao;
  44. }
  45. /**
  46. * @param string $openId
  47. * @param array $userInfo
  48. * @param bool $mode
  49. * @return mixed|void
  50. * @throws DataNotFoundException
  51. * @throws DbException
  52. * @throws ModelNotFoundException
  53. * @author xaboy
  54. * @day 2020-04-28
  55. */
  56. public function syncUser(string $openId, array $userInfo, bool $mode = false)
  57. {
  58. if (($mode && (!isset($userInfo['subscribe']) || !$userInfo['subscribe'])) || !isset($userInfo['openid']))
  59. return;
  60. $wechatUser = null;
  61. $userInfo['nickname'] = filter_emoji($userInfo['nickname']);
  62. if (isset($userInfo['unionid']))
  63. $wechatUser = $this->dao->unionIdByWechatUser($userInfo['unionid']);
  64. if (!$wechatUser)
  65. $wechatUser = $this->dao->openIdByWechatUser($openId);
  66. unset($userInfo['qr_scene'], $userInfo['qr_scene_str'], $userInfo['qr_scene_str'], $userInfo['subscribe_scene']);
  67. if (isset($userInfo['tagid_list'])) {
  68. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  69. }
  70. return Db::transaction(function () use ($userInfo, $wechatUser) {
  71. if ($wechatUser) {
  72. $wechatUser->save($userInfo);
  73. } else {
  74. $wechatUser = $this->dao->create($userInfo);
  75. }
  76. /** @var UserRepository $userRepository */
  77. $userRepository = app()->make(UserRepository::class);
  78. $user = $userRepository->syncWechatUser($wechatUser);
  79. return [$wechatUser, $user];
  80. });
  81. }
  82. /**
  83. * @param string $routineOpenid
  84. * @param array $routine
  85. * @return mixed
  86. * @throws DataNotFoundException
  87. * @throws DbException
  88. * @throws ModelNotFoundException
  89. * @author xaboy
  90. * @day 2020-05-11
  91. */
  92. public function syncRoutineUser(string $routineOpenid, array $routine)
  93. {
  94. $routineInfo = [];
  95. $routineInfo['nickname'] = filter_emoji($routine['nickName']);//姓名
  96. $routineInfo['sex'] = $routine['gender'];//性别
  97. $routineInfo['language'] = $routine['language'];//语言
  98. $routineInfo['city'] = $routine['city'];//城市
  99. $routineInfo['province'] = $routine['province'];//省份
  100. $routineInfo['country'] = $routine['country'];//国家
  101. $routineInfo['headimgurl'] = $routine['avatarUrl'];//头像
  102. $routineInfo['routine_openid'] = $routineOpenid;//openid
  103. $routineInfo['session_key'] = $routine['session_key'] ?? '';//会话密匙
  104. $routineInfo['unionid'] = $routine['unionId'];//用户在开放平台的唯一标识符
  105. $routineInfo['user_type'] = 'routine';//用户类型
  106. $wechatUser = null;
  107. if ($routineInfo['unionid'])
  108. $wechatUser = $this->dao->unionIdByWechatUser($routineInfo['unionid']);
  109. if (!$wechatUser)
  110. $wechatUser = $this->dao->routineIdByWechatUser($routineOpenid);
  111. return Db::transaction(function () use ($routineInfo, $wechatUser) {
  112. if ($wechatUser) {
  113. $wechatUser->save($routineInfo);
  114. } else {
  115. $wechatUser = $this->dao->create($routineInfo);
  116. }
  117. /** @var UserRepository $userRepository */
  118. $userRepository = app()->make(UserRepository::class);
  119. $user = $userRepository->syncWechatUser($wechatUser, 'routine');
  120. return [$wechatUser, $user];
  121. });
  122. }
  123. /**
  124. * @param array $where
  125. * @param $page
  126. * @param $limit
  127. * @return array
  128. * @throws DataNotFoundException
  129. * @throws DbException
  130. * @throws ModelNotFoundException
  131. * @author xaboy
  132. * @day 2020-04-29
  133. */
  134. public function getList(array $where, $page, $limit)
  135. {
  136. $query = $this->dao->search($where);
  137. $count = $query->count($this->dao->getPk());
  138. $list = $query->setOption('field', [])->field('uid,openid,nickname,headimgurl,sex,country,province,city,subscribe')
  139. ->page($page, $limit)->select()->each(function ($item) {
  140. $item['subscribe_time'] = $item['subscribe_time'] ? date('Y-m-d H:i', $item['subscribe_time']) : '';
  141. return $item;
  142. });
  143. return compact('count', 'list');
  144. }
  145. /**
  146. * @param $id
  147. * @return Form
  148. * @throws DataNotFoundException
  149. * @throws DbException
  150. * @throws FormBuilderException
  151. * @throws ModelNotFoundException
  152. * @author xaboy
  153. * @day 2020-04-29
  154. */
  155. public function updateUserTagForm($id)
  156. {
  157. $wechatUserTagService = new WechatUserTagService();
  158. $lst = $wechatUserTagService->lst();
  159. $user = $this->dao->get($id);
  160. return Elm::createForm(Route::buildUrl('wechat/user/tag', ['id' => $id]), [
  161. Elm::select('tag_id', '用户标签', explode(',', $user->tagid_list))->options(function () use ($lst) {
  162. $options = [];
  163. foreach ($lst as $item) {
  164. $options[] = ['value' => $item['id'], 'label' => $item['name']];
  165. }
  166. return $options;
  167. })->multiple(true)
  168. ])->setTitle('编辑用户标签');
  169. }
  170. /**
  171. * @param $id
  172. * @param array $tags
  173. * @throws DataNotFoundException
  174. * @throws DbException
  175. * @throws ModelNotFoundException
  176. * @author xaboy
  177. * @day 2020-04-29
  178. */
  179. public function updateTag($id, array $tags)
  180. {
  181. $user = $this->dao->get($id);
  182. $oTags = explode(',', $user->tagid_list);
  183. $user->save(['tagid_list' => implode(',', $tags)]);
  184. $wechatUserTagService = (new WechatUserTagService())->userTag();
  185. foreach ($oTags as $tag) {
  186. $wechatUserTagService->batchUntagUsers([$user->openid], $tag);
  187. }
  188. foreach ($tags as $tag) {
  189. $wechatUserTagService->batchTagUsers([$user->openid], $tag);
  190. }
  191. }
  192. /**
  193. * @param $id
  194. * @return Form
  195. * @throws DataNotFoundException
  196. * @throws DbException
  197. * @throws FormBuilderException
  198. * @throws ModelNotFoundException
  199. * @author xaboy
  200. * @day 2020-04-29
  201. */
  202. public function updateUserGroupForm($id)
  203. {
  204. $wechatUserGroupService = new WechatUserGroupService();
  205. $lst = $wechatUserGroupService->lst();
  206. $user = $this->dao->get($id);
  207. return Elm::createForm(Route::buildUrl('wechat/user/group', ['id' => $id]), [
  208. Elm::select('group_id', '用户标签', (string)$user->groupid)->options(function () use ($lst) {
  209. $options = [];
  210. foreach ($lst as $item) {
  211. $options[] = ['value' => $item['id'], 'label' => $item['name']];
  212. }
  213. return $options;
  214. })
  215. ])->setTitle('编辑用户分组');
  216. }
  217. /**
  218. * @param $id
  219. * @param $groupid
  220. * @throws DataNotFoundException
  221. * @throws DbException
  222. * @throws ModelNotFoundException
  223. * @author xaboy
  224. * @day 2020-04-29
  225. */
  226. public function updateGroup($id, $groupid)
  227. {
  228. $user = $this->dao->get($id);
  229. $user->save(['groupid' => $groupid]);
  230. $wechatUserGroupService = (new WechatUserGroupService())->userGroup();
  231. $wechatUserGroupService->moveUser($user->openid, $groupid);
  232. }
  233. /**
  234. * @param $id
  235. * @param array $ids
  236. * @author xaboy
  237. * @day 2020-05-11
  238. */
  239. public function sendNews($id, array $ids)
  240. {
  241. if (!count($ids)) return;
  242. /** @var ArticleRepository $make */
  243. $make = app()->make(ArticleRepository::class);
  244. $articles = $make->wechatNewIdByData($id);
  245. $news = [];
  246. foreach ($articles as $article) {
  247. $news[] = [
  248. 'title' => $article['title'],
  249. 'image' => $article['image_input'],
  250. 'date' => $article['create_time'],
  251. 'description' => $article['synopsis'],
  252. 'id' => $article['article_id']
  253. ];
  254. }
  255. foreach ($ids as $_id) {
  256. if ($this->dao->isSubscribeWechatUser($_id)) {
  257. Queue::push(SendNewsJob::class, [$_id, $news]);
  258. }
  259. }
  260. }
  261. }