123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- export const getOperationalPrecision = (num1, num2) => {
- const val = ((num1 * 10000) * (num2 * 10000) / 100000000).toFixed(2) || 0
- return val != 0 ? val : 0
- }
- export const forEach = (arr, fn) => {
- if (!arr.length || !fn) return
- let i = -1
- const len = arr.length
- while (++i < len) {
- const item = arr[i]
- fn(item, i, arr)
- }
- }
- export const getIntersection = (arr1, arr2) => {
- const len = Math.min(arr1.length, arr2.length)
- let i = -1
- const res = []
- while (++i < len) {
- const item = arr2[i]
- if (arr1.indexOf(item) > -1) res.push(item)
- }
- return res
- }
- export const getUnion = (arr1, arr2) => {
- return Array.from(new Set([...arr1, ...arr2]))
- }
- export const hasOneOf = (targetarr, arr) => {
- if (!targetarr) return true
- if (!arr) return true
- return targetarr.some(_ => arr.indexOf(_) > -1)
- }
- export function oneOf (value, validList) {
- for (let i = 0; i < validList.length; i++) {
- if (value === validList[i]) {
- return true
- }
- }
- return false
- }
- const isMillisecond = timeStamp => {
- const timeStr = String(timeStamp)
- return timeStr.length > 10
- }
- const isEarly = (timeStamp, currentTime) => {
- return timeStamp < currentTime
- }
- const getHandledValue = num => {
- return num < 10 ? '0' + num : num
- }
- const getDate = (timeStamp, startType) => {
- const d = new Date(timeStamp * 1000)
- const year = d.getFullYear()
- const month = getHandledValue(d.getMonth() + 1)
- const date = getHandledValue(d.getDate())
- const hours = getHandledValue(d.getHours())
- const minutes = getHandledValue(d.getMinutes())
- const second = getHandledValue(d.getSeconds())
- let resStr = ''
- if (startType === 'year') resStr = year + '-' + month + '-' + date + ' ' + hours + ':' + minutes + ':' + second
- else resStr = month + '-' + date + ' ' + hours + ':' + minutes
- return resStr
- }
- export const formtDate = (timeStamp, fmt) => {
- const d = new Date(timeStamp)
- var o = {
- 'M+': d.getMonth() + 1,
- 'd+': d.getDate(),
- 'h+': d.getHours(),
- 'm+': d.getMinutes(),
- 's+': d.getSeconds(),
- 'q+': Math.floor((d.getMonth() + 3) / 3),
- 'S': d.getMilliseconds()
- }
- if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (d.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 getThreeMonthsAfter = (dtstr) => {
- var s = dtstr.split('-')
- var yy = parseInt(s[0])
- var mm = parseInt(s[1])
- var dd = parseInt(s[2])
- var dt = new Date(yy, mm + 2, dd)
- return dt.valueOf()
- }
- export const getRelativeTime = timeStamp => {
-
- const IS_MILLISECOND = isMillisecond(timeStamp)
-
- if (IS_MILLISECOND) Math.floor(timeStamp /= 1000)
-
- timeStamp = Number(timeStamp)
-
- const currentTime = Math.floor(Date.parse(new Date()) / 1000)
-
- const IS_EARLY = isEarly(timeStamp, currentTime)
-
- let diff = currentTime - timeStamp
-
- if (!IS_EARLY) diff = -diff
- let resStr = ''
- const dirStr = IS_EARLY ? '前' : '后'
-
- if (diff <= 59) resStr = diff + '秒' + dirStr
-
- else if (diff > 59 && diff <= 3599) resStr = Math.floor(diff / 60) + '分钟' + dirStr
-
- else if (diff > 3599 && diff <= 86399) resStr = Math.floor(diff / 3600) + '小时' + dirStr
-
- else if (diff > 86399 && diff <= 2623859) resStr = Math.floor(diff / 86400) + '天' + dirStr
-
- else if (diff > 2623859 && diff <= 31567859 && IS_EARLY) resStr = getDate(timeStamp)
- else resStr = getDate(timeStamp, 'year')
- return resStr
- }
- export const formatSubmitDate = (val, type) => {
- if (val == null || val == '' || val == undefined) {
- return ''
- } else {
- const _date = new Date(val)
- const _year = _date.getFullYear()
- const _montn = (_date.getMonth() + 1) < 10 ? '0' + (_date.getMonth() + 1) : (_date.getMonth() + 1)
- const _day = _date.getDate() < 10 ? '0' + _date.getDate() : _date.getDate()
- const _hour = _date.getHours() < 10 ? '0' + _date.getHours() : _date.getHours()
- const _minutes = _date.getMinutes() < 10 ? '0' + _date.getMinutes() : _date.getMinutes()
- const _seconds = _date.getSeconds() < 10 ? '0' + _date.getSeconds() : _date.getSeconds()
- if (type == 'minutes') return _year + '-' + _montn + '-' + _day + ' ' + _hour + ':' + _minutes
- else if (type == 'seconds') return _year + '-' + _montn + '-' + _day + ' ' + _hour + ':' + _minutes + ':' + _seconds
- else return _year + '-' + _montn + '-' + _day
- }
- }
- export const isLicensePlate = function (str) {
- return /^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/.test(str)
- }
- export const isCarNumber = function (str) {
- let _value = str + ''
- _value = _value.replace(/[^\w\.挂学警港澳使领]/ig, '')
- return _value
- }
- export const numberToFixed = function (val, num) {
- let _value = val + ''
- _value = _value.replace(/[^\d.]/g, '')
- _value = _value.replace(/^\./g, '')
- _value = _value.replace(/\.{2,}/g, '')
- if (num == 1)_value = _value.replace(/^(\-)*(\d+)\.(\d).*$/, '$1$2.$3')
- else if (num == 3)_value = _value.replace(/^(\-)*(\d+)\.(\d\d\d).*$/, '$1$2.$3')
- else if (num == 4)_value = _value.replace(/^(\-)*(\d+)\.(\d\d\d\d).*$/, '$1$2.$3')
- else if (num == 5)_value = _value.replace(/^(\-)*(\d+)\.(\d\d\d\d\d).*$/, '$1$2.$3')
- else if (num == 0)_value = _value.replace(/^(\-)*(\d+)\.*$/, '$1$2')
- else _value = _value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
- return _value
- }
- export const formatDecimal = function (num, decimal) {
- num = num.toString()
- const index = num.indexOf('.')
- if (index !== -1) {
- num = num.substring(0, decimal + index + 1)
- } else {
- num = num.substring(0)
- }
- return parseFloat(num).toFixed(decimal)
- }
- export const toThousands = (num, decimal) => {
- if (num == undefined) {
- return '--'
- }
- if (decimal) {
- num = formatDecimal(num, decimal)
- }
- return num.toString().replace(/\d+/, function (n) {
- return n.replace(/(\d)(?=(\d{3})+$)/g, function ($1) {
- return $1 + ','
- })
- })
- }
- export const justNumber = function (val) {
- let _value = val + ''
- _value = _value.replace(/\D/g, '')
- return _value
- }
- export const getExplorer = () => {
- const ua = window.navigator.userAgent
- const isExplorer = (exp) => {
- return ua.indexOf(exp) > -1
- }
- if (isExplorer('MSIE')) return 'IE'
- else if (isExplorer('Firefox')) return 'Firefox'
- else if (isExplorer('Chrome')) return 'Chrome'
- else if (isExplorer('Opera')) return 'Opera'
- else if (isExplorer('Safari')) return 'Safari'
- }
- export const on = (function () {
- if (document.addEventListener) {
- return function (element, event, handler) {
- if (element && event && handler) {
- element.addEventListener(event, handler, false)
- }
- }
- } else {
- return function (element, event, handler) {
- if (element && event && handler) {
- element.attachEvent('on' + event, handler)
- }
- }
- }
- })()
- export const off = (function () {
- if (document.removeEventListener) {
- return function (element, event, handler) {
- if (element && event) {
- element.removeEventListener(event, handler, false)
- }
- }
- } else {
- return function (element, event, handler) {
- if (element && event) {
- element.detachEvent('on' + event, handler)
- }
- }
- }
- })()
- export const hasKey = (obj, key) => {
- if (key) return key in obj
- else {
- const keysArr = Object.keys(obj)
- return keysArr.length
- }
- }
- export const objEqual = (obj1, obj2) => {
- const keysArr1 = Object.keys(obj1)
- const keysArr2 = Object.keys(obj2)
- if (keysArr1.length !== keysArr2.length) return false
- else if (keysArr1.length === 0 && keysArr2.length === 0) return true
-
- else return !keysArr1.some(key => obj1[key] != obj2[key])
- }
- export const removeListById = (id, list) => {
- list.splice(list.findIndex(item => item.id === id), 1)
- }
- export const objExtend = (obj1, obj2) => {
- for (var a in obj1) {
- obj2[a] = obj1[a]
- }
- return obj2
- }
- export const cloneObj = (obj) => {
- const ret = {}
- for (var a in obj) {
- ret[a] = obj[a]
- }
- return ret
- }
- export const checkIdNumberValid = (tex) => {
-
- let num = tex
- num = num.toUpperCase()
- const len = num.length
- let re
- if (len == 0) return true
-
- if (!(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(num))) {
- return false
- }
-
- 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) {
- return false
- }
-
- 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) {
- return false
- }
- }
-
- 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) {
- return false
- } else {
-
-
- let valnum
- 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]
- }
- valnum = arrCh[nTemp % 11]
- if (valnum != num.substr(17, 1)) {
- return false
- }
- }
- }
- return true
- }
|