Article.php
28.6 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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
<?php
namespace app\api\controller;
use EasyWeChat\Factory;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Request;
/**
* 动态相关
* @package app\api\controller
*/
class Article extends BaseApi
{
protected $noNeedLogin = '';
protected $noNeedRight = '*';
/**
* 增加动态
* @ApiTitle (增加动态)
* @ApiMethod (POST)
* @ApiRoute (/api/article/addArticle)
* @ApiParams (name="content", type="string", required=true, description="内容")
* @ApiParams (name="images", type="string", required=true, description="图片数组(不带域名)")
* @ApiParams (name="article_category_id", type="integer", required=true, description="动态栏目id")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "增加动态成功",
"time": "1608793664",
"data": {
"user_id": 1,
"article_category_id": "7",
"content": "内容",
"images": [
"图片数组"
],
"createtime": "2020-12-24 15:07:44",
"updatetime": "2020-12-24 15:07:44",
"id": "8"
}
})
*/
public function addArticle(){
//1.验证用户权限
if ($this->auth->authlist == '' || $this->auth->authlist == ','){
$this->error('请您先实名认证');
}
//2.获取数据
$data = $this->get_data_array([
['article_category_id','栏目id不存在'],
['content','内容不存在'],
['images','图片必须上传'],
]);
//3.查询栏目
$article_category = model('article_category')->where('id',$data['article_category_id'])->find();
if (!$article_category){
$this->error('栏目不存在');
}
//4.获取图片数据
$files = str_replace('"','"',$data['images']);
$images = implode(',',json_decode($files,true));
//5.增加动态
$article = model('article')->create([
'user_id'=>$this->auth->id,
'article_category_id'=>$data['article_category_id'],
'content'=>$data['content'],
'images'=>$images
]);
//6.查询数据
$article = model('article')->where('id',$article['id'])->find();
//7.返回结果
$this->success('增加动态成功',$article);
}
/**
* 查看动态详情
* @ApiTitle (查看动态详情)
* @ApiMethod (POST)
* @ApiRoute (/api/article/getArticleContent)
* @ApiParams (name="article_id", type="integer", required=true, description="动态内容")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "获取动态成功",
"time": "1608885679",
"data": {
"id": 11,
"user_id": 1,
"article_category_id": "动态栏目id",
"content": "内容",
"images": "详情图片",
"show_num": "显示数量",
"zan_num": "点赞数量",
"ping_num": "评价数量",
"xiang_num": "分享数量",
"createtime": "2020-12-25 16:28:01",
"updatetime": "2020-12-25 16:41:19",
"deletetime": null,
"message": null,
"is_zan": "是否点赞 0未点赞 1已经点赞",
"user": {
"id": 1,
"avatar": "用户头像",
"nickname": "用户昵称",
"username": "用户名",
"authlist": "用户身份id",
"url": "/u/1",
"authlist_array": "用户身份"
},
"str_time": "显示时间"
}
})
*/
public function getArticleContent(){
//1.获取数据
$article_id = $this->get_data('article_id','动态id不能为空');
//2.查询栏目
$article = model('article')->with(['user'])->where('id',$article_id)->find();
if (!$article){
$this->error('动态不存在');
}
//3.判断删除
if ($article['deletetime'] != null){
$this->error('当前动态已被删除');
}
//4.增加显示数量
$article->show_num += 1;
$article->save();
//5.查询是否当前用户是否点赞
$article_zan = model('article_zan')->where('user_id',$this->auth->id)->find();
$article_zan?$article['is_zan'] = 1:$article['is_zan'] = 0;
//6.返回结果
$this->success('获取动态成功',$article);
}
/**
* 动态点赞或取消点赞{注意}
* @ApiTitle (动态点赞或取消点赞{注意})
* @ApiMethod (POST)
* @ApiRoute (/api/article/setArticleZan)
* @ApiParams (name="article_id", type="integer", required=true, description="动态内容")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "查询成功",
"time": "1608886856",
"data": {
"code": "0取消点赞 1=点赞成功",
"msg": "提示"
}
})
*/
public function setArticleZan(){
//1.获取数据
$article_id = $this->get_data('article_id','动态id不能为空');
//2.查询动态数据
$article = model('article')->where('id',$article_id)->find();
//3.查询点赞
$article_zan = model('article_zan')
->where('user_id',$this->auth->id)
->where('article_id',$article_id)
->find();
if ($article_zan){
$article_zan->delete();
$article->zan_num -= 1;
$article->save();
$this->success('查询成功',['code'=>0,'msg'=>'取消点赞成功']);
}
//4.增加点赞
model('article_zan')->create(['user_id'=>$this->auth->id,'article_id'=>$article_id]);
//5.增加点赞数量
$article->zan_num += 1;
$article->save();
//6.返回结果
$this->success('查询成功',['code'=>1,'msg'=>'点赞成功']);
}
/**
* 获取动态评论列表
* @ApiTitle (获取动态评论列表)
* @ApiMethod (POST)
* @ApiRoute (/api/article/getComment)
* @ApiParams (name="article_id", type="integer", required=true, description="动态id")
* @ApiParams (name="page", type="integer", required=true, description="分页次数")
* @ApiParams (name="num", type="integer", required=true, description="分页数量")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "查询成功",
"time": "1608890670",
"data": [
{
"id": 2,
"article_id": "动态id",
"user_id": "用户id",
"content": "评论内容",
"pid": "0一级评论 不是0是父级评论id",
"zan_num": "点赞数量",
"createtime": "2020-12-25 17:53:07",
"updatetime": "2020-12-25 17:53:07",
"deletetime": null,
"message": null,
"is_zan": "是否点赞 0未点赞 1点赞",
"son_num":"子评论数量",
"user": "用户信息参考首页列表注释",
"str_time": "发布时间"
}
]
})
*/
public function getComment(){
Db::startTrans();
try {
//1.获取动态id
$data = $this->get_data_array([
['article_id','动态id不能为空'],
['page','分页次数不能为空'],
['num','分页数量不能为空']
]);
//2.获取一级评论
$article_comment = model('article_comment')
->with(['user'])
->where('article_id',$data['article_id'])
->where('pid',0)
->order('createtime','desc')
->order('deletetime',null)
->page($data['page'],$data['num'])
->select();
//3.循环父级评论
foreach ($article_comment as $key => $val){
//3.1 判断用户有没有点赞
$is_zan = model('article_comment_zan')
->where('user_id',$this->auth->id)
->where('article_comment_id',$val['id'])
->find();
$article_comment[$key]['is_zan'] = 1;
if (!$is_zan){
$article_comment[$key]['is_zan'] = 0;
}
//3.2 获取子评论数量
$article_comment[$key]['son_num'] = model('article_comment')
->where('pid',$val['id'])
->count();
}
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
//4.返回数据
$this->success('查询成功',$article_comment);
}
/**
* 发布评论
* @ApiTitle (发布评论)
* @ApiMethod (POST)
* @ApiRoute (/api/article/setComment)
* @ApiParams (name="article_id", type="integer", required=true, description="动态id")
* @ApiParams (name="content", type="integer", required=true, description="评论内容")
* @ApiParams (name="pid", type="integer", required=true, description="父级评论id")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ( )
*/
public function setComment(){
Db::startTrans();
try {
//1.获取动态id
$data= $this->get_data_array([
['article_id','动态id不能为空'],
['content','评论内容'],
]);
//2.发布评论
$pid = empty(input('param.pid'))?0:input('param.pid');
$data['pid'] = $pid;
$data['user_id'] = $this->auth->id;
$data['zan_num'] = 0;
$res = model('article_comment')->create($data);
Db::commit();
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
//3.发布成功
$this->success('发布评论成功',$res);
}
/**
* 根据父级评论获取子评论
* @ApiTitle (根据父级评论获取子评论)
* @ApiMethod (POST)
* @ApiRoute (/api/article/getTwoComment)
* @ApiParams (name="article_comment_id", type="integer", required=true, description="动态评论id")
* @ApiParams (name="page", type="integer", required=true, description="分页次数")
* @ApiParams (name="num", type="integer", required=true, description="分页数量")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "获取评论成功",
"time": "1608892708",
"data": [
{
"id": 2,
"article_id": "动态ID",
"user_id": "用户ID",
"content": "评论内容",
"pid": "父级id 注意:第一次返回的第一个数据是父级数据 第二次不会返回",
"zan_num": "点赞数量",
"createtime": "2020-12-25 17:53:07",
"updatetime": "2020-12-25 17:53:07",
"deletetime": null,
"message": null,
"is_zan": "是否点赞",
"is_this": "是不是自己发布的评论 1=是,0=不是",
"user": "用户信息 参考首页列表注释",
"str_time": "时间"
}
]
})
*/
public function getTwoComment(){
Db::startTrans();
try {
//1.获取动态id
$data= $this->get_data_array([
['article_comment_id','动态评论id不能为空'],
['page','分页次数不能为空'],
['num','分页数量不能为空']
]);
//2.发布评论
$father_article_comment = model('article_comment')
->with(['user'])
->where('id',$data['article_comment_id'])
->find();
if (!$father_article_comment){
$this->error('您的父级评论不存在');
}
//3.判断是不是第一次 如果是第一次添加进去父级评论
$array = [];
if ($data['page'] == 1){
$array[] = $father_article_comment;
}
//4.查询子级数据
$son_article_somment_array = model('article_comment')
->with(['user'])
->where('pid',$father_article_comment['id'])
->order('createtime','desc')
->page($data['page'],$data['num'])
->select();
foreach ($son_article_somment_array as $val){
$array[] = $val;
}
//5.循环查询是否点赞
foreach ($array as $key => $val){
//3.1 判断用户有没有点赞
$is_zan = model('article_comment_zan')
->where('user_id',$this->auth->id)
->where('article_comment_id',$val['id'])
->find();
$array[$key]['is_zan'] = 1;
if (!$is_zan){
$array[$key]['is_zan'] = 0;
}
//3.2判断是不是自己发布的评论
$array[$key]['is_this'] = 0;
if ($val['user_id'] == $this->auth->id){
$array[$key]['is_this'] = 1;
}
}
Db::commit();
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
//3.发布成功
$this->success('获取评论成功',$array);
}
/**
* 动态评论点赞或取消点赞{注意}
* @ApiTitle (动态评论点赞或取消点赞{注意})
* @ApiMethod (POST)
* @ApiRoute (/api/article/setArticleCommentZan)
* @ApiParams (name="article_comment_id", type="integer", required=true, description="动态内容")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "查询成功",
"time": "1608886856",
"data": {
"code": "0取消点赞 1=点赞成功",
"msg": "提示"
}
})
*/
public function setArticleCommentZan(){
//1.获取数据
$article_comment_id = $this->get_data('article_comment_id','动态评论id不能为空');
//2.查询动态数据
$article_comment = model('article_comment')->where('id',$article_comment_id)->find();
//3.查询点赞
$article_comment_zan = model('article_comment_zan')
->where('user_id',$this->auth->id)
->where('article_comment_id',$article_comment_id)
->find();
if ($article_comment_zan){
$article_comment_zan->delete();
$article_comment->zan_num -= 1;
$article_comment->save();
$this->success('查询成功',['code'=>0,'msg'=>'取消点赞成功']);
}
//4.增加点赞
model('article_comment_zan')->create(['user_id'=>$this->auth->id,'article_comment_id'=>$article_comment_id]);
//5.增加点赞数量
$article_comment->zan_num += 1;
$article_comment->save();
//6.返回结果
$this->success('查询成功',['code'=>1,'msg'=>'点赞成功']);
}
/**
* 举报动态
* @ApiTitle (举报动态)
* @ApiMethod (POST)
* @ApiRoute (/api/article/setArticleReport)
* @ApiParams (name="article_id", type="integer", required=true, description="动态id")
* @ApiParams (name="report_type_id", type="integer", required=true, description="举报类型ID")
* @ApiParams (name="content", type="integer", required=true, description="举报内容")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ()
*/
public function setArticleReport(){
try {
//1.获取动态id
$data= $this->get_data_array([
['article_id','动态ID不能为空'],
['report_type_id','举报类型ID不能为空'],
['content','举报内容不能为空'],
]);
//2.查询举报内容
$article_report = model('article_report')
->where('user_id',$this->auth->id)
->where('article_id',$data['article_id'])
->where('report_type_id',$data['report_type_id'])
->where('status',0)
->find();
if ($article_report){
$this->error('您已经举报该动态,请等待管理员审核!');
}
//3.查询举报类型
$data['user_id'] = $this->auth->id;
$data['avatar'] = $this->auth->avatar;
$data['nickname'] = $this->auth->nickname;
$data['username'] = $this->auth->username;
$data['mobile'] = $this->auth->mobile;
$data['status'] = 0;
$data['report_type_name'] = model('report_type')->where('id',$data['report_type_id'])->find()['name']??'';
$res = model('article_report')->create($data);
Db::commit();
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
//3.发布成功
$this->success('举报成功',$res);
}
/**
* 举报评论
* @ApiTitle (举报评论)
* @ApiMethod (POST)
* @ApiRoute (/api/article/setArticleCommentReport)
* @ApiParams (name="article_comment_id", type="integer", required=true, description="评论id")
* @ApiParams (name="report_type_id", type="integer", required=true, description="举报类型ID")
* @ApiParams (name="content", type="integer", required=true, description="举报内容")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ()
*/
public function setArticleCommentReport(){
try {
//1.获取动态id
$data= $this->get_data_array([
['article_comment_id','评论ID不能为空'],
['report_type_id','举报类型ID不能为空'],
['content','举报内容不能为空'],
]);
//2.查询举报内容
$article_report = model('article_comment_report')
->where('user_id',$this->auth->id)
->where('article_comment_id',$data['article_comment_id'])
->where('report_type_id',$data['report_type_id'])
->where('status',0)
->find();
if ($article_report){
$this->error('您已经举报该评论,请等待管理员审核!');
}
//3.查询举报类型
$data['user_id'] = $this->auth->id;
$data['avatar'] = $this->auth->avatar;
$data['nickname'] = $this->auth->nickname;
$data['username'] = $this->auth->username;
$data['mobile'] = $this->auth->mobile;
$data['status'] = 0;
$data['report_type_name'] = model('report_type')->where('id',$data['report_type_id'])->find()['name']??'';
$res = model('article_comment_report')->create($data);
Db::commit();
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
//3.发布成功
$this->success('举报成功',$res);
}
/**
* 获取礼物列表
* @ApiTitle (获取礼物列表)
* @ApiMethod (POST)
* @ApiRoute (/api/article/getArticleGift)
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "获取数据成功",
"time": "1608963789",
"data": [
{
"id": 1,
"name": "礼物名称",
"price": "礼物价格",
"image": "图片",
"weigh": 999,
"createtime": "2020-12-26 13:44:37",
"updatetime": "2020-12-26 13:59:05"
}
]
})
*/
public function getArticleGift(){
//1.获取礼物数据
$list = model('gift')->order('weigh','desc')->limit(7)->select();
//2.返回数据
$this->success('获取数据成功',$list);
}
/**
* 送给动态礼物
* @ApiTitle (送给动态礼物)
* @ApiMethod (POST)
* @ApiRoute (/api/article/giveGift)
* @ApiParams (name="article_id", type="integer", required=true, description="动态id")
* @ApiParams (name="gift_id", type="integer", required=true, description="礼物id")
* @ApiParams (name="price", type="integer", required=true, description="价格")
* @ApiParams (name="status", type="integer", required=true, description="是否是自定义价格:0=不是{需要填写gift_id},1=是{需要填写金额}")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "获取数据成功",
"time": "1608963789",
"data": ""
*/
public function giveGift(){
//1.获取数据
$data = $this->get_data_array([
['article_id','您的动态ID不能为空'],
['status','您的自定义价格字段不能为空']
]);
//2.查询动态
$article = model('article')->where('id',$data['article_id'])->find();
if (!$article)$this->error('您的动态不存在,请您检查article_id');
//3.判断不是自定义价格
if ($data['status'] == 0){
//3.1 查询礼物
$article_gift = model('gift')->where('id',$this->get_data('gift_id','礼物id不能为空'))->find();
if (!$article_gift)$this->error('未查询到相关礼物,请您检查gift_id');
//3.2 查询礼物订单
$is_order = model('article_gift_order')
->where('article_id',$article['id'])
->where('gift_id',$article_gift['id'])
->where('is_custom',0)
->where('status',0)
->where('price',$article_gift['price'])
->find();
if (!$is_order){
$a_data = [
'number'=>time()+rand(111111,999999),
'article_id'=>$article['id'],
'article_user_id'=>$article['user_id'],
'user_id'=>$this->auth->id,
'avatar'=>$this->auth->avatar,
'nickname'=>$this->auth->nickname,
'username'=>$this->auth->username,
'mobile'=>$this->auth->mobile,
'price'=>$article_gift['price'],
'status'=>0,
'is_custom'=>0,
'gift_id'=>$article_gift['id'],
'article_gift_name'=>$article_gift['name'],
];
$is_order = model('article_gift_order')->create($a_data);
}
}
//4.判断是自定义价格
if ($data['status'] == 1){
//4.1 查询礼物
$article_gift = $this->get_data('price','价格不能为空');
//4.2 查询礼物订单
$is_order = model('article_gift_order')
->where('article_id',$article['id'])
->where('is_custom',1)
->where('status',0)
->where('price',$article_gift['price'])
->find();
if (!$is_order){
$a_data = [
'number'=>time()+rand(111111,999999),
'article_id'=>$article['id'],
'article_user_id'=>$article['user_id'],
'user_id'=>$this->auth->id,
'avatar'=>$this->auth->avatar,
'nickname'=>$this->auth->nickname,
'username'=>$this->auth->username,
'mobile'=>$this->auth->mobile,
'price'=>$article_gift['price'],
'status'=>0,
'is_custom'=>0,
];
$is_order = model('article_gift_order')->create($a_data);
}
}
//------------ 支付 -------------
//5.获取配置信息
$config = config('EasyWeChat');
$app = Factory::payment($config);
//6.查询用户的第三方登录信息
$fa_third = Db::name('third')->where('user_id',$this->auth->id)->find();
if (!$fa_third)$this->error('第三方授权数据不存在');
//3.统一下单接口
$result = $app->order->unify([
'body' => '送出礼物',
'out_trade_no' => $is_order['number'],
//'total_fee' => $order['price'] * 100,
'total_fee' => 1,
'notify_url' =>this_url().'/api/article/setWechatGiftNotify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
'openid' => $fa_third['openid'],
]);
//4.判断返回结果
if ($result['result_code'] == 'SUCCESS' && $result['return_code'] == 'SUCCESS') {
$prepayId = $result['prepay_id'];
$jssdk = $app->jssdk;
$config = $jssdk->sdkConfig($prepayId);
$this->success('获取支付成功',$config);
} else {
$this->error('失败',$result);
}
$list = model('article_gift')->order('weigh','desc')->limit(7)->select();
//2.返回数据
$this->success('获取数据成功',$list);
}
/**
* 支付礼物回调
* @ApiMethod (POST)
* @ApiRoute (/api/article/setWechatGiftNotify)
* @ApiReturn ()
*/
public function setWechatNotify(){
$config = config('EasyWeChat');
$app = Factory::payment($config);
$response = $app->handlePaidNotify(function ($message, $fail) {
//将礼物进行支付
$order = model('article_gift_order')->where('number',$message['out_trade_no'])->find();
if (!$order)$fail('订单不存在');
if ($order['status'] == 1)$fail('您的订单已经支付');
$order->status = 1;
$order->save();
//给礼物用户一个通知
//
});
$response->send(); // Laravel 里请使用:return $response;
}
}