Party.php 5.1 KB

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