MiniProgram.php
1.79 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
<?php
/**
* 微信小程序
*/
class MiniProgram
{
const API_URL = 'https://api.weixin.qq.com/sns/jscode2session';
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('MINI_PROGRAM_CONFIG.appid');
$this->appsecret = isset($options['appsecret']) ? $options['appsecret'] : C('MINI_PROGRAM_CONFIG.appsecret');
}
/**
* 获取sessionkey(返回session_key,openid,unionid)
* @param [type] $code [description]
* @return [type] [description]
*/
public function getSessionKey($code)
{
$url = self::API_URL;
$param = array(
'appid' => $this->appid,
'secret' => $this->appsecret,
'js_code' => $code,
'grant_type' => 'authorization_code',
);
$res = get_curl($url, $param);
$info = json_decode($res, true);
if (empty($info)) {
return $this->returnMsg(11024);
}
return $this->returnMsg(0, '', $info);
}
}