SignController.php
19.1 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<?php
namespace api\portal\controller;
use cmf\controller\RestBaseController;
use Think\Db;
use api\portal\service\ParamService;
/**
* @title 签到
* @description 签到相关接口
*/
class SignController extends RestBaseController
{
public function _initialize(){
if (empty(Db::name('user')->where('id',$this->getUserId())->find()['mobile'])){
$this->error(['code' => 10002, 'msg' => '用户未注册手机号']);
}
if (Db::name('user')->where('id',$this->getUserId())->find()['user_status']==0){
$this->error(['code' => 10003, 'msg' => '用户未填写注册信息']);
}
}
/**
* @title 首页接口
* @description 麦夫子首页渲染
* @author panhaowen
* @url /portal/Sign/index
* @method POST
* @header name:XX-Token require:1 default: desc:token
* @return is_week:是否应该周打卡(优先判断),0:不需要周打卡,1:需要周打卡
* @return sign:1是早打卡,2是午打卡,3是晚打卡,4是睡前打卡,0不能打卡
* @return slogan:口号
* @return start_time:显示时间(开始)
* @return end_time:显示时间(结束)
*/
public function index()
{
$user_id=$this->getUserId();
$re=Db::name('config')->where('id',1)->find();
$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
//判断是否需要进行周打卡
$res=Db::name('user_week_sign')->where(['user_id'=>$this->getUserId()])->order('create_time','desc')->find();
if (!$res){
$is_week=1;
}
else if ($res['create_time']<$beginToday-(7*24*60*60)){
$is_week=1;
}else{
$is_week=0;
}
$data['morning_sign_time']=date('H:i',$re['morning_sign_time']);
$data['morning_sign_end_time']=date('H:i',$re['morning_sign_end_time']);
$data['noon_sign_time']=date('H:i',$re['noon_sign_time']);
$data['noon_sign_end_time']=date('H:i',$re['noon_sign_end_time']);
$data['night_sign_time']=date('H:i',$re['night_sign_time']);
$data['night_sign_end_time']=date('H:i',$re['night_sign_end_time']);
$data['sleep_sign_time']=date('H:i',$re['sleep_sign_time']);
$data['sleep_sign_end_time']=date('H:i',$re['sleep_sign_end_time']);
$time=date('H:i:s',time());
$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
$user_sign=Db::name('user_sign')->where(['user_id'=>$user_id,'create_time'=>[ 'between time', [$beginToday,$endToday]]])->find();
if ($time>$data['morning_sign_time']&&$time<$data['morning_sign_end_time']){
if ($user_sign['morning']!=null) {
$data['sign'] = -1;
}else{
$data['sign'] = 1;
}
$data['start_time']=$data['morning_sign_time'];
$data['end_time']=$data['morning_sign_end_time'];
}else if ($time>$data['noon_sign_time']&&$time<$data['noon_sign_end_time']){
if ($user_sign['noon']!=null) {
$data['sign'] = -1;
}else{
$data['sign'] = 2;
}
$data['start_time']=$data['noon_sign_time'];
$data['end_time']=$data['noon_sign_end_time'];
}else if ($time>$data['night_sign_time']&&$time<$data['night_sign_end_time']){
if ($user_sign['night']!=null) {
$data['sign'] = -1;
}else{
$data['sign'] = 3;
}
$data['start_time']=$data['night_sign_time'];
$data['end_time']=$data['night_sign_end_time'];
}else if ($time>$data['sleep_sign_time']&&$time<$data['sleep_sign_end_time']){
if ($user_sign['sleep']!=null) {
$data['sign'] = -1;
}else{
$data['sign'] = 4;
}
$data['start_time']=$data['sleep_sign_time'];
$data['end_time']=$data['sleep_sign_end_time'];
}else{
if ($time<$data['morning_sign_time']){
$data['start_time']=$data['morning_sign_time'];
$data['end_time']=$data['morning_sign_end_time'];
}elseif ($time>$data['morning_sign_end_time']&&$time<$data['noon_sign_time']){
$data['start_time']=$data['noon_sign_time'];
$data['end_time']=$data['noon_sign_end_time'];
}elseif ($time>$data['noon_sign_end_time']&&$time<$data['night_sign_time']){
$data['start_time']=$data['night_sign_time'];
$data['end_time']=$data['night_sign_end_time'];
}else{
$data['start_time']=$data['sleep_sign_time'];
$data['end_time']=$data['sleep_sign_end_time'];
}
$data['sign']=0;
}
// $is_week=1;
$this->success('首页信息',array(
'is_week'=>$is_week,
'sign'=>$data['sign'],
'slogan'=>$re['slogan'],
'start_time'=>$data['start_time'],
'end_time'=>$data['end_time'],
));
}
/**
* @title 日打卡页面
* @description 麦夫子日打卡页面渲染
* @author panhaowen
* @url /portal/Sign/daySign
* @method POST
* @header name:XX-Token require:1 default: desc:token
* @return all:总打卡天数
* @return last:连续打卡天数
* @return longest:最长连续打卡天数
* @return morning:早打卡记录(null为未进行早打卡)
* @return noon:午打卡记录(null为未进行午打卡)
* @return night:晚打卡记录(null为未进行晚打卡)
* @return sleep:睡前打卡记录(null为未进行睡前打卡)
*/
public function daySign(){
// $param=$this->request->param();
// if ($param['status']==1){
// $type='morning';
// }elseif ($param['status']==2){
// $type='noon';
// }elseif ($param['status']==3){
// $type='night';
// }elseif ($param['status']==4){
// $type='sleep';
// }
//用户信息
$user=Db::name('user')->where('id',$this->getUserId())->find();
//当天签到记录
$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
$daySign=Db::name('user_sign')->where(['user_id'=>$user['id'],'create_time'=>[ 'between time', [$beginToday,$endToday]]])->find();
$daySign['morning']=empty($daySign['morning'])?null:date('Y/m/d H:i:s',$daySign['morning']);
$daySign['noon']=empty($daySign['noon'])?null:date('Y/m/d H:i:s',$daySign['noon']);
$daySign['night']=empty($daySign['night'])?null:date('Y/m/d H:i:s',$daySign['night']);
$daySign['sleep']=empty($daySign['sleep'])?null:date('Y/m/d H:i:s',$daySign['sleep']);
//返回
$this->success('日打卡信息',array(
'all'=>$user['all_sign_day'],
'last'=>$user['stay_sign_day'],
'longest'=>$user['longest_sign_day'],
'morning'=>$daySign['morning'],
'noon'=>$daySign['noon'],
'night'=>$daySign['night'],
'sleep'=>$daySign['sleep'],
));
}
/**
* @title 日打卡提交
* @description 麦夫子日打卡提交接口
* @author panhaowen
* @url /portal/Sign/daySignPost
* @method POST
* @header name:XX-Token require:1 default: desc:token
* @param name:status type:int require:1 other: desc:日打卡种类(1是早打卡,2是午打卡,3是晚打卡,4是睡前打卡)
* @return day_sign:是否需要进入每日一签页面(0:不需要,1:需要)
*/
public function daySignPost(){
$param=$this->request->param();
$user=Db::name('user')->where('id',$this->getUserId())->find();
//根据签到种类判断增加积分数
$score=Db::name('config')->where('id',1)->find();
$end_time=time();
if ($param['status']==1){
//早签到积分
$end_time=date('H:i',$score['morning_sign_end_time']);
$score=$score['morning_sign_score'];
$type='morning';
}elseif ($param['status']==2){
$end_time=date('H:i',$score['noon_sign_end_time']);
$score=$score['noon_sign_score'];
$type='noon';
}elseif ($param['status']==3){
$end_time=date('H:i',$score['night_sign_end_time']);
$score=$score['night_sign_score'];
$type='night';
}elseif ($param['status']==4){
$end_time=date('H:i',$score['sleep_sign_end_time']);
$score=$score['sleep_sign_score'];
$type='sleep';
}
if(date('H:i:s',time())>$end_time){
$this->error(['msg'=>'当前签到时间已过','code'=>40001]);
}
if(Db::name('user_sign')->where('user_id',$user['id'])->whereNotNull("$type")->find()){
$this->error(['msg'=>'当前时间段已签到','code'=>40002]);
}
//将签到写入记录
$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
$yesterday = strtotime(date("Y-m-d",strtotime("-1 day")));
//已有记录
$re=Db::name('user_sign')->where(['user_id'=>$user['id'],'create_time'=>[ 'between time', [$beginToday,$endToday]]])->find();
if ($re){
//是否需要进入每日一签页面
$day_sign=0;
//更新打卡
Db::name('user_sign')->where('id',$re['id'])->update(["$type"=>time()]);
//判断是否打满了4次
if (!empty($re['morning'])&&!empty($re['noon'])&&!empty($re['night'])&&!empty($re['sleep'])){
Db::name('user_sign')->where('id',$re['id'])->update(['status'=>1]);
Db::name('user')->where('id',$user['id'])->setInc('all_sign_day',1);
//判断是否为连续打卡
$last=Db::name('user_sign')->where(['user_id'=>$this->$user['id'],'status'=>1,'create_time'=>[ 'between time', [$yesterday,$beginToday]]])->find();
if ($user['stay_sign_day']==0||$last)
{
Db::name('user')->where('id',$user['id'])->setInc('stay_sign_day',1);
//判断最大连续打卡天数
if (($user['stay_sign_day']+1)>$user['longest_sign_day']){
Db::name('user')->where('id',$user['id'])->update(['longest_sign_day'=>($user['stay_sign_day']+1)]);
}
}else{
//非连续打卡
Db::name('user')->where('id',$user['id'])->update('stay_sign_day',0);
}
}
}
//无记录
else{
//是否需要进入每日一签页面
$day_sign=1;
Db::name('user_sign')->insert(['user_id'=>$this->getUserId(),'create_time'=>time(),"$type"=>time()]);
}
//记录打卡积分
Db::name('user')->where('id',$user['id'])->setInc('score',$score);
Db::name('user_score_log')->insert(['score'=>$score,'create_time'=>time(),'action'=>'打卡','user_id'=>$user['id'],'now_score'=>$user['score']+$score]);
//返回
$this->success('日打卡提交结果',array(
'day_sign'=>$day_sign,
));
}
/**
* @title 每日一签页面
* @description 每日一签页面渲染
* @author panhaowen
* @url /portal/Sign/day_sign
* @method POST
* @header name:XX-Token require:1 default: desc:token
* @param name:status type:int require:1 other: desc:日打卡种类(1是早打卡,2是午打卡,3是晚打卡,4是睡前打卡)
* @return background:背景图片
* @return music:背景音乐
* @return music_name:背景音乐名称
* @return score:签到积分
* @return title:签到显示内容
*/
public function day_sign()
{
$param=$this->request->param();
$score=Db::name('config')->where('id',1)->find();
if ($param['status']==1){
//早签到积分
$score=$score['morning_sign_score'];
}elseif ($param['status']==2){
$score=$score['noon_sign_score'];
}elseif ($param['status']==3){
$score=$score['night_sign_score'];
}elseif ($param['status']==4){
$score=$score['sleep_sign_score'];
}
$date=date("w",time()); //这个可以获取数字星期比如123,注意0是星期日
$config=Db::name('sign_image')->where('date',$date)->find();
if (!empty($config['music'])) {
$music = json_decode($config['music'], true);
$music = $music[rand(0, count($music, 0) - 1)];
}
$this->success('渲染数据',array(
'background'=>cmf_get_image_preview_url($config['image']),
'music'=>cmf_get_file_download_url($music['url']),
'music_name'=>$music['name'],
'score'=>$score,
'title'=>$config['title']
));
}
/**
* @title 分享接口
* @description 分享接口结果返回
* @author panhaowen
* @url /portal/Sign/share
* @method POST
* @header name:XX-Token require:1 default: desc:token
*/
public function share()
{
//用户信息
$user= Db::name('user')->where('id',$this->getUserId())->find();
$config = Db::name('config')->where('id', 1)->find();
$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
$re=Db::name('user_score_log')->where(['user_id'=>$user['id'],'action'=>'分享','create_time'=>[ 'between time', [$beginToday,$endToday]]])->find();
if ($re){
$this->error('您今天已经分享过了');
}
Db::name('user')->where('id',$user['id'])->setInc('score',$config['share_score']);
Db::name('user_score_log')->insert(['user_id'=>$user['id'],'score'=>$config['share_score'],'now_score'=>($user['score']+$config['share_score'])]);
$this->success('分享成功');
}
/**
* @title 周打卡页面
* @description 麦夫子周打卡页面页面渲染
* @author panhaowen
* @url /portal/Sign/weekSign
* @method POST
* @header name:XX-Token require:1 default: desc:token
* @return param:各个参数
* @return record:周打卡记录
*/
public function weekSign(){
//用户信息
$user=Db::name('user')->where('id',$this->getUserId())->find();
//判断是否是第一次周打卡
$re=Db::name('user_week_sign')->where('user_id',$user['id'])->order('create_time','desc')->find();
$paramService=new ParamService();
if (!$re){
$param['weight']=$user['now_weight'];
// $param['status']=$paramService->status($user['before_weight'],$user['height']);
$param['increase']=$user['now_weight']-$user['before_weight'];
$param['show_weight']=$user['now_weight'];
$param['overplus']=$paramService->overplus($user['before_weight'],$user['height'])-($user['now_weight']-$user['before_weight']);
$record=[0=>['day'=>ceil((time()-$user['register_time'])/(60*60*24)),'weight'=> $param['weight'],'status'=>$paramService->status($user['before_weight'],$user['height'])]];
}else{
$param['weight']=$re['weight'];
$param['increase']=$re['weight']-$user['before_weight'];
$param['overplus']=$paramService->overplus($user['before_weight'],$user['height'])-($re['weight']-$user['before_weight']);
$record=Db::name('user_week_sign')->alias('uw')->join('user u','uw.user_id=u.id')->where('uw.user_id',$user['id'])->select()->each(function ($item) {
if ($item['status']==1){
$item['status']='消瘦';
}elseif ($item['status']==2){
$item['status']='正常';
}elseif ($item['status']==3){
$item['status']='肥胖';
}
elseif ($item['status']==4){
$item['status']='超重';
}
$item['day']=ceil((time()-$item['register_time'])/(60*60*24));
return $item;
});
}
$param['bashBoard']=[$paramService->overplus($user['before_weight'],$user['height'])*0.2+$user['before_weight'],$paramService->overplus($user['before_weight'],$user['height'])*0.4+$user['before_weight'],$paramService->overplus($user['before_weight'],$user['height'])*0.6+$user['before_weight'],$paramService->overplus($user['before_weight'],$user['height'])*0.8+$user['before_weight']];
$param['day']=ceil((time()-$user['register_time'])/(60*60*24));
$param['pregnant_week']=$user['pregnant_week'];
$param['angle']=180*(($user['now_weight']-$user['before_weight'])/$paramService->overplus($user['before_weight'],$user['height']));
//返回
$this->success('周打卡信息',array(
'param'=>$param,
'record'=>$record
));
}
/**
* @title 周打卡提交
* @description 周打卡提交接口
* @author panhaowen
* @url /portal/Sign/weekSignPost
* @method POST
* @header name:XX-Token require:1 default: desc:token
* @param name:weight require:1
* @return param:各个参数
* @return record:周打卡记录
*/
public function weekSignPost(){
$param=$this->request->param();
$user=Db::name('user')->where('id',$this->getUserId())->find();
Db::name('user')->where('id',$user['id'])->update(['now_weight'=>$param['weight']]);
$paramService=new ParamService();
$param['status']=$paramService->week_status($param['weight'],$user['height']);
Db::name('user_week_sign')->insert(['user_id'=>$user['id'],'create_time'=>time(),'weight'=>$param['weight'],'status'=>$param['status']]);
//返回
$this->success('周打卡提交成功');
}
/**
* @title 我的体重数据
* @description 我的体重数据接口
* @author panhaowen
* @url /portal/Sign/myWeight
* @method POST
* @header name:XX-Token require:1 default: desc:token
* @return param:各个参数
* @return record:周打卡记录
*/
public function myWeight(){
$user=Db::name('user')->where('id',$this->getUserId())->find();
$paramService=new ParamService();
$nengliang=$paramService->nengliang($user['before_weight'],$user['height'],$user['weight_level'],$user['pregnant_week'],$user['register_time']);
$param['day_energy']=ceil($nengliang);
$param['protein']=ceil($nengliang*0.2/4);
$param['fat']=ceil($nengliang*0.2/9);
$param['water']=ceil($nengliang*0.6/4);
$data=Db::name('user_week_sign')->where('user_id',$this->getUserId())->select()->toArray();
foreach ($data as $k=>$v){
$data[$k]['week']=ceil(($data[$k]['create_time'] - $user['register_time'])/(7*24*60*60));
$data[$k]['week_time']=date('m-d',strtotime('+'.$data[$k]['week'].' week',$data[$k]['create_time']));
}
//返回
$this->success('我的体重数据',[$param,$data]);
}
}