| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- import { defineStore } from "pinia";
- import * as Pinia from 'pinia';
- import { login, getUserInfo } from '@/api/user/login';
- import { useAppStore } from './app.js';
- const appStore = useAppStore(Pinia.createPinia())
- export const useUserStore = defineStore("userStore", {
- state() {
- return {
- token: "",
- userInfo: null
- };
- },
- getters:{
- GET_TOKEN(){
- return this.token
- },
- GET_USER_INFO(){
- return this.userInfo
- }
- },
- actions:{
- loginAction(formData){
- login({data:formData}).then(res=>{
- let { token, refreshToken } = res
- uni.setStorageSync('token', token)
- this.token = token
- getUserInfo().then(info=>{
- uni.setStorageSync('userInfo', info)
- this.userInfo = info
- uni.switchTab({
- url:'/pages/index/index'
- })
- })
- })
- },
- async logoutAction(){
- uni.removeStorageSync('token')
- uni.removeStorageSync('userInfo')
- uni.removeStorageSync('version')
- appStore.setSysVersion('1.0.0')
- this.userInfo = null
- this.token = ""
- uni.reLaunch({
- url:"/pages/login/index"
- })
- }
- },
- unistorage: {
- // 初始化恢复前触发
- beforeRestore(ctx) {},
- // 初始化恢复后触发
- afterRestore(ctx) {},
- },
- });
|