接口初始化 第一版

This commit is contained in:
yuping
2022-12-11 23:56:13 +08:00
parent b98e1f29be
commit 1693c2db1d
5 changed files with 111 additions and 36 deletions

View File

@@ -1,5 +1,5 @@
import router from "@/router";
import { ref, watch } from "vue";
import {ref, watch} from "vue";
import axios from 'axios';
export function useRequest(_url, params = {}) {
@@ -30,7 +30,7 @@ export function useRequest(_url, params = {}) {
export async function request(_url, params) {
const s = _url.split(' ')
let url = s[0]
const method = s[1] || 'get'
const method = s[1]?.toLowerCase() || 'get'
if (method === 'get') {
let paramsArray = [];
//拼接参数
@@ -49,22 +49,23 @@ export async function request(_url, params) {
method,
headers: {
'X-Token': localStorage.getItem('token'),
...method !== 'get' ? { 'Content-Type': 'application/json' } : {}
...method !== 'get' ? {'Content-Type': 'application/json'} : {}
},
baseURL: '',
...method !== 'get' ? { data: JSON.stringify(body) } : {}
...method !== 'get' ? {data: JSON.stringify(body)} : {}
}).then(resp => resp.data).then(response => {
if (response.code !== 200 && response.code !== 0) {
if (response.code === 3 || response.code === 4 || response.code === 100) {
// router.push({path: '/login'})
return
console.log(1111111111)
console.log(response.code)
if (import.meta.env.DEV && response.code === 1000) {
router.push({path: '/login'})
} else {
// response.showMsg && notification.open({
// message: response.showMsg,
// duration: 2,
// });
return
// response.showMsg && notification.open({
// message: response.showMsg,
// duration: 2,
// });
}
throw new Error('接口异常')
}
return response
}).catch(e => {