util.js
3.83 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import Vue from "vue";
import { Toast, Dialog } from "vant";
/**
* 日期格式化
* let time = new Date().format('yyyy-MM-dd hh:mm:ss')
*/
Date.prototype.format = function (fmt) {
let 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 (let 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;
};
const WindowEvent = (type, callBack) => {
if (window.attachEvent) {
window.attachEvent(`on${type}`, function () {
callBack.call(window);
});
} else {
window.addEventListener(type, callBack, false);
}
};
const throttle = (fn, gapTime) => {
if (gapTime == null || gapTime == undefined) {
gapTime = 1500;
}
let _lastTime = null;
return function () {
let _nowTime = +new Date();
if (_nowTime - _lastTime > gapTime || !_lastTime) {
fn.apply(this, arguments);
_lastTime = _nowTime;
}
};
};
/**防止重复点击 */
// const noScroll = (e) => { e.preventDefault() }
const showLoading = (title = "加载中...") => {
Toast({
type: "loading",
message: title,
forbidClick: true,
duration: 0,
});
};
/**监听页面滚动事件 */
const onPageScroll = (callBack) => {
WindowEvent("scroll", ($event) => {
return callBack($event);
});
};
/**num:要转换的数值,fixed:精确几位小数 */
const decimal = (num, fixed) => {
let pow = Math.pow(10, fixed);
return Math.round(num * pow) / pow;
};
const isMobileDevice = () => {
//判断当前设备是否为移动端
const ua = navigator.userAgent.toLowerCase();
const t1 =
/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(ua);
const t2 = !ua.match("iphone") && navigator.maxTouchPoints > 1;
return t1 || t2;
};
const showToast = (title = "") => {
Toast({
duration: 1500,
message: title,
forbidClick: true,
});
};
/**触摸样式 */
const touchstart = (e) => {
e.target.classList.add("btn-on-leave");
};
const touchend = (e) => {
e.target.classList.remove("btn-on-leave");
};
/**dialog弹框 */
const dialog = (message, callBack) => {
Dialog.alert({
message,
}).then(() => {
callBack();
});
};
/**
* 时间戳转化为年 月 日 时 分 秒
* ts: 传入时间戳
* format:返回格式,支持自定义,但参数必须与formateArr里保持一致
*/
const formatNumber = (n) => {
n = n.toString();
return n[1] ? n : "0" + n;
};
const hideLoading = () => {
Toast.clear();
};
const tsFormatTime = (timestamp, format) => {
format = format || "Y-M-D h:m:s";
const formateArr = ["Y", "M", "D", "h", "m", "s"];
let returnArr = [];
let date = new Date(timestamp);
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
let hour = date.getHours();
let minute = date.getMinutes();
let second = date.getSeconds();
returnArr.push(year, month, day, hour, minute, second);
returnArr = returnArr.map(formatNumber);
for (var i in returnArr) {
format = format.replace(formateArr[i], returnArr[i]);
}
return format;
};
const getLocalStorage = (key) => {
return JSON.parse(window.localStorage.getItem(key));
};
const utils = {
getLocalStorage,
onPageScroll,
showLoading,
hideLoading,
touchstart,
showToast,
throttle,
touchend,
decimal,
dialog,
isMobileDevice,
tsFormatTime,
};
export default { utils };
Object.keys(utils).map((key) => {
Vue.prototype[key] = utils[key];
});