global.js
3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import rules from "./rules"
import request from "@/api"
import * as echarts from "echarts"
export default {
install (Vue, options) {
Vue.prototype.$echarts = echarts
Vue.prototype.$request = request
Vue.prototype.$rules = rules
/** 时间字符串
* @method $getDateDiff
* @param timespan
*/
Vue.prototype.$getDateDiff = function (timespan) {
var dateTime = new Date(timespan)
var year = dateTime.getFullYear()
var month = (dateTime.getMonth() + 1) < 10 ? "0" + (dateTime.getMonth() + 1) : (dateTime.getMonth() + 1)
var day = dateTime.getDate() < 10 ? "0" + dateTime.getDate() : dateTime.getDate()
var hour = dateTime.getHours() < 10 ? "0" + dateTime.getHours() : dateTime.getHours()
var minute = dateTime.getMinutes() < 10 ? "0" + dateTime.getMinutes() : dateTime.getMinutes()
var seconds = dateTime.getSeconds() < 10 ? "0" + dateTime.getSeconds() : dateTime.getSeconds()
var now = new Date()
var nowNew = now.getTime()
var milliseconds = 0
var timeSpanStr
milliseconds = nowNew - dateTime
if (milliseconds <= 1000 * 60 * 1) {
timeSpanStr = "刚刚"
} else if (1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) {
timeSpanStr = Math.round((milliseconds / (1000 * 60))) + "分钟前"
} else if (1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) {
timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60)) + "小时前"
} else if (1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 3) {
timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + "天前"
} else if (milliseconds > 1000 * 60 * 60 * 24 * 3 && year === now.getFullYear()) {
timeSpanStr = month + "-" + day + " " + hour + ":" + minute + ":" + seconds
} else {
timeSpanStr = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + seconds
}
return timeSpanStr
}
/** 当前地址ip
* @method $path
* @param {}
*/
Vue.prototype.$path = process.env.API_HOST
/** 传入路径转换成完整连接
* @method $getPath
* @param {url: 路径}
*/
Vue.prototype.$getPath = function (url) {
const base = process.env.API_HOST
if (/^http/.test(url)) return url
return base + url
}
/** 是否开发模式
* @method $env
* @param {}
*/
Vue.prototype.$env = process.env.NODE_ENV
/** 导出,下载处理文件流
* @method $exportClick
* @param {res:文件流,name : 下载文件名}
*/
Vue.prototype.$exportClick = function (res, name = "下载.zip") {
const content = res
const blob = new Blob([content])
const fileName = name
if ("download" in document.createElement("a")) { // 非IE下载
const elink = document.createElement("a")
elink.download = fileName
elink.style.display = "none"
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href) // 释放URL 对象
document.body.removeChild(elink)
} else { // IE10+下载
navigator.msSaveBlob(blob, fileName)
}
}
/** 当res.code === 200 时
* @method $restBack
* @param res
* @param fn
* @param message
* @param type
*/
Vue.prototype.$restBack = function (res, fn = () => {}, message, type = "success") {
if (res.code === 200) {
this.$message({
message: message || res.message,
type: type
})
fn()
}
}
}
}