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) => {
},
// 关闭 response data 校验(只判断 xhr status
closeResDataCheck: false,
// 请求拦截器
requestInterceptors: [],
// 相应拦截器

View File

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

View File

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

View File

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