mirror of
https://github.com/dataease/dataease.git
synced 2026-05-23 22:08:34 +08:00
feat: 初始化
This commit is contained in:
@@ -1,9 +0,0 @@
|
||||
import message from "@/plugins/message";
|
||||
import request from "@/plugins/request";
|
||||
|
||||
export default {
|
||||
install(Vue) {
|
||||
Vue.use(message);
|
||||
Vue.use(request);
|
||||
}
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
import {MessageBox, Message} from 'element-ui';
|
||||
import i18n from "@/i18n";
|
||||
|
||||
export const $alert = (message, callback, options) => {
|
||||
let title = i18n.t("common.message_box.alert");
|
||||
MessageBox.alert(message, title, options).then(() => {
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
export const $confirm = (message, callback, options = {}) => {
|
||||
let defaultOptions = {
|
||||
confirmButtonText: i18n.t("common.button.ok"),
|
||||
cancelButtonText: i18n.t("common.button.cancel"),
|
||||
type: 'warning',
|
||||
...options
|
||||
}
|
||||
let title = i18n.t("common.message_box.confirm");
|
||||
MessageBox.confirm(message, title, defaultOptions).then(() => {
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
export const $success = (message, duration) => {
|
||||
Message.success({
|
||||
message: message,
|
||||
type: "success",
|
||||
showClose: true,
|
||||
duration: duration || 1500
|
||||
})
|
||||
}
|
||||
|
||||
export const $info = (message, duration) => {
|
||||
Message.info({
|
||||
message: message,
|
||||
type: "info",
|
||||
showClose: true,
|
||||
duration: duration || 3000
|
||||
})
|
||||
}
|
||||
|
||||
export const $warning = (message, duration) => {
|
||||
Message.warning({
|
||||
message: message,
|
||||
type: "warning",
|
||||
showClose: true,
|
||||
duration: duration || 5000
|
||||
})
|
||||
}
|
||||
|
||||
export const $error = (message, duration) => {
|
||||
Message.error({
|
||||
message: message,
|
||||
type: "error",
|
||||
showClose: true,
|
||||
duration: duration || 10000
|
||||
})
|
||||
}
|
||||
|
||||
export default {
|
||||
install(Vue) {
|
||||
// 使用$$前缀,避免与Element UI的冲突
|
||||
Vue.prototype.$$confirm = $confirm;
|
||||
Vue.prototype.$$alert = $alert;
|
||||
|
||||
Vue.prototype.$success = $success;
|
||||
Vue.prototype.$info = $info;
|
||||
Vue.prototype.$warning = $warning;
|
||||
Vue.prototype.$error = $error;
|
||||
}
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
import axios from 'axios'
|
||||
import {$alert, $error} from "./message"
|
||||
import store from '@/store'
|
||||
import i18n from "@/i18n";
|
||||
import {TokenKey, getToken} from '@/utils/token'
|
||||
|
||||
const instance = axios.create({
|
||||
baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
|
||||
withCredentials: true,
|
||||
timeout: 60000 // request timeout, default 1 min
|
||||
})
|
||||
|
||||
// 每次请求加上Token。如果没用使用Token,删除这个拦截器
|
||||
instance.interceptors.request.use(
|
||||
config => {
|
||||
if (store.getters.token) {
|
||||
config.headers[TokenKey] = getToken()
|
||||
}
|
||||
return config
|
||||
},
|
||||
error => {
|
||||
console.log(error) // for debug
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
const checkAuth = response => {
|
||||
// 请根据实际需求修改
|
||||
if (response.headers["authentication-status"] === "invalid" || response.status === 401) {
|
||||
let message = i18n.t('login.expires');
|
||||
$alert(message, () => {
|
||||
store.dispatch('user/logout').then(() => {
|
||||
location.reload()
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const checkPermission = response => {
|
||||
// 请根据实际需求修改
|
||||
if (response.status === 403) {
|
||||
location.href = "/403";
|
||||
}
|
||||
}
|
||||
|
||||
// 请根据实际需求修改
|
||||
instance.interceptors.response.use(response => {
|
||||
checkAuth(response);
|
||||
return response;
|
||||
}, error => {
|
||||
let msg;
|
||||
if (error.response) {
|
||||
checkAuth(error.response);
|
||||
checkPermission(error.response);
|
||||
msg = error.response.data.message || error.response.data;
|
||||
} else {
|
||||
console.log('error: ' + error) // for debug
|
||||
msg = error.message;
|
||||
}
|
||||
$error(msg)
|
||||
return Promise.reject(error);
|
||||
});
|
||||
|
||||
export const request = instance
|
||||
|
||||
/* 简化请求方法,统一处理返回结果,并增加loading处理,这里以{success,data,message}格式的返回值为例,具体项目根据实际需求修改 */
|
||||
const promise = (request, loading = {}) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
loading.status = true;
|
||||
request.then(response => {
|
||||
if (response.data.success) {
|
||||
resolve(response.data);
|
||||
} else {
|
||||
reject(response.data)
|
||||
}
|
||||
loading.status = false;
|
||||
}).catch(error => {
|
||||
reject(error)
|
||||
loading.status = false;
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export const get = (url, data, loading) => {
|
||||
return promise(request({url: url, method: "get", params: data}), loading)
|
||||
};
|
||||
|
||||
export const post = (url, data, loading) => {
|
||||
return promise(request({url: url, method: "post", data}), loading)
|
||||
};
|
||||
|
||||
export const put = (url, data, loading) => {
|
||||
return promise(request({url: url, method: "put", data}), loading)
|
||||
};
|
||||
|
||||
export const del = (url, loading) => {
|
||||
return promise(request({url: url, method: "delete"}), loading)
|
||||
};
|
||||
|
||||
export default {
|
||||
install(Vue) {
|
||||
Vue.prototype.$get = get;
|
||||
Vue.prototype.$post = post;
|
||||
Vue.prototype.$put = put;
|
||||
Vue.prototype.$delete = del;
|
||||
Vue.prototype.$request = request;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user