mirror of
https://github.com/sunniejs/vue-h5-template.git
synced 2025-11-19 14:06:48 +08:00
40 lines
971 B
JavaScript
40 lines
971 B
JavaScript
import { createFetch } from '@vueuse/core';
|
|
import { showNotify } from 'vant';
|
|
|
|
const useFetchApi = createFetch({
|
|
baseUrl: '',
|
|
options: {
|
|
async beforeFetch({ options }) {
|
|
const myToken = 'token';
|
|
options.headers = {
|
|
...options.headers,
|
|
Authorization: `Bearer ${myToken}`,
|
|
};
|
|
return { options };
|
|
},
|
|
afterFetch(ctx) {
|
|
const { data, response } = ctx;
|
|
if (response.status >= 200 && response.status < 300) {
|
|
try {
|
|
const jsonObj = data;
|
|
if (jsonObj.code != 200) {
|
|
showNotify({ type: 'danger', message: jsonObj.message || 'Error' });
|
|
}
|
|
|
|
ctx.data = jsonObj.data;
|
|
} catch (error) {
|
|
console.error(error);
|
|
ctx.data = null;
|
|
}
|
|
} else {
|
|
showNotify({ type: 'danger', message: response.statusText || 'Error' });
|
|
ctx.data = null;
|
|
}
|
|
|
|
return ctx;
|
|
},
|
|
},
|
|
});
|
|
|
|
export default useFetchApi;
|