WechatUserRepository.php 11 KB

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