From d952aa900832e2bff448991c19808f30d9ba53df Mon Sep 17 00:00:00 2001 From: bac-joker Date: Mon, 12 Apr 2021 10:49:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=9D=9E=200=20code=20=E4=BD=9C?= =?UTF-8?q?=E4=B8=BA=E5=BC=82=E5=B8=B8=E5=93=8D=E5=BA=94=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/zh/reference/plugin/plugins/request.md | 9 ++++-- .../create-fes-app/templates/app/h5/.fes.js | 2 +- .../create-fes-app/templates/app/pc/.fes.js | 2 +- .../src/template/resErrorProcess.js | 28 ++++++++++++++----- packages/fes-template-h5/.fes.js | 2 +- packages/fes-template-h5/src/app.js | 5 +++- 6 files changed, 34 insertions(+), 14 deletions(-) diff --git a/docs/zh/reference/plugin/plugins/request.md b/docs/zh/reference/plugin/plugins/request.md index 08c23e32..91306232 100644 --- a/docs/zh/reference/plugin/plugins/request.md +++ b/docs/zh/reference/plugin/plugins/request.md @@ -59,11 +59,14 @@ export const request = { // 内部以 reponse.data.code === '0' 判断请求是否成功 // 若使用其他字段判断,可以使用 responseDataAdaptor 对响应数据进行格式 errorHandler: { - 11199: (response) => { + 11199(response) { + // 特殊 code 处理逻辑 + }, + 404(error) { }, - 404: (error) => { - + commonAbnormalCodeHandler(response) { + // 非 0,且非 11199(前面已经配置过 11199) code 其他异常code处理逻辑 } }, // 其他 axios 配置 diff --git a/packages/create-fes-app/templates/app/h5/.fes.js b/packages/create-fes-app/templates/app/h5/.fes.js index ad88fd84..72fd6ff2 100644 --- a/packages/create-fes-app/templates/app/h5/.fes.js +++ b/packages/create-fes-app/templates/app/h5/.fes.js @@ -31,6 +31,6 @@ export default { }) ], devServer: { - port: 8080 + port: 8000 } }; diff --git a/packages/create-fes-app/templates/app/pc/.fes.js b/packages/create-fes-app/templates/app/pc/.fes.js index 03ee6f39..a0883b42 100644 --- a/packages/create-fes-app/templates/app/pc/.fes.js +++ b/packages/create-fes-app/templates/app/pc/.fes.js @@ -26,7 +26,7 @@ export default { legacy: true }, devServer: { - port: 8080 + port: 8000 }, enums: { status: [['0', '无效的'], ['1', '有效的']] diff --git a/packages/fes-plugin-request/src/template/resErrorProcess.js b/packages/fes-plugin-request/src/template/resErrorProcess.js index 50961d49..067034b3 100644 --- a/packages/fes-plugin-request/src/template/resErrorProcess.js +++ b/packages/fes-plugin-request/src/template/resErrorProcess.js @@ -1,12 +1,26 @@ import { isObject } from './helpers'; -export default async ({ - error, - errorHandler = {}, - response -}, next) => { - if (response && isObject(response.data) && response.data.code && response.data.code !== '0') { - errorHandler[response.data.code] && errorHandler[response.data.code](response); +function handleAbnormalCode(errorHandler = {}, code, response) { + if (errorHandler[code]) { + errorHandler[code](response); + } else if (errorHandler.commonAbnormalCodeHandler) { + // 处理其他异常 + errorHandler.commonAbnormalCodeHandler(response); + } +} + +export default async (ctx, next) => { + const { + error, + errorHandler = {}, + response + } = ctx; + if (response && isObject(response.data)) { + const code = response.data.code; + if (code !== '0') { + handleAbnormalCode(errorHandler, code, response); + ctx.error = response; // code 不为零进入 reject + } } else if (error) { if (error.type) { errorHandler[error.type] && errorHandler[error.type](error); diff --git a/packages/fes-template-h5/.fes.js b/packages/fes-template-h5/.fes.js index beb486d8..cc9f10b2 100644 --- a/packages/fes-template-h5/.fes.js +++ b/packages/fes-template-h5/.fes.js @@ -33,6 +33,6 @@ export default { }) ], devServer: { - port: 8080 + port: 8000 } }; diff --git a/packages/fes-template-h5/src/app.js b/packages/fes-template-h5/src/app.js index 140e25e5..db279236 100644 --- a/packages/fes-template-h5/src/app.js +++ b/packages/fes-template-h5/src/app.js @@ -1,7 +1,10 @@ export const request = { - errorConfig: { + errorHandler: { 404() { console.log('to 404 page'); + }, + commonAbnormalCodeHandler(res) { + console.log(res.data.code); } } };