import { config } from "../config/config" import { log } from "./log" import { tools } from "./tools" export class util { /** * 获取本地数据 */ public static getStorage(key:string){ try { let d = uni.getStorageSync(key) if(util.isNull(d)){ return null } return d } catch(e) { console.error('本地获取失败', e); return null } } /** * 设置本地数据 */ public static setStorage(key:string,value:string){ try { uni.setStorageSync(key,value) } catch (e) { console.error('本地存储失败', e); } } /** * 删除本地key数据 */ public static removeStorageForKey(key:string){ try { uni.removeStorageSync(key) } catch (e) { console.error('本地删除失败', e); } } /** * 清除本地数据 */ public static clearAllStorage(){ try { uni.clearStorageSync() } catch (e) { console.error('本地清空失败', e); } } public static isNull(v:any){ if(v==null||v==undefined||v==""){ return true } return false } public static alert(v:string){ let Platform = tools.getCurPlatform() if(Platform==config.Platform.H5){ alert(v) }else if(Platform==config.Platform.TOUTIAO){ log.Debug("TOUTIAO",v) }else if(Platform==config.Platform.WEIXIN){ log.Debug("WEIXIN",v) } } // Loadding public static showLoading(title='加载中...',mask:boolean=false) { if(title.length==0) { title = '加载中...' } uni.showLoading({ title:title, mask:mask }) } // delaytime:毫秒 public static hideLoading(delaytime:number=0, cb:Function=null) { if(delaytime<=0) { uni.hideLoading() cb && cb() return } setTimeout(()=>{ uni.hideLoading() cb && cb() },delaytime) } // Toast public static showInfoToast(title:string,duration=1500,mask=false) { uni.showToast({ title:title, icon:'none', duration:duration, mask:mask }) } public static showSuccessToast(title:string,duration=1500,mask=false) { uni.showToast({ title:title, icon:'success', duration:duration, mask:mask }) } public static showErrorToast(title:string,duration=1500,mask=false) { uni.showToast({ title:title, icon:'error', duration:duration, mask:mask }) } // Modal public static showModal(title:string, content:string, confirm_cb:Function=null, cancel_cb:Function=null, confirmText:string='确定', showCancel:boolean=true, cancelText:string='取消') { uni.showModal({ title: title, content: content, confirmText: confirmText, showCancel: showCancel, cancelText: cancelText, success: function (res) { if (res.confirm) { // console.log('用户点击确定') confirm_cb && confirm_cb() } else if (res.cancel) { // console.log('用户点击取消') cancel_cb && cancel_cb() } } }) } public static showModalNoCancel(title:string, content:string, confirm_cb:Function=null, cancel_cb:Function=null, confirmText:string='确定'){ this.showModal(title,content,confirm_cb,cancel_cb,confirmText,false) } // ActionSheet public static showActionSheet(alertText:string, itemList:string[], confirm_cb:Function, cancel_cb:Function=null) { uni.showActionSheet({ alertText:alertText, itemList: itemList, success: function (res) { // console.log('选中了第' + (res.tapIndex + 1) + '个按钮'); confirm_cb && confirm_cb(res.tapIndex) }, fail: function (err) { // console.log('showActionSheet err=',err.errMsg); cancel_cb && cancel_cb() } }); } /** * 格式化时间 * dateTime(String|Number) 需要格式化的时间戳 * formatStr(String) 格式化规则 yyyy:mm:dd|yyyy:mm|yyyy年mm月dd日|yyyy年mm月dd日 hh时MM分等,可自定义组合 默认yyyy-mm-dd */ public static timeFormat(dateTime = null, formatStr = 'yyyy-mm-dd') { let date:any // 若传入时间为假值,则取当前时间 if (!dateTime) { date = new Date() } // 若为unix秒时间戳,则转为毫秒时间戳(逻辑有点奇怪,但不敢改,以保证历史兼容) else if (/^\d{10}$/.test(dateTime?.toString().trim())) { date = new Date(dateTime * 1000) } // 若用户传入字符串格式时间戳,new Date无法解析,需做兼容 else if (typeof dateTime === 'string' && /^\d+$/.test(dateTime.trim())) { date = new Date(Number(dateTime)) } // 处理平台性差异,在Safari/Webkit中,new Date仅支持/作为分割符的字符串时间 // 处理 '2022-07-10 01:02:03',跳过 '2022-07-10T01:02:03' else if (typeof dateTime === 'string' && dateTime.includes('-') && !dateTime.includes('T')) { date = new Date(dateTime.replace(/-/g, '/')) } // 其他都认为符合 RFC 2822 规范 else { date = new Date(dateTime) } const timeSource = { 'y': date.getFullYear().toString(), // 年 'm': (date.getMonth() + 1).toString().padStart(2, '0'), // 月 'd': date.getDate().toString().padStart(2, '0'), // 日 'h': date.getHours().toString().padStart(2, '0'), // 时 'M': date.getMinutes().toString().padStart(2, '0'), // 分 's': date.getSeconds().toString().padStart(2, '0') // 秒 // 有其他格式化字符需求可以继续添加,必须转化成字符串 } for (const key in timeSource) { const [ret] = new RegExp(`${key}+`).exec(formatStr) || [] if (ret) { // 年可能只需展示两位 const beginIndex = key === 'y' && ret.length === 2 ? 2 : 0 formatStr = formatStr.replace(ret, timeSource[key].slice(beginIndex)) } } return formatStr } // 转换时间(timeStamp:毫秒) public static convertTimeString (timeStamp:number) { if (timeStamp <= 0) { return "" } let nowTimeDate = new Date() let nowTimeStamp = nowTimeDate.getTime() // 判断用户输入的时间戳是秒还是毫秒,一般前端js获取的时间戳是毫秒(13位),后端传过来的为秒(10位) if (timeStamp.toString().length == 10) timeStamp *= 1000 let between = (nowTimeStamp - timeStamp)/1000 //转换秒 // console.log('between=',between,'timeStamp=',timeStamp,'nowTimeStamp=',nowTimeStamp) if (between < 300) { return "刚刚" } else if (between >= 300 && between < 3600) { return `${Math.round(between/60)}分钟前` } else if (between >= 3600 && between < 86400) { return `${Math.round(between/3600)}小时前` } else if (between >= 86400 && between < 86400 * 2) { return "昨天" } else if (between >= 86400 * 2 && between < 86400 * 30) { return `${Math.round(between/86400)}天前` } else { return util.timeFormat(timeStamp) } } // 转换单位 public static convertUnit(arg:number) { let realVal = arg+'' if(arg.toString().length>=4) { let number = arg/10000 realVal = number.toFixed(1) + '万' } return realVal } // 去除空格 public static trim(str:string, pos = 'both') { str = String(str) if (pos == 'both') { return str.replace(/^\s+|\s+$/g, '') } if (pos == 'left') { return str.replace(/^\s*/, '') } if (pos == 'right') { return str.replace(/(\s*$)/g, '') } if (pos == 'all') { return str.replace(/\s+/g, '') } return str } public static reLaunch(url:string){ uni.reLaunch({ url:url }) } }