// 正在表达式 export const REGEXPS = { "mobile": /^1\d{10}$/ } // 验证手机 export function checkMobile(str) { return REGEXPS.mobile.test(str); } /** * 链接参数转换为obj * 入参 完整链接 * @param {*} url */ export function param2Obj(url) { const search = url.split('?')[1] if (!search) { return {} } return JSON.parse( '{"' + decodeURIComponent(search) .replace(/"/g, '\\"') .replace(/&/g, '","') .replace(/=/g, '":"') + '"}' ) } //获取cookie、 export function getCookie(name) { var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); if (arr = document.cookie.match(reg)) return (arr[2]); else return null; } //设置cookie export function setCookie(c_name, value, second) { var exdate = new Date(); exdate.setTime(exdate.getTime() + second * 1000); document.cookie = c_name + "=" + escape(value) + ((second == null) ? "" : ";expires=" + exdate.toGMTString()); }; //删除cookie export function delCookie(name) { var exp = new Date(); exp.setTime(exp.getTime() - 1); var cval = getCookie(name); if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString(); }; /** * 获取环境信息 * @return {Object} 环境信息对象 */ export function getEnv() { var nav = window.navigator; var env = { "iphone": false, "ipad": false, "android": false, "pc": false, "ios": false, "ver": "0" }; var ua = nav.userAgent; var android = ua.match(/(Android)\s+([\d.]+)/); var ipad = ua.match(/(iPad).*OS\s([\d_]+)/); var iphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/); if (ipad) { env.ipad = ipad[1] && true || false; env.ver = ipad[2] && ipad[2].replace(/-/g, ".") || ""; env.ios = true; } else if (iphone) { env.iphone = iphone[1] && true || false; env.ver = iphone[2] && iphone[2].replace(/-/g, ".") || ""; env.ios = true; } else if (android) { env.android = android[1] && true || false; env.ver = android[2]; } else { env.pc = true; } return env; } /** * 设定页面 title * @param {[type]} title [description] */ export function setTitle(title) { if (!title) { return; } document.title = title; // if (ENV.ios && navigator.userAgent.toLowerCase().indexOf("micromessenger") !== -1) { // 修复微信端IOS无法修改document.title的情况 if (getEnv().ios && (navigator.userAgent.toLowerCase().indexOf("micromessenger") !== -1 || navigator.userAgent.toLowerCase().indexOf("alipay") !== -1)) { //修复IOS微信端和支付宝无法修改document.title的情况 var $iframe = document.createElement('iframe'); $iframe.className = "C-hiddenIframe"; $iframe.src = "/" + location.pathname.split('/')[1] + "/favicon.ico"; $iframe.style.visibility = 'hidden'; $iframe.style.width = '1px'; $iframe.style.height = '1px'; $iframe.onload = function onIframeLoad() { setTimeout(function () { $iframe.onload = null; onIframeLoad = null; document.body.removeChild($iframe); $iframe = null; }, 0); }; document.body.appendChild($iframe); } } // 为链接添加参数 export function addQuery(url, query) { query = query || {} query = (function (query) { var q = [] Object.keys(query).forEach(function (_q) { q.push(_q + '=' + query[_q]) }) return q.join('&') })(query) if (url.indexOf('?') !== -1) { url += '&' + query } else { url += '?' + query } return url } /** * 获得当前页面的path * @return {String} 页面path */ export function getPath() { var path = window.location.hash; path = path || "#/"; path = path === "#/" ? "#/index" : path; path = path.split("?"); return path[0]; } // 获取 url 参数 export function getQuery(name) { return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null; } /** * 从数组中获取 key未value的对象 * @param {*} value * @param {*} key * @param {*} list */ export function getObjByListKeyValue(value, key, list) { let result = null; list.forEach(element => { if (element[key + ""] == value) { result = element; } }); return result; } /** * 把 \n换行符转换成<br> * 转换后需要用 v-html渲染 * 用{{}}会当成字符串把 html渲染出来 */ export function formatBr(str) { str = str.replace(/\n/g, '<br/>') return str }; /** * @desc 函数防抖 * @param func 函数 * @param wait 延迟执行毫秒数 * @param immediate true 表立即执行,false 表非立即执行 */ export function debounce(func, wait, immediate) { let timeout; return function () { let context = this; let args = arguments; if (timeout) clearTimeout(timeout); if (immediate) { var callNow = !timeout; timeout = setTimeout(() => { timeout = null; }, wait) if (callNow) func.apply(context, args) } else { timeout = setTimeout(function () { func.apply(context, args) }, wait); } } } /** * @desc 函数节流 * @param func 函数 * @param wait 延迟执行毫秒数 * @param type 1 表时间戳版,2 表定时器版 * 时间戳版的函数触发是在时间段内开始的时候,而定时器版的函数触发是在时间段内结束的时候。 */ export function throttle(func, wait, type) { if (type === 1) { var previous = 0; } else if (type === 2) { var timeout; } return function () { let context = this; let args = arguments; if (type === 1) { let now = Date.now(); if (now - previous > wait) { func.apply(context, args); previous = now; } } else if (type === 2) { if (!timeout) { timeout = setTimeout(() => { timeout = null; func.apply(context, args) }, wait) } } } } /** * 几种常用的校验方式 * @param {string} type * @param {string} value */ export function contactMethodCheck(type, value) { if (type == 'hkmobile') { // return /^[5689]{1}\d{7}$/.test(value); return /^(\+)?(852)?(\-)?(5|6|8|9)\d{7}$/.test(value); } if (type == 'mobile') { // return /^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0-9])|(18[0-9])|(19[0-9]))\d{8}$/.test(value); return /^1\d{10}$/.test(value); } if (type == 'email') { return /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/.test(value); } return true; } export function policyNumberCheck(policyNumber) { return /^(P|p)(0001|0002)\d{11}$/.test(policyNumber); } export function checkVerifyCode(verifyCode) { return /^\d{6}$/.test(verifyCode) } export function checkName(name) { return /^\D+$/.test(name); }