Index.php
21.8 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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
<?php
namespace app\api\controller;
use app\api\model\Idea;
use app\api\model\IdeaComment;
use app\api\model\IdeaType;
use app\common\controller\Api;
use fast\Http;
use think\Config;
use think\Db;
/**
* 首页接口
*/
class Index extends Api
{
protected $noNeedLogin = ['searchTypeList','feelList'];
protected $noNeedRight = ['*'];
/**
* @ApiTitle (首页信息)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiReturn ({
'code':'1',
'msg':'首页信息'
* today_times:用户今日体验所剩次数
* my_idea_number:用户体验总次数
* idea_total:点子总数
})
*/
public function index()
{
$times = Db::name('user_everyday_times')->where('user_id',$this->auth->id)->where('time',strtotime(date('Y-m-d')))->value('times');
$times = $times ? Config::get('site.everyday_times')-$times : 0;
$count = Db::name('idea')->count();
$this->success('首页信息',['today_times'=>(int)$times,'my_idea_number'=>$this->auth->idea_number,'idea_total'=>$count]);
}
/**
* @ApiTitle (搜索分类列表)
* @ApiMethod (POST)
* @ApiReturn ({
'code':'1',
'msg':'搜索分类列表'
* weather:天气列表
* id:天气id
* name:天气名称
* city:城市列表
* id:城市列表
* name:城市列表
* type:点子分类列表
* id:分类id
* name:分类名称
})
*/
public function searchTypeList()
{
$weather = Db::name('idea_weather')->select();
$city = Db::name('idea_area')->select();
$type = Db::name('idea_type')->field('id,name')->select();
$this->success('搜索分类列表',['weather'=>$weather,'city'=>$city,'type'=>$type]);
}
/**
* @ApiTitle (搜索)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name=text, type=int, required=false, description="关键词")
* @ApiParams (name=city, type=int, required=false, description="城市id")
* @ApiParams (name=weather, type=int, required=false, description="天气id")
* @ApiParams (name=pepole_number, type=int, required=false, description="参与人数")
* @ApiParams (name=time, type=int, required=false, description="体验时长")
* @ApiParams (name=type, type=int, required=false, description="分类id")
* @ApiReturn ({
'code':'1',
'msg':'搜索分类列表'
"data": {
"list": {
"total": 1,
"per_page": 10,
"current_page": 1,
"last_page": 1,
"data": [
{
"id": 1,
"title": "测试",
"collection_status": 0, 收藏状态 0未收藏
"experience_status": 1 体验状态 0未体验
}
]
}
}
})
*/
public function searchCondition()
{
$city = $this->request->post('city');
$weather = $this->request->post('weather');
$pepole = $this->request->post('pepole_number');
$time = $this->request->post('time');
$type = $this->request->post('type');
$text = $this->request->post('text');
$where = [];
if (is_numeric($city)) $where['idea_area_id'] = $city;
if (is_numeric($weather)) $where['idea_weather_id'] = $weather;
if (is_numeric($pepole)) $where['all_number'] = $pepole;
if (is_numeric($time)) $where['duration'] = $time;
if (is_numeric($type)) $where['idea_type_id'] = $type;
if ($text) $where['key_word'] = $text;
$ideaModel = new Idea();
$list = $ideaModel
->where($where)
->field('id,title')
->order('id','desc')
->limit(20)
->select();
$list = $list->toArray();
foreach ($list as $key=>$value){
$db = Db::name('user_idea_record')->where('user_id',$this->auth->id)->where('idea_id',$value['id'])->where('type',2)->value('id');
$list[$key]['collection_status'] = $db ? 1:0;
$db = Db::name('user_idea_record')->where('user_id',$this->auth->id)->where('idea_id',$value['id'])->where('type',1)->value('id');
$list[$key]['experience_status'] = $db ? 1:0;
}
$this->success('条件搜索列表',['list'=>$list]);
}
/**
* @ApiTitle (点子详情)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name=id, type=int, required=true, description="点子id")
* @ApiReturn ({
'code':'1',
'msg':'点子详情'
"detail": {
"id": 1,
"title": "测试",
"duration": 10,时长
"mid_feel": "高兴",体验中位数
"all_number": 0,体验人数
"collection": 0,收藏人数
"idea_type": {
"name": "舒适圈",分类
"image_text": "http://mane.comasdasd"点子图片
},
"avatar_text": [
"http://mane.comasdasda",体验者头像
"http://mane.comasdasds"
],
"collection_status": 0,收藏状态
"experience_status": 1 体验状态
}
})
*/
public function ideaDetail()
{
$id = $this->request->post('id');
if (!is_numeric($id)) $this->error('id参数不合法');
$ideaModel = new Idea();
$detail = $ideaModel
->where('id',$id)
->find();
if (!$detail) $this->error('点子不存在');
//统计用户每日点子浏览次数
$time = strtotime(date('Y-m-d'));
$times = Db::name('user_everyday_times')
->where('user_id',$this->auth->id)
->where('time',$time)
->find();
if ($times){
if ($times['times'] >= Config::get('site.everyday_times')) $this->error('用户每日体验次数已达上限');
Db::name('user_everyday_times')->where('id',$times['id'])->setInc('times');
}else{
Db::name('user_everyday_times')->insertGetId(['user_id'=>$this->auth->id,'time'=>$time,'times'=>1]);
}
$detail->visible(['title','id','ideaType','all_number','collection','idea_type_id','duration','mid_feel']);
$detail = $detail->toArray();
//查找点子分类名称
$ideatype = explode(',',$detail['idea_type_id']);
foreach ($ideatype as $key => $value){
$detail['idea_type'][] = IdeaType::where('id',$value)->find()->visible(['name']);
}
//收藏状态
$db = Db::name('user_idea_record')->where('user_id',$this->auth->id)->where('idea_id',$id)->where('type',2)->value('id');
$detail['collection_status'] = $db ? 1:0;
//体验状态
$db = Db::name('user_idea_record')->where('user_id',$this->auth->id)->where('idea_id',$id)->where('type',1)->value('id');
$detail['experience_status'] = $db ? 1:0;
//体验过的用户头像
$db = Db::name('user_idea_record')->where('idea_id',$id)->where('type',1)->limit(50)->column('user_id');
$user = Db::name('user')->whereIn('id',$db)->field('id,avatar')->select()->toArray();
foreach ($user as $key => $value){
if ($value['id'] == $this->auth->id){
$user[$key]['my_status'] = 1;
}else{
$user[$key]['my_status'] = 0;
}
}
$detail['user_avatars'] = $user;
$this->success('点子详情',['detail'=>$detail]);
}
/**
* @ApiTitle (点子一级评论列表)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name=id, type=int, required=true, description="点子id")
* @ApiParams (name=num, type=int, required=false, description="页数 默认第一页")
* @ApiReturn ({
'code':'1',
'msg':'点子评论列表'
"list": {
"total": 2,
"per_page": 10,
"current_page": 1,
"last_page": 1,
"data": [
{
"id": 1,一级评论id
"pid": 0, 父级id
"user_id": 1,发布评论用户id
"touser_id": 0,被回复用户id
"comment": "nihao", 评论内容
"like_number": 0, 点赞数
"comment_number": 0, 评论数
"area": "天津市", 城市
"idea_name": "剑来", 点子名称
"user": {
"id": 1,发布评论用户id
"nickname": "admin",昵称
"avatar_text": ""头像
},
"like_nicknames_text": [
"葛二蛋" 点赞用户昵称
],
"createtime_text": "2021-06-02",发布时间
"images_text": [
"http://mane.comqweddd" 评论图片
],
},
]
},
"num": 1 一级评论页数
})
*/
public function ideaFirstComment()
{
$id = $this->request->post('id');
$num = $this->request->post('num');
$num = $num ? $num : 1;
if (!is_numeric($id)) $this->error('id参数不合法');
$ideaCommentModel = new IdeaComment();
$list = $ideaCommentModel
->with(['user'])
->where('idea_id',$id)
->where('pid',0)
->order('id','desc')
->paginate(5,false,['page'=>$num])
->each(function ($item,$key){
$item->getRelation('user')->visible(['nickname','id']);
$item->hidden(['createtime','images','like_nicknames','idea_id']);
$item['like_status'] = Db::name('idea_comment_user_like')
->where('user_id',$this->auth->id)
->where('idea_comment_id',$item['id'])
->value('id') ? 1:0;
$item['like_nicknames_text'] = Db::name('idea_comment_user_like')
->alias('i')
->join('fa_user u','i.user_id=u.id')
->where('idea_comment_id',$item['id'])
->limit(7)
->column('u.nickname');
});
$list = $list->toArray();
$this->success('点子评论列表',['list'=>$list,'num'=>$num]);
}
/**
* @ApiTitle (点子二级评论列表)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name=id, type=int, required=true, description="一级评论id")
* @ApiParams (name=num, type=int, required=false, description="页数 默认第一页")
* @ApiReturn ({
'code':'1',
'msg':'点子二级评论列表'
"list": {
"total": 2,
"per_page": 10,
"current_page": 1,
"last_page": 1,
"data": [ 二级评论
{
"id": 2,二级评论id
"pid": 1,父级id
"user_id": 1,发布评论用户id
"touser_id": 1,被回复用户id
"comment": "lkajskldjsal",评论内容
"user": {
"id": 1,发布评论用户id
"nickname": "admin"昵称
},
"touser": {
"id": 1,被回复用户id
"nickname": "admin"昵称
}
}
]
},
"num": 1 二级评论页数
})
*/
public function ideaSecondComment()
{
$id = $this->request->post('id');
$num = $this->request->post('num');
$num = $num ? $num : 1;
if (!is_numeric($id)) $this->error('id参数不合法');
$ideaCommentModel = new IdeaComment();
$list = $ideaCommentModel
->with(['user','touser'])
->where('pid',$id)
->order('id','desc')
->paginate(5,false,['page'=>$num])
->each(function ($item,$key){
$item->getRelation('user')->visible(['nickname','id']);
$item->visible(['user','touser','comment','id','user_id','touser_id','pid']);
if ($item['touser']) $item->getRelation('touser')->visible(['nickname','id']);
$item->unsetRelationAppend('user');
$item->unsetRelationAppend('touser');
$item->unsetAppend();
});
$list = $list->toArray();
$this->success('点子二级评论列表',['list'=>$list,'num'=>$num]);
}
/**
* @ApiTitle (获取体验感受列表)
* @ApiMethod (POST)
* @ApiReturn ({
'code':'1',
'msg':'体验感受列表'
})
*/
public function feelList()
{
$list = Db::name('idea_feel')->select();
$this->success('体验感受列表',['list'=>$list]);
}
/**
* @ApiTitle (发布一级评论)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name=comment, type=string, required=true, description="评论内容 长度50")
* @ApiParams (name=idea_id, type=int, required=true, description="点子id")
* @ApiParams (name=images, type=string, required=false, description="图片组 图片路径不要带域名 逗号隔开")
* @ApiParams (name=area, type=string, required=false, description="用户当前地址 市区格式")
* @ApiParams (name=feel_id, type=int, required=true, description="体验感受id")
* @ApiReturn ({
'code':'1',
'msg':'发布成功'
})
*/
public function firstCommentSubmit()
{
$ideaid = $this->request->post('idea_id');
$comment = $this->request->post('comment');
$images = $this->request->post('images');
$area = $this->request->post('area');
$feel_id = $this->request->post('feel_id');
$data = [
'idea_id'=>$ideaid,
'comment'=>$comment,
'feel_id'=>$feel_id,
];
$rule = [
'idea_id'=>'require|number',
'comment'=>'require|max:50',
'feel_id'=>'require|number',
];
$msg = [
'idea_id'=>'请正确上传点子id',
'comment'=>'请填写五十字符以内长度的评论',
'feel_id'=>'请正确选择体验感受',
];
$validate = new \think\Validate($rule,$msg);
if (!$validate->check($data)){
$this->error($validate->getError());
}
$msg = $this->checkMsg($this->auth->id,$comment);
$msg = json_decode($msg,true);
if ($msg['errcode'] != 0 || $msg['result']['label'] != 100){
$this->error('您的评论违规,请修改');
}
if ($images){
$images = str_replace('"','"',$images);
$imgarr = json_decode($images,true);
if (count($imgarr) > 3){
$this->error('图片上传最多三张');
}
$images = implode(',',$imgarr);
}
//计算中位数
$feel = Db::name('idea_feel')->where('id',$feel_id)->find();
if ($feel['fraction'] !== 0){
Db::name('idea')->where('id',$ideaid)->setInc('feel_number');
Db::name('idea')->where('id',$ideaid)->setInc('feel_number',$feel['fraction']);
}
$idea = Db::name('idea')->where('id',$ideaid)->find();
$fraction = $idea['feel_number'] == 0 ? 0 : $idea['feel_fraction']/$idea['feel_number'];
if ($fraction > 19){
$fraction = 19;
}elseif ($fraction < 1 && $fraction != 0){
$fraction = 1;
}else{
$fraction = (int)$fraction;
}
if ($area == ''){
$area = Db::name('idea_area')->where('id',$idea['idea_area_id'])->value('name');
}
$data = [
'idea_id'=>$ideaid,
'idea_name'=>'#'.$idea['title'],
'user_id'=>$this->auth->id,
'comment'=>$comment,
'area'=>$area,
'images'=>$images,
'idea_feel_id'=>$feel_id,
'createtime'=>time(),
];
Db::name('idea_comment')->insert($data);
$feel = Db::name('idea_feel')->where('fraction',$fraction)->find();
Db::name('idea')->where('id',$ideaid)->update(['idea_feel_id'=>$feel['id']]);
$this->success('发布成功');
}
/**
* @ApiTitle (发布二级评论)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name=comment, type=string, required=true, description="评论内容 长度50")
* @ApiParams (name=id, type=int, required=true, description="一级评论id或者二级评论id")
* @ApiParams (name=type, type=int, required=true, description="发布类型1=发布二级评论,2=回复二级评论")
* @ApiReturn ({
'code':'1',
'msg':'发布成功'
})
*/
public function secondCommentSubmit()
{
$id = $this->request->post('id');
$comment = $this->request->post('comment');
$type = $this->request->post('type');
if (!is_numeric($id)){
$this->error('id参数不合法');
}
if (!$comment || mb_strlen($comment) > 50){
$this->error('请填写字符长度50以内的评论');
}
$ideacomment = Db::name('idea_comment')->where('id',$id)->find();
if (!$ideacomment){
$this->error('该评论不存在');
}
$msg = $this->checkMsg($this->auth->id,$comment);
$msg = json_decode($msg,true);
if ($msg['errcode'] != 0 || $msg['result']['label'] != 100){
$this->error('您的评论违规,请修改');
}
if ($type == 1){
$data = [
'pid'=>$id,
'user_id'=>$this->auth->id,
'comment'=>$comment,
'idea_id'=>$ideacomment['idea_id'],
'idea_name'=>$ideacomment['idea_name'],
'createtime'=>time(),
];
}elseif ($type == 2){
$data = [
'pid'=>$ideacomment['pid'],
'user_id'=>$this->auth->id,
'touser_id'=>$ideacomment['user_id'],
'comment'=>$comment,
'idea_id'=>$ideacomment['idea_id'],
'idea_name'=>$ideacomment['idea_name'],
'createtime'=>time(),
];
}else{
$this->error('type参数不合法');
}
Db::name('idea_comment')->insert($data);
Db::name('idea_comment')->where('id',$id)->setInc('comment_number');
$this->success('发布成功');
}
/**
* @ApiTitle (点赞一级评论)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name=id, type=int, required=true, description="一级评论id")
* @ApiParams (name=type, type=int, required=true, description="类型1=点赞一级评论,2=取消点赞一级评论")
* @ApiReturn ({
'code':'1',
'msg':'SUCCESS'
})
*/
public function commentLikes()
{
$id = $this->request->post('id');
$type = $this->request->post('type');
if (!is_numeric($id)){
$this->error('id参数不合法');
}
$ideacomment = Db::name('idea_comment')->where('id',$id)->find();
if (!$ideacomment){
$this->error('该评论不存在');
}
if ($type == 1){
if (!Db::name('idea_comment_user_like')->where('user_id',$this->auth->id)->where('idea_comment_id',$id)->value('id')){
$data = [
'user_id'=>$this->auth->id,
'idea_comment_id'=>$ideacomment['idea_id'],
];
Db::name('idea_comment_user_like')->insert($data);
Db::name('idea_comment')->where('id',$id)->setInc('like_number');
}
}elseif ($type == 2){
Db::name('idea_comment_user_like')->where('user_id',$this->auth->id)->where('idea_comment_id',$id)->delete();
Db::name('idea_comment')->where('id',$id)->setDec('like_number');
}else{
$this->error('type参数不合法');
}
$this->success('SUCCESS');
}
protected function getAccessToken()
{
$url = 'https://api.weixin.qq.com/cgi-bin/token';
$appid = Config::get('site.appid');
$secret = Config::get('site.secret');
$data = [
'grant_type' => 'client_credential',
'appid' => $appid,
'secret' => $secret,
];
$wechattoken = Http::get($url, $data);
$wechattoken = json_decode($wechattoken, true);
if (isset($wechattoken['errcode'])){
$this->error($wechattoken['errmsg']);
}
return $wechattoken['access_token'];
}
protected function checkMsg($userid,$msg)
{
$access_token = $this->getAccessToken();
$openid = Db::name('third')->where('user_id',$userid)->value('openid');
$url = 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token='.$access_token;
$data = [
'version'=>2,
'openid'=>$openid,
'scene'=>2,
'content'=>$msg,
];
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
}