import {defineStore} from 'pinia'
// 加载storage模块：获取token，存储token
import {getItem, setItem} from "@/utils/storage/storage";

const TOKEN_KEY: String = "X-Token";

export const useAuthStore = defineStore('user', () => {
        const user = ref<any>({
            token: getItem(TOKEN_KEY) ? getItem(TOKEN_KEY) : null,
            userId: Number,
            LOADING: false,
            userInfo: Object
        })

        function setUser(data: any) {
            user.value = data
            setItem(TOKEN_KEY, user.value.token);
        }

        function getUser() {
            return user.value
        }

        function clearUser() {
            user.value = {
                token: null,
                userId: null,
                isLogin: false,
                userInfo: {}
            }
        }

        function showLoading() {
            user.value.LOADING = true;
        }

        function hideLoading() {
            user.value.LOADING = false;
        }

        return {
            user,
            setUser,
            getUser,
            clearUser,
            showLoading,
            hideLoading
        }
    }, {
        // 开启数据持久化
        persist: true
    }
)
