import Vue from 'vue' function tip(data) { setTimeout(() => { uni.showToast(data) }, 100) } // 身份证格式校验 export const checkIdCard = function (sIdCard) { //Wi 加权因子 Xi 余数0~10对应的校验码 Pi省份代码 let Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2], Xi = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2], Pi = [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], checkStatus = 0 // 检查身份证长度 if (sIdCard.length == 18) { checkStatus += 1 } //检验输入的省份编码是否有效 if (checkStatus >= 1) { let p2 = sIdCard.substr(0, 2) for (let i = 0; i < Pi.length; i++) { if (Pi[i] == p2) { checkStatus += 1 } } } //检验18位身份证号码出生日期是否有效 //parseFloat过滤前导零,年份必需大于等于1900且小于等于当前年份,用Date()对象判断日期是否有效。 if (checkStatus >= 2) { let year = parseFloat(sIdCard.substr(6, 4)) let month = parseFloat(sIdCard.substr(10, 2)) let day = parseFloat(sIdCard.substr(12, 2)) let checkDay = new Date(year, month - 1, day) let nowDay = new Date() if (1900 <= year && year <= nowDay.getFullYear() && month == checkDay.getMonth() + 1 && day == checkDay.getDate()) { checkStatus += 1 } } //检验校验码是否有效 if (checkStatus >= 3) { let aIdCard = sIdCard.split('') let sum = 0 for (let j = 0; j < Wi.length; j++) { sum += Wi[j] * aIdCard[j] //线性加权求和 } let index = sum % 11 //求模,可能为0~10,可求对应的校验码是否于身份证的校验码匹配 if (Xi[index] == aIdCard[17].toUpperCase()) { checkStatus += 1 } } if (checkStatus == 4) { return true } else { return false } } /** * 时间转换为XX前 */ export const clickDateDiff = function (value) { let dataValue = value if (typeof value == 'string') { dataValue = new Date(value.replace(/-/g, '/')).getTime() } var result var minute = 1000 * 60 var hour = minute * 60 var day = hour * 24 var month = day * 30 var now = new Date().getTime() var diffValue = parseInt(now) - parseInt(dataValue) if (diffValue < 0) { return } var monthC = diffValue / month var weekC = diffValue / (7 * day) var dayC = diffValue / day var hourC = diffValue / hour var minC = diffValue / minute if (monthC >= 1) { result = '' + parseInt(monthC) + '月前' } else if (weekC >= 1) { result = '' + parseInt(weekC) + '周前' } else if (dayC >= 1) { result = '' + parseInt(dayC) + '天前' } else if (hourC >= 1) { result = '' + parseInt(hourC) + '小时前' } else if (minC >= 1) { result = '' + parseInt(minC) + '分钟前' } else { result = '刚刚' } return result } // 保存图片 let settingWritePhotosAlbum = false export const saveImg = function (url, callback) { if (url) { // #ifdef MP-WEIXIN if (settingWritePhotosAlbum) { uni.getSetting({ success: (res) => { if (res.authSetting['scope.writePhotosAlbum']) { uni.showLoading({ title: '正在下载' }) uni.downloadFile({ url: url, success: (data) => { if (data.statusCode == 200) { uni.saveImageToPhotosAlbum({ filePath: data.tempFilePath, success: () => { uni.hideLoading() callback && callback() uni.showToast({ title: '保存成功' }) }, fail(e) { uni.hideLoading() tip({ title: '下载失败,错误原因:' + e.errMsg, icon: 'none' }) } }) } else { uni.hideLoading() uni.showToast({ title: '下载失败', icon: 'none' }) } }, fail(e) { uni.hideLoading() uni.showToast({ title: '下载失败,错误原因:' + e.errMsg, icon: 'none' }) } }) } else { uni.showModal({ title: '提示', content: '请先在设置页面打开“保存相册”使用权限', confirmText: '去设置', cancelText: '算了', success: (data) => { if (data.confirm) { uni.openSetting() } } }) } } }) } else { settingWritePhotosAlbum = true uni.authorize({ scope: 'scope.writePhotosAlbum', success: () => { uni.showLoading({ title: '正在下载' }) uni.downloadFile({ url: url, success: (data) => { if (data.statusCode == 200) { uni.saveImageToPhotosAlbum({ filePath: data.tempFilePath, success: () => { uni.hideLoading() callback && callback() uni.showToast({ title: '保存成功' }) }, fail(e) { uni.hideLoading() tip({ title: '下载失败,错误原因:' + e.errMsg, icon: 'none' }) } }) } else { uni.hideLoading() uni.showToast({ title: '下载失败', icon: 'none' }) } }, fail(e) { uni.hideLoading() uni.showToast({ title: '下载失败,错误原因:' + e.errMsg, icon: 'none' }) } }) } }) } // #endif // #ifdef H5 uni.showLoading({ title: '正在下载' }) uni.downloadFile({ url: url, success: (data) => { uni.hideLoading() if (data.statusCode == 200) { callback && callback() window.open(data.tempFilePath) } else { uni.showToast({ title: '下载失败', icon: 'none' }) } }, fail(e) { uni.hideLoading() uni.showToast({ title: '下载失败,错误原因:' + e.errMsg, icon: 'none' }) } }) // #endif // #ifdef APP-PLUS uni.showLoading({ title: '正在下载' }) uni.saveImageToPhotosAlbum({ filePath: url, success: () => { uni.hideLoading() callback && callback() uni.showToast({ title: '保存成功' }) }, fail(e) { uni.hideLoading() uni.showToast({ title: '下载失败,错误原因:' + e.errMsg, icon: 'none' }) } }) // #endif } else { uni.showToast({ title: '未找到图片', icon: 'none' }) } } // 保存视频 export const saveVideo = function (url, callback) { if (url) { // #ifdef MP-WEIXIN if (settingWritePhotosAlbum) { uni.getSetting({ success: (res) => { if (res.authSetting['scope.writePhotosAlbum']) { // let urlArr = url.split("/"); // let updateUrl = urlArr[urlArr.length - 1]; // let filePath = wx.env.USER_DATA_PATH + '/' + updateUrl; uni.showLoading({ title: '正在下载' }) uni.downloadFile({ url: url, // filePath: filePath, success: (data) => { if (data.statusCode == 200) { uni.saveVideoToPhotosAlbum({ filePath: data.tempFilePath, success: () => { uni.hideLoading() callback && callback() tip({ title: '保存成功' }) }, fail(e) { uni.hideLoading() tip({ title: '下载失败,错误原因:' + e.errMsg, icon: 'none' }) } }) } else { uni.hideLoading() tip({ title: '下载失败', icon: 'none' }) } }, fail(e) { uni.hideLoading() tip({ title: '下载失败,错误原因:' + e.errMsg, icon: 'none' }) } }) } else { uni.showModal({ title: '提示', content: '请先在设置页面打开“保存相册”使用权限', confirmText: '去设置', cancelText: '算了', success: (data) => { if (data.confirm) { uni.openSetting() } } }) } } }) } else { settingWritePhotosAlbum = true uni.authorize({ scope: 'scope.writePhotosAlbum', success: () => { // let urlArr = url.split("/"); // let updateUrl = urlArr[urlArr.length - 1]; // let filePath = wx.env.USER_DATA_PATH + '/' + updateUrl; uni.showLoading({ title: '正在下载' }) uni.downloadFile({ url: url, // filePath: filePath, success: (data) => { if (data.statusCode == 200) { uni.saveVideoToPhotosAlbum({ filePath: data.tempFilePath, success: () => { uni.hideLoading() callback && callback() tip({ title: '保存成功' }) }, fail(e) { uni.hideLoading() tip({ title: '下载失败,错误原因:' + e.errMsg, icon: 'none' }) } }) } else { uni.hideLoading() tip({ title: '下载失败,错误原因:' + data.errMsg, icon: 'none' }) } }, fail(e) { uni.hideLoading() tip({ title: '下载失败,错误原因:' + e.errMsg, icon: 'none' }) } }) } }) } // #endif // #ifdef H5 uni.showLoading({ title: '正在下载' }) uni.downloadFile({ url: url, success: (data) => { uni.hideLoading() if (data.statusCode == 200) { callback && callback() window.open(data.tempFilePath) } else { tip({ title: '下载失败', icon: 'none' }) } }, fail(e) { uni.hideLoading() tip({ title: '下载失败,错误原因:' + e.errMsg, icon: 'none' }) } }) // #endif // #ifdef APP-PLUS uni.showLoading({ title: '正在下载' }) uni.saveVideoToPhotosAlbum({ filePath: url, success: () => { uni.hideLoading() callback && callback() tip({ title: '保存成功' }) }, fail(e) { uni.hideLoading() tip({ title: '下载失败,错误原因:' + e.errMsg, icon: 'none' }) } }) // #endif } else { tip({ title: '未找到视频', icon: 'none' }) } } // 微信小程序获取定位权限判断 function wxAppletsLocation(successCallback, errCallback) { uni.getSetting({ success: (res) => { if (res.authSetting['scope.userLocation']) { uni.getLocation({ type: 'gcj02', success: (res) => { successCallback(res) }, fail: (err) => { console.log('位置信息错误', err) errCallback('位置信息获取失败') } }) } else { errCallback('“位置信息”未授权') uni.showModal({ title: '提示', content: '请先在设置页面打开“位置信息”使用权限', confirmText: '去设置', cancelText: '再逛会', success: (res) => { if (res.confirm) { uni.openSetting() } } }) } } }) } // 获取地址信息 let locationAuthorize = true export const getAppWxLatLon = function (successCallback, errCallback) { // #ifdef MP if (locationAuthorize) { uni.authorize({ scope: 'scope.userLocation', success: () => { wxAppletsLocation(successCallback, errCallback) locationAuthorize = false }, fail: () => { locationAuthorize = false } }) } else { wxAppletsLocation(successCallback, errCallback) } // #endif } /** * 图片转base64 * @returns {Promise} 返回一个 promise 对象,resolve 结果或 reject 错误 */ export function imgToBase64(filePath) { return new Promise((resolve, reject) => { if (filePath.indexOf('wxfile') === 0 || filePath.indexOf('http://tmp/') === 0) { wx.getFileSystemManager().readFile({ filePath: filePath, encoding: 'base64', success: (res) => { wx.getImageInfo({ src: filePath, success: (infoRes) => { resolve(`data:image/${infoRes.type};base64,${res.data}`) }, fail: (err) => { reject('Error getting image info') } }) }, fail: (err) => { reject('Error reading file') } }) } else { wx.downloadFile({ url: filePath, success: (res) => { wx.getFileSystemManager().readFile({ filePath: res.tempFilePath, encoding: 'base64', success: (res) => { wx.getImageInfo({ src: filePath, success: (infoRes) => { resolve(`data:image/${infoRes.type};base64,${res.data}`) }, fail: (err) => { reject('Error getting image info') } }) }, fail: (err) => { console.log('网络', err) } }) }, fail: (err) => { console.log('下载网络失败', err) } }) } }) } export function sleep(delay) { var start = new Date().getTime() while (new Date().getTime() - start < delay) { continue } } /** * 字典数据转成option list * @param {Object} data 字典数据 * @returns list */ export const dictToOptionList = (data) => { return Object.keys(data).map((k) => { const kstr = typeof k === 'number' ? k : k + '' return { value: kstr, label: data[k] } }) } /* 日期格式化 */ export const dateFormat = (date, fmt = 'yyyy-MM-dd hh:mm', isDefault = '-') => { if (!date) { return '-' } if (date.toString().length === 10) { date *= 1000 } date = new Date(date) if (date.valueOf() < 1) { return isDefault } const o = { 'M+': date.getMonth() + 1, // 月份 'd+': date.getDate(), // 日 'h+': date.getHours(), // 小时 'm+': date.getMinutes(), // 分 's+': date.getSeconds(), // 秒 'q+': Math.floor((date.getMonth() + 3) / 3), // 季度 S: date.getMilliseconds() // 毫秒 } if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, `${date.getFullYear()}`.substr(4 - RegExp.$1.length)) } 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 } /** * 将目标对象中有的属性值与源对象中的属性值合并 * @param {Object} target 目标对象 * @param {Object} sources 源对象 */ export function objAssign(target, sources) { let targ = { ...target } for (let k in targ) { targ[k] = Object.prototype.hasOwnProperty.call(sources, k) ? sources[k] : targ[k] } return targ } /** * 获取随机code,默认获取16位 * @param {Number} len 推荐8的倍数 * */ export function randomCode(len = 16) { if (len <= 0) return let steps = Math.ceil(len / 8) let stepNums = [] for (let i = 0; i < steps; i++) { let ranNum = Math.random().toString(32).slice(-8) stepNums.push(ranNum) } return stepNums.join('') } export function fillZero(num, fullLength = 1) { return ('0'.repeat(fullLength) + num).slice(-fullLength) } /** * 判断对象类型 * @param {*} obj 对象 */ export function objTypeOf(obj) { const toString = Object.prototype.toString const map = { '[object Boolean]': 'boolean', '[object Number]': 'number', '[object String]': 'string', '[object Function]': 'function', '[object Array]': 'array', '[object Date]': 'date', '[object RegExp]': 'regExp', '[object Undefined]': 'undefined', '[object Null]': 'null', '[object Object]': 'object', '[object Blob]': 'blob' } return map[toString.call(obj)] } /* 打卡时间比较 */ export const timeCompare = (startStr, time, endStr) => { let start = new Date(startStr) //开始时间 let end = new Date(endStr) //结束时间 return start.getTime() > time && time < end.getTime() } /** * 判断是否未数值 * @param {Object} val */ export function isNumber(val) { return !isNaN(Number(val)) } /** * 处理大小单位 * @param {Object} val */ export function formatSize(val, unit = 'rpx') { return isNumber(val) ? `${val}${unit}` : val }