Party.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2023 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. }
  44. // 是否存在第三方用户
  45. $oauthId = UserOauthModel::getOauthIdByUserId($userId, $partyData['oauth']);
  46. // 如果不存在oauth则写入
  47. if (empty($oauthId)) {
  48. return (new UserOauthModel)->add([
  49. 'user_id' => $userId,
  50. 'oauth_type' => $partyData['oauth'],
  51. 'oauth_id' => $oauthInfo['oauth_id'],
  52. 'unionid' => $oauthInfo['unionid'] ?? '', // unionid可以不存在
  53. 'store_id' => $this->storeId
  54. ]);
  55. }
  56. // 如果存在第三方用户, 需判断oauthId是否相同
  57. if ($oauthId != $oauthInfo['oauth_id']) {
  58. // isBack参数代表需重新获取code, 前端拿到该参数进行页面返回
  59. throwError('很抱歉,当前手机号已绑定其他微信号', null, ['isBack' => true]);
  60. }
  61. return true;
  62. }
  63. /**
  64. * 获取微信小程序登录态(session)
  65. * 这里支持静态变量缓存, 用于实现第二次调用该方法时直接返回已获得的session
  66. * @param string $code
  67. * @return array|false
  68. * @throws BaseException
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\DbException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. */
  73. public static function getMpWxSession(string $code)
  74. {
  75. static $session;
  76. if (empty($session)) {
  77. try {
  78. // 微信小程序通过code获取session
  79. $session = OauthService::wxCode2Session($code);
  80. } catch (BaseException $e) {
  81. // showError参数表示让前端显示错误
  82. throwError($e->getMessage());
  83. }
  84. }
  85. return $session;
  86. }
  87. /**
  88. * 第三方用户信息
  89. * @param array $partyData 第三方用户信息
  90. * @param bool $defaultNickName 是否需要生成默认用户昵称 (仅首次注册时)
  91. * @param bool $isGetAvatarUrl 是否下载头像
  92. * @return array
  93. * @throws BaseException
  94. * @throws \think\Exception
  95. */
  96. public static function partyUserInfo(array $partyData, bool $defaultNickName = false, bool $isGetAvatarUrl = true): array
  97. {
  98. $partyUserInfo = $partyData['userInfo'] ?? [];
  99. $data = [];
  100. if (!empty($partyUserInfo['nickName'])) {
  101. $data['nick_name'] = $partyUserInfo['nickName'];
  102. }
  103. // 生成默认的用户昵称
  104. if ($defaultNickName && empty($data['nick_name'])) {
  105. $data['nick_name'] = self::getDefaultNickName($partyData);
  106. }
  107. if ($isGetAvatarUrl) {
  108. // 记录avatarId
  109. if (!empty($partyUserInfo['avatarId']) && $partyUserInfo['avatarId'] > 0) {
  110. $data['avatar_id'] = (int)$partyUserInfo['avatarId'];
  111. }
  112. // 通过外链下载头像
  113. if (empty($data['avatar_id']) && !empty($partyUserInfo['avatarUrl'])) {
  114. $data['avatar_id'] = static::partyAvatar($partyUserInfo['avatarUrl']);
  115. }
  116. }
  117. return $data;
  118. }
  119. /**
  120. * 下载第三方头像并写入文件库
  121. * @param string $avatarUrl
  122. * @return int
  123. * @throws BaseException
  124. * @throws \think\Exception
  125. */
  126. private static function partyAvatar(string $avatarUrl): int
  127. {
  128. $Avatar = new AvatarService;
  129. $fileId = $Avatar->party($avatarUrl);
  130. return $fileId ? $fileId : 0;
  131. }
  132. /**
  133. * 获取第三方用户session信息 (openid、unionid、session_key等)
  134. * @param array $partyData
  135. * @return array|null
  136. * @throws BaseException
  137. * @throws \think\db\exception\DataNotFoundException
  138. * @throws \think\db\exception\DbException
  139. * @throws \think\db\exception\ModelNotFoundException
  140. */
  141. private function getOauthInfo(array $partyData): ?array
  142. {
  143. if ($partyData['oauth'] === 'MP-WEIXIN') {
  144. $wxSession = static::getMpWxSession($partyData['code']);
  145. return ['oauth_id' => $wxSession['openid'], 'unionid' => $wxSession['unionid'] ?? null];
  146. }
  147. return null;
  148. }
  149. }