User.php
13.5 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use EasyWeChat\Foundation\Application;
use fast\Random;
use think\Db;
use think\Request;
use think\Validate;
use function fast\e;
/*小程序Config*/
define('appid', 'wx7334e2c232b4ccee');
define('secret', '608b8b235d8f31c81f8df29518a7554c');
/**
* 用户接口
*/
class User extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = '*';
/**
* @ApiTitle (小程序授权)
* @ApiSummary (小程序授权)
* @ApiMethod (POST)
* @ApiRoute (/api/User/MiniAppLogin)
* @ApiParams (name="nickName", type="string", required=true, description="微信名")
* @ApiParams (name="avatarUrl", type="string", required=true, description="头像")
* @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":登录令牌,
"status":身份:0=用户,1=教练,2=超级教练员,
}
})
*/
public function MiniAppLogin()
{
$params = Request::instance()->post(false);
// $options = [
// // ...
// 'mini_program' => [
// 'app_id' => appid,
// 'secret' => secret,
// 'token' => 'component-token',
// 'aes_key' => 'component-aes-key'
// ],
// // ...
// ];
// $app = new Application($options);
// $miniProgram = $app->mini_program;
// $sessionKey = $miniProgram->sns->getSessionKey($params['code']);
// $Json = $miniProgram->encryptor->decryptData($sessionKey['session_key'], urldecode($params['iv']), urldecode($params['encryptData']));
$Json['openId'] = $this->OpenId($params['code']);
$Json['avatarUrl'] = $params['avatarUrl'];
$Json['nickName'] = $params['nickName'];
// dump($Json);
// die;
// if (!$Json['openId']) $this->error('授权失败,未获取用户令牌', 0);
$OpenInfo = Db::name('user')->where(['open_id' => $Json['openId']])->find();
$Token = $this->request->token();
if (empty($OpenInfo)) {
/*新用户*/
$IsInsert = Db::name('user')->insert(['open_id' => $Json['openId'], 'avatar' => $Json['avatarUrl'], 'nickname' => $Json['nickName'], 'CountMinutes' => 0, 'vip' => 0, 'exp_time' => 0, 'status' => 0, 'type' => 0, 'token' => $Token, 'createtime' => time(), 'updatetime' => time()]);
if (!$IsInsert) $this->error('注册失败', 0);
} else {
/*老用户*/
$IsUpdate = Db::name('user')->where('id', $OpenInfo['id'])->update(['token' => $Token, 'updatetime' => time()]);
if (!$IsUpdate) $this->error('登录失败', 0);
}
if (Db::name('user')->where('open_id', $Json['openId'])->value('type') == 1) $this->error('您已被拉黑', 0);
$this->success('成功', Db::name('user')->where('open_id', $Json['openId'])->field('token,status,base_id')->find());
}
/**
* @ApiTitle (Code换Token)
* @ApiSummary (Code换Token)
* @ApiMethod (POST)
* @ApiRoute (/api/User/OtherToken)
* @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":登录令牌,
}
})
*/
public function OtherToken()
{
$code = input('code');
$IsOpen = Db::name('user')->where(['open_id' => $this->OpenId($code)])->find();
$Token = $this->request->token();
if ($IsOpen) Db::name('user')->where('id', $IsOpen['id'])->update(['token' => $Token, 'updatetime' => time()]);
else $Token = '';
$this->success('成功', $Token);
}
/**
* @ApiTitle (个人信息)
* @ApiSummary (个人信息)
* @ApiMethod (POST)
* @ApiRoute (/api/User/UserInfo)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
"avatar": "https://thirdwx.qlogo.cn/mmopen/vi_32/dUsuTnYJVYOVz8aosicMWpO2KLTdjOTcW0baOaWstIyslUyXvFDe49GZ0skc4icIfNAMqHzDcxHePUgltkr12ibBQ/132",
"nickname": "王叨叨",
"CountMinutes": 0.02, //飞行时间
"vip": 2, //会员等级:0=*,1=普通会员,2=白金会员,3=终身会员
"exp_time": "2021-06-11 13:36:01", //会员到期时间
"status": 0, //身份:0=用户,1=教练,2=超级教练员
"days": 64, //剩余天数
"HaveMinutes": 3.33 //剩余小时数
}
})
*/
public function UserInfo()
{
$UserId = $this->IsToken();
$UserInfo = Db::name('user')->where('id', $UserId)->field('avatar,nickname,CountMinutes,vip,exp_time,status,mobile,card_num,learn_image,address,jingli_id')->find();
if (empty($UserInfo)) $this->error('用户不存在', 0);
$UserInfo['avatarUrl'] = $UserInfo['avatar'];
$UserInfo['avatar'] = $this->UserAvatar($UserInfo['avatar']);
$UserInfo['days'] = ($UserInfo['exp_time'] == 0) ? '*' : floor(($UserInfo['exp_time'] - time()) / 86400);
$UserInfo['exp_time'] = ($UserInfo['exp_time'] == 0) ? '*' : date('Y-m-d H:i:s', $UserInfo['exp_time']);
/*累计飞行时间*/
$UserInfo['CountMinutes'] = ($UserInfo['CountMinutes'] == 0) ? 0 : round($UserInfo['CountMinutes'] / 60, 2);
/*剩余飞行时间*/
$UserInfo['HaveMinutes'] = ($this->HaveMinutes($UserId) == 0) ? 0 : round($this->HaveMinutes($UserId) / 60, 2);
$UserInfo['VipContent'] = Db::name('vip')->where('id', $UserInfo['vip'])->value('content');
$UserInfo['LearnImageUrl'] = ($UserInfo['learn_image'] == null) ? '' : $UserInfo['learn_image'];
$UserInfo['learn_image'] = ($UserInfo['learn_image'] == null) ? '' : $this->UserAvatar($UserInfo['learn_image']);
$UserInfo['jingli_name'] = ($UserInfo['jingli_id'] == null) ? '' : Db::name('jingli')->where('id', $UserInfo['jingli_id'])->value('name');
$city = Db::name('address')->where('user_id', $UserId)->value('city');
$UserInfo['address'] = ($city == null) ? '' : $city;
$this->success('成功', $UserInfo);
}
/**
* @ApiTitle (更改个人信息)
* @ApiSummary (更改个人信息)
* @ApiMethod (POST)
* @ApiRoute (/api/User/UpdateUserInfo)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="avatar", type="string", required=true, description="头像")
* @ApiParams (name="nickname", type="string", required=true, description="昵称")
* @ApiParams (name="mobile", type="string", required=true, description="手机号")
* @ApiParams (name="card_num", type="string", required=true, description="身份证号")
* @ApiParams (name="learn_image", type="string", required=true, description="学历证书")
* @ApiParams (name="jingli_id", type="string", required=true, description="客户经理 空穿null")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
}
})
*/
public function UpdateUserInfo()
{
$UserId = $this->IsToken();
$Params = $this->request->param();
$data = [
'avatar' => $Params['avatar'],
'nickname' => $Params['nickname'],
'mobile' => $Params['mobile'],
'card_num' => $Params['card_num'],
'learn_image' => $Params['learn_image'],
'jingli_id' => $Params['jingli_id'],
];
$Res = Db::name('user')->where('id', $UserId)->update($data);
$this->Res($Res);
}
/**
* @ApiTitle (地址回显示)
* @ApiSummary (地址回显示)
* @ApiMethod (POST)
* @ApiRoute (/api/User/UserAddress)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
})
*/
public function UserAddress()
{
$UserId = $this->IsToken();
$this->success('成功', Db::name('address')->where('user_id', $UserId)->find());
}
/**
* @ApiTitle (地址更改)
* @ApiSummary (地址更改)
* @ApiMethod (POST)
* @ApiRoute (/api/User/UserUpdate)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="name", type="string", required=true, description="姓名")
* @ApiParams (name="mobile", type="string", required=true, description="手机号")
* @ApiParams (name="city", type="string", required=true, description="市区")
* @ApiParams (name="address", 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':'返回成功',
})
*/
public function UserUpdate()
{
$UserId = $this->IsToken();
$Params = $this->request->param();
$Info = Db::name('address')->where('user_id', $UserId)->find();
if (empty($Info)) Db::name('address')->insert(['user_id' => $UserId, 'name' => $Params['name'], 'mobile' => $Params['mobile'], 'city' => $Params['city'], 'address' => $Params['address']]);
else Db::name('address')->where('user_id', $UserId)->update(['name' => $Params['name'], 'mobile' => $Params['mobile'], 'city' => $Params['city'], 'address' => $Params['address'], 'updatetime' => time()]);
$this->success('成功', 1);
}
/**
* @ApiTitle (会员页面)
* @ApiSummary (会员页面)
* @ApiMethod (POST)
* @ApiRoute (/api/User/VipHtml)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
//会员返回
"data": {
"vip": 1, //会员等级:0=*,1=普通会员,2=白金会员,3=终身会员
"exp_time": "2021-04-10 14:07:46", //到期时间
"days": 2, //剩余天数
"content": "<p>普通会员普通会员</p>"
}
//非会员返回List数组
"data": {
"vip": 0, //会员等级:0=*,1=普通会员,2=白金会员,3=终身会员
"List": [
{
"id": 1,
"title": "普通会员",
"days": 365,
"content": "<p>普通会员普通会员</p>",
"price": "0.01"
},
{
"id": 2,
"title": "白金会员",
"days": 365,
"content": "<p>白金会员白金会员白金会员</p>",
"price": "0.01"
}
]
}
})
*/
public function VipHtml()
{
$UserId = $this->IsToken();
$VipInfo = Db::name('user')->where('id', $UserId)->field('vip,exp_time')->find();
if ($VipInfo['vip'] == 0)/*非会员*/ $data = ['vip' => $VipInfo['vip'], 'List' => Db::name('vip')->select()];
else/*会员*/ $data = ['vip' => $VipInfo['vip'], 'exp_time' => ($VipInfo['vip'] == 3) ? '*' : date('Y-m-d H:i:s', $VipInfo['exp_time']), 'days' => ($VipInfo['vip'] == 3) ? '*' : floor(($VipInfo['exp_time'] - time()) / 86400), 'content' => Db::name('vip')->where('id', $VipInfo['vip'])->value('content')];
$this->success('成功', $data);
}
/**
* @ApiTitle (客户经理列表)
* @ApiSummary (客户经理列表)
* @ApiMethod (POST)
* @ApiRoute (/api/User/JingLiList)
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
})
*/
public function JingLiList()
{
$this->success('成功', Db::name('jingli')->order('id desc')->select());
}
/**
* @ApiTitle (是否首次预约)
* @ApiSummary (是否首次预约)
* @ApiMethod (POST)
* @ApiRoute (/api/User/IsFirstYuyue)
* @ApiParams (name="id", type="string", required=true, description="预约单ID")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
'data':1=首次,0=非首次
})
*/
public function IsFirstYuyue()
{
$UserId = Db::name('yuyue')->where('id', input('id'))->value('user_id');
$CountMinutes = Db::name('user')->where('id', $UserId)->value('CountMinutes');
$this->success('成功', ($CountMinutes == 0) ? 1 : 0);
}
}