|
|
@@ -6,7 +6,7 @@ import JwtService from "@/core/services/JwtService"
|
|
|
import configs from "@/core/config/Index"
|
|
|
import { dialog, toast } from "@/core/utils/message"
|
|
|
import { useRouter } from "vue-router"
|
|
|
-const router=useRouter()
|
|
|
+const router = useRouter()
|
|
|
export interface errorObj {
|
|
|
message: string
|
|
|
details?: string
|
|
|
@@ -14,20 +14,20 @@ export interface errorObj {
|
|
|
const DEFAULR_ERROR = {
|
|
|
error: {
|
|
|
message: "发生了错误!",
|
|
|
- details: "服务器未发送错误详细信息。"
|
|
|
+ details: "服务器未发送错误详细信息。",
|
|
|
},
|
|
|
error401: {
|
|
|
message: "您未通过身份验证!",
|
|
|
- details: "您应该经过身份验证(登录)才能执行此操作。"
|
|
|
+ details: "您应该经过身份验证(登录)才能执行此操作。",
|
|
|
},
|
|
|
error403: {
|
|
|
message: "您没有权限!",
|
|
|
- details: "您无权执行此操作。"
|
|
|
+ details: "您无权执行此操作。",
|
|
|
},
|
|
|
|
|
|
error404: {
|
|
|
message: "找不到资源!",
|
|
|
- details: "在服务器上找不到请求的资源。"
|
|
|
+ details: "在服务器上找不到请求的资源。",
|
|
|
},
|
|
|
|
|
|
logError: function (error: string) {
|
|
|
@@ -48,7 +48,7 @@ const DEFAULR_ERROR = {
|
|
|
DEFAULR_ERROR.showError(DEFAULR_ERROR.error401, {
|
|
|
beforeClose: () => {
|
|
|
router.push({ name: "auth" })
|
|
|
- }
|
|
|
+ },
|
|
|
})
|
|
|
break
|
|
|
case 403:
|
|
|
@@ -61,7 +61,7 @@ const DEFAULR_ERROR = {
|
|
|
DEFAULR_ERROR.showError(DEFAULR_ERROR.error)
|
|
|
break
|
|
|
}
|
|
|
- }
|
|
|
+ },
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -88,28 +88,29 @@ class RequestService {
|
|
|
responseType: "json",
|
|
|
// xsrfCookieName: tokenCookieName,
|
|
|
// xsrfHeaderName: tokenHeaderName,
|
|
|
- withCid:true,
|
|
|
+ withCid: true,
|
|
|
contentType: "application/json",
|
|
|
successAlert: true,
|
|
|
errorAlert: true,
|
|
|
- needProcess: true
|
|
|
+ needProcess: true,
|
|
|
}
|
|
|
RequestService.vueInstance.axios.defaults.timeout = 5000
|
|
|
- RequestService.vueInstance.axios.interceptors.request.use((config) => {
|
|
|
+ RequestService.vueInstance.axios.interceptors.request.use(
|
|
|
+ (config) => {
|
|
|
if (config.loading) {
|
|
|
//toast.loading()
|
|
|
}
|
|
|
config.headers = Object.assign({}, config.headers, {
|
|
|
Authorization: `${JwtService.getToken()}`,
|
|
|
//"Authorization": `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6IjUiLCJodHRwOi8vd3d3Lml3Ym5ldC5jb20vaWRlbnRpdHkvY2xhaW1zL3JlYWxOYW1lIjoiIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6InNoX2xleXVueXVuIiwiaHR0cDovL3d3dy5pd2JuZXQuY29tL2lkZW50aXR5L2NsYWltcy91c2VyVHlwZSI6IjQiLCJodHRwOi8vd3d3Lml3Ym5ldC5jb20vaWRlbnRpdHkvY2xhaW1zL3VzZXJBY291bnRUeXBlQ2xhaW1UeXBlIjoiMiIsImh0dHA6Ly93d3cuaXdibmV0LmNvbS9pZGVudGl0eS9jbGFpbXMvdXNlckFjb3VudE5vQ2xhaW1UeXBlIjoic2hfbGV5dW55dW4iLCJzdWIiOiI1IiwianRpIjoiYjhjOGRkMjAtMGM0ZS00Mjg1LTk1OGUtMzRlMzgwZGVjNmM4IiwiaWF0IjoxNjc3MTQxNzc3LCJuYmYiOjE2NzcxNDE3NzcsImV4cCI6MTk5MjUwMTc3NywiaXNzIjoiWUNERiIsImF1ZCI6IllDREYifQ.WsRFJccMqIOiaaCbvHxZ2pg0CaQHJaBjf_lbakU6vn0`,
|
|
|
- "Content-Type": config.contentType || "application/json;charset=UTF-8"
|
|
|
+ "Content-Type": config.contentType || "application/json;charset=UTF-8",
|
|
|
})
|
|
|
- if (config.withCid) {
|
|
|
- config.headers = Object.assign({}, config.headers, {
|
|
|
- "cid":""
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
+ if (config.withCid) {
|
|
|
+ config.headers = Object.assign({}, config.headers, {
|
|
|
+ cid: "",
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
//config.headers[tokenHeaderName] = getAntForgeryToken()
|
|
|
|
|
|
return config
|
|
|
@@ -118,9 +119,10 @@ class RequestService {
|
|
|
return Promise.reject(error)
|
|
|
}
|
|
|
)
|
|
|
- RequestService.vueInstance.axios.interceptors.response.use((res) => {
|
|
|
+ RequestService.vueInstance.axios.interceptors.response.use(
|
|
|
+ (res) => {
|
|
|
const { status, config, data } = res
|
|
|
- console.debug(`${config.url} =========>`,config.data, data.result || data)
|
|
|
+ console.debug(`${config.url} =========>`, config.data, data.result || data)
|
|
|
if (config.loading) {
|
|
|
//toast.close()
|
|
|
}
|
|
|
@@ -129,9 +131,9 @@ class RequestService {
|
|
|
}
|
|
|
if (status === 200) {
|
|
|
if (data.code == 0) {
|
|
|
- const result:any = {
|
|
|
+ const result: any = {
|
|
|
data: data.restluLis ?? data.data ?? data.rows,
|
|
|
- total: data.total
|
|
|
+ total: data.total,
|
|
|
}
|
|
|
if (config.successAlert) {
|
|
|
toast.success()
|
|
|
@@ -149,17 +151,22 @@ class RequestService {
|
|
|
} else {
|
|
|
return Promise.resolve(result.data ? result : data)
|
|
|
}
|
|
|
+ } else {
|
|
|
+ if (config.errorAlert) {
|
|
|
+ dialog.error(data.msg)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (config.errorAlert) {
|
|
|
+ DEFAULR_ERROR.handleErrorStatusCode(status)
|
|
|
+ } else if (status === 401) {
|
|
|
+ router.push({ name: configs.SIGN_ROUTE_NAME })
|
|
|
}
|
|
|
}
|
|
|
- if (config.errorAlert) {
|
|
|
- DEFAULR_ERROR.handleErrorStatusCode(status)
|
|
|
- } else if (status === 401) {
|
|
|
- router.push({ name: configs.SIGN_ROUTE_NAME })
|
|
|
- }
|
|
|
- return Promise.reject(data)
|
|
|
+ return Promise.reject(data)
|
|
|
},
|
|
|
(error: any) => {
|
|
|
- console.log("ERROR",error )
|
|
|
+ console.log("ERROR", error)
|
|
|
// console.error(`ERROR ==>${config.url} ==>`, response)
|
|
|
// if (config.loading) {
|
|
|
// //toast.close()
|
|
|
@@ -186,10 +193,7 @@ class RequestService {
|
|
|
return RequestService.vueInstance.axios.request(opt)
|
|
|
}
|
|
|
|
|
|
- public static get(
|
|
|
- url: AxiosRequestConfig | string,
|
|
|
- option?: AxiosRequestConfig
|
|
|
- ): Promise<AxiosResponse> {
|
|
|
+ public static get(url: AxiosRequestConfig | string, option?: AxiosRequestConfig): Promise<AxiosResponse> {
|
|
|
if (typeof url == "string") {
|
|
|
option = option || {}
|
|
|
option.url = url.toString()
|
|
|
@@ -201,10 +205,7 @@ class RequestService {
|
|
|
return this.request(option)
|
|
|
}
|
|
|
|
|
|
- public static post(
|
|
|
- url: AxiosRequestConfig | string,
|
|
|
- option?: AxiosRequestConfig
|
|
|
- ): Promise<AxiosResponse> {
|
|
|
+ public static post(url: AxiosRequestConfig | string, option?: AxiosRequestConfig): Promise<AxiosResponse> {
|
|
|
if (typeof url == "string") {
|
|
|
option = option || {}
|
|
|
option.url = url.toString()
|
|
|
@@ -215,17 +216,14 @@ class RequestService {
|
|
|
if (option.url && option.url.toLocaleLowerCase().indexOf("/get") > 0) {
|
|
|
option.successAlert = option.successAlert ?? false
|
|
|
}
|
|
|
- if (!option.data&&option.data==undefined) {
|
|
|
- option.data={}
|
|
|
+ if (!option.data && option.data == undefined) {
|
|
|
+ option.data = {}
|
|
|
}
|
|
|
option.method = "post"
|
|
|
return this.request(option)
|
|
|
}
|
|
|
|
|
|
- public static put(
|
|
|
- url: AxiosRequestConfig | string,
|
|
|
- option?: AxiosRequestConfig
|
|
|
- ): Promise<AxiosResponse> {
|
|
|
+ public static put(url: AxiosRequestConfig | string, option?: AxiosRequestConfig): Promise<AxiosResponse> {
|
|
|
if (url instanceof String) {
|
|
|
option = option || {}
|
|
|
option.url = url.toString()
|
|
|
@@ -236,10 +234,7 @@ class RequestService {
|
|
|
return this.request(option)
|
|
|
}
|
|
|
|
|
|
- public static delete(
|
|
|
- url: AxiosRequestConfig | string,
|
|
|
- option?: AxiosRequestConfig
|
|
|
- ): Promise<AxiosResponse> {
|
|
|
+ public static delete(url: AxiosRequestConfig | string, option?: AxiosRequestConfig): Promise<AxiosResponse> {
|
|
|
if (url instanceof String) {
|
|
|
option = option || {}
|
|
|
option.url = url.toString()
|
|
|
@@ -262,10 +257,10 @@ class RequestService {
|
|
|
option.successAlert = option.successAlert ?? false
|
|
|
}
|
|
|
const params = option.params || option.data || ""
|
|
|
- delete option.params
|
|
|
- delete option.data
|
|
|
+ delete option.params
|
|
|
+ delete option.data
|
|
|
option.method = "post"
|
|
|
- return RequestService.vueInstance.axios.post(url,params,option)
|
|
|
+ return RequestService.vueInstance.axios.post(url, params, option)
|
|
|
}
|
|
|
}
|
|
|
|