/*
 * @Descripttion: 
 * @version: 1.0.0
 * @Author: YeeChan
 * @Date: 2020-07-09 18:54:40
 */

import AppPlatform from "../Util/AppPlatform";


export class StorageReq {
    public key_custom: string = null;
    public data_custom: any = {};
    public success_custom: () => void = null;
    public fail_custom: () => void = null;
    public complete_custom: () => void = null;
}

/**
 * 数据本地持久化保存
 */
export default class StorageMgr {

    /**
     * 保存
     * @param req 
     */
    public static setStorage_custom(req: StorageReq) {
        let dataStr: string = JSON.stringify(req.data_custom);
        if (AppPlatform.is_WECHAT_GAME_custom()) {
            window["wx"].setStorage({
                key: req.key_custom,
                data: dataStr,
                success: req.success_custom,
                fail: req.fail_custom,
                complete: req.complete_custom
            });
        } else {
            cc.sys.localStorage.setItem(req.key_custom, dataStr);
        }

    }

    /**
     * 获取
     * @param key 
     */
    public static getStorage_custom(key): any {
        let value = null;
        if (AppPlatform.is_WECHAT_GAME_custom()) {
            try {
                value = window["wx"].getStorageSync(key);
            } catch (e) {
            }
        } else {
            value = cc.sys.localStorage.getItem(key);
        }
        return value;
    }
}