RoutineServices.php 19 KB

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