gin-vue-admin 2.0代码重构
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
module.exports = file => () => {
|
||||
return import ('@/' + file)
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
const _import = require('./_import') //获取组件的方法
|
||||
export const asyncRouterHandle = (asyncRouter) => {
|
||||
asyncRouter.map(item => {
|
||||
if (item.component) {
|
||||
item.component = _import(item.component)
|
||||
} else {
|
||||
delete item['component']
|
||||
}
|
||||
if (item.children) {
|
||||
asyncRouterHandle(item.children)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
const install = (Vue) => {
|
||||
const Bus = new Vue({
|
||||
methods: {
|
||||
emit(event, ...args) {
|
||||
this.$emit(event, ...args)
|
||||
},
|
||||
on(event, cb) {
|
||||
this.$on(event, cb)
|
||||
},
|
||||
off(event, cb) {
|
||||
this.$off(event, cb)
|
||||
}
|
||||
},
|
||||
})
|
||||
Vue.prototype.$bus = Bus
|
||||
}
|
||||
|
||||
export default install
|
||||
@@ -0,0 +1,30 @@
|
||||
// 对Date的扩展,将 Date 转化为指定格式的String
|
||||
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
|
||||
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
|
||||
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
|
||||
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
|
||||
Date.prototype.Format = function(fmt) {
|
||||
var o = {
|
||||
"M+": this.getMonth() + 1, //月份
|
||||
"d+": this.getDate(), //日
|
||||
"h+": this.getHours(), //小时
|
||||
"m+": this.getMinutes(), //分
|
||||
"s+": this.getSeconds(), //秒
|
||||
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
|
||||
"S": this.getMilliseconds() //毫秒
|
||||
};
|
||||
if (/(y+)/.test(fmt))
|
||||
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
|
||||
for (var k in o)
|
||||
if (new RegExp("(" + k + ")").test(fmt))
|
||||
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
|
||||
return fmt;
|
||||
}
|
||||
|
||||
export function formatTimeToStr(times, pattern) {
|
||||
var d = new Date(times).Format("yyyy-MM-dd hh:mm:ss");
|
||||
if (pattern) {
|
||||
d = new Date(times).Format(pattern);
|
||||
}
|
||||
return d.toLocaleString();
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
export const downloadImage = (imgsrc, name) => { //下载图片地址和图片名
|
||||
var image = new Image();
|
||||
image.setAttribute("crossOrigin", "anonymous");
|
||||
image.onload = function() {
|
||||
var canvas = document.createElement("canvas");
|
||||
canvas.width = image.width;
|
||||
canvas.height = image.height;
|
||||
var context = canvas.getContext("2d");
|
||||
context.drawImage(image, 0, 0, image.width, image.height);
|
||||
var url = canvas.toDataURL("image/png"); //得到图片的base64编码数据
|
||||
|
||||
var a = document.createElement("a"); // 生成一个a元素
|
||||
var event = new MouseEvent("click"); // 创建一个单击事件
|
||||
a.download = name || "photo"; // 设置图片名称
|
||||
a.href = url; // 将生成的URL设置为a.href属性
|
||||
a.dispatchEvent(event); // 触发a的单击事件
|
||||
};
|
||||
image.src = imgsrc;
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
import axios from 'axios'; // 引入axios
|
||||
import { Message, Loading } from 'element-ui';
|
||||
import { store } from '@/store/index'
|
||||
const service = axios.create({
|
||||
baseURL: process.env.VUE_APP_BASE_API,
|
||||
timeout: 99999
|
||||
})
|
||||
let acitveAxios = 0
|
||||
let loadingInstance
|
||||
let timer
|
||||
const showLoading = () => {
|
||||
acitveAxios++
|
||||
if (timer) {
|
||||
clearTimeout(timer)
|
||||
}
|
||||
timer = setTimeout(() => {
|
||||
if (acitveAxios > 0) {
|
||||
loadingInstance = Loading.service({ fullscreen: true })
|
||||
}
|
||||
}, 400);
|
||||
}
|
||||
|
||||
const closeLoading = () => {
|
||||
acitveAxios--
|
||||
if (acitveAxios <= 0) {
|
||||
clearTimeout(timer)
|
||||
loadingInstance && loadingInstance.close()
|
||||
}
|
||||
}
|
||||
//http request 拦截器
|
||||
service.interceptors.request.use(
|
||||
config => {
|
||||
showLoading()
|
||||
const token = store.getters['user/token']
|
||||
config.data = JSON.stringify(config.data);
|
||||
config.headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'x-token': token
|
||||
}
|
||||
return config;
|
||||
},
|
||||
error => {
|
||||
closeLoading()
|
||||
Message({
|
||||
showClose: true,
|
||||
message: error,
|
||||
type: 'error'
|
||||
})
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
//http response 拦截器
|
||||
service.interceptors.response.use(
|
||||
response => {
|
||||
closeLoading()
|
||||
if (response.data.success || response.headers.success === "true") {
|
||||
return response.data
|
||||
} else {
|
||||
Message({
|
||||
showClose: true,
|
||||
message: response.data.msg,
|
||||
type: 'error',
|
||||
onClose: () => {
|
||||
if (response.data.data && response.data.data.reload) {
|
||||
store.commit('user/LoginOut')
|
||||
}
|
||||
}
|
||||
})
|
||||
return Promise.reject(response.data.msg)
|
||||
}
|
||||
},
|
||||
error => {
|
||||
closeLoading()
|
||||
Message({
|
||||
showClose: true,
|
||||
message: error,
|
||||
type: 'error'
|
||||
})
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
export default service
|
||||
@@ -0,0 +1,7 @@
|
||||
export const toUpperCase = (str) => {
|
||||
if (str[0]) {
|
||||
return str.replace(str[0], str[0].toUpperCase())
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user