/* * 公共方法 * */ //时间 export function formatDate(date, fmt) { if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)); } let o = { 'M+': date.getMonth() + 1, 'd+': date.getDate(), 'h+': date.getHours(), 'm+': date.getMinutes(), 's+': date.getSeconds() }; for (let k in o) { if (new RegExp(`(${k})`).test(fmt)) { let str = o[k] + ''; fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str)); } } return fmt; }; function padLeftZero(str) { return ('00' + str).substr(str.length); }; export function getAgeByIdcard(identityCard) { let len = (identityCard + "").length; if (len == 0) { return 0; } else { if ((len != 15) && (len != 18)) //身份证号码只能为15位或18位其它不合法 { return 0; } } let strBirthday = ""; if (len == 18) //处理18位的身份证号码从号码中得到生日和性别代码 { strBirthday = identityCard.substr(6, 4) + "/" + identityCard.substr(10, 2) + "/" + identityCard.substr(12, 2); } if (len == 15) { strBirthday = "19" + identityCard.substr(6, 2) + "/" + identityCard.substr(8, 2) + "/" + identityCard.substr(10, 2); } //时间字符串里,必须是“/” let birthDate = new Date(strBirthday); let nowDateTime = new Date(); let age = nowDateTime.getFullYear() - birthDate.getFullYear(); //再考虑月、天的因素;.getMonth()获取的是从0开始的,这里进行比较,不需要加1 if (nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() == birthDate.getMonth() && nowDateTime .getDate() < birthDate.getDate())) { age--; } return age; } export function getSexByIdcard (idCard) { let sexStr; if (parseInt(idCard.slice(-2, -1)) % 2 == 1) { sexStr = '男'; } else { sexStr = '女'; } return sexStr; } export const base64ToFile = (dataurl, filename = 'file') => { let arr = dataurl.split(',') let mime = arr[0].match(/:(.*?);/)[1] let suffix = mime.split('/')[1] let bstr = atob(arr[1]) let n = bstr.length let u8arr = new Uint8Array(n) while (n--) { u8arr[n] = bstr.charCodeAt(n) } return new File([u8arr], `${filename}.${suffix}`, { type: mime }) } export const writeFile = (base64Str) => new Promise((resolve, reject) => { // 后台返回的base64格式数据的回车换行换为空字符'' const base64Image = base64Str.split(',')[1].replace(/[\r\n]/g, '') // 文件管理器 const fsm = uni.getFileSystemManager() // 文件名 const FILE_BASE_NAME = 'tmp_base64src' // 文件后缀 const format = 'png' // 获取当前时间戳用于区分小程序码,防止多次写进的小程序码图片都一样,建议通过不同的列表ID来区分不同的小程序码 const timestamp = (new Date()).getTime() // base转二进制 const buffer = uni.base64ToArrayBuffer(base64Image) // 文件名 const filePath = `${wx.env.USER_DATA_PATH}/${timestamp}share.${format}` // 写文件 fsm.writeFile({ filePath, data: buffer, encoding: 'binary', success () { // 读取图片 uni.getImageInfo({ src: filePath, success (res) { const img = res.path // 把需要画出来的图片的临时url暴露出去 resolve(img) reject() }, fail (e) { console.log('读取图片报错') console.log(e) }, error (res) { console.log(res) } }) }, fail (e) { console.log(e) } }) }).catch((e) => { console.log(e) }) // 删除存储的垃圾数据 export const removeSave = () => new Promise((resolve) => { // 文件管理器 const fsm = uni.getFileSystemManager() // 获取文件列表 fsm.readdir({ dirPath: wx.env.USER_DATA_PATH, // 当时写入的文件夹 success (res) { res.files.forEach((el) => { // 遍历文件列表里的数据 // 删除存储的垃圾数据 if (el !== 'miniprogramLog') { // 过滤掉miniprogramLog fsm.unlink({ filePath: `${wx.env.USER_DATA_PATH}/${el}`, // 文件夹也要加上,如果直接文件名会无法找到这个文件 fail (e) { console.log('readdir文件删除失败:', e) } }) } }) resolve() } }) })