mirror of
https://codeup.aliyun.com/67762337eccfc218f6110e0e/vue/fe-student.git
synced 2025-12-12 04:16:50 +08:00
146 lines
4.2 KiB
JavaScript
146 lines
4.2 KiB
JavaScript
import router from "@/router";
|
|
import { reactive, ref, toRefs, watch } from "vue";
|
|
import axios from 'axios';
|
|
import { getCookie } from "@/api/utils";
|
|
import JSONBigInt from 'json-bigint';
|
|
const JSONBigIntStr = JSONBigInt({ storeAsString: true });
|
|
export function usePage(_url, param) {
|
|
|
|
const state = reactive({
|
|
data: {},
|
|
loading: false,
|
|
total: 0,
|
|
size: 10,
|
|
current: 1,
|
|
params: { pageNo: 1, pageSize: 10, ...param }
|
|
})
|
|
|
|
watch(param, () => {
|
|
state.params = { ...state.params, ...param }
|
|
fetchData()
|
|
})
|
|
|
|
function fetchData() {
|
|
state.loading = true
|
|
request(_url, state.params).then(r => {
|
|
state.params.pageNo === 1 ? (state.data = (r.data.records || r.data.rows)) : (state.data = [...state.data, ...(r.data.records || r.data.rows)])
|
|
state.size = r.data.size
|
|
state.total = r.data.total
|
|
state.current = r.data.current
|
|
state.loading = false
|
|
})
|
|
}
|
|
|
|
fetchData()
|
|
return {
|
|
...toRefs(state),
|
|
fetchData,
|
|
};
|
|
}
|
|
|
|
export function useRequest(_url, params = {}) {
|
|
|
|
const state = reactive({
|
|
data: {},
|
|
loading: false,
|
|
|
|
})
|
|
|
|
watch(params, () => {
|
|
fetchData()
|
|
})
|
|
|
|
function fetchData() {
|
|
state.loading = true
|
|
request(_url, params).then(r => {
|
|
state.data = r.data
|
|
state.loading = false
|
|
})
|
|
}
|
|
|
|
fetchData()
|
|
return {
|
|
...toRefs(state),
|
|
fetchData,
|
|
};
|
|
}
|
|
|
|
export async function request(_url, params) {
|
|
const s = _url.split(' ')
|
|
let url = s[0]
|
|
const method = s[1]?.toLowerCase() || 'get'
|
|
if (method === 'get') {
|
|
let paramsArray = [];
|
|
//拼接参数
|
|
if (params) {
|
|
Object.keys(params).forEach(key => paramsArray.push(key + '=' + params[key]))
|
|
if (url.search(/\?/) === -1) {
|
|
url += '?' + paramsArray.join('&')
|
|
} else {
|
|
url += '&' + paramsArray.join('&')
|
|
}
|
|
}
|
|
}
|
|
const body = method !== 'get' ? params || {} : {}
|
|
return axios({
|
|
url,
|
|
method,
|
|
headers: {
|
|
'token': getCookie('token'),
|
|
...method !== 'get' ? { 'Content-Type': 'application/json' } : {}
|
|
},
|
|
baseURL: import.meta.env.VITE_BASE_API,
|
|
...method !== 'get' ? { data: JSON.stringify(body) } : {}
|
|
}).then(resp => resp.data).then(response => {
|
|
if (response.code !== 200 && response.code !== 0) {
|
|
if (response.code === 1000) {
|
|
(import.meta.env.MODE === 'development' || import.meta.env.MODE === 'test') ? router.push({ path: '/login' }) : window.open(import.meta.env.VITE_BASE_LOGIN_URL,'_top')
|
|
}
|
|
// if (import.meta.env.DEV && response.code === 1000) {
|
|
// router.push({path: '/login'})
|
|
// } else {
|
|
// window.open()
|
|
// response.showMsg && notification.open({
|
|
// message: response.showMsg,
|
|
// duration: 2,
|
|
// });
|
|
// }
|
|
throw new Error('接口异常')
|
|
}
|
|
return response
|
|
}).catch(e => {
|
|
console.log('eeeee', e)
|
|
// router.push({path: '/login'})
|
|
})
|
|
}
|
|
|
|
export async function boeRequest(_url, params) {
|
|
const s = _url.split(' ')
|
|
let url = s[0]
|
|
const method = s[1]?.toLowerCase() || 'get'
|
|
if (method === 'get') {
|
|
let paramsArray = [];
|
|
//拼接参数
|
|
if (params) {
|
|
Object.keys(params).forEach(key => paramsArray.push(key + '=' + params[key]))
|
|
if (url.search(/\?/) === -1) {
|
|
url += '?' + paramsArray.join('&')
|
|
} else {
|
|
url += '&' + paramsArray.join('&')
|
|
}
|
|
}
|
|
}
|
|
const body = method !== 'get' ? params || {} : {}
|
|
return fetch(url,{
|
|
method,
|
|
headers:{
|
|
token: getCookie('token'),
|
|
...method !== 'get' ? {'Content-Type': 'application/json'} : {}
|
|
},
|
|
...method !== 'get' ? {body: JSON.stringify(body)} : {}
|
|
}).then(res=>{
|
|
return res.text()
|
|
}).then(res=>{
|
|
return JSONBigIntStr.parse(res)
|
|
})
|
|
} |