BaseController.php
16.2 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
<?php
/**
+----------------------------------------------------------------------------------------------------------------------
* 控制层基类:控制层通过路由结束用户的请求,并在每个子类中注入逻辑层的实例对象,控制层
* 只负责调度逻辑层的函数进行相关的用户请求操作,所有的代码逻辑都在逻辑层中
* 进行处理
+----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Http\Api\V1\Controllers
* @package App\Http\Api\V1\Controllers
* @author Richer <yangzi1028@163.com>
* @date 2020年11月13日 01:26:09
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Http\Api\V1\Controllers;
use App\Http\Controllers\Controller;
use App\Http\Requests\GivenRequest;
use App\Http\Requests\Order\OrderRequest;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Dingo\Api\Routing\Helpers;
use Illuminate\Support\Arr;
/**
* Class BaseController
*
* @category App\Http\Api\V1\Controllers
* @package App\Http\Api\V1\Controllers
* @author Richer <yangzi1028@163.com>
* @date 2020年11月13日 01:26:09
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
class BaseController extends Controller
{
use Helpers;
const ERROR_MSG =[
0 => 'operate_succeeded',
1 => 'operate_failed',
1001 => 'no_records',
1002 => 'query_failed',
1003 => 'add_failed',
1004 => 'update_failed',
1005 => 'delete_failed',
1006 => 'order_payment_failed',
1007 => 'order_cancellation_failed',
1008 => 'file_upload_failed',
2001 => 'send_verification_code_failed',
2002 => 'password_error',
2003 => 'verification_code_error',
2004 => 'register_failed',
2005 => 'password_reset_failed',
2006 => 'login_failed',
3001 => 'token_empty',
3002 => 'token_invalid',
3003 => 'token_expired'
];
/**
* Service 层实例
*
* @var $service
*/
protected $service;
/**
* 数据转换成层 Transformer层实例
*
* @var $transformer
*/
protected $transformer ;
/**
* 执行构造方法
* BaseController constructor.
*
* @param null $service service层实例
* @param null $transformer 表现层实例
*/
public function __construct($service = null, $transformer = null)
{
$this->service = $service;
$this->transformer = $transformer;
}
/**
* 返回 成功json response
*
* @param array $data 数据
* @param string $msg 提示消息
* @param integer $code 提示code
* @param string $token $token
*
* @return Response
*/
protected function success($data = null, $msg = '', $code = 0, $token = '')
{
$arr['code'] = $code;
$arr['msg'] = $msg ? : __('operate_succeeded');
$arr['data'] = $data;
// 判断 request中是否有token,如果有说明该token是刷新的token需要返回,更新客户端的token,做到无痛刷新
if (!$token) {
$token = request()->token;
}
// 将token返回
!empty($token) && $arr['token'] = $token;
return $this->response->array($arr);
}
/**
* 返回失败
*
* @param string $msg 提示消息
* @param integer $code 提示code
* @param null $data 返回的数据
*
* @return Response
*/
protected function error($msg = '操作失败!', $code = 1, $data = null)
{
if (!$msg) {
$msg = $this->service->getMessage() ? : Arr::get(self::ERROR_MSG, $code);
}
//return response()->json(['code' => $code, 'msg' => $msg, 'data' => $data ]);
$arr['code'] = $code;
$arr['msg'] = $msg;
$arr['data'] = $data;
$token = request()->token;
// 将token返回
!empty($token) && $arr['token'] = $token;
return $this->response->array($arr);
}
/**
* 转换操作信息
*
* @param $code
* @return string|null
*/
public static function getErrorMsg($code)
{
$msg = Arr::get(self::ERROR_MSG, $code);
return __($msg);
}
/**
* Display a listing of the resource.
* 获取全部的数据,不进行分页
*
* @return Response
*/
public function all()
{
$list = $this->service->all();
if (!$list) {
return $this->error('', 1001);
}
if ($this->transformer) {
// 对数据进行转换
return $this->collection($list, $this->transformer);
} else {
return $this->success($list);
}
}
/**
* 获取指定数量的数据,不进行分页返回数据
*
* @param Request $request 请求
*
* @return Response
*/
public function top(Request $request)
{
$list = $this->service->top($request);
if (!$list) {
return $this->error('', 1001);
}
if ($this->transformer) {
// 对数据进行转换
return $this->collection($list, $this->transformer);
} else {
return $this->success($list);
}
}
/**
* Display a listing of the resource.
* 获取列表数据,进行分页展示
*
* @return Response
*/
public function index()
{
$list = $this->service->list();
if (!$list) {
return $this->error('', 1001);
}
if ($this->transformer) {
// 对数据进行转换
return $this->paginator($list, $this->transformer);
} else {
return $this->success($list);
}
}
/**
* Display the specified resource.
*
* @param integer $id 主键id
*
* @return Response
*/
public function show($id)
{
$data = $this->service->show($id);
if (!$data) {
return $this->error('', 1001);
}
if ($this->transformer) {
// 对数据进行转换
return $this->item($data, $this->transformer);
} else {
return $this->success($data);
}
}
/**
* Display the latest resource.
*
* @return Response
*/
public function latest()
{
$data = $this->service->latest();
if (!$data) {
return $this->error('', 1001);
}
if ($this->transformer) {
// 对数据进行转换
return $this->item($data, $this->transformer);
} else {
return $this->success($data);
}
}
/**
* Display the specified resource.
*
* @return Response
*/
public function first()
{
$data = $this->service->first();
if (!$data) {
return $this->error('', 1001);
}
if ($this->transformer) {
// 对数据进行转换
return $this->item($data, $this->transformer);
} else {
return $this->success($data);
}
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
*
* @return Response
*
* @throws \Prettus\Validator\Exceptions\ValidatorException
*/
// public function store(Request $request)
// {
// $result = $this->service->store($request);
// if (!$result) {
// return $this->api_response(1003);
// }
// return $this->api_response(1,'' ,$result);
// }
/**
* Show the form for editing the specified resource.
*
* @param integer $id 主键id
*
* @return Response
*/
public function edit($id)
{
$data = $this->service->edit($id);
if (!$data) {
return $this->error('', 1001);
}
if ($this->transformer) {
// 对数据进行转换
return $this->item($data, $this->transformer);
} else {
return $this->success($data);
}
}
/**
* Remove the specified resource from storage.
*
* @param integer $id 主键
* @param boolean $forceDelete 是否强制物理删除
*
* @return Response
*/
public function destroy($id, $forceDelete = false)
{
$deleted = $this->service->destroy($id, $forceDelete);
if ($deleted) {
// 对数据进行操作
return $this->success($deleted);
}
return $this->error($this->service->getMessage(), 1005);
}
/**
* 获取代码集:将数据转换成一位数组代码集格式方便使用
*
* @return Response
*/
public function codeset()
{
$codeset = $this->service->codeset();
if (!empty($codeset)) {
return $this->success($codeset);
}
return $this->error($this->service->getMessage(), 1001);
}
/**
* 基类方法:搜索并分页显示
*
* @return Response
*/
public function search()
{
$list = $this->service->search();
if ($list) {
if ($this->transformer) {
// 对数据进行转换
return $this->item($list, $this->transformer);
} else {
return $this->success($list);
}
}
return $this->error($this->service->getMessage(), 1001);
}
/**
* 参数配置
*
* @param Request $request request
*
* @return Response
*/
public function configs(Request $request)
{
$result = $this->service->configs($request);
// 验证结果
if ($result) {
return $this->success($result);
}
return $this->error($this->service->getMessage(), 1001);
}
/**
* 评论:根据目标对象的评论一个对象
*
* @param CommentRequest $request 请求
* @param integer $id 目标对象的主键
*
* @return Response
*/
public function comment(CommentRequest $request, $id)
{
$result = $this->service->comment($request, $id);
// 验证结果
if ($result) {
return $this->success($result);
}
return $this->error($this->service->getMessage(), 1003);
}
/**
* 获取评论列表
*
* @param integer $id 主键id
*
* @return Response
*/
public function comments($id)
{
$comments = $this->service->comments($id);
// 验证结果
if ($comments) {
return $this->paginator($comments, new \App\Transformers\System\CommentTransformer());
}
return $this->error($this->service->getMessage(), 1001);
}
/**
* 点赞投票:根据目标对象的点赞一个对象
*
* @param integer $id 主键id
*
* @return Response
*/
public function vote($id)
{
$result = $this->service->vote($id);
// 验证结果
if ($result) {
return $this->success($result);
}
return $this->error($this->service->getMessage(), 1003);
}
/**
* 取消点赞投票:根据目标对象的取消点赞一个对象
*
* @param integer $id 主键id
*
* @return Response
*/
public function unvote($id)
{
$result = $this->service->unvote($id);
// 验证结果
if ($result) {
return $this->success($result);
}
return $this->error($this->service->getMessage(), 1005);
}
/**
* 收藏:根据目标对象的收藏一个对象
*
* @param integer $id 主键id
*
* @return Response
*/
public function favorite($id)
{
$result = $this->service->favorite($id);
// 验证结果
if ($result) {
return $this->success($result);
}
return $this->error($this->service->getMessage(), 1003);
}
/**
* 取消收藏:根据目标对象的取消收藏一个对象
*
* @param integer $id 主键id
*
* @return Response
*/
public function unfavorite($id)
{
$result = $this->service->unfavorite($id);
// 验证结果
if ($result) {
return $this->success($result);
}
return $this->error($this->service->getMessage(), 1005);
}
/**
* 喜欢 :根据目标对象的喜欢一个对象
*
* @param integer $id 主键id
*
* @return Response
*/
public function like($id)
{
$result = $this->service->like($id);
// 验证结果
if ($result) {
return $this->success($result);
}
return $this->error($this->service->getMessage(), 1003);
}
/**
* 取消喜欢:根据目标对象的取消喜欢一个对象
*
* @param integer $id 主键id
*
* @return Response
*/
public function unlike($id)
{
$result = $this->service->unlike($id);
// 验证结果
if ($result) {
return $this->success($result);
}
return $this->error($this->service->getMessage(), 1005);
}
/**
* 举报:根据目标对象的举报一个对象
*
* @param Request $request 请求
* @param integer $id 主键id
*
* @return Response
*/
public function report(Request $request, $id)
{
$result = $this->service->report($request, $id);
// 验证结果
if ($result) {
return $this->success($result);
}
return $this->error($this->service->getMessage(), 1003);
}
/**
* 订购:根据目标对象的id 订购一个对象
*
* @param OrderRequest $request 请求
* @param integer $id 主键id
*
* @return Response
*/
public function order(OrderRequest $request, $id)
{
$result = $this->service->order($request, $id);
// 验证结果
if ($result) {
return $this->success($result);
}
return $this->error($this->service->getMessage(), 1003);
}
/**
* 置顶
*
* @param integer $id 主键id
*
* @return Response
*/
public function setTop($id)
{
$result = $this->service->setTop($id);
// 验证结果
if ($result) {
if ($this->transformer) {
// 对数据进行转换
return $this->item($result, $this->transformer);
} else {
return $this->success($result);
}
}
return $this->error($this->service->getMessage(), 1003);
}
/**
* 取消置顶
*
* @param integer $id 主键id
*
* @return Response
*/
public function cancelTop($id)
{
$result = $this->service->cancelTop($id);
if ($this->transformer) {
// 对数据进行转换
return $this->item($result, $this->transformer);
} else {
return $this->success($result);
}
return $this->error($this->service->getMessage(), 1003);
}
/**
* 关联商品
*
* @param Request $request
* @param $id
*
* @return Response
*/
public function relateProduct(Request $request, $id)
{
$result = $this->service->relateProduct($request, $id);
// 验证结果
if ($result) {
return $this->success($result);
}
return $this->error($this->service->getMessage(), 1004);
}
/**
* 赠送
*
* @param GivenRequest $request 请求
* @param integer $id 主键id
*
* @return Response
*/
public function give(GivenRequest $request, $id)
{
$result = $this->service->give($request, $id);
// 验证结果
if ($result) {
return $this->success($result);
}
return $this->error($this->service->getMessage(), 1003);
}
}