import { defineStore } from 'pinia'

const useUserStore = defineStore('user', {
    state: () => {
        return {
            token: '',
            user_info: undefined
        }
    },

    getters: {},

    actions: {
        //用户登录
        setUserInfo(data) {
            this.token = data.token
            this.user_info = data
            //跳转工作台
            uni.switchTab({ url: '/pages/panel/index' })
        },
        //退出登录
        handleLogOut() {
            this.token = ''
            this.user_info = undefined
            //返回登录页面
            uni.redirectTo({ url: '/pages/login/index' })
        }
    },
    // 配置持久化
    persist: {
        // 调整为兼容多端的API
        storage: {
            setItem(key, value) {
                uni.setStorageSync(key, value)
            },
            getItem(key) {
                return uni.getStorageSync(key)
            },
        },
    },
})
export default useUserStore
