// store/user.js
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { login } from '@/api/login.js'
import { getUserInfo } from '@/utils/auth'

export const useUserStore = defineStore('user', () => {
	const user = ref({})
	user.value = getUserInfo()
	// 登录方法
	function Login(userInfo) {
		const username = userInfo.username.trim()
		const password = userInfo.password
		return new Promise((resolve, reject) => {
			login(username.trim(), password)
				.then(res => {
					uni.setStorageSync('userInfo', res.returnParams)
					user.value = res.returnParams
					console.log('user', user.value)
					// 可以在这里设置 token 或用户信息
					// setToken(res.token)
					resolve(res)
				})
				.catch(error => {
					reject(error)
				})
		})
	}

	return { user, Login }
})