User.php
6.2 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
namespace app\api\controller;
use think\Db;
use app\common\controller\Api;
/**
* 用户接口
*/
class User extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = '*';
/**
* @ApiTitle (用户端-授权登录)
* @ApiSummary (授权登录)
* @ApiMethod (POST)
* @ApiRoute (/api/User/Login)
* @ApiParams (name="code", type="string", 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": "a32070524e6dd73c0f6a29b7993303e8",
"type":1=已绑定,0未绑定,
'level':类型:1=公司分管领导,2=公司机电负责人,3=维修负责人,4=维修成员,5=报修负责人,6=报修成员
}
})
*/
public function Login()
{
$param = $this->request->param();
//授权登录
$ch = curl_init();
$appid = "wx2adc803c6e8fd596";
$secret = "925976a669063b7d26fd6527a1ed5197";
$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);
$token = $this->request->token();
$istype = Db::name('user')->where(['openid' => $curl_result['openid']])->find();
if (!empty($istype)) {
Db::name('user')->where(['openid' => $curl_result['openid']])->update(['token' => $token]);
$type = 1;
$is_token = $token;
$level = $istype['level'];
} else {
$type = 0;
$is_token = '';
$level = '';
}
$return = [
'token' => $is_token,
'type' => $type,
'level' => $level
];
$this->success('成功', $return);
}
/**
* @ApiTitle (用户端-账号登陆)
* @ApiSummary (账号登陆)
* @ApiMethod (POST)
* @ApiRoute (/api/User/LoginSign)
* @ApiParams (name="username", type="string", required=true, description="账号")
* @ApiParams (name="password", 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": "bf3e78fb461790218d62095d48270fbd",
'level':类型:1=公司分管领导,2=公司机电负责人,3=维修负责人,4=维修成员,5=报修负责人,6=报修成员,
"type":1=已绑定,0未绑定,
}
})
*/
public function LoginSign()
{
$param = $this->request->param();
if (empty($param['username']) || $param['username'] == null || $param['username'] == '' || $param['username'] == "") {
$this->error('账号不能为空', 0);
}
if (empty($param['password']) || $param['password'] == null || $param['password'] == '' || $param['password'] == "") {
$this->error('密码不能为空', 0);
}
$password = Db::name('user')->where(['username' => $param['username']])->find();
if (empty($password)) {
$this->error('帐号不存在', 0);
}
if (!($param['password'] == $password['password'])) {
$this->error('密码错误', 0);
}
$token = $this->request->token();
$res = Db::name('user')->where(['username' => $param['username']])->where(['password' => $param['password']])
->update(['token' => $token]);
if (empty($password['openid'])) {
$type = 0;
} else {
$type = 1;
}
$return = [
'token' => $token,
'level' => $password['level'],
'type' => $type,
];
if ($res) {
$this->success('登陆成功', $return);
} else {
$this->error('登陆失败', 0);
}
}
/**
* @ApiTitle (用户端-绑定微信)
* @ApiSummary (绑定微信)
* @ApiMethod (POST)
* @ApiRoute (/api/User/SDKWechat)
* @ApiHeaders (name="authorization", type=string, required=true, description="请求的Token")
* @ApiParams (name="code", type="string", 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": {
}
})
*/
public function SDKWechat()
{
$user_id = $this->is_token($this->request->header());
$ch = curl_init();
$appid = "wx2adc803c6e8fd596";
$secret = "925976a669063b7d26fd6527a1ed5197";
$code = input('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);
dump($curl_result);
die;
$res = Db::name('user')->where(['id' => $user_id])->update(['openid' => $curl_result['openid']]);
if ($res) {
$this->success('绑定成功', 1);
} else {
$this->error('绑定成功', 0);
}
}
}