```
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);
```