Oauth.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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\UserOauth as UserOauthModel;
  14. use app\api\model\wxapp\Setting as WxappSettingModel;
  15. use app\common\service\BaseService;
  16. use app\common\library\wechat\WxUser;
  17. use app\common\library\wechat\ErrorCode;
  18. use app\common\library\wechat\WXBizDataCrypt;
  19. use cores\exception\BaseException;
  20. /**
  21. * 服务类: 第三方用户服务类
  22. * Class Avatar
  23. * @package app\api\service\user
  24. */
  25. class Oauth extends BaseService
  26. {
  27. /**
  28. * 微信小程序通过code获取session (openid session_key unionid)
  29. * @param string $code
  30. * @return array|false
  31. * @throws BaseException
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. */
  36. public static function wxCode2Session(string $code)
  37. {
  38. // 获取当前小程序信息
  39. $config = self::getMpWxConfig();
  40. // 微信登录 (获取session_key)
  41. $WxUser = new WxUser($config['app_id'], $config['app_secret']);
  42. $result = $WxUser->jscode2session($code);
  43. empty($result) && throwError($WxUser->getError());
  44. return $result;
  45. }
  46. /**
  47. * 解密微信的加密数据encryptedData
  48. * @param string $encryptedData
  49. * @param string $iv
  50. * @param string|null $sessionKey
  51. * @return mixed
  52. * @throws BaseException
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\DbException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. * @throws \cores\exception\BaseException
  57. */
  58. public static function wxDecryptData(string $encryptedData, string $iv, ?string $sessionKey = null)
  59. {
  60. // 微信数据解密
  61. $WXBizDataCrypt = new WXBizDataCrypt($sessionKey);
  62. $plainData = null;
  63. $code = $WXBizDataCrypt->decryptData($encryptedData, $iv, $plainData);
  64. if ($code !== ErrorCode::$OK) {
  65. throwError('微信数据 encryptedData 解密失败');
  66. }
  67. return $plainData;
  68. }
  69. /**
  70. * 获取微信小程序配置项
  71. * @return array
  72. * @throws \cores\exception\BaseException
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\DbException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. */
  77. private static function getMpWxConfig(): array
  78. {
  79. $config = WxappSettingModel::getConfigBasic();
  80. if (empty($config['app_id']) || empty($config['app_secret'])) {
  81. throwError('请到后台小程序设置填写AppID和AppSecret参数');
  82. }
  83. return $config;
  84. }
  85. /**
  86. * 根据openid获取用户ID
  87. * @param string $oauthId 第三方用户唯一标识 (openid)
  88. * @param string $oauthType 第三方登陆类型
  89. * @return mixed
  90. */
  91. public static function getUserIdByOauthId(string $oauthId, string $oauthType)
  92. {
  93. return UserOauthModel::getUserIdByOauthId($oauthId, $oauthType);
  94. }
  95. }