1
This commit is contained in:
340
nx/utils/tools.js
Normal file
340
nx/utils/tools.js
Normal file
@@ -0,0 +1,340 @@
|
||||
export default {
|
||||
isNullOrEmpty: function (value) {
|
||||
//是否为空
|
||||
return value === null || value === '' || value === undefined ? true : false
|
||||
},
|
||||
trim: function (value) {
|
||||
//去空格
|
||||
return value.replace(/(^\s*)|(\s*$)/g, '')
|
||||
},
|
||||
isMobile: function (value) {
|
||||
//是否为手机号
|
||||
return /^(?:13\d|14\d|15\d|16\d|17\d|18\d|19\d)\d{5}(\d{3}|\*{3})$/.test(value)
|
||||
},
|
||||
isFloat: function (value) {
|
||||
//金额,只允许保留两位小数
|
||||
return /^([0-9]*[.]?[0-9])[0-9]{0,1}$/.test(value)
|
||||
},
|
||||
isNum: function (value) {
|
||||
//是否全为数字
|
||||
return /^[0-9]+$/.test(value)
|
||||
},
|
||||
checkPwd: function (value) {
|
||||
//密码为8~20位数字和字母组合
|
||||
return /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/.test(value)
|
||||
},
|
||||
formatNum: function (num) {
|
||||
//格式化手机号码
|
||||
if (utils.isMobile(num)) {
|
||||
num = num.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2')
|
||||
}
|
||||
return num
|
||||
},
|
||||
rmoney: function (money) {
|
||||
//金额格式化
|
||||
return parseFloat(money)
|
||||
.toFixed(2)
|
||||
.toString()
|
||||
.split('')
|
||||
.reverse()
|
||||
.join('')
|
||||
.replace(/(\d{3})/g, '$1,')
|
||||
.replace(/\,$/, '')
|
||||
.split('')
|
||||
.reverse()
|
||||
.join('')
|
||||
},
|
||||
copyText(text) {
|
||||
let inputElement = document.createElement('input')
|
||||
inputElement.value = text
|
||||
document.body.appendChild(inputElement)
|
||||
inputElement.select() //选中文本
|
||||
document.execCommand('copy') //执行浏览器复制命令
|
||||
inputElement.remove()
|
||||
},
|
||||
/**
|
||||
* fn:检测图片协议,主要用于检测海报图片协议。
|
||||
* param(imgPath): 图片地址。
|
||||
*/
|
||||
|
||||
checkImgHttp(imgPath) {
|
||||
let newPath = ''
|
||||
let pathArr = imgPath.split('://')
|
||||
// #ifdef H5
|
||||
let ishttps = 'https:' == window.location.protocol ? true : false
|
||||
ishttps ? (pathArr[0] = 'https') : (pathArr[0] = 'http')
|
||||
// #endif
|
||||
// #ifdef MP-WEIXIN
|
||||
pathArr[0] = 'https'
|
||||
// #endif
|
||||
newPath = pathArr.join('://')
|
||||
return newPath
|
||||
},
|
||||
// 打电话
|
||||
callPhone(phoneNumber = '') {
|
||||
let num = phoneNumber.toString()
|
||||
uni.makePhoneCall({
|
||||
phoneNumber: num,
|
||||
fail(err) {
|
||||
console.log('makePhoneCall出错', err)
|
||||
}
|
||||
})
|
||||
},
|
||||
// 图片处理-选择图片
|
||||
chooseImage(count = 1) {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.chooseImage({
|
||||
count: count, //默认9
|
||||
sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
|
||||
sourceType: ['album'], //从相册选择
|
||||
success: res => {
|
||||
resolve(res.tempFilePaths)
|
||||
}
|
||||
})
|
||||
}).catch(e => {
|
||||
reject(e)
|
||||
})
|
||||
},
|
||||
// 图片处理-上传图片
|
||||
uploadImage(api, url) {
|
||||
let config_url = API_URL
|
||||
uni.showLoading({
|
||||
title: '上传中'
|
||||
})
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.uploadFile({
|
||||
url: config_url + api,
|
||||
filePath: url,
|
||||
name: 'file',
|
||||
success: res => {
|
||||
res = JSON.parse(res.data)
|
||||
if (res.code === 1) {
|
||||
uni.hideLoading()
|
||||
uni.showToast({
|
||||
title: '上传成功',
|
||||
icon: 'none'
|
||||
})
|
||||
resolve(res.data)
|
||||
} else {
|
||||
uni.hideLoading()
|
||||
uni.showModal({
|
||||
title: '上传失败',
|
||||
content: res.msg
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}).catch(e => {
|
||||
reject(e)
|
||||
})
|
||||
},
|
||||
// 图片处理-预览图片
|
||||
previewImage(urls = [], current = 0) {
|
||||
uni.previewImage({
|
||||
urls: urls,
|
||||
current: current,
|
||||
indicator: 'default',
|
||||
loop: true,
|
||||
fail(err) {
|
||||
console.log('previewImage出错', urls, err)
|
||||
}
|
||||
})
|
||||
},
|
||||
// 图片处理-获取图片信息
|
||||
getImageInfo(src = '') {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.getImageInfo({
|
||||
src: src,
|
||||
success: image => {
|
||||
resolve(image)
|
||||
},
|
||||
fail(err) {
|
||||
console.log('getImageInfo出错', src, err)
|
||||
}
|
||||
})
|
||||
}).catch(e => {
|
||||
reject(e)
|
||||
})
|
||||
},
|
||||
/**
|
||||
* 格式化时间
|
||||
*/
|
||||
//时间格式化 天时分秒
|
||||
format(t) {
|
||||
let format = {
|
||||
d: '00',
|
||||
h: '00',
|
||||
m: '00',
|
||||
s: '00'
|
||||
}
|
||||
if (t > 0) {
|
||||
let d = Math.floor(t / 86400)
|
||||
let h = Math.floor((t / 3600) % 24)
|
||||
let m = Math.floor((t / 60) % 60)
|
||||
let s = Math.floor(t % 60)
|
||||
format.d = d < 10 ? '0' + d : d
|
||||
format.h = h < 10 ? '0' + h : h
|
||||
format.m = m < 10 ? '0' + m : m
|
||||
format.s = s < 10 ? '0' + s : s
|
||||
}
|
||||
return format
|
||||
},
|
||||
//时间格式化(格式化最大为小时)
|
||||
formatToHours(t) {
|
||||
let format = {
|
||||
d: '00',
|
||||
h: '00',
|
||||
m: '00',
|
||||
s: '00'
|
||||
}
|
||||
if (t > 0) {
|
||||
let h = Math.floor(t / 3600)
|
||||
let m = Math.floor((t / 60) % 60)
|
||||
let s = Math.floor(t % 60)
|
||||
|
||||
format.h = h < 10 ? '0' + h : h
|
||||
format.m = m < 10 ? '0' + m : m
|
||||
format.s = s < 10 ? '0' + s : s
|
||||
}
|
||||
return format
|
||||
},
|
||||
// 年月日
|
||||
timestamp(timestamp) {
|
||||
let date = new Date(timestamp * 1000) //根据时间戳生成的时间对象
|
||||
let y = date.getFullYear()
|
||||
let m = date.getMonth() + 1
|
||||
let d = date.getDate()
|
||||
|
||||
m = m < 10 ? '0' + m : m
|
||||
d = d < 10 ? '0' + d : d
|
||||
|
||||
let dateText = y + '-' + m + '-' + d
|
||||
return dateText
|
||||
},
|
||||
// 年月日,时分秒
|
||||
// "YYYY-mm-dd HH:MM"
|
||||
dateFormat(fmt, date) {
|
||||
let ret
|
||||
const opt = {
|
||||
'Y+': date.getFullYear().toString(), // 年
|
||||
'm+': (date.getMonth() + 1).toString(), // 月
|
||||
'd+': date.getDate().toString(), // 日
|
||||
'H+': date.getHours().toString(), // 时
|
||||
'M+': date.getMinutes().toString(), // 分
|
||||
'S+': date.getSeconds().toString() // 秒
|
||||
// 有其他格式化字符需求可以继续添加,必须转化成字符串
|
||||
}
|
||||
for (let k in opt) {
|
||||
ret = new RegExp('(' + k + ')').exec(fmt)
|
||||
if (ret) {
|
||||
fmt = fmt.replace(ret[1], ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, '0'))
|
||||
}
|
||||
}
|
||||
return fmt
|
||||
},
|
||||
/**
|
||||
* @fn 时间间隔格式化
|
||||
* @param {*} startTime 开始时间的时间戳
|
||||
* @param {*} endTime 结束时间的时间戳
|
||||
* @return {string} str 返回时间字符串
|
||||
*/
|
||||
getTimeInterval(startTime, endTime) {
|
||||
let runTime = parseInt((endTime - startTime) / 1000)
|
||||
let year = Math.floor(runTime / 86400 / 365)
|
||||
runTime = runTime % (86400 * 365)
|
||||
let month = Math.floor(runTime / 86400 / 30)
|
||||
runTime = runTime % (86400 * 30)
|
||||
let day = Math.floor(runTime / 86400)
|
||||
runTime = runTime % 86400
|
||||
let hour = Math.floor(runTime / 3600)
|
||||
runTime = runTime % 3600
|
||||
let minute = Math.floor(runTime / 60)
|
||||
runTime = runTime % 60
|
||||
let second = runTime
|
||||
let str = ''
|
||||
if (year > 0) {
|
||||
str = year + '年'
|
||||
}
|
||||
if (year <= 0 && month > 0) {
|
||||
str = month + '月'
|
||||
}
|
||||
if (year <= 0 && month <= 0 && day > 0) {
|
||||
str = day + '天'
|
||||
}
|
||||
if (year <= 0 && month <= 0 && day <= 0 && hour > 0) {
|
||||
str = hour + '小时'
|
||||
}
|
||||
if (year <= 0 && month <= 0 && day <= 0 && hour <= 0 && minute > 0) {
|
||||
str = minute + '分钟'
|
||||
}
|
||||
if (year <= 0 && month <= 0 && day <= 0 && hour <= 0 && minute <= 0 && second > 0) {
|
||||
str += second + '秒'
|
||||
}
|
||||
str += '前'
|
||||
return str
|
||||
},
|
||||
|
||||
/**提示框
|
||||
*title(标题)
|
||||
*icon(图标): success,loading,none
|
||||
*duration(延时): 0为不关闭, 毫秒数
|
||||
*options(其它参数)
|
||||
*/
|
||||
msg(title, duration = 2000, mask = false, icon = 'none') {
|
||||
//统一提示方便全局修改
|
||||
if (Boolean(title) === false) {
|
||||
return
|
||||
}
|
||||
uni.showToast({
|
||||
title,
|
||||
duration,
|
||||
mask,
|
||||
icon
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* @description 弹窗提示 showModal
|
||||
*/
|
||||
showModal(content, callback) {
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: content,
|
||||
showCancel: false,
|
||||
success(res) {
|
||||
if (res.confirm) {
|
||||
if (typeof callback === 'function') {
|
||||
callback()
|
||||
}
|
||||
} else if (res.cancel) {
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
isRsk() {
|
||||
let sysInfo = uni.getSystemInfoSync()
|
||||
let brand = sysInfo.brand.toLowerCase()
|
||||
if (sysInfo.platform == 'android' && brand.indexOf('qualcomm') > -1) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
},
|
||||
|
||||
// 自定义 replacer,将函数转换为字符串。json序列化和反序列化时避免函数丢失
|
||||
replacer(key, value) {
|
||||
if (typeof value === 'function') {
|
||||
return value.toString()
|
||||
}
|
||||
return value
|
||||
},
|
||||
|
||||
// 自定义 reviver,将字符串转换回函数.json序列化和反序列化时避免函数丢失
|
||||
reviver(key, value) {
|
||||
const functionKeys = ['change', 'dicFormatter', 'click', 'focus', 'blur']
|
||||
if (functionKeys.includes(key)) {
|
||||
// 将字符串转换为函数
|
||||
return new Function('return ' + value)()
|
||||
}
|
||||
return value
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user