Oauth.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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\user;
  13. use app\api\model\Wxapp as WxappModel;
  14. use app\common\exception\BaseException;
  15. use app\common\library\wechat\WxUser;
  16. use app\common\service\BaseService;
  17. use app\api\model\UserOauth as UserOauthModel;
  18. /**
  19. * 服务类: 第三方用户服务类
  20. * Class Avatar
  21. * @package app\api\service\user
  22. */
  23. class Oauth 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 party(int $userId, array $partyData = [])
  36. {
  37. // 是否存在第三方用户
  38. $model = new UserOauthModel;
  39. $isExistOauthType = $model->isExistOauthType($userId, $partyData['oauth']);
  40. if ($isExistOauthType) {
  41. return true;
  42. }
  43. // 获取oauth_id和unionId
  44. $oauthInfo = $this->getOauthInfo($partyData);
  45. // 如果不存在oauth则写入
  46. return $model->add([
  47. 'user_id' => $userId,
  48. 'oauth_type' => $partyData['oauth'],
  49. 'oauth_id' => $oauthInfo['oauth_id'],
  50. 'unionid' => $oauthInfo['unionid'] ?? '', // unionid可以不存在
  51. 'store_id' => $this->storeId
  52. ]);
  53. }
  54. /**
  55. * 获取第三方用户session信息 (openid、unionid、session_key等)
  56. * @param array $partyData
  57. * @return mixed|null
  58. * @throws BaseException
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. private function getOauthInfo(array $partyData)
  64. {
  65. if ($partyData['oauth'] === 'MP-WEIXIN') {
  66. $wxSession = $this->wxCode2Session($partyData['code']);
  67. return ['oauth_id' => $wxSession['openid'], 'unionid' => $wxSession['unionid'] ?? null];
  68. }
  69. return null;
  70. }
  71. /**
  72. * 微信小程序code换openid
  73. * @param string $code
  74. * @return mixed
  75. * @throws BaseException
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. */
  80. private function wxCode2Session(string $code)
  81. {
  82. // 获取当前小程序信息
  83. $wxConfig = WxappModel::getWxappCache();
  84. // 微信登录 (获取session_key)
  85. $WxUser = new WxUser($wxConfig['app_id'], $wxConfig['app_secret']);
  86. $result = $WxUser->jscode2session($code);
  87. !$result && throwError($WxUser->getError());
  88. return $result;
  89. }
  90. }