feat: request 添加关闭code校验逻辑

This commit is contained in:
bac-joker 2021-04-26 17:34:01 +08:00
parent 463291b08a
commit c6c133ca5a
4 changed files with 22 additions and 9 deletions

View File

@ -51,6 +51,8 @@ export const request = {
responseDataAdaptor: (data) => { responseDataAdaptor: (data) => {
}, },
// 关闭 response data 校验(只判断 xhr status
closeResDataCheck: false,
// 请求拦截器 // 请求拦截器
requestInterceptors: [], requestInterceptors: [],
// 相应拦截器 // 相应拦截器

View File

@ -25,9 +25,10 @@ export default async (ctx, next) => {
const { const {
error, error,
errorHandler = {}, errorHandler = {},
response response,
config
} = ctx; } = ctx;
if (response && isObject(response.data)) { if (!config.closeResDataCheck && response && isObject(response.data)) {
const code = response.data.code; const code = response.data.code;
if (code !== '0') { if (code !== '0') {
handleAbnormalCode(errorHandler, code, response); handleAbnormalCode(errorHandler, code, response);

View File

@ -1,4 +1,14 @@
export const request = { export const request = {
responseInterceptors: [(response) => {
console.log(response);
if (response.data !== '0') {
return Promise.reject({
response
});
}
return response;
}],
closeResDataCheck: true,
errorHandler: { errorHandler: {
111(responseData) { 111(responseData) {
console.log(responseData); console.log(responseData);
@ -7,7 +17,7 @@ export const request = {
console.log('to 404 page'); console.log('to 404 page');
}, },
default(error) { default(error) {
console.log(error.response.data); console.log(error);
} }
} }
}; };

View File

@ -2,8 +2,6 @@
<div class="onepiece"> <div class="onepiece">
fes & 拉夫德鲁<br /> fes & 拉夫德鲁<br />
<fes-icon :spin="true" class="one-icon" type="smile" @click="clickIcon" /> <fes-icon :spin="true" class="one-icon" type="smile" @click="clickIcon" />
<div v-if="loading" class="loading">loading</div>
<div v-else class="data">{{data}}</div>
</div> </div>
</template> </template>
<config> <config>
@ -14,7 +12,7 @@
</config> </config>
<script> <script>
import { ref, onMounted } from 'vue'; import { ref, onMounted } from 'vue';
import { useRouter, useRequest } from '@fesjs/fes'; import { useRouter, request } from '@fesjs/fes';
export default { export default {
setup() { setup() {
@ -28,12 +26,14 @@ export default {
const clickIcon = () => { const clickIcon = () => {
console.log('click Icon'); console.log('click Icon');
}; };
const { loading, data } = useRequest('/api', null, { request('/api', null, {
dataField: false dataField: false
}).then((res) => {
console.log(res);
}).catch((err) => {
console.error(err);
}); });
return { return {
loading,
data,
fes, fes,
rotate, rotate,
clickIcon clickIcon