🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
``` import moment from'moment'; import'moment/locale/zh-cn'; moment.locale('zh-cn'); /** * 格式float 例如,0 保留2位小数 0.00 | 1.1 保留2位小数 1.10 * @param x 需格式化的值 * @param pos 保留几位小数 */ export function FomatFloat(x: number, pos: number = 2): string { x = x / 100; const f = Math.round(x * Math.pow(10, pos))/Math.pow(10, pos); // pow 幂 let s = f.toString(); let rs = s.indexOf('.'); if(rs < 0){ rs = s.length; s += '.'; } while(s.length <= rs + pos){ s += '0'; } return s; } /** * * 获取时间戳 * value 如不传入默认返回当前时间戳 * @param value string 日期 2019-04-10 * @return number * */ export function Timestamp(value?: string): number { if (value === '') { return 0; } let now = moment.now(); if (value !== undefined) { now = moment(value, 'YYYY-MM-DD HH:mm:ss').valueOf(); } return Math.ceil(now / 1000); } /** * * 时间戳转换日期格式 * @param value number 时间戳 1010101010 * @param format string 日期格式化 * */ export function Timeformat(value: number, format: string = 'YYYY-MM-DD HH:mm:ss'): any { if (value === 0) { return ''; } return moment.unix(value).format(format); } /** * 延迟执行 * @param ms 延迟毫秒数 */ export function sleep(ms: number){ return new Promise((resolve)=>setTimeout(resolve,ms)); } /** *随机数 * @param min number 0 * @param max number 10 * @return number */ export function Rand(min: number, max: number): number { return Math.floor(Math.random() * (max - min)) + min; } /** * 滚动到某个位置回调 * @param callback * @param top */ export function scrollGetData(callback: () => void, top: number = 100) { let status: boolean = true; document.addEventListener("touchstart", () => { document.addEventListener("scroll", (e: any) => { const scrollHeight: number = document.body.scrollHeight; const scrollTop: number = document.documentElement.scrollTop || document.body.scrollTop; console.log(scrollHeight, scrollTop + top) if ((scrollTop + top) > (scrollHeight) && status){ status = false; } }); }); document.addEventListener("touchend", (e: any) => { document.removeEventListener("scroll", () => { console.log('清除scroll 监听') }); if (status === false) { callback(); status = true; } }); } /** * 时间转化 s > hh:mm:ss * @param result */ export function SecondToDate(result: any) { const h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600); const m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60)); const s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60)); return result = ( h !== '00' ? (h + ':') : '') + m + ':' + s; } ``` # ## 封装浏览器的 Storage ``` // localStorage(本地存储)和sessionStorage(会话存储) import {Timestamp} from './Utils'; // Timestamp // 获取当前时间戳 interface ICacheInterface { /** * 设置缓存 * @param key string 缓存键 * @param value any 缓存值 string | {} | [] * @param exp number 过期时间 默认0永久有效 */ Set(key: string, value: any, exp: number): void; /** * 获取缓存信息 * @param key string * @return any {} | string | [] */ Get(key: string): any; /** * 删除指定key * @param key string 需要删除的键 */ Remove(key: string): void; /** * 清除所有缓存 */ Clear(): void; } class Caches<T> implements ICacheInterface { private cache: any; public constructor(stroage: T) { this.cache = stroage; } /** * 设置缓存 * @param key string 缓存键 * @param value any 缓存值 string | {} | [] * @param exp number 过期时间 默认0永久有效 */ public Set(key: string, value: any, exp: number = 0): void { if (exp > 0) { exp = Timestamp() + exp; } const item = {val: value, exp, iat: Timestamp()}; this.cache.setItem(key, JSON.stringify(item)); } /** * 获取缓存信息 * @param key string * @return any {} | string | [] */ public Get(key: string): any { // 获取当前时间戳 const time: number = Timestamp(); // 根据key获取val const cacheJson: string = this.cache.getItem(key); if (cacheJson == null) { // key是否存在 return ''; } // 转换 json const cacheObj: any = JSON.parse(cacheJson); if (time > cacheObj.exp && cacheObj.exp > 0) { // 有效期是否已过期 return ''; } return cacheObj.val; } /** * 获取缓存信息 * @param key string * @return string */ public GetString(key: string): any { // 根据key获取val const cacheJson: string = this.cache.getItem(key); if (cacheJson == null) { // key是否存在 return ''; } return cacheJson; } /** * 删除指定key * @param key string 需要删除的键 */ public Remove(key: string): void { this.cache.removeItem(key); } /** * 清除所有缓存 */ public Clear(): void { this.cache.clear(); } } export const Cache = new Caches<Storage>(localStorage); export const Session = new Caches<Storage>(sessionStorage); ```