import { defineStore } from 'pinia'
import { ref } from 'vue'

// 初始化状态
const userInfoState: any = {
  headPic: '/static/images/default-avatar.png',
}

export const useUserStore = defineStore(
  'user',
  () => {
    // 定义用户信息
    const userInfo = ref<any>({ ...userInfoState })
    const token = ref<string>('')
    // 设置用户信息
    const setUserInfo = (val: any) => {
      console.log('设置用户信息', val)
      userInfo.value = val
    }
    // 删除用户信息
    const removeUserInfo = () => {
      userInfo.value = { ...userInfoState }
      token.value = ''
      uni.removeStorageSync('userInfo')
      uni.removeStorageSync('token')
    }

    return {
      userInfo,
      token,
      setUserInfo,
      removeUserInfo,
    }
  },
  {
    persist: true,
  },
)
