import page from './page'

// 可以在不同的地方共享数据、传参
function get<T>(key?: string, defaultValue?: T): T {
    if (!key) {
        key = page.getPagePath()
    }
    const val = uni.getStorageSync(key)
    return (val === undefined ? defaultValue : val) as T
}

// 设值
function set(key: string, value: any) {
    uni.setStorageSync(key, value)
}

// 弹出值
function pop<T>(key?: string, defaultValue?: T): T {
    if (!key) {
        key = page.getPagePath()
    }
    const value = uni.getStorageSync(key)
    uni.removeStorageSync(key)
    return value || defaultValue
}

// 清空
function clear() {
    uni.clearStorageSync()
}

export default {
    get,
    set,
    pop,
    clear,
}
