本篇文章给大家分享的内容是关于vue项目工具文件utils.js使用的代码分享,有着一定的参考价值,有需要的朋友可以参考一下
import vue from 'vue'
import xlsx from 'xlsx'
/**
* 图片预览方法
* 已注入所有vue实例,
* template模板里调用 $imgpreview(src)
* 组件方法里调用 this.$imgpreview(src)
*/
const imgpreview = (src) => {
let p = document.createelement('p')
let img = document.createelement('img')
let close = document.createelement('i')
p.classname = 'body__img__preview'
img.src = src
close.classname = 'body__img__close'
close.onclick = () => {
document.body.removechild(p)
}
p.appendchild(img)
p.appendchild(close)
document.body.appendchild(p)
}
/**
* 格式化日期方法
* 已注入所有vue实例,
* template模板里调用 $dateformat(date)
* 组件方法里调用 this.$dateformat(date)
* 例:this.$dateformat('dec 27, 2017 3:18:14 pm') 得到 '2017-12-27 15:18:14'
*/
const dateformat = (date) => {
if (!date) return ''
let date_format = new date(date)
let year = date_format.getfullyear()
let month = date_format.getmonth() + 1
if (month < 10) month = '0' + month
let mydate = date_format.getdate()
if (mydate < 10) mydate = '0' + mydate
let hours = date_format.gethours()
if (hours < 10) hours = '0' + hours
let minutes = date_format.getminutes()
if (minutes < 10) minutes = '0' + minutes
let seconds = date_format.getseconds()
if (seconds < 10) seconds = '0' + seconds
let time = `${year}-${month}-${mydate} ${hours}:${minutes}:${seconds}`
return time
}
/**
* 格式化日期方法
* 已注入所有vue实例,
* template模板里调用 $dateformatnotime(date)
* 组件方法里调用 this.$dateformatnotime(date)
* 例:this.$dateformatnotime('dec 27, 2017 3:18:14 pm') 得到 '2017-12-27'
*/
const dateformatnotime = (date) => {
if (!date) return ''
let date_format = new date(date)
let year = date_format.getfullyear()
let month = date_format.getmonth() + 1
if (month < 10) month = '0' + month
let mydate = date_format.getdate()
if (mydate < 10) mydate = '0' + mydate
let time = `${year}-${month}-${mydate}`
return time
}
/**
* 获取当天日期方法
* 已注入所有vue实例,
* template模板里调用 $todayformat
* 组件方法里调用 this.$todayformat
* 例:this.$todayformat() 得到 '2018-01-31'
*/
const todayformat = () => {
let date_format = new date()
let year = date_format.getfullyear()
let month = date_format.getmonth() + 1
if (month < 10) month = '0' + month
let date = date_format.getdate()
if (date < 10) date = '0' + date
let time = `${year}-${month}-${date}`
return time
}
/**
* 导出数据报表xlsx文件
* 已注入所有vue实例,
* template模板里调用 $$outputxlsxfile
* 组件方法里调用 this.$outputxlsxfile
* 例:this.$outputxlsxfile([['字段1', '字段2'], [1, 2]], [{wch: 10}, {wch: 50}], '测试导出') 得到 测试导出.xlsx 文件
* 第一个参数是导出的数组对象,第二个参数是设置字段宽度,第三个参数是文件名
*/
const outputxlsxfile = (data, wscols, xlsxname) => {
/* convert state to workbook */
const ws = xlsx.utils.aoa_to_sheet(data)
ws['!cols'] = wscols
const wb = xlsx.utils.book_new()
xlsx.utils.book_append_sheet(wb, ws, xlsxname)
/* generate file and send to client */
xlsx.writefile(wb, xlsxname + ".xlsx")
}
/**
* 判断开始时间和结束时间
* 已注入所有vue实例,
* template模板里调用 $checktime
* 组件方法里调用 this.$checktime
* 例:this.$checktime(['2018-01-01', '2018-02-02']) 得到 {'2018/01/01', '2018/02/02'}
*/
const checktime = (date) => {
if (!date) {
vue.prototype.$notify.error({
message: '日期不能为空'
})
return false
} else {
let begtime = date[0].replace(/-/g, '/')
let endtime = date[1].replace(/-/g, '/')
if (+((new date(endtime)).gettime()) < +((new date(begtime)).gettime())) {
vue.prototype.$notify.error({
message: '开始日期不能大于结束日期'
})
return false
} else {
begtime = date[0]
endtime = date[1]
return {begtime, endtime}
}
}
}
/**
* 判断性别
* 已注入所有vue实例,
* template模板里调用 $typesex
* 组件方法里调用 this.$typesex
* 例:this.$typesex(1) 得到 男
* 参数 0:未知 1:男 2:女
*/
const typesex = (value) => {
let sex = ''
switch (value) {
case '0' : sex = '未知'
break
case '1' : sex = '男'
break
case '2' : sex = '女'
break
}
return sex
}
/**
* 时间戳转换
* 已注入所有vue实例,
* template模板里调用 $timestamptotime
* 组件方法里调用 this.$timestamptotime
* 例:this.$timestamptotime(1523440865000) 得到 '2018-04-11 18:1:5'
*/
const timestamptotime = (timestamp) => {
var date = new date(timestamp)
let y = date.getfullyear() + '-'
let m = (date.getmonth() + 1 < 10 ? '0' + (date.getmonth() + 1) : date.getmonth() + 1) + '-'
let d = date.getdate() + ' '
let h = date.gethours() + ':'
let m = date.getminutes() + ':'
let s = date.getseconds()
return y + m + d + h + m + s
}
vue.prototype.$imgpreview = imgpreview
vue.prototype.$dateformat = dateformat
vue.prototype.$dateformatnotime = dateformatnotime
vue.prototype.$todayformat = todayformat
vue.prototype.$outputxlsxfile = outputxlsxfile
vue.prototype.$checktime = checktime
vue.prototype.$typesex = typesex
vue.prototype.$timestamptotime = timestamptotime
相关推荐:
vue使用xe-utils详细教程
以上就是关于vue项目工具文件utils.js使用的代码分享的详细内容。