js工具类
1、将图片转换为base64并压缩
// 将图片转换为base64并压缩 export const convertImgToBase64 = (url) => { return new Promise(function (resolve, reject) { const canvas = document.createElement('canvas') const ctx = canvas.getContext('2d') let img = document.createElement('img') img.onload = () => { const width = img.width const height = img.height let widthRate = width let heightRate = height if (width > 100) { // 限制大小100kb widthRate = 100 heightRate = 100 * height / width } // 按比例压缩4倍 canvas.width = widthRate canvas.height = heightRate ctx.drawImage(img, 0, 0, width, height, 0, 0, widthRate, heightRate) let u = canvas.toDataURL('image/png') u = u.replace(/^data:image\/(gif|png|jpg|jpge|bmp);base64,/, '') resolve(u) } img.src = url }) }
2、将图片的file格式转换为url格式
// 传入file格式,类似e.target.files[0] getImg (file) { let url = '' if (window.createObjectURL !== undefined) { // basic url = window.createObjectURL(file) } else if (window.URL !== undefined) { // mozilla(firefox) url = window.URL.createObjectURL(file) } else if (window.webkitURL !== undefined) { // webkit or chrome url = window.webkitURL.createObjectURL(file) } return url }
3、设置获取cookie
// 函数中的参数分别为 cookie 的名称、值以及过期天数 export const setCookie = function (key, value, expiredays) { var exdate = new Date() exdate.setDate(exdate.getDate() + expiredays) document.cookie = key + '=' + escape(value) + ((expiredays == null) ? '' : ';expires=' + exdate.toGMTString()) } // 函数中的参数为 要获取的cookie键的名称。 export const getCookie = function (key) { if (document.cookie.length > 0) { let start = document.cookie.indexOf(key + '=') if (start !== -1) { start = start + key.length + 1 let end = document.cookie.indexOf(';', start) if (end === -1) { end = document.cookie.length } return unescape(document.cookie.substring(start, end)) } } return '' }
4、获取url中的参数
export const getQueryString = (name) => { var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i') var r = window.location.search.substr(1).match(reg) if (r != null) { return unescape(r[2]) } return null }
5、对象的深拷贝
function clone(obj) { let ret if (Array.isArray(obj)) { ret = [] // 创建一个空数组 for (let i = 0; i < obj.length; i++) { ret[i] = clone(obj[i]) } return ret } else if (Object.prototype.toString.call(obj) === "[object Object]") { ret = {} // 创建一个空对象 for (let i in obj) { ret[i] = clone(obj[i]) } return ret } else { return obj } }
6、导出excel
// 导出excel export const $downloadExcel = async (url, params, name) => { const reg = /^\s*$/g Object.keys(params).forEach(elem => { if (!params[elem] || reg.test(params[elem])) { delete params[elem] } else { if (url.includes('?')) { url = url + '&' + elem + '=' + params[elem] } else { url = url + '?' + elem + '=' + params[elem] } } }) const ret = await axios({ method: 'GET', url: url, responseType: 'blob' }) if (ret.status !== 200) { return } console.log(ret.data) const downUrl = window.URL.createObjectURL(new Blob([ret.data], { type: 'application/octet-stream' })) const link = document.createElement('a') link.style.display = 'none' link.href = downUrl link.setAttribute('download', name) document.body.appendChild(link) link.click() }