import './shims-ivy-fe' export function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)) } export function uuid() { let s = [] let hexDigits = '0123456789abcdef' for (let i = 0; i < 36; i++) { s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1) } s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010 s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi_and_reserved to 01 s[8] = s[13] = s[18] = s[23] return s.join('') } export function parseLyric(txt) { let res: any[] = [{}] let lns = txt.split('\n').map(l => decodeURIComponent(l)) for (let ln of lns) { let m = ln.match(/\[(.*)?\](.*)/) // 分成中括号及之后两部分 if (!m) continue // 不认识的格式 let [, type, cont] = m if (type === 'CN') { res[res.length - 1].cn = cont } else if (type === 'ti:') { res[res.length - 1] = {type: 'title', cont} } else { let time = type.split(':') if (time.length === 2) { res.push({type: 'lyc', time: (+time[0]) * 60 + +time[1], cont}) } } } return res } export function getOs() { let ua = navigator.userAgent.toLowerCase() let phones = ['android'] for (let phone of phones) { if (ua.includes(phone)) return phone } if (ua.includes('mac os x')) { return 'mac' } return 'unknown' } export function isXcx() { return wx && wx.login } export function isWeixin() { return navigator.userAgent.toLowerCase().includes('micromessenger') } export const isIos = () => { const ua = navigator.userAgent.toLowerCase() return !!ua.match(/ip(hone|od|ad)/i) } export const isAndroid = () => { const ua = navigator.userAgent.toLowerCase() return ua.includes('android') } export function storeSet(key, str) { if (isXcx()) { wx.setStorageSync(key, str) } else { localStorage[key] = str } } export function storeGet(key) { return isXcx() ? wx.getStorageSync(key) : localStorage[key] } let deviceId = '' export function getDeviceId() { if (!deviceId) { let key = `ivy-device-id` deviceId = storeGet(key) || uuid() storeSet(key, deviceId) } return deviceId } export function cacheSet(key, obj, expireSeconds= -1) { let expireAt = new Date(`2099/01/01`) if (expireSeconds > 0) { expireAt = new Date(new Date().getTime() + expireSeconds * 1000) } else if (expireSeconds === 0) { expireAt = new Date(new Date().getTime() - 10 * 1000) } storeSet(`cache-${key}`, JSON.stringify({obj, expireAt: expireAt.format('YYYY/MM/DD hh:mm:ss'), createAt: new Date().format('YYYY/MM/DD hh:mm:ss')})) } export function cacheGet(key) { let v = storeGet(`cache-${key}`) if (!v) return null try { let saved = JSON.parse(v) if (new Date(saved.expireAt) < new Date()) return null return saved.obj } catch (e) { return null } } let timers = {} // 自动清除的timer,同一个key的多个timer互斥,下一个设置会清掉前一个,避免手动清除 export function setAutoTimer(cb, interval, key = 'default') { if (timers[key]) { clearInterval(timers[key]) } timers[key] = setInterval(cb, interval) } /** * cnzz事件统计 * @param {*} category 必填 string 表示事件发生在谁身上,如“视频”、“小说”、“轮显层”等等。 * @param {*} action 必填 string 表示访客跟元素交互的行为动作,如"播放"、"收藏"、"翻层"等等。 * @param {*} label 选填 string 用于更详细的描述事件,如具体是哪个视频,哪部小说。 * @param {*} value 选填 int 用于填写打分型事件的分值,加载时间型事件的时长,订单型事件的价格。请填写整数数值,如果填写为其他形式,系统将按0处理。若填写为浮点小数,系统会自动取整,去掉小数点。 * @param {*} nodeid 选填 string 填写事件元素的div元素id。请填写class id,暂不支持name。 */ export function trackEvent(category, action, label = '', value = '', nodeid = '') { if (window._czc) { window._czc.push(['_trackEvent', category, action, label, value, nodeid]) } } /** * 去处字符串中所有的的空格 * @param {*} str */ export function trim(str) { return str.replace(/\s|\xA0/g, '') } export function addDateFormat() { // 对Date的扩展,将 Date 转化为指定格式的String // 月(M)、日(D)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) // 例子: // (new Date()).Format("YYYY-MM-DD hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 // (new Date()).Format("YYYY-M-D h:m:s.S") ==> 2006-7-2 8:9:4.18 Date.prototype.Format = function(fmt) { fmt = fmt || 'YYYY-MM-dd hh:mm:ss' let o = { "M+": this.getMonth() + 1, "D+": this.getDate(), "h+": this.getHours(), "m+": this.getMinutes(), "s+": this.getSeconds(), "q+": Math.floor((this.getMonth() + 3) / 3), "S": this.getMilliseconds() // 毫秒 } if (/(Y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)) for (let k in o) { if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))) } return fmt } Date.newDate = (dt) => { return dt === undefined ? new Date() : new Date(dt.replace(/-/g, '/')) } Date.formatMs = (ms, fmt) => { fmt = fmt || 'hh:mm:ss' let o = { "D+": Math.floor(ms / 1000 / 86400), "h+": Math.floor(ms / 1000 / 3600) % 24, "m+": Math.floor(ms / 1000 / 60) % 60, "s+": Math.floor(ms / 1000) % 60, "S": ms % 1000, } for (let k in o) { if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))) } return fmt } Date.prototype.format = Date.prototype.format || Date.prototype.Format } export function ellipsedText(text, length) { return text.length > length ? text.slice(0, length - 3) + '...' : text } export function formatCount(cn) { let w = 10000 // 万 let y = w * w // 亿 if (cn >= y) { return `${Math.floor(cn * 10 / y) / 10}亿`.replace('.0', '') } if (cn >= w) { return `${Math.floor(cn * 10 / w) / 10}万`.replace('.0', '') } return cn }