WechatUserRepository.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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, $createUser = true)
  57. {
  58. if (($mode && (!isset($userInfo['subscribe']) || !$userInfo['subscribe'])) || !isset($userInfo['openid']))
  59. return;
  60. $wechatUser = null;
  61. $userInfo['nickname'] = filter_emoji(($userInfo['nickname'] ?? '') ?: ('微信用户U' . substr(uniqid(true, true), -6)));
  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 ($createUser, $mode, $userInfo, $wechatUser) {
  71. if ($wechatUser) {
  72. if ($mode) {
  73. unset($userInfo['nickname']);
  74. }
  75. $wechatUser->save($userInfo);
  76. } else {
  77. $wechatUser = $this->dao->create($userInfo);
  78. }
  79. if (!$createUser) return [$wechatUser];
  80. /** @var UserRepository $userRepository */
  81. $userRepository = app()->make(UserRepository::class);
  82. $user = $userRepository->syncWechatUser($wechatUser,'wechat');
  83. return [$wechatUser, $user];
  84. });
  85. }
  86. /**
  87. * @param string $routineOpenid
  88. * @param array $routine
  89. * @return mixed
  90. * @throws DataNotFoundException
  91. * @throws DbException
  92. * @throws ModelNotFoundException
  93. * @author xaboy
  94. * @day 2020-05-11
  95. */
  96. public function syncRoutineUser(string $routineOpenid, array $routine, $createUser = true)
  97. {
  98. $routineInfo = [];
  99. $routineInfo['nickname'] = filter_emoji($routine['nickName']);//姓名
  100. $routineInfo['sex'] = $routine['gender'] ?? 0;//性别
  101. $routineInfo['language'] = $routine['language'] ?? '';//语言
  102. $routineInfo['city'] = $routine['city'] ?? '';//城市
  103. $routineInfo['province'] = $routine['province'] ?? '';//省份
  104. $routineInfo['country'] = $routine['country'] ?? '';//国家
  105. $routineInfo['headimgurl'] = $routine['avatarUrl'];//头像
  106. $routineInfo['routine_openid'] = $routineOpenid;//openid
  107. $routineInfo['session_key'] = $routine['session_key'] ?? '';//会话密匙
  108. $routineInfo['unionid'] = $routine['unionId'];//用户在开放平台的唯一标识符
  109. $routineInfo['user_type'] = 'routine';//用户类型
  110. $wechatUser = null;
  111. if ($routineInfo['unionid'])
  112. $wechatUser = $this->dao->unionIdByWechatUser($routineInfo['unionid']);
  113. if (!$wechatUser)
  114. $wechatUser = $this->dao->routineIdByWechatUser($routineOpenid);
  115. return Db::transaction(function () use ($createUser, $routineInfo, $wechatUser) {
  116. if ($wechatUser) {
  117. $routineInfo['nickname'] = $wechatUser['nickname'];
  118. $routineInfo['headimgurl'] = $wechatUser['headimgurl'];
  119. $wechatUser->save($routineInfo);
  120. } else {
  121. $wechatUser = $this->dao->create($routineInfo);
  122. }
  123. if (!$createUser) return [$wechatUser];
  124. /** @var UserRepository $userRepository */
  125. $userRepository = app()->make(UserRepository::class);
  126. $user = $userRepository->syncWechatUser($wechatUser, 'routine');
  127. return [$wechatUser, $user];
  128. });
  129. }
  130. /**
  131. * @param string $routineOpenid
  132. * @param array $routine
  133. * @return mixed
  134. * @throws DataNotFoundException
  135. * @throws DbException
  136. * @throws ModelNotFoundException
  137. * @author xaboy
  138. * @day 2020-05-11
  139. */
  140. public function syncAppUser(string $unionId, array $userInfo, $type = 'wechat', $createUser = true)
  141. {
  142. $wechatInfo = [];
  143. $wechatInfo['nickname'] = filter_emoji($userInfo['nickName'] ?? ($userInfo['nickname'] ?? ''));//姓名
  144. $wechatInfo['sex'] = $userInfo['gender'] ?? 0;//性别
  145. $wechatInfo['city'] = $userInfo['city'] ?? '';//城市
  146. $wechatInfo['province'] = $userInfo['province'] ?? '';//省份
  147. $wechatInfo['country'] = $userInfo['country'] ?? '';//国家
  148. $wechatInfo['headimgurl'] = $userInfo['avatarUrl'] ?? ($userInfo['headimgurl'] ?? '');//头像
  149. $wechatInfo['unionid'] = $unionId;//用户在开放平台的唯一标识符
  150. $wechatInfo['user_type'] = 'app';//用户类型
  151. $wechatUser = $this->dao->unionIdByWechatUser($unionId);
  152. return Db::transaction(function () use ($createUser, $type, $wechatInfo, $wechatUser) {
  153. if ($wechatUser) {
  154. unset($wechatInfo['nickname']);
  155. $wechatUser->save($wechatInfo);
  156. } else {
  157. $wechatUser = $this->dao->create($wechatInfo);
  158. }
  159. if (!$createUser) {
  160. return [$wechatUser];
  161. }
  162. /** @var UserRepository $userRepository */
  163. $userRepository = app()->make(UserRepository::class);
  164. $user = $userRepository->syncWechatUser($wechatUser, $type);
  165. return [$wechatUser, $user];
  166. });
  167. }
  168. /**
  169. * @param array $where
  170. * @param $page
  171. * @param $limit
  172. * @return array
  173. * @throws DataNotFoundException
  174. * @throws DbException
  175. * @throws ModelNotFoundException
  176. * @author xaboy
  177. * @day 2020-04-29
  178. */
  179. public function getList(array $where, $page, $limit)
  180. {
  181. $query = $this->dao->search($where);
  182. $count = $query->count($this->dao->getPk());
  183. $list = $query->setOption('field', [])->field('uid,openid,nickname,headimgurl,sex,country,province,city,subscribe')
  184. ->page($page, $limit)->select()->each(function ($item) {
  185. $item['subscribe_time'] = $item['subscribe_time'] ? date('Y-m-d H:i', $item['subscribe_time']) : '';
  186. return $item;
  187. });
  188. return compact('count', 'list');
  189. }
  190. /**
  191. * @param $id
  192. * @return Form
  193. * @throws DataNotFoundException
  194. * @throws DbException
  195. * @throws FormBuilderException
  196. * @throws ModelNotFoundException
  197. * @author xaboy
  198. * @day 2020-04-29
  199. */
  200. public function updateUserTagForm($id)
  201. {
  202. $wechatUserTagService = new WechatUserTagService();
  203. $lst = $wechatUserTagService->lst();
  204. $user = $this->dao->get($id);
  205. return Elm::createForm(Route::buildUrl('wechat/user/tag', ['id' => $id]), [
  206. Elm::select('tag_id', '用户标签', explode(',', $user->tagid_list))->options(function () use ($lst) {
  207. $options = [];
  208. foreach ($lst as $item) {
  209. $options[] = ['value' => $item['id'], 'label' => $item['name']];
  210. }
  211. return $options;
  212. })->multiple(true)
  213. ])->setTitle('编辑用户标签');
  214. }
  215. /**
  216. * @param $id
  217. * @param array $tags
  218. * @throws DataNotFoundException
  219. * @throws DbException
  220. * @throws ModelNotFoundException
  221. * @author xaboy
  222. * @day 2020-04-29
  223. */
  224. public function updateTag($id, array $tags)
  225. {
  226. $user = $this->dao->get($id);
  227. $oTags = explode(',', $user->tagid_list);
  228. $user->save(['tagid_list' => implode(',', $tags)]);
  229. $wechatUserTagService = (new WechatUserTagService())->userTag();
  230. foreach ($oTags as $tag) {
  231. $wechatUserTagService->batchUntagUsers([$user->openid], $tag);
  232. }
  233. foreach ($tags as $tag) {
  234. $wechatUserTagService->batchTagUsers([$user->openid], $tag);
  235. }
  236. }
  237. /**
  238. * @param $id
  239. * @return Form
  240. * @throws DataNotFoundException
  241. * @throws DbException
  242. * @throws FormBuilderException
  243. * @throws ModelNotFoundException
  244. * @author xaboy
  245. * @day 2020-04-29
  246. */
  247. public function updateUserGroupForm($id)
  248. {
  249. $wechatUserGroupService = new WechatUserGroupService();
  250. $lst = $wechatUserGroupService->lst();
  251. $user = $this->dao->get($id);
  252. return Elm::createForm(Route::buildUrl('wechat/user/group', ['id' => $id]), [
  253. Elm::select('group_id', '用户标签', (string)$user->groupid)->options(function () use ($lst) {
  254. $options = [];
  255. foreach ($lst as $item) {
  256. $options[] = ['value' => $item['id'], 'label' => $item['name']];
  257. }
  258. return $options;
  259. })
  260. ])->setTitle('编辑用户分组');
  261. }
  262. /**
  263. * @param $id
  264. * @param $groupid
  265. * @throws DataNotFoundException
  266. * @throws DbException
  267. * @throws ModelNotFoundException
  268. * @author xaboy
  269. * @day 2020-04-29
  270. */
  271. public function updateGroup($id, $groupid)
  272. {
  273. $user = $this->dao->get($id);
  274. $user->save(['groupid' => $groupid]);
  275. $wechatUserGroupService = (new WechatUserGroupService())->userGroup();
  276. $wechatUserGroupService->moveUser($user->openid, $groupid);
  277. }
  278. /**
  279. * @param $id
  280. * @param array $ids
  281. * @author xaboy
  282. * @day 2020-05-11
  283. */
  284. public function sendNews($id, array $ids)
  285. {
  286. if (!count($ids)) return;
  287. /** @var ArticleRepository $make */
  288. $make = app()->make(ArticleRepository::class);
  289. $articles = $make->wechatNewIdByData($id);
  290. $news = [];
  291. foreach ($articles as $article) {
  292. $news[] = [
  293. 'title' => $article['title'],
  294. 'image' => $article['image_input'],
  295. 'date' => $article['create_time'],
  296. 'description' => $article['synopsis'],
  297. 'id' => $article['article_id']
  298. ];
  299. }
  300. $make = app()->make(UserRepository::class);
  301. foreach ($ids as $_id) {
  302. $user = $make->get($_id);
  303. if ($this->dao->isSubscribeWechatUser($user->wechat_user_id)) {
  304. Queue::push(SendNewsJob::class, [$user->wechat_user_id, $news]);
  305. }
  306. }
  307. }
  308. }