Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
kadycui
game-admin
提交
61af6c74
game-admin
项目概览
kadycui
/
game-admin
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
game-admin
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
61af6c74
编写于
8月 30, 2023
作者:
kadycui
💻
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ADD: 登录调整
上级
10f58c0d
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
41 addition
and
28 deletion
+41
-28
src/pages/login.vue
src/pages/login.vue
+5
-24
src/permission.js
src/permission.js
+10
-1
src/store/index.js
src/store/index.js
+26
-3
未找到文件。
src/pages/login.vue
浏览文件 @
61af6c74
...
...
@@ -43,9 +43,8 @@
<
script
setup
>
import
{
reactive
,
ref
}
from
'
vue
'
import
{
useStore
}
from
'
vuex
'
import
{
useStore
}
from
'
vuex
'
import
{
useRouter
}
from
'
vue-router
'
import
{
login
,
getInfo
}
from
'
@/api/manager
'
import
{
setToken
}
from
'
@/composeables/auth
'
import
{
toast
}
from
'
@/composeables/util
'
...
...
@@ -94,30 +93,12 @@ const onSubmit = () => {
return
false
}
loading
.
value
=
true
login
(
form
.
username
,
form
.
password
)
.
then
(
res
=>
{
console
.
log
(
res
)
// 提示成功
toast
(
"
登录成功
"
)
// 存储token和用户相关信息
setToken
(
res
.
token
)
// 获取管理员信息
getInfo
()
.
then
(
res2
=>
{
store
.
commit
(
"
SET_USERINFO
"
,
res2
)
console
.
log
(
res2
)
})
// 跳转到首页
router
.
push
(
"
/
"
)
})
store
.
dispatch
(
"
login
"
,
form
).
then
(
res
=>
{
toast
(
"
登录成功
"
)
router
.
push
(
"
/
"
)
})
.
finally
(()
=>
{
loading
.
value
=
false
})
...
...
src/permission.js
浏览文件 @
61af6c74
import
router
from
"
@/router
"
import
{
getToken
}
from
"
@/composeables/auth
"
import
{
toast
}
from
"
@/composeables/util
"
import
store
from
"
./store
"
// 全局前置守卫
router
.
beforeEach
((
to
,
from
,
next
)
=>
{
router
.
beforeEach
(
async
(
to
,
from
,
next
)
=>
{
console
.
log
(
"
全局前置守卫
"
)
const
token
=
getToken
()
...
...
@@ -20,6 +21,14 @@ router.beforeEach((to, from, next) => {
return
next
({
path
:
from
.
path
?
from
.
path
:
"
/
"
})
}
// 如果用户登录, 自动获取用户信息, 并存储在vuex中
if
(
token
){
await
store
.
dispatch
(
"
getinfo
"
)
}
next
()
})
\ No newline at end of file
src/store/index.js
浏览文件 @
61af6c74
import
{
createStore
}
from
'
vuex
'
import
{
login
,
getInfo
}
from
'
@/api/manager
'
import
{
setToken
}
from
'
@/composeables/auth
'
// 创建一个新的 store 实例
...
...
@@ -6,18 +9,38 @@ const store = createStore({
state
()
{
return
{
// 用户信息
user
:{}
user
:
{}
}
},
mutations
:
{
// 记录用户信息
SET_USERINFO
(
state
,
user
){
SET_USERINFO
(
state
,
user
)
{
state
.
user
=
user
}
},
actions
:
{
// 登录
login
({
commit
},
{
username
,
password
})
{
return
new
Promise
((
resolve
,
reject
)
=>
{
login
(
username
,
password
).
then
(
res
=>
{
setToken
(
res
.
token
)
resolve
(
res
)
}).
catch
(
err
=>
reject
(
err
))
})
},
// 获取当前登录用户信息
getinfo
({
commit
})
{
return
new
Promise
((
resolve
,
reject
)
=>
{
getInfo
().
then
(
res
=>
{
commit
(
"
SET_USERINFO
"
,
res
)
resolve
(
res
)
}).
catch
(
err
=>
reject
(
err
))
})
}
}
})
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录