持续更新中

本文主要整理一些工作中常用到的方法进行封装,以便日后使用

获取URL参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* @description: 获取查询字符串参数
* @param {search} query string {location.search}
* @return: Object
*/
const getQueryString = search => {
let urlArr = search.substring(1).split('&')
let params = {}
for (let i = 0; i < urlArr.length; i++) {
let arg = urlArr[i].split('=')
params[arg[0]] = arg[1]
}
return params
}

对象深拷贝

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* @description: 递归深拷贝对象每一层
* @param {obj} 原对象
* @return: Object
*/
const deepCopy = obj => {
let newObj = obj.constructor === Array ? [] : {}
if (typeof obj !== 'object') {
return obj
}
for (var i in obj) {
if (typeof obj[i] === 'object') {
newObj[i] = deepCopy(obj[i])
} else {
newObj[i] = obj[i]
}
}
return newObj
}

合并键值相同的对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* @description: 合并键值相同的对象
* @param {target} 需要处理的对象
* @param {source} 传过来的对象
* @return: Object
*/
const = mergeObj = (target, source) => {
const parmaKeys = Object.keys(target)
for (let key in source) {
if (parmaKeys.includes(key)) {
target[key] = source[key]
}
}
return target
}

生成随机字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* @description: 生成随机字符串
* @param {length} 字符串长度
* @return: String
*/
const randomString = (length = 8) => {
const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
let maxPos = chars.length
let str = ''
for (let i = 0; i < length; i++) {
str += chars.charAt(Math.floor(Math.random() * maxPos))
}
return str
}

生成随机色

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* @description: 生成12进制随机色
* @return: String
*/
const getRandomColor = () => {
const rgb = []
for (let i = 0; i < 3; ++i) {
let color = Math.floor(Math.random() * 256).toString(16)
color = color.length == 1 ? '0' + color : color
rgb.push(color)
}
return '#' + rgb.join('')
}

防抖函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* @description: 防抖
* @param {fn} 执行方法
* @param {wait} 延时
* @return {void}
*/

const debounce = (fn, wait = 50) => {
let timer = 0
return function (...args) {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
fn.apply(this, args)
}, wait)
}
}

节流函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* @description: 节流函数
* @param {fn} 执行方法
* @param {delay} 延迟
* @param {mustRunDelay} 必须执行时间
* @return {void}
*/
const throttle = (fn, delay = 100, mustRunDelay = 200) => {
let timer = null
let t_start
return function () {
let context = this, args = arguments, t_curr = +new Date()
clearTimeout(timer)
if (!t_start) {
t_start = t_curr
}
if (t_curr - t_start >= mustRunDelay) {
fn.apply(context, args)
t_start = t_curr
} else {
timer = setTimeout(function () {
fn.apply(context, args)
}, delay)
}
}
}

验证组织机构代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* @description 验证组织机构代码
* @param {code} 组织机构代码
* @return Boolean
*/
const checkOrgCodeValid = code => {
let values = code.split("-")
let ws = [3, 7, 9, 10, 5, 8, 4, 2]
let str = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
let reg = /^([0-9A-Z]){8}$/
if (!reg.test(values[0])) {
return false
}
let sum = 0
for (let i = 0; i < 8; i++) {
sum += str.indexOf(values[0].charAt(i)) * ws[i]
}
let C9 = 11 - (sum % 11)
let YC9 = values[1] + ''
if (C9 == 11) {
C9 = '0'
} else if (C9 == 10) {
C9 = 'X'
} else {
C9 = C9 + ''
}
return YC9 == C9
}

验证统一社会信用代码

1
2
3
4
5
6
7
8
9
10
/**
* @description 验证统一社会信用代码合法性
* @param {socialCode} 社会信用代码
* @return Boolean
*/
const isSocialCode = socialCode => {
const reg = /[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}/
return reg.test(socialCode)
}

时间格式化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* @description: 时间格式化
* @param {(Object|String|Number)}
* @param {String} cFormat
* @return String
*/
const parseTime = (time, cFormat) => {
if (arguments.length === 0) {
return null
}
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
let date
if (typeof time === 'object') {
date = time
} else {
if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
time = parseInt(time)
}
if ((typeof time === 'number') && (time.toString().length === 10)) {
time = time * 1000
}
date = new Date(time)
}
const formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay()
}
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
let value = formatObj[key]
if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
if (result.length > 0 && value < 10) {
value = '0' + value
}
return value || 0
})
return time_str
}

获取上个月第一天和最后一天

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* @description 取上个月第一天与最后一天
* @return Array
*/
const getPrevMonth = () => {
const nowdays = new Date()

let year = nowdays.getFullYear()
let month = nowdays.getMonth()

if (month === 0) {
month = 12
year = year - 1
}

if (month < 10) {
month = '0' + month
}

const firstDay = year + '-' + month + '-' + '01'
const myDate = new Date(year, month, 0)
const lastDay = year + '-' + month + '-' + myDate.getDate()

return [firstDay, lastDay]
}

计算两个日期相差多少天

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* @description: 计算两个日期之间相差多少天
* @param {startDate} - string 开始时间
* @param {endDate} - String 结束时间
*/
const intervalDate = (startDate, endDate) => {
let separator = "-"
let oDate1
let oDate2
let iDays
oDate1 = startDate.split(separator)
oDate2 = endDate.split(separator)
let strDateS = new Date(oDate1[0], oDate1[1] - 1, oDate1[2])
let strDateE = new Date(oDate2[0], oDate2[1] - 1, oDate2[2])
iDays = parseInt(Math.abs(strDateS - strDateE) / 1000 / 60 / 60 / 24)
return iDays
}