import { loginPassword } from '/@/api'; import { useCookies } from '@vueuse/integrations/useCookies'; import { defineStore } from 'pinia'; import { wxLogin } from '/@/api' const { VITE_TOKEN_KEY } = import.meta.env; const token = useCookies().get(VITE_TOKEN_KEY as string); interface StoreUser { token: string; wxOpenId: string; info: Record; } export const useUserStore = defineStore({ id: 'user', state: (): StoreUser => ({ token: token, wxOpenId:'o3S0L1Hyd3O0vYI2Kr1lFDEtEO2k', info: { name:'test', wxOpenId:'test1', }, }), getters: { getUserInfo(): any { return this.info || {}; }, getWxOpenId(): any { return this.wxOpenId || useCookies().get('wxOpenId'); }, }, actions: { setInfo(info: any) { this.info = info ? info : ''; }, setWxOpenId(code: string){ wxLogin(code).then(res => { console.log('wxlogin',res) let { openid } = res.data this.wxOpenId = openid ? openid : ''; if(openid){ useCookies().set('wxOpenId', openid) } }) }, login() { return new Promise((resolve) => { const { execute } = loginPassword(); execute().then((res) => { // this.setInfo(res); resolve(res); }); }); }, }, persist: { key: 'token', storage: localStorage, paths: ['token'], }, });