常用JS工具类 .....持续更新中 ...CV大法好
前端导出表格(兼容ie) if (res.status == "200") { const filename = res.headers["content-disposition"] .split(";")[1] .split("=")[1]; const fileName = decodeURIComponent(filename); const blod = new Blob([res.data], { type: "application/x-xls;charset=utf-8", }); if (!window.navigator.msSaveBlob) { const elink = document.createElement("a"); elink.href = window.URL.createObjectURL(blod); elink.download = fileName; document.body.appendChild(elink); elink.click(); window.URL.revokeObjectURL(elink.href); document.body.removeChild(elink); } else { // IE下载 window.navigator.msSaveBlob(blod, fileName); } }
** * 格式化秒 * @param int value 总秒数 * @return string result 格式化后的字符串 */ function formatSeconds(value) { var theTime = parseInt(value);// 需要转换的时间秒 var theTime1 = 0;// 分 var theTime2 = 0;// 小时 var theTime3 = 0;// 天 if(theTime > 60) { theTime1 = parseInt(theTime/60); theTime = parseInt(theTime%60); if(theTime1 > 60) { theTime2 = parseInt(theTime1/60); theTime1 = parseInt(theTime1%60); if(theTime2 > 24){ //大于24小时 theTime3 = parseInt(theTime2/24); theTime2 = parseInt(theTime2%24); } } } var result = ''; if(theTime > 0){ result = ""+parseInt(theTime)+"秒"; } if(theTime1 > 0) { result = ""+parseInt(theTime1)+"分"+result; } if(theTime2 > 0) { result = ""+parseInt(theTime2)+"小时"+result; } if(theTime3 > 0) { result = ""+parseInt(theTime3)+"天"+result; } return result; }
//复制 copy(id) { // 创建节点 const range = document.createRange(); // 选择节点 range.selectNode(document.getElementById(id + "")); // 选择的文本范围 const selection = window.getSelection(); // (清除之前粘贴板内容,否则复制不上) if (selection.rangeCount > 0) {selection.removeAllRanges()}; // 将一个Range对象添加到Selection对象中,该Range对象中所包含的内容将变为高亮 *选取* 状态。 selection.addRange(range); // 调用复制功能 document.execCommand("Copy"); vueInstance.$message({ message: '复制成功!', type: 'success', duration: 1000 }); },
//转化日期格式 yyyy-MM-dd hh:mm:ss formatDate() { Date.prototype.format = function (format) { if (!format) { format = "yyyy-MM-dd hh:mm:ss"; } var o = { "M+": this.getMonth() + 1 /* month*/, "d+": this.getDate() /* day*/, "h+": this.getHours() /* hour*/, "m+": this.getMinutes() /* minute*/, "s+": this.getSeconds() /* second*/, "q+": Math.floor((this.getMonth() + 3) / 3) /* quarter*/, S: this.getMilliseconds() }; if (/(y+)/.test(format)) { format = format.replace( RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length) ); } for (var k in o) { if (new RegExp("(" + k + ")").test(format)) { format = format.replace( RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length) ); } } return format; }; }
//设置cookie setCookie(c_name, value, expiredays) { var exdate = new Date(); exdate.setFullYear(exdate.getFullYear() + expiredays); document.cookie = c_name + "=" + escape(value) + (expiredays == null ? "" : ";expires=" + exdate.toGMTString()) + ";path=/"; }, //获取cookie getCookie(c_name) { if (document.cookie.length > 0) { let c_start = document.cookie.indexOf(c_name + "="); if (c_start != -1) { c_start = c_start + c_name.length + 1; let c_end = document.cookie.indexOf(";", c_start); if (c_end == -1) c_end = document.cookie.length; return unescape(document.cookie.substring(c_start, c_end)); } } return ""; },
// 当前环境是否是 wechat const userAgent = navigator.userAgent export const IsWeChat = () => { const Agents = ['Wechat', 'WeChat', 'wxwork'] let flag = false for (let v = 0; v < Agents.length; v++) { if (userAgent.indexOf(Agents[v]) > 0) { flag = true break } } return flag }
// 判断移动or手机 const userAgent = navigator.userAgent export const isPcFun = () => { if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(userAgent)) { return false } else { return true } }
// 判断是否为ios const userAgent = navigator.userAgent export const isIos = () => { return !!userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) // 判断是否为ios }
在技术路上探索的菜鸟,如有不足,欢迎大佬们指正!