import dayjs from 'dayjs' import { validateIdCard } from './commonReg.js' // sessionStorage 保存 获取 export function setSessionStorage(key, data) { if (!key || data == null) { return } if (typeof data === 'object') { sessionStorage.setItem(key, JSON.stringify(data)) return } sessionStorage.setItem(key, data) } export function getSessionStorage(key) { if (!key) { return '' } let result = sessionStorage.getItem(key) || '' try { return JSON.parse(result) } catch { return result } } // localStorage 保存 获取 export function setLocalStorage(key, data) { if (!key || data == null) { return } if (typeof data === 'object') { localStorage.setItem(key, JSON.stringify(data)) return } localStorage.setItem(key, data) } export function getLocalStorage(key) { if (!key) { return '' } let result = localStorage.getItem(key) || '' if (!result) { return result } return JSON.parse(result) } /** * 防抖 * @param {Function} fu * @param {Number} wait * @returns */ export function debounce(fu, wait = 300) { let timer return function () { const context = this const args = [...arguments] if (timer) clearTimeout(timer) timer = setTimeout(() => { fu.apply(context, args) }, wait) } } // 复制文本 export function copyToClip(content) { if (!content) { console.warn('copyToClip content 为空') } if (navigator.clipboard) { return navigator.clipboard.writeText(content) } if (document.execCommand) { let dom = document.createElement('textarea') dom.value = content document.body.appendChild(dom) dom.select() document.execCommand('copy') document.body.removeChild(dom) return true } return } /** * 请求参数处理 * @param {Object} source 原数据 * @param {Object} obj 处理规则 */ export function fetchDataHandle(source = {}, obj = {}) { const data = source || {} const result = {} Reflect.ownKeys(obj).forEach(key => { // 数组 <=> 字符串 if (obj[key] === 'arrToStr') { const temp = data[key] || [] temp.sort((a, b) => a - b) result[key] = temp.join(',') || '' } else if (obj[key] === 'strToArr') { result[key] = data[key] ? data[key].split(',') : [] } else if (obj[key] === 'strToArrNum') { result[key] = data[key] ? data[key].split(',').map(e => +e) : [] } else if (obj[key] === 'arrToAdd') { // 地址数组 => 最后一级地址 result[key] = data[key] ? data[key][data[key].length - 1] : '' } else if (obj[key] === 'addToArr') { result[key] = addToArr(data[key]) } }) return { ...data, ...result } } // 最后一级地址 => 地址数组 export function addToArr(str = '', level = 5) { if (!str) return [] if (str.length === 2) { return [str] } let temp = [ str.substring(0, 2), str.substring(2, 4), str.substring(4, 6), str.substring(6, 9), str.substring(9, 12), ] temp = temp.filter(e => e !== '00' || e !== '000') const suffix = {1: '00000000', 2: '000000', 3: '000', 4: ''} for (let i = 1; i < temp.length; i++) { temp[i] = temp[i - 1] + temp[i] } for (let i = 1; i < temp.length; i++) { temp[i] = temp[i] + suffix[i] } return temp.slice(0, level) } // 从身份证号获取基础信息 export function getInfoByIdCard(idCard) { if (!validateIdCard(idCard)) return {} const dataBirth = `${idCard.substring(6, 10)}-${idCard.substring(10, 12)}-${idCard.substring(12, 14)}` const gender = idCard.substring(16, 17) % 2 || 2 const age = dayjs().diff(dayjs(dataBirth), 'years') return { dataBirth, gender, age } } // 根据身高体重自动计算BMI export function calculateBMI(heightVal, weightVal) { let bmi = null if (heightVal && weightVal) { let height = parseFloat(heightVal) let weight = parseFloat(weightVal) let res = weight / (height * height / 100 / 100) bmi = res.toFixed(2) } return bmi } // 多选框选项排斥 export function checkboxReject(val = [], exclude = []) { if (!val.length) return [] const last = val[val.length - 1] let arr = [] if (exclude.includes(last)) { arr = [last] } else { arr = val.filter(e => !exclude.includes(e)) } return arr } //获取url参数 export function getQueryVariable(variable, urlInfo) { let url = decodeURI(decodeURI(urlInfo || window.location.href)) console.log(url) let i = url.indexOf('?') let queryStr = url.substr(i + 1) let vars = queryStr.split("&"); for (let i = 0; i < vars.length; i++) { let pair = vars[i].split("="); if (pair[0] == variable) { return pair[1]; } } return ''; } //原生方法调用 当前方法传参数两种 字符串或者map export function callMobile(handlerInterface, parameters) { let classStr = Object.prototype.toString.call(parameters); if (classStr === '[object String]' || classStr === '[object Object]') { let param = parameters; if (classStr === "[object Object]") { //判断传参是str ,还是object //handlerInterface由iOS addScriptMessageHandler与andorid addJavascriptInterface 代码注入而来。 param = JSON.stringify(parameters);//参数必须转化成json格式 } try { if (isIOSWebKit()) {//ios if (window.webkit !== undefined) { if (param == '{}') { window.webkit.messageHandlers[handlerInterface].postMessage(null); } else { window.webkit.messageHandlers[handlerInterface].postMessage(param); } } } else if (isAndroid()) { //安卓传输不了js json对象,只能传输string if (param == '{}') { window['function'][handlerInterface](); } else { window['function'][handlerInterface](param); } } } catch (e) { } } } // 判断是否是ios export function isIOSWebKit() { const aa = window.navigator.userAgent; if (!!aa.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {// ios端 return true; } } // 判断是否安卓 export function isAndroid() { const aa = window.navigator.userAgent; if (aa.indexOf('Android') !== -1 || aa.indexOf('Adr') !== -1) { return true } } // 判断是否在微信中 export function isWeiXin() { const ua = window.navigator.userAgent.toLowerCase() return /micromessenger/.test(ua) } //关闭页面 export function backHome() { if (isIOSWebKit()) { //ios console.log('ios') callMobile("onBack", {}) } else if (isAndroid()) { console.log('Android') callMobile("closePage", {}) } if (isWeiXin()) { // 微信中 console.log('weixin') WeixinJSBridge.call('closeWindow') } } //是否显示nav-bar export function showNav() { let res = false let wx = window.sessionStorage.getItem('embed') if (wx == 'wx') { res = false } else { res = true } return res } //用户是否存在慢病档案 export function isResidentInfo() { let res = false let jsonInfo =window.sessionStorage.getItem('userInfo') if (jsonInfo == '{}') { res = false } else { res = true } return res } //一维数组去重 export function uniqueArr(arr =[], key= '') { let res = [] if (key) { arr.forEach(item => { let tmp = res.filter(i => i[key] == item[key]) if (!tmp.length) { res.push(item) } }) } return res }