RoutineServices.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. declare (strict_types=1);
  12. namespace app\services\wechat;
  13. use app\services\BaseServices;
  14. use app\dao\wechat\WechatUserDao;
  15. use app\services\message\SystemNotificationServices;
  16. use app\services\message\TemplateMessageServices;
  17. use app\services\other\QrcodeServices;
  18. use app\services\user\LoginServices;
  19. use app\services\user\UserServices;
  20. use app\services\user\UserVisitServices;
  21. use crmeb\exceptions\ApiException;
  22. use crmeb\services\CacheService;
  23. use crmeb\services\CacheService as Cache;
  24. use crmeb\services\app\MiniProgramService;
  25. use crmeb\services\oauth\OAuth;
  26. /**
  27. *
  28. * Class RoutineServices
  29. * @package app\services\wechat
  30. */
  31. class RoutineServices extends BaseServices
  32. {
  33. /**
  34. * RoutineServices constructor.
  35. * @param WechatUserDao $dao
  36. */
  37. public function __construct(WechatUserDao $dao)
  38. {
  39. $this->dao = $dao;
  40. }
  41. /**
  42. * 小程序授权登录
  43. * @param $code
  44. * @param $post_cache_key
  45. * @param $login_type
  46. * @param $spread_spid
  47. * @param $spread_code
  48. * @param $iv
  49. * @param $encryptedData
  50. * @return mixed
  51. */
  52. public function mp_auth($code, $post_cache_key, $login_type, $spread_spid, $spread_code, $iv, $encryptedData)
  53. {
  54. /** @var OAuth $oauth */
  55. $oauth = app()->make(OAuth::class, ['mini_program']);
  56. [$userInfoCong, $userInfo] = $oauth->oauth($code, [
  57. 'iv' => $iv,
  58. 'encryptedData' => $encryptedData
  59. ]);
  60. $session_key = $userInfoCong['session_key'];
  61. $userInfo['unionId'] = isset($userInfoCong['unionid']) ? $userInfoCong['unionid'] : '';
  62. $userInfo['openId'] = $openid = $userInfoCong['openid'];
  63. $userInfo['spid'] = $spread_spid;
  64. $userInfo['code'] = $spread_code;
  65. $userInfo['session_key'] = $session_key;
  66. $userInfo['login_type'] = $login_type;
  67. $createData = $this->routineOauth($userInfo);
  68. /** @var WechatUserServices $wechatUserServices */
  69. $wechatUserServices = app()->make(WechatUserServices::class);
  70. $user = $wechatUserServices->getAuthUserInfo($openid, 'routine');
  71. if (!$user) {
  72. $user = $wechatUserServices->wechatOauthAfter($createData);
  73. } else {
  74. //更新用户信息
  75. $wechatUserServices->wechatUpdata([$user['uid'], $createData[1]]);
  76. }
  77. $token = $this->createToken((int)$user['uid'], 'routine');
  78. if ($token) {
  79. /** @var UserVisitServices $visitServices */
  80. $visitServices = app()->make(UserVisitServices::class);
  81. $visitServices->loginSaveVisit($user);
  82. return [
  83. 'userInfo' => $user
  84. ];
  85. } else
  86. throw new ApiException(410038);
  87. }
  88. /**
  89. * 小程序授权登录
  90. * @param $code
  91. * @param $spid
  92. * @param $spread
  93. * @param $iv
  94. * @param $encryptedData
  95. * @return array
  96. * @throws \think\db\exception\DataNotFoundException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. */
  99. public function newAuth($code, $spid, $spread, $iv, $encryptedData)
  100. {
  101. if (!$code) {
  102. throw new ApiException(100100);
  103. }
  104. /** @var OAuth $oauth */
  105. $oauth = app()->make(OAuth::class, ['mini_program']);
  106. [$userInfoCong, $userInfo] = $oauth->oauth($code, [
  107. 'iv' => $iv,
  108. 'encryptedData' => $encryptedData
  109. ]);
  110. $session_key = $userInfoCong['session_key'];
  111. $userInfo['unionId'] = isset($userInfoCong['unionid']) ? $userInfoCong['unionid'] : '';
  112. $userInfo['openId'] = $openid = $userInfoCong['openid'];
  113. $userInfo['spid'] = $spid;
  114. $userInfo['code'] = $spread;
  115. $userInfo['session_key'] = $session_key;
  116. $userInfo['login_type'] = 'routine';
  117. $createData = $this->routineOauth($userInfo);
  118. /** @var WechatUserServices $wechatUserServices */
  119. $wechatUserServices = app()->make(WechatUserServices::class);
  120. $user = $wechatUserServices->getAuthUserInfo($openid, 'routine');
  121. //获取是否强制绑定手机号
  122. $storeUserMobile = sys_config('store_user_mobile');
  123. if ($storeUserMobile && !$user) {
  124. $userInfoKey = md5($openid . '_' . time() . '_rouine');
  125. Cache::setTokenBucket($userInfoKey, $createData, 7200);
  126. return ['key' => $userInfoKey];
  127. } else if (!$user) {
  128. $user = $wechatUserServices->wechatOauthAfter($createData);
  129. } else {
  130. //更新用户信息
  131. $wechatUserServices->wechatUpdata([$user['uid'], $createData[1]]);
  132. }
  133. $token = $this->createToken((int)$user['uid'], 'routine');
  134. if ($token) {
  135. /** @var UserVisitServices $visitServices */
  136. $visitServices = app()->make(UserVisitServices::class);
  137. $visitServices->loginSaveVisit($user);
  138. $token['userInfo'] = $user;
  139. return $token;
  140. } else
  141. throw new ApiException(410019);
  142. }
  143. /**
  144. * 小程序创建用户后返回uid
  145. * @param $routineInfo
  146. * @return mixed
  147. */
  148. public function routineOauth($routine)
  149. {
  150. $routineInfo['nickname'] = filter_emoji($routine['nickName']);//姓名
  151. $routineInfo['sex'] = $routine['gender'];//性别
  152. $routineInfo['language'] = $routine['language'];//语言
  153. $routineInfo['city'] = $routine['city'];//城市
  154. $routineInfo['province'] = $routine['province'];//省份
  155. $routineInfo['country'] = $routine['country'];//国家
  156. $routineInfo['headimgurl'] = $routine['avatarUrl'];//头像
  157. $routineInfo['openid'] = $routine['openId'];
  158. $routineInfo['session_key'] = $routine['session_key'];//会话密匙
  159. $routineInfo['unionid'] = $routine['unionId'];//用户在开放平台的唯一标识符
  160. $routineInfo['user_type'] = 'routine';//用户类型
  161. $routineInfo['phone'] = $routine['phone'] ?? $routine['purePhoneNumber'] ?? '';
  162. $spid = $routine['spid'] ?? 0;//绑定关系uid
  163. //获取是否有扫码进小程序
  164. /** @var QrcodeServices $qrcode */
  165. $qrcode = app()->make(QrcodeServices::class);
  166. if (isset($routine['code']) && $routine['code'] && ($info = $qrcode->get($routine['code']))) {
  167. $spid = $info['third_id'];
  168. }
  169. return [$routine['openId'], $routineInfo, $spid, $routine['login_type'] ?? 'routine', 'routine'];
  170. }
  171. /**
  172. * 小程序支付回调
  173. */
  174. public function notify()
  175. {
  176. return MiniProgramService::handleNotify();
  177. }
  178. /**
  179. * 获取小程序订阅消息id
  180. * @return mixed
  181. */
  182. public function tempIds()
  183. {
  184. return Cache::get('TEMP_IDS_LIST', function () {
  185. /** @var SystemNotificationServices $sysNotify */
  186. $sysNotify = app()->make(SystemNotificationServices::class);
  187. $marks = $sysNotify->getColumn([['routine_id', '>', 0]], 'routine_id', 'mark');
  188. $ids = array_values($marks);
  189. /** @var TemplateMessageServices $tempMsgServices */
  190. $tempMsgServices = app()->make(TemplateMessageServices::class);
  191. $list = $tempMsgServices->getColumn([['id', 'in', $ids]], 'tempid', 'id');
  192. $tempIdsList = [];
  193. foreach ($marks as $key => $item) {
  194. $tempIdsList[$key] = $list[$item];
  195. }
  196. return $tempIdsList;
  197. });
  198. }
  199. /**
  200. * 获取小程序直播列表
  201. * @param $pgae
  202. * @param $limit
  203. * @return mixed
  204. */
  205. public function live($page, $limit)
  206. {
  207. $list = Cache::get('WECHAT_LIVE_LIST_' . $page . '_' . $limit, function () use ($page, $limit) {
  208. $list = MiniProgramService::getLiveInfo((int)$page, (int)$limit);
  209. foreach ($list as &$item) {
  210. $item['_start_time'] = date('m-d H:i', $item['start_time']);
  211. }
  212. return $list;
  213. }, 600) ?: [];
  214. return $list;
  215. }
  216. /**
  217. * 静默授权
  218. * @param $code
  219. * @param $spread
  220. * @return mixed
  221. */
  222. public function silenceAuth($code, $spread, $spid)
  223. {
  224. /** @var OAuth $oauth */
  225. $oauth = app()->make(OAuth::class, ['mini_program']);
  226. $userInfoConfig = $oauth->oauth($code, ['silence' => true]);
  227. if (!isset($userInfoConfig['openid'])) {
  228. throw new ApiException(410078);
  229. }
  230. $routineInfo = [
  231. 'unionid' => $userInfoConfig['unionid'] ?? ''
  232. ];
  233. /** @var QrcodeServices $qrcode */
  234. $qrcode = app()->make(QrcodeServices::class);
  235. if ($spread && ($info = $qrcode->getOne(['id' => $spread, 'status' => 1]))) {
  236. $spid = $info['third_id'];
  237. }
  238. $openid = $userInfoConfig['openid'];
  239. $routineInfo['openid'] = $openid;
  240. $routineInfo['headimgurl'] = sys_config('h5_avatar');
  241. /** @var WechatUserServices $wechatUserServices */
  242. $wechatUserServices = app()->make(WechatUserServices::class);
  243. $user = $wechatUserServices->getAuthUserInfo($openid, 'routine');
  244. $createData = [$openid, $routineInfo, $spid, '', 'routine'];
  245. //获取是否强制绑定手机号
  246. $storeUserMobile = sys_config('store_user_mobile');
  247. if ($storeUserMobile && !$user) {
  248. $userInfoKey = md5($openid . '_' . time() . '_routine');
  249. Cache::setTokenBucket($userInfoKey, $createData, 7200);
  250. return ['key' => $userInfoKey];
  251. } else if (!$user) {
  252. //写入用户信息
  253. $user = $wechatUserServices->wechatOauthAfter($createData);
  254. $token = $this->createToken((int)$user['uid'], 'routine');
  255. if ($token) {
  256. return $token;
  257. } else
  258. throw new ApiException(410019);
  259. } else {
  260. //更新用户信息
  261. $wechatUserServices->wechatUpdata([$user['uid'], ['code' => $spid]]);
  262. $token = $this->createToken((int)$user['uid'], 'routine');
  263. /** @var UserVisitServices $visitServices */
  264. $visitServices = app()->make(UserVisitServices::class);
  265. $visitServices->loginSaveVisit($user);
  266. if ($token) {
  267. return $token;
  268. } else
  269. throw new ApiException(410019);
  270. }
  271. }
  272. /**
  273. * 静默授权
  274. * @param $code
  275. * @param $spread
  276. * @return mixed
  277. */
  278. public function silenceAuthNoLogin($code, $spread, $spid)
  279. {
  280. /** @var OAuth $oauth */
  281. $oauth = app()->make(OAuth::class, ['mini_program']);
  282. $userInfoConfig = $oauth->oauth($code, ['silence' => true]);
  283. if (!isset($userInfoConfig['openid'])) {
  284. throw new ApiException(410078);
  285. }
  286. $routineInfo = [
  287. 'unionid' => $userInfoConfig['unionid'] ?? ''
  288. ];
  289. /** @var QrcodeServices $qrcode */
  290. $qrcode = app()->make(QrcodeServices::class);
  291. if ($spread && ($info = $qrcode->getOne(['id' => $spread, 'status' => 1]))) {
  292. $spid = $info['third_id'];
  293. }
  294. $openid = $userInfoConfig['openid'];
  295. $routineInfo['openid'] = $openid;
  296. $routineInfo['headimgurl'] = sys_config('h5_avatar');
  297. /** @var WechatUserServices $wechatUserServices */
  298. $wechatUserServices = app()->make(WechatUserServices::class);
  299. $user = $wechatUserServices->getAuthUserInfo($openid, 'routine');
  300. $createData = [$openid, $routineInfo, $spid, '', 'routine'];
  301. if (!$user) {
  302. $userInfoKey = md5($openid . '_' . time() . '_routine');
  303. Cache::setTokenBucket($userInfoKey, $createData, 7200);
  304. return ['auth_login' => 1, 'key' => $userInfoKey];
  305. } else {
  306. //更新用户信息
  307. $wechatUserServices->wechatUpdata([$user['uid'], ['code' => $spid]]);
  308. $token = $this->createToken((int)$user['uid'], 'routine');
  309. /** @var UserVisitServices $visitServices */
  310. $visitServices = app()->make(UserVisitServices::class);
  311. $visitServices->loginSaveVisit($user);
  312. if ($token) {
  313. $token['userInfo'] = $user;
  314. return $token;
  315. } else
  316. throw new ApiException(410019);
  317. }
  318. }
  319. /**
  320. * 手机号登录 静默授权绑定关系
  321. * @param $code
  322. * @param $spread
  323. * @return mixed
  324. */
  325. public function silenceAuthBindingPhone($code, $spread, $spid, $phone)
  326. {
  327. /** @var OAuth $oauth */
  328. $oauth = app()->make(OAuth::class, ['mini_program']);
  329. $userInfoConfig = $oauth->oauth($code, ['silence' => true]);
  330. if (!isset($userInfoConfig['openid'])) {
  331. throw new ApiException(410078);
  332. }
  333. $routineInfo = [
  334. 'unionid' => $userInfoConfig['unionid'] ?? ''
  335. ];
  336. /** @var QrcodeServices $qrcode */
  337. $qrcode = app()->make(QrcodeServices::class);
  338. if ($spread && ($info = $qrcode->getOne(['id' => $spread, 'status' => 1]))) {
  339. $spid = $info['third_id'];
  340. }
  341. $openid = $userInfoConfig['openid'];
  342. $routineInfo['openid'] = $openid;
  343. $routineInfo['headimgurl'] = sys_config('h5_avatar');
  344. $routineInfo['phone'] = $phone;
  345. /** @var WechatUserServices $wechatUserServices */
  346. $wechatUserServices = app()->make(WechatUserServices::class);
  347. $createData = [$openid, $routineInfo, $spid, '', 'routine'];
  348. //写入用户信息
  349. $user = $wechatUserServices->wechatOauthAfter($createData);
  350. $token = $this->createToken((int)$user['uid'], 'routine');
  351. /** @var UserVisitServices $visitServices */
  352. $visitServices = app()->make(UserVisitServices::class);
  353. $visitServices->loginSaveVisit($user);
  354. if ($token) {
  355. return $token;
  356. } else
  357. throw new ApiException(410019);
  358. }
  359. /**
  360. * 自动获取手机号绑定
  361. * @param $code
  362. * @param $spread
  363. * @return mixed
  364. */
  365. public function authBindingPhone($code, $iv, $encryptedData, $spread, $spid, $key = '')
  366. {
  367. $wechatInfo = [];
  368. $userInfo = [];
  369. $userType = $login_type = 'routine';
  370. if ($key) {
  371. [$openid, $wechatInfo, $spreadId, $login_type, $userType] = $createData = CacheService::getTokenBucket($key);
  372. }
  373. /** @var OAuth $oauth */
  374. $oauth = app()->make(OAuth::class, ['mini_program']);
  375. [$userInfoCong, $userInfo] = $oauth->oauth($code, [
  376. 'iv' => $iv,
  377. 'encryptedData' => $encryptedData
  378. ]);
  379. $session_key = $userInfoCong['session_key'];
  380. if (!$userInfo || !isset($userInfo['purePhoneNumber'])) {
  381. throw new ApiException(410079);
  382. }
  383. $spreadId = $spid ?? 0;
  384. /** @var QrcodeServices $qrcode */
  385. $qrcode = app()->make(QrcodeServices::class);
  386. if ($spread && ($info = $qrcode->getOne(['id' => $spread, 'status' => 1]))) {
  387. $spreadId = $info['third_id'];
  388. }
  389. $openid = $userInfoCong['openid'];
  390. $wechatInfo['openid'] = $openid;
  391. $wechatInfo['unionid'] = $userInfoCong['unionid'] ?? '';
  392. $wechatInfo['spid'] = $spreadId;
  393. $wechatInfo['code'] = $spread;
  394. $wechatInfo['session_key'] = $session_key;
  395. $wechatInfo['phone'] = $userInfo['purePhoneNumber'];
  396. /** @var WechatUserServices $wechatUserServices */
  397. $wechatUserServices = app()->make(WechatUserServices::class);
  398. //写入用户信息
  399. $user = $wechatUserServices->wechatOauthAfter([$openid, $wechatInfo, $spreadId, $login_type, $userType]);
  400. $token = $this->createToken((int)$user['uid'], 'routine');
  401. if ($token) {
  402. /** @var UserVisitServices $visitServices */
  403. $visitServices = app()->make(UserVisitServices::class);
  404. $visitServices->loginSaveVisit($user);
  405. return [
  406. 'token' => $token['token'],
  407. 'userInfo' => $user,
  408. 'expires_time' => $token['params']['exp'],
  409. ];
  410. } else
  411. throw new ApiException(410019);
  412. }
  413. public function updateUserInfo($uid, array $data)
  414. {
  415. /** @var UserServices $userServices */
  416. $userServices = app()->make(UserServices::class);
  417. $user = $userServices->getUserInfo($uid);
  418. if (!$user) {
  419. throw new ApiException(100026);
  420. }
  421. $userInfo = [];
  422. $userInfo['nickname'] = filter_emoji($data['nickName'] ?? '');//姓名
  423. $userInfo['sex'] = $data['gender'] ?? '';//性别
  424. $userInfo['language'] = $data['language'] ?? '';//语言
  425. $userInfo['city'] = $data['city'] ?? '';//城市
  426. $userInfo['province'] = $data['province'] ?? '';//省份
  427. $userInfo['country'] = $data['country'] ?? '';//国家
  428. $userInfo['headimgurl'] = $data['avatarUrl'] ?? '';//头像
  429. $userInfo['is_complete'] = 1;
  430. /** @var LoginServices $loginService */
  431. $loginService = app()->make(LoginServices::class);
  432. $loginService->updateUserInfo($userInfo, $user);
  433. //更新用户信息
  434. if (!$this->dao->update(['uid' => $user['uid'], 'user_type' => 'routine'], $userInfo)) {
  435. throw new ApiException(100013);
  436. }
  437. return true;
  438. }
  439. }