Skip to content

Commit

Permalink
[更新]增加网页微信登录支持
Browse files Browse the repository at this point in the history
  • Loading branch information
zoujingli committed May 11, 2018
1 parent 319d50d commit e623c23
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions WeOpen/Login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

// +----------------------------------------------------------------------
// | WeChatDeveloper
// +----------------------------------------------------------------------
// | 版权所有 2014~2018 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目:https://github.com/zoujingli/WeChatDeveloper
// +----------------------------------------------------------------------

namespace WeOpen;

use WeChat\Contracts\DataArray;
use WeChat\Contracts\Tools;
use WeChat\Exceptions\InvalidArgumentException;

/**
* 网站应用微信登录
* Class Login
* @package WeOpen
*/
class Login
{
/**
* 当前配置对象
* @var DataArray
*/
protected $config;

/**
* Login constructor.
* @param array $options
*/
public function __construct(array $options)
{
$this->config = new DataArray($options);
if (empty($options['appid'])) {
throw new InvalidArgumentException("Missing Config -- [appid]");
}
if (empty($options['appsecret'])) {
throw new InvalidArgumentException("Missing Config -- [appsecret]");
}
}

/**
* 第一步:请求CODE
* @param string $redirectUri 请使用urlEncode对链接进行处理
* @return string
*/
public function auth($redirectUri)
{
$appid = $this->config->get('appid');
$redirectUri = urlencode($redirectUri);
return "https://open.weixin.qq.com/connect/qrconnect?appid={$appid}&redirect_uri=${$redirectUri}&response_type=code&scope=snsapi_login&state={$appid}#wechat_redirect";
}

/**
* 第二步:通过code获取access_token
* @return mixed
*/
public function getAccessToken()
{
$appid = $this->config->get('appid');
$secret = $this->config->get('appsecret');
$code = isset($_GET['code']) ? $_GET['code'] : '';
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$secret}&code={$code}&grant_type=authorization_code";
return json_decode(Tools::get($url));
}

/**
* 刷新AccessToken有效期
* @param string $refreshToken
* @return array
*/
public function refreshToken($refreshToken)
{
$appid = $this->config->get('appid');
$url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={$appid}&grant_type=refresh_token&refresh_token={$refreshToken}";
return json_decode(Tools::get($url));
}

}

0 comments on commit e623c23

Please sign in to comment.