Oauth.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\index\service\user;
  13. use app\index\model\UserOauth as UserOauthModel;
  14. use app\index\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. $wxConfig = static::getMpWxConfig();
  40. // 微信登录 (获取session_key)
  41. $WxUser = new WxUser($wxConfig['app_id'], $wxConfig['app_secret']);
  42. $result = $WxUser->jscode2session($code);
  43. !$result && throwError($WxUser->getError());
  44. return $result;
  45. }
  46. /**
  47. * 解密微信的加密数据encryptedData
  48. * @param string $sessionKey
  49. * @param string $encryptedData
  50. * @param string $iv
  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 $sessionKey, string $encryptedData, string $iv)
  59. {
  60. // 获取当前小程序信息
  61. $wxConfig = static::getMpWxConfig();
  62. // 微信数据解密
  63. $WXBizDataCrypt = new WXBizDataCrypt($wxConfig['app_id'], $sessionKey);
  64. $content = null;
  65. $code = $WXBizDataCrypt->decryptData($encryptedData, $iv, $content);
  66. if ($code !== ErrorCode::$OK) {
  67. throwError('微信数据 encryptedData 解密失败');
  68. }
  69. return $content;
  70. }
  71. /**
  72. * 获取微信小程序配置项
  73. * @return array
  74. * @throws \cores\exception\BaseException
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\DbException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. */
  79. private static function getMpWxConfig(): array
  80. {
  81. $wxConfig = WxappSettingModel::getWxappConfig();
  82. if (empty($wxConfig['app_id']) || empty($wxConfig['app_secret'])) {
  83. throwError('请到后台小程序设置填写AppID和AppSecret参数');
  84. }
  85. return $wxConfig;
  86. }
  87. /**
  88. * 根据openid获取用户ID
  89. * @param string $oauthId 第三方用户唯一标识 (openid)
  90. * @param string $oauthType 第三方登陆类型
  91. * @return mixed
  92. */
  93. public static function getUserIdByOauthId(string $oauthId, string $oauthType)
  94. {
  95. return UserOauthModel::getUserIdByOauthId($oauthId, $oauthType);
  96. }
  97. }