JsonResource.php
5.73 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
<?php
namespace App\Helpers;
use Illuminate\Http\JsonResponse;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\Response;
class JsonResource
{
public const SUCCESS = '获取成功';
public const CREATE_SUCCESS = '创建成功';
public const UPDATE_SUCCESS = '更新成功';
public const DELETE_SUCCESS = '删除成功';
public const LOGIN_SUCCESS = '登录成功';
public const LOGOUT_SUCCESS = '登出成功';
public const UNAUTHORIZED = '未携带身份令牌';
public const AUTHORIZED_EXP = '身份令牌失效';
public const NO_PERMISSION = '无权限进行此操作';
public const USER_FOUND_ERROR = '用户不存在';
public const LOGIN_FOUND_ERROR = '账号不存在';
public const LOGIN_FORBID_ERROR = '账户已被封禁,请联系管理员';
public const LOGIN_PASSWORD_ERROR = '密码错误';
public const REPEAT_SUBMIT = '请勿重复提交';
public const IS_TOP_LIMIT = '最多允许置顶3条内容';
public const INVITE_SUCCESS = '邀请成功';
public const APPLY_SUCCESS = '申请成功';
public const INVITE_LIMIT = '1天内仅能向他发起一次,请不要频繁邀请';
public const APPLY_LIMIT = '1天内仅能向他发起一次,请不要频繁申请';
public const TEAM_INVITE_SINGER_LIMIT = '团队内歌手名额已满,暂无法邀请 ~';
public const TEAM_JOIN_SINGER_LIMIT = '团队内歌手名额已满';
public const JOINED_TEAM = '对方已在其他团队';
public const MEMBER_JOINED_TEAM = 'Ta已加入他人团队,您暂无法同意 ~';
public const SELF_MEMBER_JOINED_TEAM = '你已加入他人团队,您暂无法同意 ~';
public const INVITE_JOINED_TEAM = 'Ta已加入他人团队,您暂无法加入 ~';
public const SELF_INVITE_JOINED_TEAM = '你已加入他人团队,您暂无法同意 ~';
public const JOIN_TEAM_INVALID = '超时,已失效';
public const INVALIDED = '已失效';
public const HANDLE_SUCCESS = '处理成功';
public const OPERATION_SUCCESS = '操作成功';
public const MEMBER_TOP_LIMIT = '最多可置顶5人';
public const MEMBER_OUT_INVALID = '退出申请已被处理';
public const PROJECT_INVALIDED = '厂牌已被禁用或删除';
public const HANDEL_FAIL = '操作失败,请刷新后再试';
public const IS_PROJECT_MEMBER = '操作失败,您已经是厂牌成员了';
public const IS_NOT_MASTER = '操作失败,对方已经不是该厂牌管理员';
public const YOU_NOT_MASTER = '操作失败,您已经不是该厂牌管理员';
public const OUT_IS_HANDLE = '操作失败,该申请已被其他管理员处理';
/**
* @param string|null $message
* @param int $code
* @return JsonResponse
*/
public static function empty(string $message = NULL, int $code = Response::HTTP_OK): JsonResponse
{
return self::send('success', $code, $message ?? Response::$statusTexts[$code]);
}
/**
* @param string|null $message
* @param mixed $data
* @param int $code
* @param array $extend
* @return JsonResponse
*/
public static function success(string $message = NULL, $data = NULL, int $code = Response::HTTP_OK, array $extend = []): JsonResponse
{
return self::send('success', $code, $message ?? Response::$statusTexts[$code], $data, $extend);
}
/**
* @param mixed $data
* @param string|null $message
* @param int $code
* @return JsonResponse
*/
public static function fail(string $message = NULL, $data = NULL, int $code = Response::HTTP_UNPROCESSABLE_ENTITY): JsonResponse
{
return self::send('fail', $code, $message ?? Response::$statusTexts[$code], $data);
}
public static function unauthenticated(string $message = NULL, $data = NULL, int $code = 401): JsonResponse
{
return self::send('fail', $code, $message ?? Response::$statusTexts[$code], $data);
}
/**
* @param mixed $data
* @param string|null $message
* @param int $code
* @return JsonResponse
*/
public static function error(string $message = NULL, $data = NULL, int $code = Response::HTTP_INTERNAL_SERVER_ERROR): JsonResponse
{
return self::send('error', $code, $message ?? Response::$statusTexts[$code], config('app.debug') && $code >= 500 ? $data : []);
}
/**
* @param LengthAwarePaginator|\Illuminate\Contracts\Pagination\LengthAwarePaginator $data
* @return array
*/
public static function paginator(LengthAwarePaginator $data): array
{
return [
'data' => $data->items(), 'meta' => ['total' => (int)$data->total(), 'page' => (int)$data->currentPage(), 'size' => (int)$data->perPage()]
];
}
/**
* @param string $status
* @param int $code
* @param string $message
* @param $data
* @param array $extend
* @return JsonResponse
*/
public static function send(string $status, int $code, string $message, $data = NULL, array $extend = []): JsonResponse
{
$resource = array_merge(
['status' => $status, 'code' => $code, 'message' => $message],
$data instanceof LengthAwarePaginator ? self::paginator($data) : compact('data'),
);
if (is_null($resource['data']) && Str::startsWith(request()?->getRequestUri(), '/mapi')) {
unset($resource['data']);
}
if (count($extend) !== 0) {
Arr::set($resource, 'meta', array_merge(Arr::get($resource, 'meta', []), $extend));
}
return response()->json($resource);
}
}