<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                返回結果處理,歸根結底 主要是有兩點 數據結構和返回的數據類型? 1、數據類型 :一般情況下,API 需要返回數據類型是JSON 2、數據結構:需要一個code來表明 返回結果狀態,一個msg用戶狀態的描述 一個data用于包含所需要的返回的數據? 明白上面兩點,后續的無非就是就行封裝,具體怎么封裝,看個人習慣。 JsonResponse.php 文件 ~~~php namespace app\common\response; use app\common\ApiErrCode; use think\facade\Log; /** * Trait JsonResponse * @package app\http\response */ trait JsonResponse { /** * 成功時返回的數據 * @param $message * @param $data */ public function jsonSuccessData($data = ''){ return $this->jsonResponse(ApiErrCode::success['code'],ApiErrCode::success['msg'],$data); } /** * 錯誤時返回的數據 * @param $code * @param $message * @param $data */ public function jsonData($code,$message,$data = ''){ Log::error([ 'code'=>$code, 'message'=>$message, 'data'=>$data ]); return $this->jsonResponse($code,$message,$data); } /** * 接口返回數據結構 * @param $code * @param $message * @param $data */ private function jsonResponse($code,$message,$data){ $content = [ 'code'=>$code, 'msg'=>$message, 'data'=>$data, 'timestamp'=>time() ]; return json($content); } } ~~~ ApiErrCode.php 是錯誤碼類? 在開發過程 ,基礎的判斷是少不了的,不能相信來自用戶的輸入,當數據或者參數異常時,需要返回指定狀態,告訴用戶出了什么問題。 但是一個項目會有很多不同的錯誤碼 ,規整到一起,方便管理。 ~~~php namespace app\common; /** * 錯誤碼類 * @user yiqiu * @email 529857614@qq.com * @date 2021/2/19 20:01 * @blog http://www.upwqy.com */ class ApiErrCode{ /** * 通用錯誤 * code < 1000 */ const success = ['code'=>200,'msg'=>'成功']; const invalid_params = ['code'=>204,'msg'=>'參數錯誤']; const unknown_err = ['code'=>500,'msg'=>'未知錯誤']; const login_invalid = ['code'=>40001,'msg'=>'請重新登錄。']; ~~~ 在需要使用的地方 ~~~php use JsonResponse; public function getList(){ return $this->jsonSuccessData($result) } ~~~ ThinkPHP6? 在異常處理類中 增加JsonResponse返回,自定義返回結構 ~~~php namespace app; use app\common\ApiErrCode; use app\common\exception\BaseException; use app\common\response\JsonResponse; use think\db\exception\DataNotFoundException; use think\db\exception\ModelNotFoundException; use think\exception\Handle; use think\exception\HttpException; use think\exception\HttpResponseException; use think\exception\ValidateException; use think\Response; use Throwable; /** * 應用異常處理類 */ class ExceptionHandle extends Handle { use JsonResponse; /** * 不需要記錄信息(日志)的異常類列表 * @var array */ protected $ignoreReport = [ HttpException::class, HttpResponseException::class, ModelNotFoundException::class, DataNotFoundException::class, ValidateException::class, ]; /** * 記錄異常信息(包括日志或者其它方式記錄) * * @access public * @param Throwable $exception * @return void * */ public function report(Throwable $exception): void { // 使用內置的方式記錄異常日志 parent::report($exception); } /** * Render an exception into an HTTP response. * @access public * @param \think\Request $request * @param Throwable $e * @return Response */ public function render($request, Throwable $e): Response { // 其他錯誤交給系統處理 // return parent::render($request, $e); // 添加自定義異常處理機制 if($e instanceof BaseException){ $code = $e->getCode(); $message = $e->getMessage(); }else{ $code = $e->getCode(); if(!$code || $code < 0){ $code = ApiErrCode::unknown_err['code']; } $message = $e->getMessage() ? : ApiErrCode::unknown_err['msg']; } return $this->jsonData($code,$message); } } ~~~ 下面來看 BaseException ,這里表示基礎異常類? 其中 ApiErrCode 是定義的 錯誤碼類 可以去[http://www.upwqy.com/details/216.html](http://www.upwqy.com/details/216.html)查看 ~~~php namespace app\common\exception; use app\common\ApiErrCode; use think\Exception; /** * 基礎異常 * @user yiqiu * @email 529857614@qq.com * @date 2021/2/19 20:45 * @blog http://www.upwqy.com */ class BaseException extends \Exception { protected $code = ApiErrCode::unknown_err['code']; protected $message = ApiErrCode::unknown_err['msg']; public function __construct($params = []) { if(is_array($params) ){ if(isset($params['code']) && $params['code']){ $this->code = $params['code']; } if(isset($params['msg']) && $params['msg']){ $this->message = $params['msg']; } }else if(is_string($params)){ $this->message = $params; } parent::__construct($this->message, $this->code); } } ~~~ 然后我們可以自定義一些異常類 ,比如下面的 ParameterException.php 表示參數異常時的處理 ~~~php namespace app\common\exception; use app\common\ApiErrCode; class ParameterException extends BaseException { protected $code = ApiErrCode::invalid_params['code']; protected $message = ApiErrCode::invalid_params['msg']; } ~~~ 實例: ~~~php $user = User::where('id',1)->find(); if(!$user){ throw new ParameterException('用戶不存在'); } ~~~ 當需要指定的異常,直接 使用? throw new ParameterException('用戶不存在'); 即可,返回結果如下,并且可以在任何地方使用 ~~~javascript { "code": 204, "msg": "用戶不存在", "data": "", "timestamp": 1622604524 } ~~~ THinkPHP5 在tp5框架中,我們需要手動創建應用異常處理類。ExceptionHandler.php 并且在配置中 修改配置 ~~~php 'exception_handle' => 'api\lib\exception\ExceptionHandler', ~~~ ThinkPHP5 中 應用異常處理類 需要手動創建? 全局異常捕獲 可以參考[http://www.upwqy.com/details/273.html](http://www.upwqy.com/details/273.html)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看