import { defineStore } from 'pinia' import { login, loginByCode, logout } from '../services/common' export const useUserStore = defineStore('user', () => { const userInfo = ref(uni.getStorageSync('userInfo')) const setUserInfo = data => { userInfo.value = data uni.setStorageSync('userInfo', data) } const token = ref(uni.getStorageSync('token') || '') const setToken = data => { token.value = data uni.setStorageSync('token', data) } const login = data => { return login(data).then(res => { setUserInfo(res.userInfo) setToken(res.token) }) } const loginByOpenId = data => { return loginByCode().then(res => { setUserInfo(res.userInfo) setToken(res.token) }) } const logout = () => { return new Promise(resolve => { setUserInfo({}) setToken('') resolve() }) } return { userInfo, setUserInfo, token, setToken, login, loginByOpenId, logout } })