RoutineServices.php 20 KB

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