BaseController.php
4.3 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
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2024/1/4
* Time: 16:25
*/
namespace App\Http\Controllers\Api;
use App\Enums\Common\Code;
use App\Http\Controllers\Controller;
use App\Models\WorkOrder\TicketProject;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
/**
* Class BaseController
* @package App\Http\Controllers\Api
*/
class BaseController extends Controller
{
protected $param = [];//所有请求参数
protected $token = ''; //token
protected $request = [];//助手函数
protected $page = 1;//当前页
protected $row = 20;//每页条数
protected $header = [];//设置请求头参数
protected $order = 'created_at';
protected $order_type = 'desc';
protected $map = [];//处理后的参数
public function __construct(Request $request)
{
$this->request = $request;
$this->param = $this->request->all();
$this->getParam();
}
/**
* @remark :请求参数处理
* @name :getParam
* @author :lyh
* @method :post
* @time :2023/6/17 16:34
*/
public function getParam(){
foreach ($this->param as $k => $v){
if(is_array($v)){
$this->map[$k] = $v;
}else{
$this->getMap($k,$v);
}
}
}
/**
* @remark :搜索参数处理
* @name :getMap
* @author :lyh
* @method :post
* @time :2023/8/28 10:22
*/
public function getMap($k,$v){
switch ($k){
case "order":
$this->order = $v;
break;
case "order_type":
$this->order_type = $v;
break;
case 'page':
$this->page = $v;
break;
case 'row':
case 'size':
$this->row = $v;
break;
case "name":
$this->map['name'] = ['like','%'.$v.'%'];
break;
case "start_at":
$this->_btw[0] = $v;
$this->_btw[1] = date('Y-m-d H:i:s',time());
$this->map['created_at'] = ['between', $this->_btw];
break;
case "end_at":
$this->_btw[1] = $v;
$this->map['created_at'] = ['between', $this->_btw];
break;
default:
if (!empty($v) || $v == 0) {
$this->map[$k] = $v;
}
break;
}
}
/**
* @param array $data
* @param string $message
* @param int $status
* @return false|string
*/
protected function success($data = [], $message = 'success', $status = 200)
{
$array = compact('status', 'message', 'data');
return json_encode($array, JSON_UNESCAPED_UNICODE);
}
/**
* @param string $message
* @param int $status
* @param array $data
* @return false|string
*/
protected function error($message = 'error', $status = 400, $data = [])
{
$array = compact('status', 'message', 'data');
return json_encode($array, JSON_UNESCAPED_UNICODE);
}
/**
* @name :统一返回参数
* @return JsonResponse
* @author :liyuhang
* @method
*/
public function response($msg = null,string $code = Code::SUCCESS,$data = [],$result_code = 200,$type = 'application/json'): JsonResponse
{
$code = Code::fromValue($code);
$result = [
'code' => $code->value,
'data' => $data,
'message' => $msg == ' ' ? $code->description : $msg,
];
$header['Content-Type'] = $type;
$response = response($result,$result_code,$header);
throw new HttpResponseException($response);
}
/**
* 表单单独响应数据格式
*/
public function responseA($data = [], $code = 200, $msg = 'success', $result_code = 200, $type = 'application/json')
{
$result = [
'msg' => $msg,
'code' => $code,
'data' => $data,
];
$header = [
'Content-Type' => $type,
];
$response = response($result, $result_code, $header);
throw new HttpResponseException($response);
}
}