1.在main.js中引入全局方法,
// 全局方法 import Common from './util/public' Vue.prototype.Common = Common;
2. 在文件夹util/public.js 写入方法
import {getServeDate} from '@/api/system/user' let serveDate; getServeDate().then( res => { serveDate = res.data.data.dateTime.substring(0, 10) });
export default {
/* @method 消息提示确认框 @Author *** 2019-11-19 @param Vue 方法对象 必填 this @param title 标题 必填 @param descripion 提示框描述文字 必填 @param callBack 回调函数 必填 @param data 函数所需参数 非必填 @param index 操作下标 非必填 */ confirm: function (Vue, title, descripion, callBack, data, index) { Vue.$confirm(descripion, title, { confirmButtonText: '确定', cancelButtonText: '取消', closeOnClickModal: false, type: 'warning' }).then(() => { callBack(data, index); }).catch(() => { Vue.$message.info('已取消操作'); }); },
}
/* @method 获取上月或下月 @Author create *** 2020-06-17 @param date 传入的日期 必填 String @param type 上月(prev) 下月(next) 必填 */ getMonth(date, type) { const arr = date.split('-'); const year = arr[0]; const month = arr[1]; let year2 = year; let month2 = type === 'prev' ? parseInt(month) - 1 : parseInt(month) + 1; if(month2 === 0 || month2 === 13){ // if (month2 === type === 'prev' ? 0 : 13) { year2 = type === 'prev' ? parseInt(year2) - 1 : parseInt(year2) + 1; month2 = type === 'prev' ? 12 : 1; } if (month2 < 10) { month2 = '0' + month2; } return this.getNowFormatDate(new Date(year2 + '-' + month2)); },
/* @method 获取周一至周日的日期 @Author create *** 2019-12-26 @param date 传入的日期 非必填 @param number 哪一周 必填 (0:本周;7:下周,以此类推) */ getNextweek(number, date) { let weeks = []; let info = date ? date : serveDate; for (let i = 0; i < 7; i++) { let Stamp = new Date(info); let num; if (Stamp.getDay() == 0) { num = number - 7 + 1 + i; } else { num = number - Stamp.getDay() + 1 + i; } Stamp.setDate(Stamp.getDate() + num); let month = Stamp.getMonth() + 1 < 10 ? '0' + (Stamp.getMonth() + 1) : Stamp.getMonth() + 1; let day = Stamp.getDate < 10 ? '0' + Stamp.getDate() : Stamp.getDate(); weeks[i] = Stamp.getFullYear() + '-' + month + '-' + (day < 10 ? '0' + day : day); } return weeks },
/* @method 删除字符串开头和结尾的空格 @Author *** 2020-05-08 */ strTrim(str){ const str1 = str.replace(/\s+$/,''); const str2 = str1.replace(/^\s+/,''); return str2; },
/* @method 减法 @Author *** 2020-06-10 @param num1, num2 参与运算 必填 */ accSub(num1, num2){ let r1,r2,m,n; try{ r1 = num1.toString().split(".")[1].length } catch(e){ r1 =0 } try{ r2 = num2.toString().split(".")[1].length } catch(e){ r2 = 0 } m = Math.pow(10,Math.max(r1,r2)); n = ( r1 >= r2 ) ? r1 : r2; return ( ( num1 * m - num2 * m ) / m ).toFixed(n); },
/* @method 加法 @Author *** 2020-06-10 @param num1, num2 参与运算 必填 */ accAdd(num1,num2){ let r1,r2,m; try{ r1 = num1.toString().split(".")[1].length } catch(e){ r1 = 0 } try{ r2 = num2.toString().split(".")[1].length } catch(e){ r2 = 0 } m = Math.pow(10,Math.max(r1,r2)); return ( Math.round(num1 * m ) + Math.round(num2 * m ) ) / m },
/* @method 元 ——> 万元 @Author *** 2020-06-10 @param num 转换金额 必填 */ unitConvert(num) { let moneyUnits = ["元", "万元", "亿元", "万亿"]; let dividend = 10000; let curentNum = Number(num); //转换数字 let curentUnit = moneyUnits[0]; //转换单位 if(curentNum > dividend){ for (let i = 0; i <4; i++) { curentUnit = moneyUnits[i]; if(this.strNumSize(curentNum)<5){ break; } curentNum = curentNum / dividend } } let m = {num: 0, unit: ""}; m.num = curentNum.toFixed(2); m.unit = curentUnit; return m; },
/* @method 获取三个月前日期 @Author create *** 2020-06-10 */ getTreeMonthDate(a){ const nowDate = a ? a : serveDate; let year = nowDate.substr(0,4); let month = nowDate.substr(5,2); let date = nowDate.substr(8,2); let startDate; if(parseInt(month) > 3){ startDate = year + '-0' + (month - 3) + '-' + date; }else if(parseInt(month) === 3 ){ startDate = (year - 1) + '-' + 12 + '-' + date; }else { startDate = (year - 1) + '-' + (12 - month) + '-' + date; } return startDate },
/* @method 获取某天前日期 @Author create *** 2021-01-19 @param num num是正数表示之后的时间,num负数表示之前的时间,0表示今天 必填 */ getDayDate(num){ let time = new Date(serveDate).getTime() + 1000 * 60 * 60 * 24 * (num); let year = new Date(time).getFullYear(); let month = new Date(time).getMonth() + 1 < 10 ? '0' + (new Date(time).getMonth() + 1) : new Date(time).getMonth() + 1; let date = new Date(time).getDate(); let time1 = year + "-" + month + "-" + date; return time1; },
strNumSize(tempNum){ const stringNum = tempNum.toString(); const index = stringNum.indexOf("."); let newNum = stringNum; if(index !== -1){ newNum = stringNum.substring(0,index) } return newNum.length },
/*
@method 转时间
@param timestamp 时间
@param type 类型
*/
timestampToTime(timestamp, type = 'en') {
let date = new Date();
if (timestamp) date = timestamp
let Y = date.getFullYear();
let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours());
let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
let ms = date.getMilliseconds() < 100 ? '0' + date.getMilliseconds() : date.getMilliseconds();
if (type == 'en') {
return {
times: `${Y}-${M}-${D}`,
time: `${Y}-${M}-${D} ${h}:${m}:${s}`,
h: `${h}:${m}:${s}`
}
} else {
return {
times: `${Y}年${date.getMonth() + 1}月${D}日`,
time: `${Y}年${M}月${D}日${h}时${m}分${s}秒`,
h: `${h}:${m}:${s}`
}
}
},
/*
@method 时间戳转日期格式化
@Author create *** 2020-09-15
@param stamp 时间戳 必填
@param type 是否需要毫秒 非必填 默认不需要
*/
stampChangeDate(stamp, type) {
const date = new Date(stamp);
const YY = date.getFullYear() + '-';
const MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
const DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
const hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
const mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
const ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
const mill = ':' + date.getMilliseconds();
return type ? YY + MM + DD +" "+ hh + mm + ss + mill : YY + MM + DD +" "+ hh + mm + ss;
},
3.vue页面引用