Former-commit-id: e761056ea154aedf2ae927936fff49ba5b9fcb66 [formerly e761056ea154aedf2ae927936fff49ba5b9fcb66 [formerly e761056ea154aedf2ae927936fff49ba5b9fcb66 [formerly e761056ea154aedf2ae927936fff49ba5b9fcb66 [formerly 3cc73e4d679f97554d2c1f8f253bc4d6447694c1 [formerly a05de779fd7fb56b722cdb448baee6b1d44208ed]]]]] Former-commit-id: 02aa23fd3e50eb0780d75b3bc9c5209dbf1f0e67 Former-commit-id: b21e3fe71b527439904a1a574e3d3a3032bcef09 Former-commit-id: bba111d14373493a4ef35318bfb7f41c8c388914 [formerly c1dd1cdbe86b65faff26c35db32d1ee942a97054] Former-commit-id: d2c677083ddb6e285ea033ce8e62432ca2a89907 Former-commit-id: 8fa9b2196bc47bf6bf97b1355630eb443c4d2564 Former-commit-id: ec58dc5a2aff6a11fa31144035ca0bd250acbe63 Former-commit-id: 27ef4661fcb60de5968ce0b75d7d006774f88f0c Former-commit-id: 352e82ebff5f22931fae48cb2525ad9867a98fa6
116 lines
4.1 KiB
JavaScript
116 lines
4.1 KiB
JavaScript
import { Message, MessageBox } from 'element-ui'
|
|
import util from '@/libs/util.js'
|
|
import router from '@/router'
|
|
import { AccountLogin } from '@api/sys.login'
|
|
import i18n from '../../../../i18n'
|
|
|
|
export default {
|
|
namespaced: true,
|
|
actions: {
|
|
/**
|
|
* @description 登录
|
|
* @param {Object} param context
|
|
* @param {Object} param username {String} 用户账号
|
|
* @param {Object} param password {String} 密码
|
|
* @param {Object} param route {Object} 登录成功后定向的路由对象 任何 vue-router 支持的格式
|
|
*/
|
|
login ({ dispatch }, {
|
|
username = '',
|
|
password = ''
|
|
} = {}) {
|
|
return new Promise((resolve, reject) => {
|
|
// 开始请求登录接口
|
|
AccountLogin({
|
|
username,
|
|
password
|
|
})
|
|
.then(async res => {
|
|
// 设置 cookie 一定要存 uuid 和 token 两个 cookie
|
|
// 整个系统依赖这两个数据进行校验和存储
|
|
// uuid 是用户身份唯一标识 用户注册的时候确定 并且不可改变 不可重复
|
|
// token 代表用户当前登录状态 建议在网络请求中携带 token
|
|
// 如有必要 token 需要定时更新,默认保存一天
|
|
util.cookies.set('uuid', res.uuid)
|
|
util.cookies.set('token', res.token)
|
|
// 设置 vuex 用户信息
|
|
await dispatch('d2admin/user/set', {
|
|
name: res.name
|
|
}, { root: true })
|
|
// 用户登录后从持久化数据加载一系列的设置
|
|
await dispatch('load')
|
|
// 结束
|
|
resolve()
|
|
})
|
|
.catch(err => {
|
|
console.log('err: ', err)
|
|
reject(err)
|
|
})
|
|
})
|
|
},
|
|
/**
|
|
* @description 注销用户并返回登录页面
|
|
* @param {Object} param context
|
|
* @param {Object} param confirm {Boolean} 是否需要确认
|
|
*/
|
|
logout ({ commit, dispatch }, { confirm = false } = {}) {
|
|
/**
|
|
* @description 注销
|
|
*/
|
|
async function logout () {
|
|
// 删除cookie
|
|
util.cookies.remove('token')
|
|
util.cookies.remove('uuid')
|
|
// 清空 vuex 用户信息
|
|
await dispatch('d2admin/user/set', {}, { root: true })
|
|
// 跳转路由
|
|
router.push({
|
|
name: 'login'
|
|
})
|
|
}
|
|
// 判断是否需要确认
|
|
if (confirm) {
|
|
commit('d2admin/gray/set', true, { root: true })
|
|
MessageBox.confirm(i18n.t('public.confirm.special.logout.message'), i18n.t('public.confirm.special.logout.title'), {
|
|
confirmButtonText: i18n.t('public.confirm.special.logout.button.confirm'),
|
|
cancelButtonText: i18n.t('public.confirm.special.logout.button.cancel'),
|
|
type: 'warning'
|
|
})
|
|
.then(() => {
|
|
commit('d2admin/gray/set', false, { root: true })
|
|
logout()
|
|
})
|
|
.catch(() => {
|
|
commit('d2admin/gray/set', false, { root: true })
|
|
Message({
|
|
message: i18n.t('public.message.special.logout.cancel')
|
|
})
|
|
})
|
|
} else {
|
|
logout()
|
|
}
|
|
},
|
|
/**
|
|
* @description 用户登录后从持久化数据加载一系列的设置
|
|
* @param {Object} state vuex state
|
|
*/
|
|
load ({ dispatch }) {
|
|
return new Promise(async resolve => {
|
|
// DB -> store 加载用户名
|
|
await dispatch('d2admin/user/load', null, { root: true })
|
|
// DB -> store 加载主题
|
|
await dispatch('d2admin/theme/load', null, { root: true })
|
|
// DB -> store 加载页面过渡效果设置
|
|
await dispatch('d2admin/transition/load', null, { root: true })
|
|
// DB -> store 持久化数据加载上次退出时的多页列表
|
|
await dispatch('d2admin/page/openedLoad', null, { root: true })
|
|
// DB -> store 持久化数据加载侧边栏折叠状态
|
|
await dispatch('d2admin/menu/asideCollapseLoad', null, { root: true })
|
|
// DB -> store 持久化数据加载全局尺寸
|
|
await dispatch('d2admin/size/load', null, { root: true })
|
|
// end
|
|
resolve()
|
|
})
|
|
}
|
|
}
|
|
}
|