Party.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\service\passport;
  13. use app\api\model\UserOauth as UserOauthModel;
  14. use app\api\service\user\Oauth as OauthService;
  15. use app\api\service\user\Avatar as AvatarService;
  16. use app\common\service\BaseService;
  17. use cores\exception\BaseException;
  18. /**
  19. * 第三方用户注册登录服务
  20. * Class Party
  21. * @package app\api\service\passport
  22. */
  23. class Party extends BaseService
  24. {
  25. /**
  26. * 保存用户的第三方认证信息
  27. * @param int $userId 用户ID
  28. * @param array $partyData 第三方登录信息
  29. * @return bool
  30. * @throws BaseException
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. */
  35. public function createUserOauth(int $userId, array $partyData = []): bool
  36. {
  37. try {
  38. // 获取oauthId和unionId
  39. $oauthInfo = $this->getOauthInfo($partyData);
  40. } catch (BaseException $e) {
  41. // isBack参数代表需重新获取code, 前端拿到该参数进行页面返回
  42. throwError($e->getMessage(), null, ['isBack' => true]);
  43. return false;
  44. }
  45. // 是否存在第三方用户
  46. $oauthId = UserOauthModel::getOauthIdByUserId($userId, $partyData['oauth']);
  47. // 如果不存在oauth则写入
  48. if (empty($oauthId)) {
  49. return (new UserOauthModel)->add([
  50. 'user_id' => $userId,
  51. 'oauth_type' => $partyData['oauth'],
  52. 'oauth_id' => $oauthInfo['oauth_id'],
  53. 'unionid' => $oauthInfo['unionid'] ?? '', // unionid可以不存在
  54. 'store_id' => $this->storeId
  55. ]);
  56. }
  57. // 如果存在第三方用户, 需判断oauthId是否相同
  58. if ($oauthId != $oauthInfo['oauth_id']) {
  59. // isBack参数代表需重新获取code, 前端拿到该参数进行页面返回
  60. throwError('很抱歉,当前手机号已绑定其他微信号', null, ['isBack' => true]);
  61. }
  62. return true;
  63. }
  64. /**
  65. * 获取微信小程序登录态(session)
  66. * 这里支持静态变量缓存, 用于实现第二次调用该方法时直接返回已获得的session
  67. * @param string $code
  68. * @return array|false
  69. * @throws BaseException
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. */
  74. public static function getMpWxSession(string $code)
  75. {
  76. static $session;
  77. if (empty($session)) {
  78. try {
  79. // 微信小程序通过code获取session
  80. $session = OauthService::wxCode2Session($code);
  81. } catch (BaseException $e) {
  82. // showError参数表示让前端显示错误
  83. throwError($e->getMessage());
  84. return false;
  85. }
  86. }
  87. return $session;
  88. }
  89. /**
  90. * 第三方用户信息
  91. * @param array $partyData 第三方用户信息
  92. * @param bool $isGetAvatarUrl 是否下载头像
  93. * @return array
  94. * @throws BaseException
  95. * @throws \think\Exception
  96. */
  97. public static function partyUserInfo(array $partyData, bool $isGetAvatarUrl = true): array
  98. {
  99. $partyUserInfo = $partyData['userInfo'];
  100. $data = [
  101. 'nick_name' => $partyUserInfo['nickName'],
  102. 'gender' => $partyUserInfo['gender']
  103. ];
  104. // 下载用户头像
  105. if ($isGetAvatarUrl) {
  106. $data['avatar_id'] = static::partyAvatar($partyUserInfo['avatarUrl']);
  107. }
  108. return $data;
  109. }
  110. /**
  111. * 下载第三方头像并写入文件库
  112. * @param string $avatarUrl
  113. * @return int
  114. * @throws BaseException
  115. * @throws \think\Exception
  116. */
  117. private static function partyAvatar(string $avatarUrl): int
  118. {
  119. $Avatar = new AvatarService;
  120. $fileId = $Avatar->party($avatarUrl);
  121. return $fileId ? $fileId : 0;
  122. }
  123. /**
  124. * 获取第三方用户session信息 (openid、unionid、session_key等)
  125. * @param array $partyData
  126. * @return array|null
  127. * @throws BaseException
  128. * @throws \think\db\exception\DataNotFoundException
  129. * @throws \think\db\exception\DbException
  130. * @throws \think\db\exception\ModelNotFoundException
  131. */
  132. private function getOauthInfo(array $partyData): ?array
  133. {
  134. if ($partyData['oauth'] === 'MP-WEIXIN') {
  135. $wxSession = static::getMpWxSession($partyData['code']);
  136. return ['oauth_id' => $wxSession['openid'], 'unionid' => $wxSession['unionid'] ?? null];
  137. }
  138. return null;
  139. }
  140. }