export function timeFix () { const time = new Date() const hour = time.getHours() return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好' } export function welcome () { const arr = ['休息一会儿吧', '准备吃什么呢?', '要不要喝杯咖啡', '我猜你可能累了'] const index = Math.floor(Math.random() * arr.length) return arr[index] } /** * 触发 window.resize */ export function triggerWindowResizeEvent () { const event = document.createEvent('HTMLEvents') event.initEvent('resize', true, true) event.eventType = 'message' window.dispatchEvent(event) } export function handleScrollHeader (callback) { let timer = 0 let beforeScrollTop = window.pageYOffset callback = callback || function () {} window.addEventListener( 'scroll', event => { clearTimeout(timer) timer = setTimeout(() => { let direction = 'up' const afterScrollTop = window.pageYOffset const delta = afterScrollTop - beforeScrollTop if (delta === 0) { return false } direction = delta > 0 ? 'down' : 'up' callback(direction) beforeScrollTop = afterScrollTop }, 50) }, false ) } /** * Remove loading animate * @param id parent element id or class * @param timeout */ export function removeLoadingAnimate (id = '', timeout = 1500) { if (id === '') { return } setTimeout(() => { document.body.removeChild(document.getElementById(id)) }, timeout) } /** * 身份证15位编码规则:dddddd yymmdd xx p * dddddd:6位地区编码 * yymmdd: 出生年(两位年)月日,如:910215 * xx: 顺序编码,系统产生,无法确定 * p: 性别,奇数为男,偶数为女 * * 身份证18位编码规则:dddddd yyyymmdd xxx y * dddddd:6位地区编码 * yyyymmdd: 出生年(四位年)月日,如:19910215 * xxx:顺序编码,系统产生,无法确定,奇数为男,偶数为女 * y: 校验码,该位数值可通过前17位计算获得 * * 前17位号码加权因子为 Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ] * 验证位 Y = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ] * 如果验证码恰好是10,为了保证身份证是十八位,那么第十八位将用X来代替 * 校验位计算公式:Y_P = mod( ∑(Ai×Wi),11 ) * i为身份证号码1...17 位; Y_P为校验码Y所在校验码数组位置 */ export function isIdCardNo (num) { num = num.toUpperCase() // 身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X。 if (!(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(num))) { // alert('输入的身份证号长度不对,或者号码不符合规定!\n15位号码应全为数字,18位号码末位可以为数字或X。'); // alert('身份证号长度不正确或不符合规定!'); return false } // 验证前2位,城市符合 const aCity = { 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: '国外' } if (aCity[parseInt(num.substr(0, 2))] == null) { // alert('身份证号不正确或不符合规定!'); return false } // alert('城市:'+aCity[parseInt(num.substr(0,2))]); // 下面分别分析出生日期和校验位 let re const len = num.length if (len === 15) { re = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/) const arrSplit = num.match(re) // 检查生日日期是否正确 const dtmBirth = new Date('19' + arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4]) const bGoodDay = (dtmBirth.getYear() === Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) === Number(arrSplit[3])) && (dtmBirth.getDate() === Number(arrSplit[4])) if (!bGoodDay) { // alert('身份证号的出生日期不对!'); return false } else { // 将15位身份证转成18位 //校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。 const arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] const arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'] let nTemp = 0; let i num = num.substr(0, 6) + '19' + num.substr(6, num.length - 6) for (i = 0; i < 17; i++) { nTemp += num.substr(i, 1) * arrInt[i] } num += arrCh[nTemp % 11] return true } } if (len === 18) { re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/) const arrSplit = num.match(re) // 检查生日日期是否正确 const dtmBirth = new Date(arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4]) const bGoodDay = (dtmBirth.getFullYear() === Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) === Number(arrSplit[3])) && (dtmBirth.getDate() === Number(arrSplit[4])) if (!bGoodDay) { // alert(dtmBirth.getYear()); // alert(arrSplit[2]); // alert('身份证号的出生日期不对!'); return false } else { // 检验18位身份证的校验码是否正确。 //校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。 const arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] const arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'] let nTemp = 0; let i for (i = 0; i < 17; i++) { nTemp += num.substr(i, 1) * arrInt[i] } const valnum = arrCh[nTemp % 11] if (valnum !== num.substr(17, 1)) { // alert('18位身份证的校验码不正确!应该为:' + valnum); // alert('18位身份证号的校验码不正确!'); return false } return true } } return false } export function formatDate (date, fmt) { console.log(date) if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)) } const o = { 'M+': date.getMonth() + 1, 'd+': date.getDate(), 'h+': date.getHours(), 'm+': date.getMinutes(), 's+': date.getSeconds() } for (const k in o) { if (new RegExp(`(${k})`).test(fmt)) { const str = o[k] + '' fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str)) } } return fmt } export function timestampToTime (cjsj) { // 时间戳为10位需*1000,时间戳为13位的话不需乘1000 const date = new Date(cjsj) const Y = date.getFullYear() + '-' const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-' const D = date.getDate() + ' ' const h = date.getHours() + ':' const m = date.getMinutes() + ':' const s = date.getSeconds() return Y + M + D + h + m + s } function padLeftZero (str) { return ('00' + str).substr(str.length) } export function calculateRepayInterest (rate, capital, days) { return Math.round(rate / 360 * capital * days) / 100 } export function transformNumberIntoCHN (number) { if (!/^(0|[1-9]\d*)(\.\d{0,2})?$/.test(number)) { this.$message.error('不合法的金额:' + number) return '' } let unit = '仟佰拾亿仟佰拾万仟佰拾元角分' let str = '' number += '00' const point = number.indexOf('.') if (point >= 0) { number = number.substring(0, point) + number.substr(point + 1, 2) } unit = unit.substr(unit.length - number.length) for (let i = 0; i < number.length; i++) { str += '零壹贰叁肆伍陆柒捌玖'.charAt(number.charAt(i)) + unit.charAt(i) } if (str.replace(/零(仟|佰|拾|角)/g, '零').replace(/(零)+/g, '零').replace(/零(万|亿|元)/g, '$1').replace(/(亿)万|(拾)/g, '$1$2').replace(/^元零?|零分/g, '').replace(/元$/g, '元整') === '分') { return '零元整' } else { return str.replace(/零(仟|佰|拾|角)/g, '零').replace(/(零)+/g, '零').replace(/零(万|亿|元)/g, '$1').replace(/(亿)万|(拾)/g, '$1$2').replace(/^元零?|零分/g, '').replace(/元$/g, '元整') } }