// #ifdef APP-PLUS import { judgePermission } from './permission' // #endif import Vue from 'vue'; export const addressCode = function(address) { let rule = /.+?(省|市|自治区|自治州|县|区)/g; const targetValues = address.match(rule) console.log(targetValues) return targetValues; } //根据身份证号计算年龄 export const getAgeByIdCard = function(idCard) { const sexAndAge = {} //获取用户身份证号码 const userCard = idCard //如果用户身份证号码为undefined则返回空 if (!userCard) { return sexAndAge } // 获取出生日期 const yearBirth = userCard.substring(6, 10) const monthBirth = userCard.substring(10, 12) const dayBirth = userCard.substring(12, 14) // 获取当前年月日并计算年龄 const myDate = new Date() const monthNow = myDate.getMonth() + 1 const dayNow = myDate.getDate() let age = myDate.getFullYear() - yearBirth if (monthNow < monthBirth || (monthNow == monthBirth && dayNow < dayBirth)) { age-- } // 得到年龄 sexAndAge.age = age return sexAndAge.age } // 身份证格式校验 export const checkIdCard = function(sIdCard) { //Wi 加权因子 Xi 余数0~10对应的校验码 Pi省份代码 let Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2], Xi = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2], Pi = [11, 12, 13, 14, 15, 21, 22, 23, 31, 32, 33, 34, 35, 36, 37, 41, 42, 43, 44, 45, 46, 50, 51, 52, 53, 54, 61, 62, 63, 64, 65, 71, 81, 82, 91 ], checkStatus = 0; // 检查身份证长度 if (sIdCard.length == 18) { checkStatus += 1; } //检验输入的省份编码是否有效 if (checkStatus >= 1) { let p2 = sIdCard.substr(0, 2); for (let i = 0; i < Pi.length; i++) { if (Pi[i] == p2) { checkStatus += 1; } } } //检验18位身份证号码出生日期是否有效 //parseFloat过滤前导零,年份必需大于等于1900且小于等于当前年份,用Date()对象判断日期是否有效。 if (checkStatus >= 2) { let year = parseFloat(sIdCard.substr(6, 4)); let month = parseFloat(sIdCard.substr(10, 2)); let day = parseFloat(sIdCard.substr(12, 2)); let checkDay = new Date(year, month - 1, day); let nowDay = new Date(); if (1900 <= year && year <= nowDay.getFullYear() && month == (checkDay.getMonth() + 1) && day == checkDay .getDate()) { checkStatus += 1; } } //检验校验码是否有效 if (checkStatus >= 3) { let aIdCard = sIdCard.split(""); let sum = 0; for (let j = 0; j < Wi.length; j++) { sum += Wi[j] * aIdCard[j]; //线性加权求和 } let index = sum % 11; //求模,可能为0~10,可求对应的校验码是否于身份证的校验码匹配 if (Xi[index] == aIdCard[17].toUpperCase()) { checkStatus += 1; } } if (checkStatus == 4) { return true; } else { return false; } }; /** * 时间转换为XX前 */ export const clickDateDiff = function(value) { var result; var minute = 1000 * 60; var hour = minute * 60; var day = hour * 24; var month = day * 30; var now = new Date().getTime(); var diffValue = parseInt(now) - parseInt(value); if (diffValue < 0) { return; } var monthC = diffValue / month; var weekC = diffValue / (7 * day); var dayC = diffValue / day; var hourC = diffValue / hour; var minC = diffValue / minute; if (monthC >= 1) { result = "" + parseInt(monthC) + '月前'; } else if (weekC >= 1) { result = "" + parseInt(weekC) + '周前'; } else if (dayC >= 1) { result = "" + parseInt(dayC) + '天前'; } else if (hourC >= 1) { result = "" + parseInt(hourC) + '小时前'; } else if (minC >= 1) { result = "" + parseInt(minC) + '分钟前'; } else { result = '刚刚'; } return result; }; /** * 时间戳转换为想要的时间格式 */ //时间戳转换为时间 format('yyyy-MM-dd hh:mm:ss') //时间格式转换 Date.prototype.format = function(fmt = 'yyyy-MM-dd hh:mm:ss') { //author: meizz var 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 (var 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; }; export const delEmptyQueryNodes = (obj) => { Object.keys(obj).forEach((key) => { let value = obj[key]; (value === '' || value === null || value === undefined || value.length === 0 || Object.keys( value) .length === 0) && delete obj[key]; }); return obj; };