Oauth.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 $data 第三方登录信息
  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 $data = [])
  36. {
  37. // try {
  38. // // 获取oauthId和unionId
  39. // $oauthInfo = $this->getOauthInfo($data);
  40. // } catch (BaseException $e) {
  41. // // isBack参数代表需重新获取code, 前端拿到该参数进行页面返回
  42. // throwError($e->getMessage(), null, ['isBack' => true]);
  43. // return false;
  44. // }
  45. // 是否存在第三方用户
  46. $oauthId = UserOauthModel::getOauthIdByUserId($userId, $data['oauth']);
  47. // 如果不存在oauth则写入
  48. if (empty($oauthId)) {
  49. return (new UserOauthModel)->add([
  50. 'user_id' => $userId,
  51. 'oauth_type' => $data['oauth'],
  52. 'oauth_id' => $data['openid'],
  53. 'unionid' => $data['unionid'] ?? '', // unionid可以不存在
  54. 'store_id' => $this->storeId
  55. ]);
  56. }
  57. // 如果存在第三方用户, 需判断oauthId是否相同
  58. if ($oauthId != $data['openid']) {
  59. throwError('很抱歉,当前手机号已绑定其他微信号', null, ['isBack' => true]);
  60. }
  61. return true;
  62. }
  63. /**
  64. * 获取第三方用户session信息 (openid、unionid、session_key等)
  65. * @param array $partyData
  66. * @return mixed|null
  67. * @throws BaseException
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. */
  72. private function getOauthInfo(array $data)
  73. {
  74. if ($data['oauth'] === 'MP-WEIXIN') {
  75. $wxSession = static::wxCode2Session($data['code']);
  76. return ['oauth_id' => $wxSession['openid'], 'unionid' => $wxSession['unionid'] ?? null];
  77. }
  78. return null;
  79. }
  80. /**
  81. * 微信小程序code换openid
  82. * @param string $code
  83. * @return mixed
  84. * @throws BaseException
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\DbException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. */
  89. public static function wxCode2Session(string $code)
  90. {
  91. // 获取当前小程序信息
  92. $wxConfig = WxappModel::getWxappCache();
  93. // 微信登录 (获取session_key)
  94. $WxUser = new WxUser($wxConfig['app_id'], $wxConfig['app_secret']);
  95. $result = $WxUser->jscode2session($code);
  96. !$result && throwError($WxUser->getError());
  97. return $result;
  98. }
  99. /**
  100. * 根据openid获取用户ID
  101. * @param string $oauthId 第三方用户唯一标识 (openid)
  102. * @param string $oauthType 第三方登陆类型
  103. * @return mixed
  104. */
  105. public static function getUserIdByOauthId(string $oauthId, string $oauthType)
  106. {
  107. return UserOauthModel::getUserIdByOauthId($oauthId, $oauthType);
  108. }
  109. }