WechatOpen.php
4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
* 微信开放平台
*/
class WechatOpen
{
const ACCESS_URL = 'https://open.weixin.qq.com/connect/qrconnect';
const API_URL = 'https://api.weixin.qq.com/sns/';
const WX_STATE = 'wx_state';
static public $CODE = array(
'11024' => '获取access_token失败',
'11025' => '微信错误信息',
'11028' => '获取用户微信信息失败',
);
/**
* 返回固定信息
* @param integer $errcode [description]
* @param string $err_msg [description]
* @param array $data [description]
* @return [type] [description]
*/
public function returnMsg($errcode = 0, $err_msg = '', $data = array())
{
if (empty($err_msg) && !empty(self::$CODE[$errcode])) {
$err_msg = self::$CODE[$errcode];
}
$res = array(
'err_code' => $errcode,
'err_msg' => $err_msg,
'data' => $data,
);
return $res;
}
public function __construct($options = array())
{
$this->appid = isset($options['appid']) ? $options['appid'] : C('WX_OAUTH.appid');
$this->appsecret = isset($options['appsecret']) ? $options['appsecret'] : C('WX_OAUTH.appsecret');
$this->callback = isset($options['callback']) ? $options['callback'] : C('WX_OAUTH.callback');
}
/**
* 获取授权二维码地址
* @param [type] $backUrl [description]
* @return [type] [description]
*/
public function qrConnect($backUrl, $act = '')
{
$state = hash_key(32);
session(self::WX_STATE, $state);
$param = array(
'appid' => $this->appid,
'redirect_uri' => $this->callback . '?backUrl=' . urlencode($backUrl),
'response_type' => 'code',
'scope' => 'snsapi_login',
'state' => $state,
);
!empty($act) && $param['redirect_uri'] .= '&act=' . urlencode($act);
$url = self::ACCESS_URL . '?' . http_build_query($param);
return $url;
}
/**
* 获取access_token
* @param [type] $code [description]
* @return [type] [description]
*/
public function getAccessToken($code)
{
// $info = session('access_info');
// if (empty($info) || $info['expires_time'] < $_SERVER['REQUEST_TIME']) {
$url = self::API_URL . 'oauth2/access_token';
$param = array(
'appid' => $this->appid,
'secret' => $this->appsecret,
'code' => $code,
'grant_type' => 'authorization_code',
);
$res = get_curl($url, $param);
$info = json_decode($res, true);
if (empty($info)) {
return $this->returnMsg(11024);
}
if ($info['errcode'] != 0) {
return $this->returnMsg($info['errcode'], '微信错误信息:'.$info['errmsg']);
}
$info['expires_time'] = $_SERVER['REQUEST_TIME'] + $info['expires_in'] - 30;//保险,减少30秒有效期
// session('access_info', $info);//缓存access_token
// }
// dump($info);
return $this->returnMsg(0, '', $info);
}
/**
* 验证csrf
* @param [type] $state [description]
* @return [type] [description]
*/
public function validState($state)
{
$valid = session(self::WX_STATE);
session(self::WX_STATE, null);
if (empty($state) || $valid != $state) {
return false;
}
return true;
}
/**
* 获取用户信息
* @param [type] $access_token [description]
* @param [type] $open_id [description]
* @return [type] [description]
*/
public function getUserInfo($access_token, $open_id)
{
$url = self::API_URL . 'userinfo';
$param = array(
'access_token' => $access_token,
'openid' => $open_id,
'lang' => 'zh-CN',
);
$res = get_curl($url, $param);
$info = json_decode($res, true);
if (empty($info)) {
return $this->returnMsg(11028);
}
if ($info['err_code'] != 0) {
return $this->returnMsg($info['errcode'], '微信错误信息:'.$info['errmsg']);
}
return $this->returnMsg(0, '', $info);
}
}