|
@@ -0,0 +1,590 @@
|
|
|
|
+// 字符串处理相关函数
|
|
|
|
+// 手机号中间4位加* ------------>
|
|
|
|
+export function addConfusionForTel (tel) {
|
|
|
|
+ const reg = /^(\d{3})\d{4}(\d{4})$/
|
|
|
|
+ return tel.replace(reg, '$1****$2')
|
|
|
|
+}
|
|
|
|
+// 手机号加空格 ------------>
|
|
|
|
+export function addSpaceForTel (tel) {
|
|
|
|
+ const regMap = {
|
|
|
|
+ isConfuse: /^(\d{3})\*{4}(\d{4})$/,
|
|
|
|
+ addSpace: /^(\d{3})(\d{4})(\d{4})$/
|
|
|
|
+ }
|
|
|
|
+ const confusion = regMap.isConfuse.test(tel)
|
|
|
|
+ if (confusion) {
|
|
|
|
+ return tel.replace(regMap.isConfuse, '$1 **** $2')
|
|
|
|
+ } else {
|
|
|
|
+ return tel.replace(regMap.addSpace, '$1 $2 $3')
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+// 银行卡加空格
|
|
|
|
+export function addSpaceForBank (v, hasConfusion = false) {
|
|
|
|
+ // 无*银行卡号加空格
|
|
|
|
+ if (hasConfusion) {
|
|
|
|
+ // 带有*的银行卡号字符串加空格
|
|
|
|
+ return v.replace(/\s/g, '').replace(/(.{4})/g, '$1 ')
|
|
|
|
+ } else {
|
|
|
|
+ // 纯数字银行卡号字符串加空格
|
|
|
|
+ return v.replace(/\s/g, '').replace(/(\d{4})(?=\d)/g, '$1 ')
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+// 银行卡加 **
|
|
|
|
+export function addConfusionForBank (v) {
|
|
|
|
+ if (String(v).length < 12) {
|
|
|
|
+ return v.replace(/\s/g, '').replace(/^(\d{2})\d+(\d{2})$/, '$1 **** **** $2')
|
|
|
|
+ } else {
|
|
|
|
+ return v.replace(/\s/g, '').replace(/^(\d{4})\d+(\d{4})$/, '$1 **** **** $2')
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 金额处理
|
|
|
|
+// 分转元
|
|
|
|
+export function fen2Yuan (v) {
|
|
|
|
+ return v / 100
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 金额大写,链接:https://juejin.im/post/5a2a7a5051882535cd4abfce
|
|
|
|
+// upDigit(1682) result:"人民币壹仟陆佰捌拾贰元整"
|
|
|
|
+// upDigit(-1693) result:"欠壹仟陆佰玖拾叁元整"
|
|
|
|
+export function upPrice (n) {
|
|
|
|
+ const fraction = ['角', '分', '厘']
|
|
|
|
+ const digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
|
|
|
|
+ const unit = [
|
|
|
|
+ ['元', '万', '亿'],
|
|
|
|
+ ['', '拾', '佰', '仟']
|
|
|
|
+ ]
|
|
|
|
+ // const head = n < 0 ? '欠人民币' : '人民币'
|
|
|
|
+ const head = ''
|
|
|
|
+ n = Math.abs(n)
|
|
|
|
+ let s = ''
|
|
|
|
+ for (let i = 0; i < fraction.length; i++) {
|
|
|
|
+ s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '')
|
|
|
|
+ }
|
|
|
|
+ s = s || '整'
|
|
|
|
+ n = Math.floor(n)
|
|
|
|
+ for (let i = 0; i < unit[0].length && n > 0; i++) {
|
|
|
|
+ let p = ''
|
|
|
|
+ for (let j = 0; j < unit[1].length && n > 0; j++) {
|
|
|
|
+ p = digit[n % 10] + unit[1][j] + p
|
|
|
|
+ n = Math.floor(n / 10)
|
|
|
|
+ }
|
|
|
|
+ s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s
|
|
|
|
+ // s = p + unit[0][i] + s;
|
|
|
|
+ }
|
|
|
|
+ return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整')
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 金额3位逗号分隔 ------------>
|
|
|
|
+/**
|
|
|
|
+ * @param s 要格式化的数字(四舍五入)
|
|
|
|
+ * @param n 保留几位小数(不传或者传-1 --> 如果为整数,则不保留小数。如果为浮点数,则保留两位小数)
|
|
|
|
+ * @param comma 是否小数点前每3位添加逗号
|
|
|
|
+ */
|
|
|
|
+export function newFormat (s = 0, n = -1, comma = false) {
|
|
|
|
+ n = n === -1 ? 0 : n
|
|
|
|
+ if (n > 20 || n < -1) {
|
|
|
|
+ n = 2
|
|
|
|
+ }
|
|
|
|
+ s = Number(s)
|
|
|
|
+ return s.toLocaleString('zh-CN', {
|
|
|
|
+ style: 'decimal',
|
|
|
|
+ useGrouping: comma,
|
|
|
|
+ minimumFractionDigits: n,
|
|
|
|
+ maximumFractionDigits: n
|
|
|
|
+ })
|
|
|
|
+}
|
|
|
|
+export function formatPrice (s, n = -1, comma = false) {
|
|
|
|
+ // 如果不传s或者s为空,则直接返回0
|
|
|
|
+ if (!s) return 0
|
|
|
|
+
|
|
|
|
+ if (n !== -1) n = n > 0 && n <= 20 ? n : 2
|
|
|
|
+ const intS = parseInt(String(s))
|
|
|
|
+ let point = '.'
|
|
|
|
+ let left = []
|
|
|
|
+ let right = ''
|
|
|
|
+ s = parseFloat((s + '').replace(/[^\d.-]/g, ''))
|
|
|
|
+ // 没传n或者n为-1,默认(如果为整数,则不保留小数。如果为浮点数,则保留两位小数)
|
|
|
|
+ if (n === -1) {
|
|
|
|
+ if (s === intS) {
|
|
|
|
+ n = 0
|
|
|
|
+ right = ''
|
|
|
|
+ point = ''
|
|
|
|
+ } else {
|
|
|
|
+ n = 2
|
|
|
|
+ s = s.toFixed(n)
|
|
|
|
+ right = s.split('.')[1]
|
|
|
|
+ }
|
|
|
|
+ s = s + ''
|
|
|
|
+ left = s.split('.')[0].split('').reverse()
|
|
|
|
+ } else {
|
|
|
|
+ s = parseFloat((s + '').replace(/[^\d.-]/g, '')).toFixed(n) + ''
|
|
|
|
+ left = s.split('.')[0].split('').reverse()
|
|
|
|
+ right = s.split('.')[1]
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (comma) {
|
|
|
|
+ let t = ''
|
|
|
|
+ for (let i = 0; i < left.length; i++) {
|
|
|
|
+ t += left[i] + ((i + 1) % 3 === 0 && (i + 1) !== left.length ? ',' : '')
|
|
|
|
+ }
|
|
|
|
+ return t.split('').reverse().join('') + point + right
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return left.reverse().join('') + point + right
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 时间格式化相关函数
|
|
|
|
+/*
|
|
|
|
+* 时间格式化函数(将时间格式化为,2019年08月12日,2019-08-12,2019/08/12的形式)
|
|
|
|
+* pattern参数(想要什么格式的数据就传入什么格式的数据)
|
|
|
|
+* · 'yyyy-MM-dd' ---> 输出如2019-09-20
|
|
|
|
+* · 'yyyy-MM-dd HH:mm' ---> 输出如2019-09-20 18:20
|
|
|
|
+* · 'yyyy-MM-dd HH:mm:ss' ---> 输出如2019-09-20 06:20:23
|
|
|
|
+* · 'yyyy/MM/dd' ---> 输出如2019/09/20
|
|
|
|
+* · 'yyyy年MM月dd日' ---> 输出如2019年09月20日
|
|
|
|
+* · 'yyyy年MM月dd日 HH时mm分' ---> 输出如2019年09月20日 18时20分
|
|
|
|
+* · 'yyyy年MM月dd日 HH时mm分ss秒' ---> 输出如2019年09月20日 18时20分23秒
|
|
|
|
+* · 'yyyy年MM月dd日 HH时mm分ss秒 EE' ---> 输出如2019年09月20日 18时20分23秒 周二
|
|
|
|
+* · 'yyyy年MM月dd日 HH时mm分ss秒 EEE' ---> 输出如2019年09月20日 18时20分23秒 星期二
|
|
|
|
+* 参考: https://www.cnblogs.com/mr-wuxiansheng/p/6296646.html
|
|
|
|
+*/
|
|
|
|
+export function dateFormatter (date, fmt = 'yyyy-MM-dd HH:mm:ss') {
|
|
|
|
+ // 将传入的date转为时间对象
|
|
|
|
+ if (!date) return ''
|
|
|
|
+ date = new Date(date)
|
|
|
|
+ const o = {
|
|
|
|
+ 'y+': date.getFullYear(),
|
|
|
|
+ 'M+': date.getMonth() + 1, // 月份
|
|
|
|
+ 'd+': date.getDate(), // 日
|
|
|
|
+ // 12小时制
|
|
|
|
+ 'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, // 小时
|
|
|
|
+ // 24小时制
|
|
|
|
+ 'H+': date.getHours(), // 小时
|
|
|
|
+ 'm+': date.getMinutes(), // 分
|
|
|
|
+ 's+': date.getSeconds(), // 秒
|
|
|
|
+ 'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
|
|
|
|
+ S: date.getMilliseconds(), // 毫秒
|
|
|
|
+ 'E+': date.getDay() // 周
|
|
|
|
+ }
|
|
|
|
+ const week = ['日', '一', '二', '三', '四', '五', '六']
|
|
|
|
+
|
|
|
|
+ if (/(y+)/.test(fmt)) {
|
|
|
|
+ fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
|
|
|
|
+ }
|
|
|
|
+ if (/(E+)/.test(fmt)) {
|
|
|
|
+ fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '星期' : '周') : '') + week[date.getDay()])
|
|
|
|
+ }
|
|
|
|
+ for (const 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 debounce = (func, delay = 200, immediate) => {
|
|
|
|
+ let timer = null
|
|
|
|
+ return function () {
|
|
|
|
+ const context = this
|
|
|
|
+ const args = arguments
|
|
|
|
+ if (timer) clearTimeout(timer)
|
|
|
|
+ if (immediate) {
|
|
|
|
+ const doNow = !timer
|
|
|
|
+ timer = setTimeout(function () {
|
|
|
|
+ timer = null
|
|
|
|
+ }, delay)
|
|
|
|
+ if (doNow) {
|
|
|
|
+ func.apply(context, args)
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ timer = setTimeout(function () {
|
|
|
|
+ func.apply(context, args)
|
|
|
|
+ }, delay)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 时间戳转换 多少秒、多少分、多少小时前、多少天前 超出10天显示年月日
|
|
|
|
+// 传入一个时间戳
|
|
|
|
+export function dateFromNow (originTime, useOld = false) {
|
|
|
|
+ if (!originTime) return
|
|
|
|
+ // 原始时间 - 传入的时间戳
|
|
|
|
+ const originTimeStamp = +new Date(originTime)
|
|
|
|
+ // 当前时间戳
|
|
|
|
+ const nowTimeStamp = +new Date()
|
|
|
|
+ // 时间戳相差多少
|
|
|
|
+ const diffTimeStamp = nowTimeStamp - originTimeStamp
|
|
|
|
+
|
|
|
|
+ const postfix = diffTimeStamp > 0 ? '前' : '后'
|
|
|
|
+ // 求绝对值 ms(毫秒)
|
|
|
|
+ const diffTimeStampAbsMs = Math.abs(diffTimeStamp)
|
|
|
|
+ const diffTimeStampAbsS = Math.round(diffTimeStampAbsMs / 1000)
|
|
|
|
+
|
|
|
|
+ // 10天的秒数
|
|
|
|
+ const days11 = 11 * 24 * 60 * 60
|
|
|
|
+
|
|
|
|
+ const dataMap = {
|
|
|
|
+ zh: ['天', '小时', '分钟', '秒'],
|
|
|
|
+ number: [24 * 60 * 60, 60 * 60, 60, 1]
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ let timeString = ''
|
|
|
|
+ // 10天前
|
|
|
|
+ const tenDaysAgo = diffTimeStampAbsS > days11
|
|
|
|
+ // 是否是当天
|
|
|
|
+ const isCurrentDay = dateFormatter(originTimeStamp, 'yyyy.MM.dd') === dateFormatter(nowTimeStamp, 'yyyy.MM.dd')
|
|
|
|
+
|
|
|
|
+ let condition = !isCurrentDay
|
|
|
|
+ if (useOld) {
|
|
|
|
+ condition = tenDaysAgo
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (condition) {
|
|
|
|
+ // 不是当天,则使用正常日期显示
|
|
|
|
+ const originDate = new Date(originTimeStamp)
|
|
|
|
+ const nowDate = new Date()
|
|
|
|
+ // 是否同年
|
|
|
|
+ const sameYear = originDate.getFullYear() === nowDate.getFullYear()
|
|
|
|
+ // 如果是当年,则不显示年
|
|
|
|
+ const patternString = sameYear ? 'MM-dd' : 'yyyy-MM-dd'
|
|
|
|
+ timeString = dateFormatter(originDate, patternString)
|
|
|
|
+ } else {
|
|
|
|
+ for (let i = 0; i < dataMap.number.length; i++) {
|
|
|
|
+ const inm = Math.floor(diffTimeStampAbsS / dataMap.number[i])
|
|
|
|
+ if (inm !== 0) {
|
|
|
|
+ timeString = inm + dataMap.zh[i] + postfix
|
|
|
|
+ break
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return timeString
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 文件大小格式化
|
|
|
|
+export function formatSize (size, pointLength, units) {
|
|
|
|
+ size = Number(size)
|
|
|
|
+ let unit = ''
|
|
|
|
+ units = units || ['B', 'K', 'M', 'G', 'TB']
|
|
|
|
+ while ((unit = units.shift()) && size > 1024) {
|
|
|
|
+ size = size / 1024
|
|
|
|
+ }
|
|
|
|
+ return (unit === 'B' ? size : size.toFixed(pointLength === undefined ? 2 : pointLength)) + (unit || '')
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 金额类型转换
|
|
|
|
+export function moneyUnit (m, type = 'string', lv = 0) {
|
|
|
|
+ const mUnit = {
|
|
|
|
+ levelArr: ['元', '万', '亿', '万亿'],
|
|
|
|
+ test (num, type, lv) {
|
|
|
|
+ if (num === 0) {
|
|
|
|
+ if (type === 'string') {
|
|
|
|
+ return '0元'
|
|
|
|
+ }
|
|
|
|
+ if (type === 'lv') {
|
|
|
|
+ return this.levelArr[lv]
|
|
|
|
+ }
|
|
|
|
+ if (type === 'number') {
|
|
|
|
+ return 0
|
|
|
|
+ }
|
|
|
|
+ if (type === 'index') {
|
|
|
|
+ return lv
|
|
|
|
+ }
|
|
|
|
+ if (type === 'transfer') {
|
|
|
|
+ return 0
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ const result = num / Math.pow(10000, lv)
|
|
|
|
+
|
|
|
|
+ if (result > 10000 && lv < 2) {
|
|
|
|
+ return this.test(num, type, lv + 1)
|
|
|
|
+ } else {
|
|
|
|
+ if (type === 'string') {
|
|
|
|
+ return String(Math.floor(result * 100) / 100).replace('.00', '') + this.levelArr[lv]
|
|
|
|
+ }
|
|
|
|
+ if (type === 'lv') {
|
|
|
|
+ return this.levelArr[lv]
|
|
|
|
+ }
|
|
|
|
+ if (type === 'number') {
|
|
|
|
+ return String(Math.floor(result * 100) / 100).replace('.00', '')
|
|
|
|
+ }
|
|
|
|
+ if (type === 'index') {
|
|
|
|
+ return lv
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ },
|
|
|
|
+ // 需要传入固定的lv(此时lv为 levelArr 中的一个)
|
|
|
|
+ transfer (num, lvString) {
|
|
|
|
+ const index = this.levelArr.indexOf(lvString)
|
|
|
|
+ if (index === -1 || index === 0) {
|
|
|
|
+ return num
|
|
|
|
+ } else {
|
|
|
|
+ return (num / Math.pow(10000, index)).toFixed(2) + lvString
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (m === undefined || m === null) {
|
|
|
|
+ return ''
|
|
|
|
+ } else {
|
|
|
|
+ if (type === 'transfer') {
|
|
|
|
+ return mUnit.transfer(m, lv)
|
|
|
|
+ } else {
|
|
|
|
+ return mUnit.test(m, type, lv)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 通用关键字高亮替换
|
|
|
|
+ * @param {String} value 要高亮的字符串
|
|
|
|
+ * @param {String|Array} oldChar 要被替换的字符串(或数组)
|
|
|
|
+ * @param {String|Array} newChar 要替换成的字符串(或数组)
|
|
|
|
+ *
|
|
|
|
+ * 比如:要将 - `剑鱼标讯工具函数` 字符串中的 `工具` 高亮
|
|
|
|
+ * 则此时 value -> `剑鱼标讯工具函数`
|
|
|
|
+ * oldChar -> `工具`
|
|
|
|
+ * newChar -> `<span class="highlight-text">工具</span>`
|
|
|
|
+ *
|
|
|
|
+ * 批量高亮-----
|
|
|
|
+ * 比如:要将 - `剑鱼标讯工具函数` 字符串中的 `工具` `剑鱼` 高亮
|
|
|
|
+ * 则此时 value -> `剑鱼标讯工具函数`批量高亮
|
|
|
|
+ * oldChar -> ['工具', '剑鱼']
|
|
|
|
+ * newChar -> ['<span class="highlight-text">', '</span>']
|
|
|
|
+ *
|
|
|
|
+ * 注意:此时newChar为一个长度为2的数组,数组中为高亮标签的起始标签和结束标签
|
|
|
|
+ *
|
|
|
|
+ */
|
|
|
|
+export function replaceKeyword (value, oldChar, newChar) {
|
|
|
|
+ if (!oldChar || !newChar) return value
|
|
|
|
+ // oldChar的字符串数组,用来循环替换
|
|
|
|
+ let oldCharArr = []
|
|
|
|
+
|
|
|
|
+ if (Array.isArray(oldChar)) {
|
|
|
|
+ oldCharArr = oldChar.concat()
|
|
|
|
+ } else {
|
|
|
|
+ oldCharArr.push(oldChar)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 数组去重
|
|
|
|
+ oldCharArr = Array.from(new Set(oldCharArr))
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ oldCharArr.forEach(function (item) {
|
|
|
|
+ // 去空格之后为空字符串,则直接跳过当前替换
|
|
|
|
+ if (item && item.replace(/\s+/g, '')) {
|
|
|
|
+ let oc = item
|
|
|
|
+ oc = oc.replace(/\$/g, '\\$')
|
|
|
|
+ .replace(/\(/g, '\\(')
|
|
|
|
+ .replace(/\)/g, '\\)')
|
|
|
|
+ .replace(/\*/g, '\\*')
|
|
|
|
+ .replace(/\+/g, '\\+')
|
|
|
|
+ .replace(/\./g, '\\.')
|
|
|
|
+ .replace(/\[/g, '\\[')
|
|
|
|
+ .replace(/\]/g, '\\]')
|
|
|
|
+ .replace(/\?/g, '\\?')
|
|
|
|
+ .replace(/\\/g, '\\')
|
|
|
|
+ .replace(/\//g, '\\/')
|
|
|
|
+ .replace(/\^/g, '\\^')
|
|
|
|
+ .replace(/\{/g, '\\{')
|
|
|
|
+ .replace(/\}/g, '\\}')
|
|
|
|
+ .replace(/\|/g, '\\|')
|
|
|
|
+
|
|
|
|
+ if (Array.isArray(newChar)) {
|
|
|
|
+ // 批量高亮
|
|
|
|
+ const tempV = value
|
|
|
|
+ value = value.replace(new RegExp('(' + oc + ')', 'gmi'), newChar[0] + oc + newChar[1])
|
|
|
|
+ if (value === tempV && oc.indexOf('+') !== -1) {
|
|
|
|
+ const splitReg = oc.split('\\+')
|
|
|
|
+ splitReg.map((v) => {
|
|
|
|
+ value = value.replace(new RegExp('(' + v + ')', 'gmi'), newChar[0] + v + newChar[1])
|
|
|
|
+ return value
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ // 普通单个高亮
|
|
|
|
+ value = value.replace(new RegExp('(' + oc + ')', 'gmi'), newChar)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ } catch (e) {
|
|
|
|
+ console.log(e)
|
|
|
|
+ return value
|
|
|
|
+ }
|
|
|
|
+ return value
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 获取随机字符串
|
|
|
|
+// 不传参数则获取长度不固定的字符串
|
|
|
|
+export const getRandomString = (len) => {
|
|
|
|
+ let randomString = ''
|
|
|
|
+ if (len) {
|
|
|
|
+ /** 默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1 **/
|
|
|
|
+ const $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
|
|
|
|
+ const maxPos = $chars.length
|
|
|
|
+ for (let i = 0; i < len; i++) {
|
|
|
|
+ randomString += $chars.charAt(Math.floor(Math.random() * maxPos))
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ // Math.random() 生成随机数字, eg: 0.123456
|
|
|
|
+ // .toString(36) 转化成36进制 : "0.4fzyo82mvyr"
|
|
|
|
+ // .substring(2) 去掉前面两位 : "yo82mvyr"
|
|
|
|
+ // .slice(-8) 截取最后八位 : "yo82mvyr"
|
|
|
|
+ randomString = Math.random().toString(36).substring(2)
|
|
|
|
+ }
|
|
|
|
+ return randomString
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+export function getParam (name) {
|
|
|
|
+ let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
|
|
|
|
+ let r = window.location.search.substr(1).match(reg)
|
|
|
|
+ let context = '' // eslint-disable-line no-unused-vars
|
|
|
|
+ if (r !== null) context = r[2]
|
|
|
|
+ // 释放变量
|
|
|
|
+ reg = null
|
|
|
|
+ r = null
|
|
|
|
+ return context === null || context === '' || context === 'undefined' ? '' : context
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* eslint-disable */
|
|
|
|
+Number.prototype.fixed = function (len) {
|
|
|
|
+ len = isNaN(len) ? 0 : len
|
|
|
|
+ const num = Math.pow(10, len)
|
|
|
|
+ return Math.round(this * num) / num
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* eslint-disable */
|
|
|
|
+Date.prototype.pattern = function (fmt) {
|
|
|
|
+ if (!fmt) return ''
|
|
|
|
+ const o = {
|
|
|
|
+ 'y+': this.getFullYear(),
|
|
|
|
+ 'M+': this.getMonth() + 1, // 月份
|
|
|
|
+ 'd+': this.getDate(), // 日
|
|
|
|
+ // 12小时制
|
|
|
|
+ 'h+': this.getHours() % 12 === 0 ? 12 : this.getHours() % 12, // 小时
|
|
|
|
+ // 24小时制
|
|
|
|
+ 'H+': this.getHours(), // 小时
|
|
|
|
+ 'm+': this.getMinutes(), // 分
|
|
|
|
+ 's+': this.getSeconds(), // 秒
|
|
|
|
+ 'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
|
|
|
|
+ 'S': this.getMilliseconds(), // 毫秒
|
|
|
|
+ 'E+': this.getDay() // 周
|
|
|
|
+ }
|
|
|
|
+ const week = {
|
|
|
|
+ '0': '日',
|
|
|
|
+ '1': '一',
|
|
|
|
+ '2': '二',
|
|
|
|
+ '3': '三',
|
|
|
|
+ '4': '四',
|
|
|
|
+ '5': '五',
|
|
|
|
+ '6': '六'
|
|
|
|
+ }
|
|
|
|
+ if (/(y+)/.test(fmt)) {
|
|
|
|
+ fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
|
|
|
|
+ }
|
|
|
|
+ if (/(E+)/.test(fmt)) {
|
|
|
|
+ fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '星期' : '周') : '') + week[this.getDay() + ''])
|
|
|
|
+ }
|
|
|
|
+ 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 function bSort (arr, value) {
|
|
|
|
+ const len = arr.length
|
|
|
|
+ for (let i = 0; i < len - 1; i++) {
|
|
|
|
+ for (let j = 0; j < len - 1 - i; j++) {
|
|
|
|
+ // 相邻元素两两对比,元素交换,大的元素交换到后面
|
|
|
|
+ if (arr[j][value] > arr[j + 1][value]) {
|
|
|
|
+ const temp = arr[j]
|
|
|
|
+ arr[j] = arr[j + 1]
|
|
|
|
+ arr[j + 1] = temp
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return arr
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+export function recoveryPageData (key, defaultValues = {}) {
|
|
|
|
+ return JSON.parse(localStorage.getItem(key) || JSON.stringify(defaultValues))
|
|
|
|
+}
|
|
|
|
+export function defaultLocalPageData (key, defaultValues = {}) {
|
|
|
|
+ return JSON.parse(localStorage.getItem(key) || JSON.stringify(defaultValues))
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function dateTime(date, fmt) {
|
|
|
|
+ if (fmt === void 0) {
|
|
|
|
+ fmt = 'yyyy年MM月dd日 hh:mm';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 时间戳转换
|
|
|
|
+ if (!date) return '';
|
|
|
|
+ var d = new Date(date);
|
|
|
|
+ var o = {
|
|
|
|
+ "M+": d.getMonth() + 1,
|
|
|
|
+ //月份
|
|
|
|
+ "d+": d.getDate(),
|
|
|
|
+ //日
|
|
|
|
+ "h+": d.getHours(),
|
|
|
|
+ //小时
|
|
|
|
+ "m+": d.getMinutes(),
|
|
|
|
+ //分
|
|
|
|
+ "s+": d.getSeconds() //秒
|
|
|
|
+
|
|
|
|
+ }; // 根据y的长度来截取年
|
|
|
|
+
|
|
|
|
+ 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;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function dateYear() {
|
|
|
|
+ // 获取当年的第一天
|
|
|
|
+ var date = new Date();
|
|
|
|
+ date.setDate(1);
|
|
|
|
+ date.setMonth(0);
|
|
|
|
+ return date.getTime();
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function dateLast() {
|
|
|
|
+ // 获取昨天
|
|
|
|
+ var day = new Date();
|
|
|
|
+ day.setTime(day.getTime() - 24 * 60 * 60 * 1000);
|
|
|
|
+ return dateTime(day, 'yyyy/MM/dd');
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+export function dateMatter(time, drag = 'normal') {
|
|
|
|
+ // 时间转换
|
|
|
|
+ var timeStr = '';
|
|
|
|
+ var currentDay = dateTime(time, 'yyyy/MM/dd'); // 当前日期
|
|
|
|
+
|
|
|
|
+ var lingDay = new Date(new Date().toLocaleDateString()).getTime(); // 获取当天0点
|
|
|
|
+
|
|
|
|
+ var isTime = new Date(time) - lingDay;
|
|
|
|
+ var diff = isTime < 0 ? '昨天' : '';
|
|
|
|
+ var isDate = new Date(time) - dateYear();
|
|
|
|
+
|
|
|
|
+ if (isDate < 0) {
|
|
|
|
+ drag == 'normal' ? timeStr = dateTime(time) : timeStr = dateTime(time, 'yyyy年MM月dd日');
|
|
|
|
+ } else {
|
|
|
|
+ if (currentDay === dateLast()) {
|
|
|
|
+ drag == 'normal' ? timeStr = diff + dateTime(time, 'hh:mm') : timeStr = diff;
|
|
|
|
+ } else {
|
|
|
|
+ if (isTime < 0) {
|
|
|
|
+ drag == 'normal' ? timeStr = dateTime(time, 'MM月dd日 hh:mm') : timeStr = dateTime(time, 'MM月dd日');
|
|
|
|
+ } else {
|
|
|
|
+ timeStr = dateTime(time, 'hh:mm');
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return timeStr;
|
|
|
|
+}
|