Oauth.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2023 ThinkAdmin [ thinkadmin.top ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免责声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/WeChatDeveloper
  13. // | github 代码仓库:https://github.com/zoujingli/WeChatDeveloper
  14. // +----------------------------------------------------------------------
  15. namespace WeChat;
  16. use WeChat\Contracts\BasicWeChat;
  17. /**
  18. * 微信网页授权
  19. * Class Oauth
  20. * @package WeChat
  21. */
  22. class Oauth extends BasicWeChat
  23. {
  24. /**
  25. * Oauth 授权跳转接口
  26. * @param string $redirect_url 授权回跳地址
  27. * @param string $state 为重定向后会带上state参数(填写a-zA-Z0-9的参数值,最多128字节)
  28. * @param string $scope 授权类类型(可选值snsapi_base|snsapi_userinfo)
  29. * @return string
  30. */
  31. public function getOauthRedirect($redirect_url, $state = '', $scope = 'snsapi_base')
  32. {
  33. $appid = $this->config->get('appid');
  34. $redirect_uri = urlencode($redirect_url);
  35. return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$redirect_uri}&response_type=code&scope={$scope}&state={$state}#wechat_redirect";
  36. }
  37. /**
  38. * 通过 code 获取 AccessToken 和 openid
  39. * @param string $code 授权Code值,不传则取GET参数
  40. * @return array
  41. * @throws \WeChat\Exceptions\InvalidResponseException
  42. * @throws \WeChat\Exceptions\LocalCacheException
  43. */
  44. public function getOauthAccessToken($code = '')
  45. {
  46. $appid = $this->config->get('appid');
  47. $appsecret = $this->config->get('appsecret');
  48. $code = $code ? $code : (isset($_GET['code']) ? $_GET['code'] : '');
  49. $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$appsecret}&code={$code}&grant_type=authorization_code";
  50. return $this->httpGetForJson($url);
  51. }
  52. /**
  53. * 刷新AccessToken并续期
  54. * @param string $refresh_token
  55. * @return array
  56. * @throws \WeChat\Exceptions\InvalidResponseException
  57. * @throws \WeChat\Exceptions\LocalCacheException
  58. */
  59. public function getOauthRefreshToken($refresh_token)
  60. {
  61. $appid = $this->config->get('appid');
  62. $url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={$appid}&grant_type=refresh_token&refresh_token={$refresh_token}";
  63. return $this->httpGetForJson($url);
  64. }
  65. /**
  66. * 检验授权凭证(access_token)是否有效
  67. * @param string $access_token 网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同
  68. * @param string $openid 用户的唯一标识
  69. * @return array
  70. * @throws \WeChat\Exceptions\InvalidResponseException
  71. * @throws \WeChat\Exceptions\LocalCacheException
  72. */
  73. public function checkOauthAccessToken($access_token, $openid)
  74. {
  75. $url = "https://api.weixin.qq.com/sns/auth?access_token={$access_token}&openid={$openid}";
  76. return $this->httpGetForJson($url);
  77. }
  78. /**
  79. * 拉取用户信息(需scope为 snsapi_userinfo)
  80. * @param string $access_token 网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同
  81. * @param string $openid 用户的唯一标识
  82. * @param string $lang 返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语
  83. * @return array
  84. * @throws \WeChat\Exceptions\InvalidResponseException
  85. * @throws \WeChat\Exceptions\LocalCacheException
  86. */
  87. public function getUserInfo($access_token, $openid, $lang = 'zh_CN')
  88. {
  89. $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang={$lang}";
  90. return $this->httpGetForJson($url);
  91. }
  92. }