You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
2.3 KiB
117 lines
2.3 KiB
import config from '@/config' |
|
import storage from '@/utils/storage' |
|
import constant from '@/utils/constant' |
|
import { |
|
login, |
|
logout, |
|
getInfo |
|
} from '@/api/login' |
|
import { |
|
setToken, |
|
removeToken, |
|
} from '@/utils/auth' |
|
|
|
const baseUrl = config.baseUrl |
|
|
|
const user = { |
|
state: { |
|
id: storage.get(constant.userId), |
|
name: storage.get(constant.name), |
|
avatar: storage.get(constant.avatar), |
|
phone: storage.get(constant.phone), |
|
roles: storage.get(constant.roles), |
|
permissions: storage.get(constant.permissions) |
|
}, |
|
mutations: { |
|
SET_ID: (state, id) => { |
|
state.id = id |
|
storage.set(constant.userId, id) |
|
}, |
|
SET_NAME: (state, name) => { |
|
state.name = name |
|
storage.set(constant.name, name) |
|
}, |
|
SET_PHONE: (state, phone) => { |
|
state.phone = phone |
|
storage.set(constant.phone, phone) |
|
}, |
|
SET_AVATAR: (state, avatar) => { |
|
state.avatar = avatar |
|
storage.set(constant.avatar, avatar) |
|
}, |
|
SET_ROLES: (state, roles) => { |
|
state.roles = roles |
|
storage.set(constant.roles, roles) |
|
}, |
|
SET_PERMISSIONS: (state, permissions) => { |
|
state.permissions = permissions |
|
storage.set(constant.permissions, permissions) |
|
} |
|
}, |
|
actions: { |
|
// 登录 |
|
Login({ |
|
commit |
|
}, userInfo) { |
|
return new Promise((resolve, reject) => { |
|
login(userInfo).then(res => { |
|
const { |
|
data |
|
} = res |
|
// 设置 token |
|
setToken(data) |
|
resolve() |
|
}).catch(error => { |
|
reject(error) |
|
}) |
|
}) |
|
}, |
|
|
|
// 获取用户信息 |
|
GetInfo({ |
|
commit, |
|
state |
|
}) { |
|
return new Promise((resolve, reject) => { |
|
getInfo().then(res => { |
|
const user = res.data.user |
|
if (res.data.roles && res.data.roles.length > 0) { |
|
commit('SET_ROLES', res.roles) |
|
commit('SET_PERMISSIONS', res.permissions) |
|
} else { |
|
commit('SET_ROLES', ['ROLE_DEFAULT']) |
|
} |
|
commit('SET_ID', user.id) |
|
commit('SET_AVATAR', user.avatar) |
|
commit('SET_NAME', user.name) |
|
commit('SET_PHONE', user.mobile) |
|
resolve(res) |
|
}).catch(error => { |
|
reject(error) |
|
}) |
|
}) |
|
}, |
|
|
|
// 退出系统 |
|
LogOut({ |
|
commit, |
|
state |
|
}) { |
|
return new Promise((resolve, reject) => { |
|
logout(state.token).then(() => { |
|
commit('SET_ROLES', []) |
|
commit('SET_PERMISSIONS', []) |
|
removeToken() |
|
storage.clean() |
|
resolve() |
|
}).catch(error => { |
|
reject(error) |
|
}) |
|
}) |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
export default user |