Party.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. }
  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 $isGetAvatarUrl 是否下载头像
  91. * @return array
  92. * @throws BaseException
  93. * @throws \think\Exception
  94. */
  95. public static function partyUserInfo(array $partyData, bool $isGetAvatarUrl = true): array
  96. {
  97. $partyUserInfo = $partyData['userInfo'];
  98. $data = [
  99. 'nick_name' => $partyUserInfo['nickName'],
  100. 'gender' => $partyUserInfo['gender']
  101. ];
  102. // 下载用户头像
  103. if ($isGetAvatarUrl) {
  104. $data['avatar_id'] = static::partyAvatar($partyUserInfo['avatarUrl']);
  105. }
  106. return $data;
  107. }
  108. /**
  109. * 下载第三方头像并写入文件库
  110. * @param string $avatarUrl
  111. * @return int
  112. * @throws BaseException
  113. * @throws \think\Exception
  114. */
  115. private static function partyAvatar(string $avatarUrl): int
  116. {
  117. $Avatar = new AvatarService;
  118. $fileId = $Avatar->party($avatarUrl);
  119. return $fileId ? $fileId : 0;
  120. }
  121. /**
  122. * 获取第三方用户session信息 (openid、unionid、session_key等)
  123. * @param array $partyData
  124. * @return array|null
  125. * @throws BaseException
  126. * @throws \think\db\exception\DataNotFoundException
  127. * @throws \think\db\exception\DbException
  128. * @throws \think\db\exception\ModelNotFoundException
  129. */
  130. private function getOauthInfo(array $partyData): ?array
  131. {
  132. if ($partyData['oauth'] === 'MP-WEIXIN') {
  133. $wxSession = static::getMpWxSession($partyData['code']);
  134. return ['oauth_id' => $wxSession['openid'], 'unionid' => $wxSession['unionid'] ?? null];
  135. }
  136. return null;
  137. }
  138. }