123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <?php
- /**
- * Wxqrcode.php UTF-8
- * 微信网站扫码登陆Api
- *
- * @date : 2018/4/25 12:30
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @version : HUOSDK 8.0
- * https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN
- */
- namespace huolib\oauth\driver;
- use huolib\oauth\OAuth;
- use huolib\tool\Http;
- use think\Exception;
- class Wxqrcode extends OAuth {
- /**
- * 获取requestCode的api接口
- *
- * @var string
- */
- protected $request_code_url = 'https://open.weixin.qq.com/connect/qrconnect';
- /**
- * 获取Access token的api接口
- *
- * @var String
- */
- protected $access_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token';
- /**
- * 更新Access token的api接口
- *
- * @var String
- */
- protected $refresh_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token';
- /**
- * API根路径
- *
- * @var string
- */
- protected $api_base = 'https://api.weixin.qq.com/sns/';
- /**
- * 获取request_code的额外参数 URL查询字符串格式
- *
- * @var string
- */
- protected $authorize = 'snsapi_login';
- /**
- * WxQrcode constructor.
- *
- * @param array $config
- * @param array|null $token
- *
- * @throws \think\Exception
- */
- public function __construct(array $config = [], array $token = null) {
- $_config = $config;
- if (empty($config)) {
- if (file_exists(GLOBAL_CONF_PATH."extra/oauth/wxqrcode.php")) {
- $_config = include GLOBAL_CONF_PATH."extra/oauth/wxqrcode.php";
- } else {
- $_config = array();
- }
- }
- parent::__construct($_config, $token);
- }
- /**
- *
- * 第一步:请求CODE 跳转URL
- * 请求Authorize访问地址
- *
- * @param string $display
- *
- * @return string 跳转地址
- */
- public function getRequestCodeUrl($display = 'pc') {
- $params = array(
- 'appid' => $this->app_key,
- 'redirect_uri' => $this->callback,
- 'response_type' => $this->response_type,
- 'scope' => $this->authorize,
- 'state' => $this->getState(),
- );
- return $this->request_code_url.'?'.http_build_query($params).'#wechat_redirect';
- }
- /**
- * 第二步:通过code获取access_token
- *
- * @param $code
- * @param null $extend
- *
- * @return array|null
- * @throws Exception
- */
- public function getAccessToken($code, $extend = null) {
- $_params = array(
- 'appid' => $this->app_key,
- 'secret' => $this->app_secret,
- 'grant_type' => $this->grant_type,
- 'code' => $code,
- );
- $_rdata = Http::get($this->access_token_url, $_params);
- $this->token = $this->parseToken($_rdata);
- return $this->token;
- }
- /**
- * 刷新access_token有效期
- *
- * @param $refresh_token
- *
- * @return array|mixed|null
- * @throws Exception
- */
- public function getRefreshAccessToken($refresh_token) {
- $_params = array(
- 'appid' => $this->app_key,
- 'refresh_token' => $refresh_token,
- 'grant_type' => $this->grant_type,
- );
- $_rdata = Http::get($this->refresh_token_url, $_params);
- $this->token = $this->parseToken($_rdata);
- return $this->token;
- }
- /**
- * 第三步:通过access_token调用接口
- *
- * @param string $api API
- * @param string $param 调用API的额外参数
- * @param string $method HTTP请求方法 默认为GET
- * @param null $multi
- *
- * @return mixed
- * @throws Exception
- */
- public function call($api, $param = '', $method = 'GET', $multi = null) {
- $params = array(
- 'access_token' => $this->token['access_token'],
- 'openid' => $this->getOpenid(),
- 'lang' => 'zh_CN'
- );
- $data = Http::request($this->url($api), $params, $method);
- return json_decode($data, true);
- }
- /**
- * 获取授权用户的用户信息
- *
- * @return array
- * @throws \Exception
- * https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316518&lang=zh_CN
- *
- * openid 普通用户的标识,对当前开发者帐号唯一
- * nickname 普通用户昵称
- * sex 普通用户性别,1为男性,2为女性
- * province 普通用户个人资料填写的省份
- * city 普通用户个人资料填写的城市
- * country 国家,如中国为CN
- * headimgurl 用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空
- * privilege 用户特权信息,json数组,如微信沃卡用户为(chinaunicom)
- * unionid 用户统一标识。针对一个微信开放平台帐号下的应用,同一用户的unionid是唯一的。
- *
- */
- public function getUserInfo() {
- $_response = $this->call('userinfo');
- if (empty($_response) || (isset($_response['errcode']) && $_response['errcode'] != 0)) {
- throw new Exception('接口访问失败!'.$_response['errmsg']);
- } else {
- $_oauth_data = array(
- 'openid' => $_response['openid'],
- 'unionid' => isset($_response['unionid']) ? $_response['unionid'] : '',
- 'channel' => 'wxqrcode',
- 'nickname' => $_response['nickname'],
- 'gender' => $_response['sex'],
- 'avatar' => $_response['headimgurl'],
- 'token' => $this->token
- );
- return $_oauth_data;
- }
- }
- /**
- * 获取当前授权用户的SNS标识
- *
- * @throws Exception
- */
- public function getOpenid() {
- $_data = $this->token;
- if (isset($_data['openid'])) {
- return $_data['openid'];
- } else {
- throw new Exception('没有获取到微信用户ID!');
- }
- }
- /**
- * @param string $refresh_token
- *
- * @return array
- */
- protected function getRefreshParam($refresh_token = '') {
- $_params = array(
- 'appid' => $this->app_key,
- 'grant_type' => 'refresh_token',
- 'refresh_token' => $refresh_token,
- );
- return $_params;
- }
- /**
- * 解析access_token方法请求后的返回值
- *
- * @param $result
- * @param null $extend
- *
- * @return mixed
- * @throws Exception
- */
- protected function parseToken($result, $extend = null) {
- $_rdata = json_decode($result, true);
- if ($_rdata['access_token'] && $_rdata['expires_in']) {
- $this->token = $_rdata;
- $_rdata['openid'] = $this->getOpenid();
- return $_rdata;
- } else {
- throw new Exception("获取微信 ACCESS_token 出错:{$result}");
- }
- }
- }
|