|
|
|
@ -1,5 +1,6 @@ |
|
|
|
import request, { showFullScreenLoading, tryHideFullScreenLoading } from "@/api/request"; |
|
|
|
import request from "@/api/request"; |
|
|
|
import store from "../store/index"; |
|
|
|
import { Message, Loading } from "element-ui"; |
|
|
|
|
|
|
|
const sysConfig = getSysConfig() |
|
|
|
|
|
|
|
@ -13,8 +14,33 @@ function getSysConfig(){ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function startLoading() { |
|
|
|
let loading = Loading.service({ |
|
|
|
lock: true, |
|
|
|
text: "正在加载中,请稍后", |
|
|
|
spinner: "el-icon-loading", |
|
|
|
background: "rgba(0, 0, 0, 0)", |
|
|
|
customClass: "boxStyle" |
|
|
|
}); |
|
|
|
|
|
|
|
// 调用接口 5秒 没结束,关闭锁屏
|
|
|
|
setTimeout(() => { |
|
|
|
endLoading(loading) |
|
|
|
}, 5000); |
|
|
|
return loading |
|
|
|
} |
|
|
|
|
|
|
|
function endLoading(loading) { |
|
|
|
try { |
|
|
|
loading.close() |
|
|
|
} catch (error) { |
|
|
|
console.error('关闭锁屏',error) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
export async function getapi(url, params = {}, config) { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
let loading = startLoading() |
|
|
|
request |
|
|
|
.get(`${sysConfig.apiurl}${url}`, { |
|
|
|
params: params, |
|
|
|
@ -26,13 +52,16 @@ export async function getapi(url, params = {}, config) { |
|
|
|
.catch((err) => { |
|
|
|
reject(err.data); |
|
|
|
}) |
|
|
|
.finally(() => {}); |
|
|
|
.finally(() => { |
|
|
|
endLoading(loading) |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
//axios封装post
|
|
|
|
export async function postapi(url, paramsdata = {}, config) { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
let loading = startLoading() |
|
|
|
request |
|
|
|
.post(`${sysConfig.apiurl}${url}`, paramsdata, config) |
|
|
|
.then((res) => { |
|
|
|
@ -41,12 +70,15 @@ export async function postapi(url, paramsdata = {}, config) { |
|
|
|
.catch((err) => { |
|
|
|
reject(err.data); |
|
|
|
}) |
|
|
|
.finally(() => {}); |
|
|
|
.finally(() => { |
|
|
|
endLoading(loading) |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
//axios封装deleteapi
|
|
|
|
export async function deletapi(url, params = {}, config) { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
let loading = startLoading() |
|
|
|
request |
|
|
|
.delete(`${sysConfig.apiurl}${url}`, { |
|
|
|
params: params, |
|
|
|
@ -58,12 +90,15 @@ export async function deletapi(url, params = {}, config) { |
|
|
|
.catch((err) => { |
|
|
|
reject(err.data); |
|
|
|
}) |
|
|
|
.finally(() => {}); |
|
|
|
.finally(() => { |
|
|
|
endLoading(loading) |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
//axios封装Put方法
|
|
|
|
export async function putapi(url, params = {}, config) { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
let loading = startLoading() |
|
|
|
request |
|
|
|
.put(`${sysConfig.apiurl}${url}`, params, config) |
|
|
|
.then((res) => { |
|
|
|
@ -72,14 +107,16 @@ export async function putapi(url, params = {}, config) { |
|
|
|
.catch((err) => { |
|
|
|
reject(err.data); |
|
|
|
}) |
|
|
|
.finally(() => {}); |
|
|
|
.finally(() => { |
|
|
|
endLoading(loading) |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
// 浏览器端 fetch 流式读取(用于增量处理服务器 chunked/text-stream 响应)
|
|
|
|
export async function fetchStream(url, params = {}, onChunk = (chunk) => {}) { |
|
|
|
const fullUrl = `${sysConfig.apiurl}${url}`; |
|
|
|
showFullScreenLoading(); |
|
|
|
let loading = startLoading() |
|
|
|
try { |
|
|
|
const token = window.sessionStorage.getItem('token'); |
|
|
|
let tokentype = window.sessionStorage.getItem("tokentype"); |
|
|
|
@ -104,7 +141,7 @@ export async function fetchStream(url, params = {}, onChunk = (chunk) => {}) { |
|
|
|
const text = await resp.text(); |
|
|
|
try { onChunk(text); } catch (e) { /* ignore */ } |
|
|
|
// 已经拿到首个数据,先隐藏 loading
|
|
|
|
tryHideFullScreenLoading(); |
|
|
|
endLoading(loading) |
|
|
|
return text; |
|
|
|
} |
|
|
|
|
|
|
|
@ -120,7 +157,7 @@ export async function fetchStream(url, params = {}, onChunk = (chunk) => {}) { |
|
|
|
// 在收到第一个 chunk 时立即隐藏 loading
|
|
|
|
if (!firstChunkSeen) { |
|
|
|
firstChunkSeen = true; |
|
|
|
tryHideFullScreenLoading(); |
|
|
|
endLoading(loading) |
|
|
|
} |
|
|
|
try { onChunk(chunk); } catch (e) { /* 忽略回调内部错误 */ } |
|
|
|
} |
|
|
|
@ -128,6 +165,6 @@ export async function fetchStream(url, params = {}, onChunk = (chunk) => {}) { |
|
|
|
} catch (err) { |
|
|
|
throw err; |
|
|
|
} finally { |
|
|
|
tryHideFullScreenLoading(); |
|
|
|
endLoading(loading) |
|
|
|
} |
|
|
|
} |