utilsパッケージ要約(追加対象)
8114 ワード
vueプロジェクトでは、多くの場所で呼び出すのに便利な関数を自分でカプセル化することがよくあります.ここでは、私自身のパッケージと収集の一部を参考にしてください.
//util.js
/*
@1 local set get clear
@2 joinReqParams
@3 deviceType
@4 formDate
@5 remainTime
@6 timeDiff
@7 dateGet
*/
//@1 +++++++++ ,
// localstorage , cookie +++++++++++
let local = {
set(key, value) {
if (checkLocalStorage()) {
window.localStorage.setItem(key, value);
} else {
let Days = 30;
let exp = new Date();
exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
document.cookie = key + '=' + escape(value) + ';expires=' + exp.toGMTString();
}
},
get(key) {
if (checkLocalStorage()) {
return window.localStorage.getItem(key) ? window.localStorage.getItem(key) : null;
} else {
return getCookie(key);
}
},
clear(key) {
if (checkLocalStorage()) {
key ? window.localStorage.removeItem(key) : window.localStorage.clear();
} else {
let exp = new Date();
exp.setTime(exp.getTime() - 1);
let cval = getCookie(key);
if (cval != null) document.cookie = key + '=' + cval + ';expires=' + exp.toGMTString();
}
}
};
function checkLocalStorage() {
// Localstorage
return window.localStorage && (window.localStorage.setItem('a', 123), window.localStorage.getItem('a') == 123)
? true
: false;
}
function getCookie(name) {
let arr,
reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');
if ((arr = document.cookie.match(reg))) return unescape(arr[2]);
else return null;
}
//@2 joinReqParams
function joinReqParams(obj) {
let params = Object.values(obj).reduce((a, b, i) => `${a}${Object.keys(obj)[i]}=${b}&`, '?');
return params.substring(0, params.length - 1);
}
//@3 deviceType
/*
const UA = navigator.userAgent;
let deviceType = {
isAndroid: /android|adr/gi.test(UA),
isIos: /iphone|ipod|ipad/gi.test(UA) && !this.isAndroid,
isWeixin: /MicroMessenger/gi.test(UA),
isQQ: /QQ\/\d/gi.test(UA),
isClient: this.isWeixin || this.isQQ,
isChrome: /Chrome\/([\d.]+)/gi.test(UA) || /CriOS\/([\d.]+)/gi.test(UA),
isIosWebview: !this.isChrome && /(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/gi.test(UA)
};
*/
// @4 +++++ ++++++ "YY MM DD hh mm ss "
function formDate(time = new Date(), format = 'YY-MM-DD hh:mm:ss') {
const v_list = {
//
YY: time.getFullYear(),
//
MM: time.getMonth() + 1 < 10 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1,
//
DD: time.getDate() < 10 ? '0' + time.getDate() : time.getDate(),
//
hh: time.getHours() < 10 ? '0' + time.getHours() : time.getHours(),
//
mm: time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes(),
//
ss: time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds()
};
return [
{
id: format.indexOf('YY'),
v: v_list.YY + format.charAt(format.indexOf('YY') + 2)
},
{
id: format.indexOf('MM'),
v: v_list.MM + format.charAt(format.indexOf('MM') + 2)
},
{
id: format.indexOf('DD'),
v: v_list.DD + format.charAt(format.indexOf('DD') + 2)
},
{
id: format.indexOf('hh'),
v: v_list.hh + format.charAt(format.indexOf('hh') + 2)
},
{
id: format.indexOf('mm'),
v: v_list.mm + format.charAt(format.indexOf('mm') + 2)
},
{
id: format.indexOf('ss'),
v: v_list.ss + format.charAt(format.indexOf('ss') + 2)
}
]
.filter(v => v.id > -1)
.reduce((a, b) => a + b.v, '');
}
//@5
function remainTime(num, format = 'hh:mm:ss') {
if (!num) return '00:00:00';
num /= 1000;
let h = parseInt(num / 3600),
m = parseInt(num / 60),
s = parseInt(num % 60);
const v_list = {
//
hh: (h > 60 ? (h -= 60) : h) < 10 ? '0' + h : h,
//
mm: (m > 60 ? (m -= 60) : m) < 10 ? '0' + m : m,
//
ss: (s > 60 ? (s -= 60) : s) < 10 ? '0' + s : s
};
return [
{
id: format.indexOf('hh'),
v: v_list.hh + format.charAt(format.indexOf('hh') + 2)
},
{
id: format.indexOf('mm'),
v: v_list.mm + format.charAt(format.indexOf('mm') + 2)
},
{
id: format.indexOf('ss'),
v: v_list.ss + format.charAt(format.indexOf('ss') + 2)
}
]
.filter(v => v.id > -1)
.reduce((a, b) => a + b.v, '');
}
//@6 timeDiff startTime ==>'2018/05/01 00:00:00'
// endTime new Date()
function timeDiff(startTime, endTime) {
let date1 = startTime; //
let date2 = endTime; //
let date3 = date2.getTime() - new Date(date1).getTime();
//
let days = Math.floor(date3 / (24 * 3600 * 1000));
//
let leave1 = date3 % (24 * 3600 * 1000); //
let hours = Math.floor(leave1 / (3600 * 1000)) + days * 24; //
let hours2 = Math.floor(leave1 / (3600 * 1000));
if (hours < 10) hours = `0${hours}`;
//
let leave2 = leave1 % (3600 * 1000); //
let minutes = Math.floor(leave2 / (60 * 1000));
if (minutes < 10) minutes = `0${minutes}`;
//
let leave3 = leave2 % (60 * 1000); //
let seconds = Math.round(leave3 / 1000);
if (seconds < 10) seconds = `0${seconds}`;
const timeDiffrence = `${hours}:${minutes}:${seconds}`; //
const timeDiffrence2 = `${days} ${hours} ${minutes} ${seconds} `;
return timeDiffrence;
}
//@7 dateGet +++++++++++ ++++++++++++
/*
let date = {
datepick:computeDay(dates), ,
dateCurrent:getCurrentWeek(),
dateLastWeek:getLastWeek(),
dateMonth:computeMonth(), ,
dateCurrMonth:getCurrentMonth(),
dateLastMonth:getLastMonth(),
}
*/
// ,m
function computeDay(dates = 0) {
let t = new Date();
t.setDate(t.getDate() + dates);
let y = t.getFullYear();
let m = t.getMonth() + 1;
let d = t.getDate();
return `${y}-${m < 10 ? '0' + m : m}-${d < 10 ? '0' + d : d}`;
}
//
function getCurrentWeek() {
let d = new Date();
let day = d.getDay();
let c = day != 0 ? day - 1 : 6;
return [computeDay(c - 6), computeDay(c)];
}
//
function getLastWeek() {
let d = new Date();
let day = d.getDay();
let c = day != 0 ? day - 1 : 6;
return [computeDay(c - (6 + 7)), computeDay(c - 7)];
}
// ,
function computeMonth(months = 0) {
let t = new Date();
if (months) {
t.setMonth(t.getMonth() + months);
}
let y = t.getFullYear();
let m = t.getMonth() + 1;
let d = 0;
if (~[1, 3, 5, 7, 8, 10, 12].indexOf(m)) {
d = 31;
} else if (m == 2) {
// ( 4 100 100 400 )
if ((y % 4 == 0 && y % 100 != 0) || (y % 100 == 0 && y % 400 == 0)) {
d = 29;
} else {
d = 28;
}
} else {
d = 30;
}
return [`${y}-${m < 10 ? '0' + m : m}-${1}`, `${y}-${m < 10 ? '0' + m : m}-${d < 10 ? '0' + d : d}`];
}
//
function getCurrentMonth() {
return computeMonth();
}
//
function getLastMonth() {
return computeMonth(-1);
}
// IP
const getIPAdress = () => {
let localIPAddress = "";
let interfaces = require('os').networkInterfaces();
for (let devName in interfaces) {
let iface = interfaces[devName];
for (let i = 0; i < iface.length; i++) {
let alias = iface[i];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
localIPAddress = alias.address;
}
}
}
return localIPAddress;
}