import { defineStore } from 'pinia'
import { getToken, setToken,clearToken } from '../utils/auth'
import { getUserInfo,logout } from '../api/auth'

export const useUserStore = new defineStore({
    id: 'userStore',
    state: () => {
        return {
            userInfo: {},
            token: getToken() || null,
            temToken: null
        }
    },

    getters: {
        isLogin: (state) => state.token && state.userInfo
    },
    actions: {
        // 获取用户信息
        async getUser() {
            console.log("store=>getUser")
            const data = await getUserInfo()
            console.log(data,'xxx   ')
            this.userInfo = data
        },
        // 登录
        login(userinfo) {
            let token = userinfo.access_token;
            this.token = token
            setToken(token)
            userinfo.token = token;
            this.userInfo = userinfo
        },
        // 退出
        async logout() {
            await logout();
            this.token = ''
            this.userInfo = {}
            clearToken();
        }
    }
})