WechatUserServices.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. declare (strict_types=1);
  12. namespace app\services\wechat;
  13. use app\services\BaseServices;
  14. use app\dao\wechat\WechatUserDao;
  15. use app\services\coupon\StoreCouponIssueServices;
  16. use app\services\user\LoginServices;
  17. use app\services\user\UserServices;
  18. use crmeb\exceptions\AdminException;
  19. use crmeb\exceptions\AuthException;
  20. use crmeb\services\WechatService;
  21. use think\exception\ValidateException;
  22. /**
  23. *
  24. * Class WechatUserServices
  25. * @package app\services\wechat
  26. * @method delete($id, ?string $key = null) 删除
  27. * @method update($id, array $data, ?string $key = null) 更新数据
  28. * @method getColumn(array $where, string $field, string $key = '') 获取某个字段数组
  29. * @method get($id, ?array $field = []) 用主键获取一条数据
  30. * @method getOne(array $where, ?string $field = '*', array $with = []) 获得一条数据
  31. * @method value(array $value, string $key) 获取一条数据
  32. * @method getWechatTrendData($time, $where, $timeType, $key)
  33. * @method getWechatOpenid(int $uid, string $userType = 'wechat') 获取微信公众号openid
  34. */
  35. class WechatUserServices extends BaseServices
  36. {
  37. /**
  38. * WechatUserServices constructor.
  39. * @param WechatUserDao $dao
  40. */
  41. public function __construct(WechatUserDao $dao)
  42. {
  43. $this->dao = $dao;
  44. }
  45. public function getColumnUser($user_ids, $column, $key, string $user_type = 'wechat')
  46. {
  47. return $this->dao->getColumn([['uid', 'IN', $user_ids], ['user_type', '=', $user_type]], $column, $key);
  48. }
  49. /**
  50. * 获取单个微信用户
  51. * @param array $where
  52. * @param string $field
  53. * @return array
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\DbException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. */
  58. public function getWechatUserInfo(array $where, $field = '*')
  59. {
  60. return $this->dao->getOne($where, $field);
  61. }
  62. /**
  63. * 用uid获得 微信openid
  64. * @param $uid
  65. * @return mixed
  66. */
  67. public function uidToOpenid(int $uid, string $userType = 'wechat')
  68. {
  69. return $this->dao->value(['uid' => $uid, 'user_type' => $userType], 'openid');
  70. }
  71. /**
  72. * TODO 用openid获得uid
  73. * @param $openid
  74. * @param string $openidType
  75. * @return mixed
  76. */
  77. public function openidTouid($openid, $openidType = 'openid')
  78. {
  79. $uid = $this->dao->value([[$openidType, '=', $openid], ['user_type', '<>', 'h5']], 'uid');
  80. if (!$uid)
  81. throw new AdminException('对应的uid不存在');
  82. return $uid;
  83. }
  84. /**
  85. * 用户取消关注
  86. * @param $openid
  87. * @return bool
  88. */
  89. public function unSubscribe($openid)
  90. {
  91. if (!$this->dao->update($openid, ['subscribe' => 0, 'subscribe_time' => time()], 'openid'))
  92. throw new AdminException('取消关注失败');
  93. return true;
  94. }
  95. /**
  96. * 用户存在就更新 不存在就添加
  97. * @param $openid
  98. */
  99. public function saveUser($openid)
  100. {
  101. if ($this->getWechatUserInfo(['openid' => $openid]))
  102. $this->updateUser($openid);
  103. else
  104. $this->setNewUser($openid);
  105. }
  106. /**
  107. * 更新用户信息
  108. * @param $openid
  109. * @return bool
  110. */
  111. public function updateUser($openid)
  112. {
  113. $userInfo = WechatService::getUserInfo($openid);
  114. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  115. if (!$this->dao->update($openid, $userInfo->toArray(), 'openid'))
  116. throw new AdminException('更新失败');
  117. return true;
  118. }
  119. /**
  120. * .添加新用户
  121. * @param $openid
  122. * @return object
  123. */
  124. public function setNewUser($openid)
  125. {
  126. $userInfo = WechatService::getUserInfo($openid);
  127. if (!isset($userInfo['subscribe']) || !$userInfo['subscribe'] || !isset($userInfo['openid']))
  128. throw new ValidateException('请关注公众号!');
  129. $userInfo = $userInfo->toArray();
  130. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  131. //判断 unionid 是否存在
  132. $userInfo = is_object($userInfo) ? $userInfo->toArray() : $userInfo;
  133. $wechatInfo = [];
  134. $uid = 0;
  135. if (isset($userInfo['unionid'])) {
  136. $wechatInfo = $this->getWechatUserInfo(['unionid' => $userInfo['unionid']]);
  137. }
  138. if (!$wechatInfo) {
  139. /** @var UserServices $userServices */
  140. $userServices = app()->make(UserServices::class);
  141. $userInfoData = $userServices->setUserInfo($userInfo);
  142. if (!$userInfoData) {
  143. throw new AdminException('用户信息储存失败!');
  144. }
  145. $uid = $userInfoData->uid;
  146. } else {
  147. $uid = $wechatInfo['uid'];
  148. }
  149. $userInfo['user_type'] = 'wechat';
  150. $userInfo['add_time'] = time();
  151. $userInfo['uid'] = $uid;
  152. if (!$this->dao->save($userInfo)) {
  153. throw new AdminException('用户储存失败!');
  154. }
  155. return $userInfoData;
  156. }
  157. /**
  158. * 授权后获取用户信息
  159. * @param $openid
  160. * @param $user_type
  161. */
  162. public function getAuthUserInfo($openid, $user_type)
  163. {
  164. $user = [];
  165. //兼容老用户
  166. $uids = $this->dao->getColumn(['unionid|openid' => $openid], 'uid,user_type', 'user_type');
  167. if ($uids) {
  168. $uid = $uids[$user_type]['uid'] ?? 0;
  169. if (!$uid) {
  170. $ids = array_column($uids, 'uid');
  171. $uid = $ids[0];
  172. }
  173. /** @var UserServices $userServices */
  174. $userServices = app()->make(UserServices::class);
  175. $user = $userServices->getUserInfo($uid);
  176. }
  177. return $user;
  178. }
  179. /**
  180. * 更新微信用户信息
  181. * @param $event
  182. * @return bool
  183. */
  184. public function wechatUpdata($data)
  185. {
  186. [$uid, $userData] = $data;
  187. /** @var UserServices $userServices */
  188. $userServices = app()->make(UserServices::class);
  189. if (!$userInfo = $userServices->getUserInfo($uid)) {
  190. return false;
  191. }
  192. /** @var LoginServices $loginService */
  193. $loginService = app()->make(LoginServices::class);
  194. $loginService->updateUserInfo($userData, $userInfo);
  195. //更新用户信息
  196. /** @var WechatUserServices $wechatUser */
  197. $wechatUser = app()->make(WechatUserServices::class);
  198. $wechatUserInfo = [];
  199. if (isset($userData['nickname']) && $userData['nickname']) $wechatUserInfo['nickname'] = filter_emoji($userData['nickname'] ?? '');//姓名
  200. if (isset($userData['headimgurl']) && $userData['headimgurl']) $wechatUserInfo['headimgurl'] = $userData['avatarUrl'] ?? '';//头像
  201. if (isset($userData['sex']) && $userData['sex']) $wechatUserInfo['sex'] = $userData['gender'] ?? '';//性别
  202. if (isset($userData['language']) && $userData['language']) $wechatUserInfo['language'] = $userData['language'] ?? '';//语言
  203. if (isset($userData['city']) && $userData['city']) $wechatUserInfo['city'] = $userData['city'] ?? '';//城市
  204. if (isset($userData['province']) && $userData['province']) $wechatUserInfo['province'] = $userData['province'] ?? '';//省份
  205. if (isset($userData['country']) && $userData['country']) $wechatUserInfo['country'] = $userData['country'] ?? '';//国家
  206. if (isset($wechatUserInfo['nickname']) || isset($wechatUserInfo['headimgurl'])) $wechatUserInfo['is_complete'] = 1;
  207. if ($wechatUserInfo) {
  208. if (isset($userData['openid']) && $userData['openid'] && false === $wechatUser->update(['uid' => $userInfo['uid'], 'openid' => $userData['openid']], $wechatUserInfo)) {
  209. throw new ValidateException('更新失败');
  210. }
  211. }
  212. return true;
  213. }
  214. /**
  215. * 微信授权成功后
  216. * @param $event
  217. * @throws \think\db\exception\DataNotFoundException
  218. * @throws \think\db\exception\ModelNotFoundException
  219. * @throws \think\exception\DbException
  220. */
  221. public function wechatOauthAfter(array $data)
  222. {
  223. [$openid, $wechatInfo, $spreadId, $login_type, $userType] = $data;
  224. /** @var UserServices $userServices */
  225. $userServices = app()->make(UserServices::class);
  226. if (!$userServices->getUserInfo((int)$spreadId)) {
  227. $spreadId = 0;
  228. }
  229. if (isset($wechatInfo['subscribe_scene'])) {
  230. unset($wechatInfo['subscribe_scene']);
  231. }
  232. if (isset($wechatInfo['qr_scene'])) {
  233. unset($wechatInfo['qr_scene']);
  234. }
  235. if (isset($wechatInfo['qr_scene_str'])) {
  236. unset($wechatInfo['qr_scene_str']);
  237. }
  238. if ($login_type) {
  239. $wechatInfo['login_type'] = $login_type;
  240. }
  241. if (!isset($wechatInfo['nickname'])) {
  242. if (isset($wechatInfo['phone']) && $wechatInfo['phone']) {
  243. $wechatInfo['nickname'] = substr_replace($wechatInfo['phone'], '****', 3, 4);
  244. } else {
  245. $wechatInfo['nickname'] = 'wx' . rand(100000, 999999);
  246. }
  247. } else {
  248. $wechatInfo['is_complete'] = 1;
  249. $wechatInfo['nickname'] = filter_emoji($wechatInfo['nickname']);
  250. }
  251. $userInfo = [];
  252. $uid = 0;
  253. if (isset($wechatInfo['phone']) && $wechatInfo['phone']) {
  254. $userInfo = $userServices->getOne(['phone' => $wechatInfo['phone']]);
  255. }
  256. if (!$userInfo) {
  257. if (isset($wechatInfo['unionid']) && $wechatInfo['unionid']) {
  258. $uid = $this->dao->value(['unionid' => $wechatInfo['unionid']], 'uid');
  259. if ($uid) {
  260. $userInfo = $userServices->getOne(['uid' => $uid]);
  261. }
  262. } else {
  263. $userInfo = $this->getAuthUserInfo($openid, $userType);
  264. }
  265. }
  266. if ($userInfo) {
  267. $uid = (int)$userInfo['uid'];
  268. }
  269. $wechatInfo['user_type'] = $userType;
  270. //user表存在和wechat_user表同时存在
  271. if ($userInfo) {
  272. //更新用户表和wechat_user表
  273. //判断该类性用户在wechatUser中是否存在
  274. $wechatUser = $this->dao->getOne(['uid' => $uid, 'user_type' => $userType]);
  275. /** @var LoginServices $loginService */
  276. $loginService = app()->make(LoginServices::class);
  277. $this->transaction(function () use ($loginService, $wechatInfo, $userInfo, $uid, $userType, $spreadId, $wechatUser) {
  278. $wechatInfo['code'] = $spreadId;
  279. $loginService->updateUserInfo($wechatInfo, $userInfo);
  280. if ($wechatUser) {
  281. if (!$this->dao->update($wechatUser['id'], $wechatInfo, 'id')) {
  282. throw new ValidateException('更新数据失败');
  283. }
  284. } else {
  285. $wechatInfo['uid'] = $uid;
  286. if (!$this->dao->save($wechatInfo)) {
  287. throw new ValidateException('写入信息失败');
  288. }
  289. }
  290. });
  291. } else {
  292. //user表没有用户,wechat_user表没有用户创建新用户
  293. //不存在则创建用户
  294. $userInfo = $this->transaction(function () use ($userServices, $wechatInfo, $spreadId, $userType) {
  295. $userInfo = $userServices->setUserInfo($wechatInfo, (int)$spreadId, $userType);
  296. if (!$userInfo) {
  297. throw new AuthException('生成User用户失败!');
  298. }
  299. $wechatInfo['uid'] = $userInfo->uid;
  300. $wechatInfo['add_time'] = $userInfo->add_time;
  301. if (!$this->dao->save($wechatInfo)) {
  302. throw new AuthException('生成微信用户失败!');
  303. }
  304. return $userInfo;
  305. });
  306. }
  307. return $userInfo;
  308. }
  309. /**
  310. * 更新用户信息(同步)
  311. * @param array $openids
  312. * @return array
  313. * @throws \think\db\exception\DataNotFoundException
  314. * @throws \think\db\exception\DbException
  315. * @throws \think\db\exception\ModelNotFoundException
  316. */
  317. public function syncWechatUser(array $openids)
  318. {
  319. if (!$openids) {
  320. return [];
  321. }
  322. $wechatUser = $this->dao->getList([['openid', 'in', $openids]]);
  323. $noBeOpenids = $openids;
  324. if ($wechatUser) {
  325. $beOpenids = array_column($wechatUser, 'openid');
  326. $noBeOpenids = array_diff($openids, $beOpenids);
  327. // $beWechatUserInfo = WechatService::getUserInfo($beOpenids);
  328. if ($beOpenids) {
  329. $data = [];
  330. foreach ($beOpenids as $openid) {
  331. try {
  332. $info = WechatService::getUserInfo($openid);
  333. } catch (\Throwable $e) {
  334. $info = [];
  335. }
  336. if (!$info) continue;
  337. $data['subscribe'] = $info['subscribe'];
  338. if ($info['subscribe'] == 1) {
  339. $data['unionid'] = $info['unionid'] ?? '';
  340. $data['nickname'] = $info['nickname'] ?? '';
  341. $data['sex'] = $info['sex'] ?? 0;
  342. $data['language'] = $info['language'] ?? '';
  343. $data['city'] = $info['city'] ?? '';
  344. $data['province'] = $info['province'] ?? '';
  345. $data['country'] = $info['country'] ?? '';
  346. $data['headimgurl'] = $info['headimgurl'] ?? '';
  347. $data['subscribe_time'] = $info['subscribe_time'] ?? '';
  348. $data['groupid'] = $info['groupid'] ?? 0;
  349. $data['remark'] = $info['remark'] ?? '';
  350. $data['tagid_list'] = isset($info['tagid_list']) && $info['tagid_list'] ? implode(',', $info['tagid_list']) : '';
  351. }
  352. $this->dao->update(['openid' => $info['openid']], $data);
  353. }
  354. }
  355. }
  356. return $noBeOpenids;
  357. }
  358. }