公共封装的TS文件

2021-01-16  本文已影响0人  小米和豆豆

utils.ts

//手动分页
/**
 * data:原数组
 * index:当前页
 * size :每页数量
 */
const ProcessingData=(data: any[], index: number, size: number) =>{
    let data_ = JSON.parse(JSON.stringify(data));
    return data_.slice((index - 1) * size, index * size);
}

简单的storage方法storage.ts

/*
 * @Author: DDY
 * @Date: 2021-09-05 15:12:43
 */
export class mySession  {
    static get(key: string):any  {
        try {
            const value = sessionStorage.getItem(key);
            if (value === null || value === undefined || value === "") {
                return null;
            }
            return JSON.parse(value)

        }
        catch (err) {
            return null
        }
    }
    static set(key: string, val: any): any {
        sessionStorage.setItem(key, JSON.stringify(val))
    }
    static remove(key: string): any {
        sessionStorage.removeItem(key)
    }
    static clear(): any {
        sessionStorage.clear()
    }
}
export class myStorage {
    //取出数据
    static get(key: string): any {
        try {
            const value = localStorage.getItem(key);
            if (value === null || value === undefined || value === "") {
                return null;
            }
            return JSON.parse(value);
        } catch (err) {
            return null
        }
    }
    //存储
    static set(key: string, val: any): any {
        localStorage.setItem(key, JSON.stringify(val));
    }
    // 删除数据
    static remove(key: string): any {
        localStorage.removeItem(key);
    }
    static clear(): any {
        localStorage.clear()
    }
}

公共方法储存信息AppCommon.ts

import { session } from '../utils/session';
export class AppCommon {
    // 用户
    private static _userInfo:any;
    // 获取用户信息
    static get userInfo() {
        if (!this._userInfo) {
            this._userInfo = session.get("_userInfo");
        }
        return this._userInfo;
    }
    // 设置用户信息
    static set userInfo(value) {
        this._userInfo = value;
        session.set("_userInfo", value);
    }
    //token
    private static _token:any;
    // 获取token
    static get token() {
        if (!this._token) {
            this._token = session.get("_token");
        }
        return this._token;
    }
    // 设置token
    static set token(value) {
        this._token = value;
        session.set("_token", value);
    }

}
上一篇下一篇

猜你喜欢

热点阅读