// 绘制logo export function drawLogo(text = '', len = 4) { if (text.length > 4) { text = text.split('').filter((item) => /^[\u4E00-\u9FA5]+$/.test(item)) } if (!document) return const canvas = document.createElement('canvas') canvas.width = 400 canvas.height = 400 if (canvas.getContext) { // 绘制流程 var ctx = canvas.getContext('2d') ctx.fillStyle = '#130f40' ctx.fillRect(0, 0, 400, 400) // 绘制第一个字 ctx.fillStyle = '#eee' ctx.font = '80px 黑体' text[0] && ctx.fillText(text[0], 80, 160) text[1] && ctx.fillText(text[1], 220, 160) text[2] && ctx.fillText(text[2], 80, 300) text[3] && ctx.fillText(text[3], 220, 300) } return canvas.toDataURL() } // 跳转到授权页面 export function toAuthorization(appId, payload) { const type = payload.routePrefix.split('/')[2] console.log(process.env.LOCALHOSE) const url = `${process.env.LOCALHOSE}/auth?authUserId=${payload.authUserId}&type=${type}&appId=${appId}` const redirect_uri = encodeURIComponent(url) window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect` } /** 时间格式化 * @param {dateTime} date 标准时间格式 -> new Date() * @param {string} format 时间格式化的格式 'yyyy-MM-dd hh:mm:ss' * @returns {string} 格式化后的时间 '2017-01-01 01:00:00' */ export function dateFormat(date = new Date(), format = 'yyyy-MM-dd hh:mm:ss') { var o = { 'M+': date.getMonth() + 1, // month 'd+': date.getDate(), // day 'h+': date.getHours(), // hour 'm+': date.getMinutes(), // minute 's+': date.getSeconds(), // second 'q+': Math.floor((date.getMonth() + 3) / 3), // quarter S: date.getMilliseconds(), // millisecond } if (/(y+)/.test(format)) { format = format.replace(RegExp.$1, (date.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 } /** * @param {string} url * @returns {Object} */ export function getQueryObject(url) { url = url == null ? window.location.href : url const search = url.substring(url.lastIndexOf('?') + 1) const obj = {} const reg = /([^?&=]+)=([^?&=]*)/g search.replace(reg, (rs, $1, $2) => { const name = decodeURIComponent($1) let val = decodeURIComponent($2) val = String(val) obj[name] = val return rs }) return obj } /** * 防抖 * @param {Function} func 需要包装的函数 * @param {string} wait 等待执行时间 * @param {string} immediate 是否是立即执行 默认不立即执行 * @returns {Function} 返回包装后的函数 */ export function debounce(func, wait, immediate) { let timeout, result return function () { const context = this const args = arguments if (timeout) clearTimeout(timeout) if (immediate) { const callNow = !timeout timeout = setTimeout(function () { timeout = null }, wait) if (callNow) result = func.apply(context, args) } else { timeout = setTimeout(function () { func.apply(context, args) }, wait) } return result } } export function callMobile(mobile) { if (!mobile) return const a = document.createElement('a') a.href = 'tel:' + mobile a.click() } /** * Merges one object to another object * @param {Object} target * @param {(Object)} source * @returns {Object} */ export function objectCover(target, source) { for (const key in source) { if (Object.hasOwnProperty.call(target, key)) { target[key] = source[key] } } return target } // 获取视频封面 export function getVideoBase64(url) { return new Promise((resolve, reject) => { const video = document.createElement('video') video.setAttribute('crossOrigin', 'anonlymous') video.setAttribute('preload', 'auto') video.setAttribute('src', url) video.style.display = 'none' if (/iPad|iPhone|iPod/.test(navigator.userAgent)) { video.setAttribute('autoplay', true) } video.onloadeddata = () => { const canvas = document.createElement('canvas') const width = video.videoWidth const height = video.videoHeight canvas.width = width canvas.height = height canvas.getContext('2d').drawImage(video, 0, 0, width, height) // dataUrl = canvas.toDataURL('image/jpeg') canvas.toBlob((data) => { resolve(data) }) } }) } /** * 加载图片 * @param {string} url 图片链接 */ export function loadImage(url, width, height) { return new Promise((resolve, reject) => { const image = new Image() image.width = width image.height = height image.src = url image.onload = () => { resolve(image) } image.onerror = (err) => { reject(err) } }) }