RoutineServices.php 18 KB

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