User.php
4.9 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
namespace app\api\controller;
use think\Db;
use app\common\controller\Api;
/**
* 用户接口
*/
class User extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = '*';
public function _initialize()
{
parent::_initialize();
}
/**
* 用户接口
* @ApiTitle (用户登陆)
* @ApiSummary (用户登陆)
* @ApiMethod (POST)
* @ApiRoute (/api/User/UserLogin)
* @ApiParams (name="code", type="integer", required=true, description="Code")
* @ApiParams (name="nickname", type="string", required=true, description="微信名")
* @ApiParams (name="avatar", type="string", required=true, description="头像")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
'data':{
'token' => token,
})
*/
public function UserLogin()
{
$param = $this->request->param();
// 授权登录
$ch = curl_init();
$appid = "wx6a9080f20326f817";
$secret = "8fe9780e13dd1fa64b886c4f716cd366";
$code = $param['code'];
$url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
if ($output === FALSE) {
echo "CURL Error:" . curl_error($ch);
}
curl_close($ch);
$curl_result = json_decode($output, true);
$openid = $curl_result['openid'];
$is_open = Db::name('user')->where(['openid' => $openid])->find();
if (empty($is_open)) {
$data = [
'openid' => $openid,
'updatetime' => time(),
'createtime' => time(),
'avatar' => $param['avatar'],
'nickname' => $param['nickname'],
];
Db::name('user')->insert($data);
}
$token = $this->request->token();
$arr = [
'nickname' => $param['nickname'],
'avatar' => $param['avatar'],
'token' => $token,
'updatetime' => time(),
];
$res = Db::name("user")->where(['openid' => $openid])->update($arr);
if (!$res) {
$this->error('授权失败', 0);
die;
}
$rult = Db::name("user")->where(['openid' => $openid])->find();
$return = [
'token' => $rult['token'],
'avatar' => $param['avatar'],
'nickname' => $param['nickname']
];
$this->success('成功', $return);
}
/**
* 用户接口
* @ApiTitle (Code换token)
* @ApiSummary (Code换token)
* @ApiMethod (POST)
* @ApiRoute (/api/User/UserCode)
* @ApiParams (name="code", type="integer", required=true, description="Code")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
'data':{
'token' => token,
})
*/
public function UserCode()
{
$param = $this->request->param();
// 授权登录
$ch = curl_init();
$appid = "wx6a9080f20326f817";
$secret = "8fe9780e13dd1fa64b886c4f716cd366";
$code = $param['code'];
$url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
if ($output === FALSE) {
echo "CURL Error:" . curl_error($ch);
}
curl_close($ch);
$curl_result = json_decode($output, true);
$openid = $curl_result['openid'];
$is_open = Db::name('user')->where(['openid' => $openid])->find();
if (empty($is_open)) {
$this->error('请先注册授权', '', 99991);
} else {
$token = $this->request->token();
$arr = [
'token' => $token,
'updatetime' => time(),
];
$res = Db::name("user")->where(['openid' => $openid])->update($arr);
if (!$res) {
$this->error('Token更新失败', 0);
die;
}
$this->success('成功', $token);
}
}
}