PublicController.php
8.7 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
namespace api\wxapp\controller;
use think\Db;
use cmf\controller\RestBaseController;
use wxapp\aes\WXBizDataCrypt;
use think\Validate;
/**
* @title 公共模块
* @description 公共模块
* @package api\wxapp\controller
*/
class PublicController extends RestBaseController
{
/**
* @title 获取sessionKey和openid
* @description 小程序登录注册
* @author Tiger Yang
* @url /wxapp/public/getSessionKey
* @method POST
*
* @param name:code type:string require:1 other: desc:code
*
* @return session_key:session_key
* @return openid:openid
*/
public function getSessionKey(){
$validate = new Validate([
'code' => 'require',
]);
$validate->message([
'code.require' => '缺少参数code!',
]);
$data = $this->request->param();
if (!$validate->check($data)) {
$this->error(['code'=>'40003','msg'=>$validate->getError()]);
}
$code = $data['code'];
$appId = config('app_id');
$appSecret = config('app_secret');
$response = cmf_curl_get("https://api.weixin.qq.com/sns/jscode2session?appid=$appId&secret=$appSecret&js_code=$code&grant_type=authorization_code");
$response = json_decode($response, true);
if (!empty($response['errcode'])) {
$this->error(['code'=>'41001','msg'=>'操作失败:'.$response['errcode']]);
}
$this->success('获取成功',$response);
}
/**
* @title 小程序登录注册
* @description 小程序登录注册
* @author Tiger Yang
* @url /wxapp/public/login
* @method POST
*
* @param name:openid type:string require:1 other: desc:openid
* @param name:session_key type:string require:1 other: desc:session_key
* @param name:encrypted_data type:string require:1 other: desc:encrypted_data
* @param name:iv type:string require:1 other: desc:iv
*
* @return token:登录唯一标识
*/
public function login()
{
$validate = new Validate([
'openid' => 'require',
'session_key' => 'require',
'encrypted_data' => 'require',
'iv' => 'require',
]);
$validate->message([
'openid.require' => '缺少参数openid!',
'session_key.require' => '缺少参数session_key!',
'encrypted_data.require' => '缺少参数encrypted_data!',
'iv.require' => '缺少参数iv!',
]);
$data = $this->request->param();
if (!$validate->check($data)) {
$this->error(['code'=>'40003','msg'=>$validate->getError()]);
}
$appId = config('app_id');
$openid = $data['openid'];
$sessionKey = $data['session_key'];
$pc = new WXBizDataCrypt($appId, $sessionKey);
$errCode = $pc->decryptData($data['encrypted_data'], $data['iv'], $wxUserData);
if ($errCode != 0) {
$this->error(['code'=>'41002','msg'=>'检验数据失败!'],['errCode'=>$errCode,'param'=>$data]);
}
$findThirdPartyUser = Db::name("third_party_user")
->where('openid', $openid)
->where('app_id', $appId)
->find();
$currentTime = time();
$ip = $this->request->ip(0, true);
$wxUserData['sessionKey'] = $sessionKey;
unset($wxUserData['watermark']);
if ($findThirdPartyUser) {
$token = cmf_generate_user_token($findThirdPartyUser['user_id'], 'wxapp');
$userData = [
'last_login_ip' => $ip,
'last_login_time' => $currentTime,
'login_times' => ['exp', 'login_times+1'],
'more' => json_encode($wxUserData)
];
if (isset($wxUserData['unionId'])) {
$userData['union_id'] = $wxUserData['unionId'];
}
Db::name("third_party_user")
->where('openid', $openid)
->where('app_id', $appId)
->update($userData);
$this->success("登录成功!", ['token' => $token]);
} else {
Db::startTrans();
$userId = Db::name("user")->insertGetId([
'create_time' => $currentTime,
'user_status' => 1,
'user_type' => 2,
'sex' => $wxUserData['gender'],
'user_nickname' => $wxUserData['nickName'],
'avatar' => $wxUserData['avatarUrl'],
'last_login_ip' => $ip,
'last_login_time' => $currentTime
]);
$row=Db::name("third_party_user")->insert([
'openid' => $openid,
'user_id' => $userId,
'third_party' => 'wxapp',
'app_id' => $appId,
'last_login_ip' => $ip,
'union_id' => isset($wxUserData['unionId']) ? $wxUserData['unionId'] : '',
'last_login_time' => $currentTime,
'create_time' => $currentTime,
'login_times' => 1,
'status' => 1,
'more' => json_encode($wxUserData)
]);
if($userId && $row){
Db::commit();
$token = cmf_generate_user_token($userId, 'wxapp');
$this->success("登录成功!", ['token' => $token]);
}else{
Db::rollback();
$this->error(['code'=>'40004','msg'=>'登录失败']);
}
}
}
/**
* @title 获取token
* @description 获取token
* @author Tiger Yang
* @url /wxapp/public/get_token
* @method POST
*
* @param name:openid type:string require:1 other: desc:openid
*
* @return token:登录唯一标识
*/
public function get_token(){
$openid = $this->request->param('openid');
$third_party_user = Db::name('third_party_user')->where(array('openid'=>$openid))->find();
if(empty($third_party_user)){
/*没有相关用户 添加用户到数据库 分配token*/
$ip = $this->request->ip(0, true);
$currentTime=time();
Db::startTrans();
$appId = config('app_id');
$userId = Db::name("user")->insertGetId([
'create_time' => $currentTime,
'user_status' => 1,
'user_type' => 2,
'sex' => 0,
'user_nickname' => '用户',
'avatar' => '',
'last_login_ip' => $ip,
'last_login_time' => $currentTime
]);
$row=Db::name("third_party_user")->insert([
'openid' => $openid,
'user_id' => $userId,
'third_party' => 'wxapp',
'app_id' => $appId,
'last_login_ip' => $ip,
'union_id' => '',
'last_login_time' => $currentTime,
'create_time' => $currentTime,
'login_times' => 1,
'status' => 1
]);
if($userId && $row){
Db::commit();
$token = cmf_generate_user_token($userId, 'wxapp');
$this->success("登录成功!", ['token' => $token]);
}else{
Db::rollback();
$this->error(['code'=>'40004','msg'=>'登录失败']);
}
// $this->success('查无此人!',['code'=>20000,'msg'=>'查无此人!']);
}
$data = Db::name('user_token')->where(array('user_id'=>$third_party_user['user_id']))->find();
if($data['expire_time']<time()){
$token = cmf_generate_user_token($third_party_user['user_id'], 'wxapp');
$this->success('ok',['code'=>20000,'msg'=>'ok','token'=>$token]);
}else{
$this->success('ok',['code'=>20000,'msg'=>'ok','token'=>$data['token']]);
}
}
}