import { defineStore } from 'pinia'
import myAxios from "../API/myAxios.ts";

export const userStore = defineStore('user', {
    state: () => {
        return {
            loginUser:{
                username:'未登录',
                avatarUrl:'',
                userRole:'notLogin',
                createTime:'null'
            }
        }
    },
    persist: true,
    actions:{
        //获取用户信息
        async getLoginUser() {
            //请求登录信息
            console.log("login已经调用");
            //const res:any = await myAxios.get('/user/current');
            const res:any = await myAxios.get('/user/current', { withCredentials: true });
            console.log("11111")
            console.log(res);
            console.log("11111222222")
            if(res.code === 0 && res.data) {
                this.updateUser(res.data)
            }
        },
        //更新用户信息
        updateUser(payLoad: any) {
            this.loginUser = payLoad;

        }
    }

})