Oauth.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. $exist = $model->isExistOauthType($userId, $partyData['oauth']);
  40. if ($exist) {
  41. return true;
  42. }
  43. // 如果不存在oauth则写入
  44. return $model->add([
  45. 'user_id' => $userId,
  46. 'oauth_type' => $partyData['oauth'],
  47. 'oauth_id' => $this->getOauthId($partyData),
  48. 'unionid' => $this->geUnionid($partyData),
  49. 'store_id' => $this->storeId
  50. ]);
  51. }
  52. /**
  53. * 获取微信平台的unionid
  54. * @param array $partyData 第三方登录信息
  55. * @return mixed|null
  56. */
  57. private function geUnionid(array $partyData)
  58. {
  59. // todo: 此处暂未实现, 需解析wx.getUserInfo接口返回的encryptedData加密数据
  60. // 参考文档1: https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/union-id.html
  61. // 参考文档2: https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserInfo.html
  62. if ($partyData['oauth'] === 'MP-WEIXIN') {
  63. return '';
  64. }
  65. return null;
  66. }
  67. /**
  68. * 获取第三方用户唯一标识 (openid)
  69. * @param array $partyData
  70. * @return mixed|null
  71. * @throws BaseException
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\DbException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. */
  76. private function getOauthId(array $partyData)
  77. {
  78. if ($partyData['oauth'] === 'MP-WEIXIN') {
  79. return $this->wxCode2Openid($partyData['code']);
  80. }
  81. return null;
  82. }
  83. /**
  84. * 微信小程序code换openid
  85. * @param string $code
  86. * @return mixed
  87. * @throws BaseException
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\DbException
  90. * @throws \think\db\exception\ModelNotFoundException
  91. */
  92. private function wxCode2Openid(string $code)
  93. {
  94. // 获取当前小程序信息
  95. $wxConfig = WxappModel::getWxappCache();
  96. // 微信登录 (获取session_key)
  97. $WxUser = new WxUser($wxConfig['app_id'], $wxConfig['app_secret']);
  98. $result = $WxUser->jscode2session($code);
  99. !$result && throwError($WxUser->getError());
  100. return $result['openid'];
  101. }
  102. }