feat: 新增登录注销功能,调整接口与配置
1. 新增项目logo文件 2. 调整API基础地址与开发代理配置 3. 添加多语言登录相关文案 4. 新增登录注销API接口 5. 重构请求与账号登录注销逻辑 6. 更新文档与菜单store状态
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { Message, MessageBox } from 'element-ui'
|
||||
import util from '@/libs/util.js'
|
||||
import { loginAdminUser, logoutAdminUser } from '@/api/auth'
|
||||
|
||||
import { MessageBox, Loading } from 'element-ui'
|
||||
import router from '@/router'
|
||||
import { SYS_USER_LOGIN } from '@/api/sys.user.js'
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
@@ -17,16 +18,24 @@ export default {
|
||||
username = '',
|
||||
password = ''
|
||||
} = {}) {
|
||||
const res = await SYS_USER_LOGIN({ username, password })
|
||||
// 设置 cookie 一定要存 uuid 和 token 两个 cookie
|
||||
// 整个系统依赖这两个数据进行校验和存储
|
||||
// uuid 是用户身份唯一标识 用户注册的时候确定 并且不可改变 不可重复
|
||||
// token 代表用户当前登录状态 建议在网络请求中携带 token
|
||||
// 如有必要 token 需要定时更新,默认保存一天
|
||||
util.cookies.set('uuid', res.uuid)
|
||||
util.cookies.set('token', res.token)
|
||||
// 新的写法
|
||||
const res = await loginAdminUser({ username, password })
|
||||
// 设置用户数据
|
||||
const cookieSetting = { expires: 365 }
|
||||
util.cookies.set('uuid', res.userInfo.username, cookieSetting)
|
||||
util.cookies.set('token', res.token, cookieSetting)
|
||||
util.cookies.set('block', 'false')
|
||||
|
||||
// 存储用户数据到本地
|
||||
localStorage.setItem('user_id', res.userInfo.user_id)
|
||||
|
||||
// 设置 vuex 用户信息
|
||||
await dispatch('d2admin/user/set', { name: res.name }, { root: true })
|
||||
await dispatch('d2admin/user/set', {
|
||||
name: res.userInfo.username,
|
||||
admin: res.userInfo,
|
||||
token: res.token
|
||||
}, { root: true })
|
||||
|
||||
// 用户登录后从持久化数据加载一系列的设置
|
||||
await dispatch('load')
|
||||
},
|
||||
@@ -35,34 +44,59 @@ export default {
|
||||
* @param {Object} context
|
||||
* @param {Object} payload confirm {Boolean} 是否需要确认
|
||||
*/
|
||||
logout ({ commit, dispatch }, { confirm = false } = {}) {
|
||||
/**
|
||||
* @description 注销
|
||||
*/
|
||||
async function logout () {
|
||||
// 删除cookie
|
||||
util.cookies.remove('token')
|
||||
// 清空 vuex 用户信息
|
||||
await dispatch('d2admin/user/set', {}, { root: true })
|
||||
util.cookies.remove('uuid')
|
||||
// 跳转路由
|
||||
router.push({ name: 'login' })
|
||||
}
|
||||
// 判断是否需要确认
|
||||
if (confirm) {
|
||||
commit('d2admin/gray/set', true, { root: true })
|
||||
MessageBox.confirm('确定要注销当前用户吗', '注销用户', { type: 'warning' })
|
||||
.then(() => {
|
||||
commit('d2admin/gray/set', false, { root: true })
|
||||
logout()
|
||||
logout ({ dispatch }, { confirm = false } = {}) {
|
||||
// 加载遮罩
|
||||
let loading = null
|
||||
// 实际注销操作
|
||||
function doLogout () {
|
||||
logoutAdminUser()
|
||||
.finally(() => {
|
||||
// 删除sourceData
|
||||
util.cookies.set('block', 'true')
|
||||
dispatch('d2admin/db/set', {
|
||||
dbName: 'database',
|
||||
path: '$menu.sourceData',
|
||||
value: [],
|
||||
user: true
|
||||
}, { root: true })
|
||||
|
||||
// 删除info
|
||||
dispatch('d2admin/user/set', {}, { root: true })
|
||||
|
||||
// 删除cookie
|
||||
util.cookies.remove('token')
|
||||
util.cookies.remove('uuid')
|
||||
|
||||
// 刷新页面
|
||||
loading && loading.close()
|
||||
router.push({ name: 'login' })
|
||||
})
|
||||
.catch(() => {
|
||||
commit('d2admin/gray/set', false, { root: true })
|
||||
Message({ message: '取消注销操作' })
|
||||
})
|
||||
} else {
|
||||
logout()
|
||||
}
|
||||
|
||||
if (!confirm) {
|
||||
doLogout()
|
||||
return
|
||||
}
|
||||
|
||||
MessageBox.confirm('确定要执行注销操作吗?', '提示', {
|
||||
confirmButtonText: '确定',
|
||||
cancelButtonText: '取消',
|
||||
type: 'warning',
|
||||
closeOnClickModal: false
|
||||
})
|
||||
.then(() => {
|
||||
loading = Loading.service({
|
||||
lock: true,
|
||||
text: 'Loading',
|
||||
spinner: 'el-icon-loading',
|
||||
background: 'rgba(0, 0, 0, 0.7)'
|
||||
})
|
||||
doLogout()
|
||||
})
|
||||
.catch(() => {
|
||||
})
|
||||
},
|
||||
/**
|
||||
* @description 用户登录后从持久化数据加载一系列的设置
|
||||
@@ -83,6 +117,8 @@ export default {
|
||||
await dispatch('d2admin/size/load', null, { root: true })
|
||||
// 持久化数据加载颜色设置
|
||||
await dispatch('d2admin/color/load', null, { root: true })
|
||||
// DB -> store 持久化数据读取菜单源数据
|
||||
await dispatch('d2admin/menu/sourceDataLoad', null, { root: true })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,9 @@ export default {
|
||||
// 侧边栏收缩
|
||||
asideCollapse: setting.menu.asideCollapse,
|
||||
// 侧边栏折叠动画
|
||||
asideTransition: setting.menu.asideTransition
|
||||
asideTransition: setting.menu.asideTransition,
|
||||
// 菜单源数据
|
||||
sourceData: []
|
||||
},
|
||||
actions: {
|
||||
/**
|
||||
@@ -90,6 +92,19 @@ export default {
|
||||
}, { root: true })
|
||||
state.asideCollapse = menu.asideCollapse !== undefined ? menu.asideCollapse : setting.menu.asideCollapse
|
||||
state.asideTransition = menu.asideTransition !== undefined ? menu.asideTransition : setting.menu.asideTransition
|
||||
},
|
||||
/**
|
||||
* 持久化数据加载菜单源数据
|
||||
* @param {Object} context
|
||||
*/
|
||||
async sourceDataLoad ({ state, dispatch }) {
|
||||
const sourceData = await dispatch('d2admin/db/get', {
|
||||
dbName: 'database',
|
||||
path: '$menu.sourceData',
|
||||
defaultValue: [],
|
||||
user: true
|
||||
}, { root: true })
|
||||
state.sourceData = sourceData
|
||||
}
|
||||
},
|
||||
mutations: {
|
||||
|
||||
Reference in New Issue
Block a user