import {
  defineStore
} from 'pinia'
import {
  message
} from 'ant-design-vue';
import router from '@/router/index';
import { login } from '@/api/user.js';
export const useStore = defineStore('user', () => {
  const userInfo = ref();
  const token = ref(null);


  const getUserInfo = async (param) => {
    login(param).then(res => {
      if (res.code == '9000') {
        message.success('登录成功!');
        token.value = res.data;
        let strings = res.data.split("."); //截取token，获取载体
        userInfo.value = JSON.parse(decodeURIComponent(escape(window.atob(strings[1].replace(/-/g, "+").replace(/_/g, "/")))));
        router.push({
          name: 'homepage'
        });
      }
    }).catch(err => {
      message.error(err.message);
    })
  }
  const clearUserInfo = () => {
    userInfo.value = {};
    localStorage.removeItem('user');
  };


  return {
    userInfo,
    token,
    getUserInfo
  }
}, {
  persist: true
})