Skip to content

Commit a7ec6b2

Browse files
author
lishipeng
committed
微信开放平台扫码登录功能
1 parent ee87904 commit a7ec6b2

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

src/WxApp.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
namespace lspbupt\wechat;
3+
use \Yii;
4+
use yii\caching\Cache;
5+
use yii\di\Instance;
6+
class WxApp extends \lspbupt\curl\CurlHttp
7+
{
8+
public $appid = "";
9+
public $appsecret = "";
10+
public $host = "api.weixin.qq.com";
11+
public $protocol = "https";
12+
public $redirectUrl = "";
13+
public $cache = 'cache';
14+
const WX_CACHEKEY = "wxapp_cachekey";
15+
16+
public function init()
17+
{
18+
parent::init();
19+
$this->cache = Instance::ensure($this->cache, Cache::className());
20+
$this->beforeRequest = function($params, $curlhttp) {
21+
$action = $curlhttp->getAction();
22+
if($action != "/sns/oauth2/access_token") {
23+
$ch = clone $curlhttp;
24+
$token = $ch->getTokenFromCache();
25+
if(strpos($action, "?") != 0) {
26+
$curlhttp->setAction($action."&access_token=".$token);
27+
} else {
28+
$curlhttp->setAction($action."?access_token=".$token);
29+
}
30+
}
31+
return $params;
32+
};
33+
$this->afterRequest = function($output, $curlhttp) {
34+
$data = json_decode($output, true);
35+
if(empty($output) || empty($data)) {
36+
$data = [
37+
'errcode' => 1,
38+
'errmsg' => '网络错误!',
39+
];
40+
}
41+
if(empty($data['errcode'])) {
42+
$data = [
43+
'errcode' => 0,
44+
'errmsg' => '请求成功',
45+
'data' => $data,
46+
];
47+
}
48+
return $data;
49+
};
50+
}
51+
52+
public function getToken($code, $grantType="authorization_code")
53+
{
54+
$data = $this->setGet()->httpExec("/sns/oauth2/access_token", [
55+
'appid' => $this->appid,
56+
'secret' => $this->appsecret,
57+
'code' => $code,
58+
'grant_type' => $grantType,
59+
]);
60+
if($data['errcode']) {
61+
return $data;
62+
}
63+
$ts = time();
64+
$token = $data['data']['access_token'];
65+
$tempData = [
66+
'token' => $token,
67+
'expire' => $data['data']['expires_in'] + $ts - 100
68+
];
69+
Yii::$app->session->set(self::WX_CACHEKEY.$this->appid, $tempData);
70+
return $data;
71+
}
72+
73+
public function getTokenFromCache($code = "", &$data = [], $grantType = "authorization_code")
74+
{
75+
$tempData = Yii::$app->session->get(self::WX_CACHEKEY.$this->appid, "");
76+
$ts = time();
77+
if($tempData && $tempData['expire'] > $ts) {
78+
return $tempData['token'];
79+
}
80+
return "";
81+
}
82+
83+
public function getUserInfo($openid)
84+
{
85+
$params = ['openid' => $openid];
86+
return $this->setGet()->httpExec("/sns/userinfo", $params);
87+
}
88+
89+
}

src/assets/WxappLoginAsset.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace lspbupt\wechat\assets;
3+
4+
use yii\web\AssetBundle;
5+
class WxappLoginAsset extends AssetBundle
6+
{
7+
public $js = [
8+
'//res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js',
9+
];
10+
}

src/widgets/JsWxappLogin.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
namespace lspbupt\wechat\widgets;
3+
use \Yii;
4+
use yii\base\Widget;
5+
use yii\web\JsExpression;
6+
use yii\base\InvalidConfigException;
7+
use lspbupt\wechat\WxApp;
8+
use lspbupt\wechat\assets\WxappLoginAsset;
9+
class JsWxappLogin extends Widget
10+
{
11+
public $debug = false;
12+
public $wxApp = 'wxapp';
13+
public $id = 'login_container';
14+
public $scope = 'snsapi_login';
15+
public $redirect_uri = '';
16+
public $state = '';
17+
public $style = 'black';
18+
public $href = '';
19+
20+
public function init()
21+
{
22+
if (is_string($this->wxApp)) {
23+
$this->wxApp = Yii::$app->get($this->wxApp);
24+
} elseif (is_array($this->wxApp)) {
25+
if (!isset($this->wxApp['class'])) {
26+
$this->wxApp['class'] = WxApp::className();
27+
}
28+
$this->wxApp = Yii::createObject($this->wxApp);
29+
}
30+
if (!$this->wxApp instanceof WxApp) {
31+
throw new InvalidConfigException("微信配置错误");
32+
}
33+
}
34+
35+
public function run()
36+
{
37+
$view = $this->getView();
38+
WxappLoginAsset::register($view);
39+
$js = 'var obj = new WxLogin({
40+
id:"'.$this->id.'",
41+
appid: "'.$this->wxApp->appid.'",
42+
scope: "'.$this->scope.'",
43+
redirect_uri: "'.$this->redirect_uri.'",
44+
state: "'.$this->state.'",
45+
style: "'.$this->style.'",
46+
href: "'.$this->href.'"
47+
}); ';
48+
$view->registerJs($js);
49+
}
50+
}

0 commit comments

Comments
 (0)