use-current-user-snSpV2ft.js 1.2 KB

1
  1. import{x as s,J as u,v as n}from"./index-CSqHrY5V.js";function c(t){return s({url:"/api/auth/v2/login",method:"post",data:t})}function p(t){return s({url:"/api/auth/v2/register",method:"post",data:t})}function d(t){return s({url:`/api/auth/v2/sync?userFlag=${t}`,method:"get"})}function m(){return s({url:"/api/user/user_info",method:"get"})}const g=u("current_user",{persist:{enabled:!0,strategies:[{key:"info",storage:localStorage,paths:["username"]}]},state:()=>({name:"Jack",token_type:"",token:n.get(),username:"",nickname:"",dept:[],roles:[],permissions:[],user:{}}),actions:{updateName(t){this.name=t},toLogin(t){const{username:r,password:o}=t;return new Promise((i,a)=>{c({username:r,password:o}).then(e=>{e.code==200&&e.data.access_token.length?(this.token=e.data.access_token,this.token_type=e.data.token_type,this.username=e.data.username,this.nickname=e.data.nickname,n.set(e.data.access_token),i(e.msg),this.getInfo()):a(e.msg)}).catch(e=>{a(e)})})},async getInfo(){try{const t=await m();if(t.code===200)return this.dept=t.data.dept,this.roles=t.roles||["ROLE_DEFAULT"],this.permissions=t.permissions,t.msg;throw new Error(t.msg)}catch(t){throw t}}}});export{d as a,p as r,g as u};