1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| import { loginById,loginByToken, logout, getUserInfo,checkSession,changePassword, register} from '@/api/user' import { getToken, setToken, removeToken } from '@/utils/auth' import {setSecret,changePasswordBySecret,checkHaveSecret} from "@/api/security"; import {baseUrl} from "@/api"; import {editUser} from "@/api/admin";
const defaultAvatar=baseUrl+'/UserAvatar/default_avatar.png'
const user = { state: { id: '', token: getToken('token'), name: '', sex:'', phone:'', loc:'', role: '', avatar: '', brandId:'', status: false, secretStatus:false },
mutations: { SET_USER:(state,info)=>{ if(info.role==='normal'){ state.id=info.data.id state.name = info.data.name===null?"User":info.data.name state.sex = info.data.sex state.loc = info.data.loc state.avatar = info.data.avatar===null?defaultAvatar:baseUrl+info.data.avatar } else if(info.role==='admin' || info.role==='superAdmin'){ state.id = info.data.shopId state.name = info.data.shopName===null?"admin":info.data.shopName state.brandId=info.data.brand.brandId state.avatar = info.data.brand.logo===null?defaultAvatar:baseUrl+info.data.brand.logo } state.token = info.data.token state.phone = info.data.phone state.role = info.role state.status = true }, CLEAR_USER:(state)=>{ state.id='' state.token = '' state.name = '' state.sex = '' state.phone = '' state.loc = '' state.role = '' state.avatar = '' state.status = false state.brandId = '' } },
actions: { UserInfoInit({commit}){ return new Promise(resolve => { commit('CLEAR_USER') resolve() }) }, loginById({ commit }, loginInfo) { const id = loginInfo.id.trim() return new Promise((resolve, reject) => { loginById(id, loginInfo.encryptPassword,loginInfo.role).then(data => { if(data.code===401) reject("用户名或密码错误") else if(data.code===402) reject("用户名不存在") else if(data.code===400){ if(data.role==='admin'||data.role==='superAdmin') commit('SET_SECRET_STATUS',true) else { checkHaveSecret(id).then(response=>{ commit('SET_SECRET_STATUS',response==='success') }) } commit('SET_USER',data) setToken('token',data.data.token) if(loginInfo.radio==='1'){ setToken('id',id) setToken('password',loginInfo.password) setToken('role',loginInfo.role) }else{ removeToken('id') removeToken('password') removeToken('role') } } resolve() }).catch(error => { reject(error) }) }) } } }
export default user
|