WechatUserRepository.php 8.9 KB

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