Javascript常用开发工具封装(时间,移动端机型,微信,身份证截取)
// 时间戳转化: yy-mm-dd
export const dateTimeFormat = (datetime, isHms = false) => {
if (!datetime) {
return false
}
const t = new Date(datetime)
const m = t.getMonth() + 1
const d = t.getDate()
const _mouth = m < 10 ? `0${m}` : m
const _d = d < 10 ? `0${d}` : d
const _h = t.getHours()
const _mintius = t.getMinutes()
const _seconds = t.getSeconds()
const ymd = `${t.getFullYear()}-${_mouth}-${_d}`
const ymdhms = `${t.getFullYear()}-${_mouth}-${_d} ${_h}-${_mintius}-${_seconds}`
return isHms ? ymdhms : ymd
}
// 时间格式转化:[- -> /]
export const shortDateFormat = (datetime) => {
if (!datetime) {
return false
}
return datetime.replace(/-/g, '/')
}
// 检测机型
export const getNativeModel = () => {
const ua = navigator.userAgent.toLowerCase()
if (/(Android|Linux|agent_android)/i.test(ua)) {
return 'android'
}
if (/(iPhone|iPad|iPod|iOS|agent_ios)/i.test(ua)) {
return 'ios'
}
}
// 检测微信打开
export const isOpenWithWeChat = () => {
try {
const native = getNativeModel()
const ua = navigator.userAgent.toLowerCase()
const wx = ua.indexOf('micromessenger') !== -1
if (wx) {
return {
isWechat: true,
nativeName: native
}
} else {
return {
isWechat: false,
nativeName: native
}
}
} catch (error) {
console.log(error)
}
}
// 获取url参数,微信授权code接收
export const getQueryString = (name) => {
if (!name) {
return false
}
const urlReg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
const urlList = window.location.search.substr(1).match(urlReg)
return urlList != null ? unescape(urlList[2]) : null
}
// 字符截取返回指定长度
export const substrLengtNum = (subParam, begin, end) => {
if (!subParam) {
return false
}
return subParam.substr(begin, end)
}
// 身份证截取,出生年月日/年龄/性别 (奇数为男, 偶数为女)
export const sortIdentifyNo = (identifyNo) => {
if (!identifyNo || identifyNo.length < 15) {
return false
}
let year, month, date, birthday, age, gender
if (identifyNo.length === 15) {
year = '19' + substrLengtNum(identifyNo, 6, 2)
month = substrLengtNum(identifyNo, 8, 2)
date = substrLengtNum(identifyNo, 10, 2)
birthday = dateTimeFormat(new Date(year, month - 1, date))
age = new Date().getFullYear() - year
gender = substrLengtNum(identifyNo, -1, 1)
gender % 2 ? gender = '0' : gender = '1'
} else if (identifyNo.length === 18) {
year = substrLengtNum(identifyNo, 6, 4)
month = substrLengtNum(identifyNo, 10, 2)
date = substrLengtNum(identifyNo, 12, 2)
birthday = dateTimeFormat(new Date(year, month - 1, date))
age = new Date().getFullYear() - year
gender = substrLengtNum(identifyNo, -2, 1)
gender % 2 ? gender = '0' : gender = '1'
}
return { year, month, date, birthday, age, gender }
}
// 清空vuex-value
export const clearVuexValue = (state) => {
try {
if (!state) { return false }
Object.keys(state).forEach(key => {
if (typeof (state[key]) === 'object') {
clearVuexValue(state[key])
} else if (typeof (state[key]) === 'boolean') {
state[key] = false
} else if (Array.isArray(state[key])) {
state[key] = []
} else {
state[key] = ''
}
})
} catch (error) {
console.log(error)
}
}
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。