1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace crmeb\utils;
- use think\Response;
- /**
- * Json输出类
- * Class Json
- * @package crmeb\utils
- */
- class Json
- {
- private $code = 200;
- private $header = [];
- public function code(int $code): self
- {
- $this->code = $code;
- return $this;
- }
- public function header(array $header): self
- {
- $this->header = $header;
- return $this;
- }
- public function make(int $status, string $msg, ?array $data = null, ?array $replace = []): Response
- {
- $res = compact('status', 'msg');
- if (!is_null($data))
- $res['data'] = $data;
- if (is_numeric($res['msg'])) {
- $res['code'] = $res['msg'];
- $res['msg'] = getLang($res['msg'], $replace);
- }
- return Response::create($res, 'json', $this->code)->header($this->header);
- }
- public function success($msg = 'success', ?array $data = null, ?array $replace = []): Response
- {
- if (is_array($msg)) {
- $data = $msg;
- $msg = 'success';
- }
- return $this->make(200, $msg, $data, $replace);
- }
- public function fail($msg = 'fail', ?array $data = null, ?array $replace = []): Response
- {
- if (is_array($msg)) {
- $data = $msg;
- $msg = 'fail';
- }
- return $this->make(400, $msg, $data, $replace);
- }
- public function status($status, $msg, $result = [])
- {
- $status = strtoupper($status);
- if (is_array($msg)) {
- $result = $msg;
- $msg = 'success';
- }
- return $this->success($msg, compact('status', 'result'));
- }
- }
|