| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- namespace app\http;
- use think\exception\HttpResponseException;
- use think\Paginator;
- use think\Response;
- use think\response\Json;
- class IResponse
- {
- /**
- * 成功消息
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/3/16 11:27
- *
- * @param array $data
- * @param string $message
- * @return mixed
- */
- public static function success($data = [], $message = 'OK!')
- {
- if (gettype($data) == 'string'){
- $message = $data;
- $data = [];
- }
- return self::response(10000, $data, $message);
- }
- /**
- * 分页
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/3/16 11:28
- *
- * @param Paginator $list
- * @return mixed
- */
- public static function paginate(Paginator $list)
- {
- return self::success([
- 'total' => $list->total(),
- 'list' => $list->getCollection(),
- ]);
- }
- /**
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/3/16 11:33
- *
- * @param string $message
- * @return mixed
- */
- public static function failure($message = 'Error!')
- {
- return self::response(90001, [], $message);
- }
- /**
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @date 2020/3/16 11:20
- *
- * @param int $code
- * @param array $data
- * @param string $message
- * @return mixed
- * @throws HttpResponseException
- */
- final private static function response($code = 0, $data = [], $message = 'OK!')
- {
- throw new HttpResponseException(Response::create([
- 'REQUEST_ID' => RequestID::generate(),
- 'RUN_IP' => request()->ip(),
- 'code' => $code,
- 'message' => $message,
- 'data' => $data
- ],'json'));
- }
- }
|