前端整合和显示用户登录信息
# 一、创建布局模板
layout/sign.vue
<template>
<div class="sign">
<!--标题-->
<div class="logo">
<img src="~/assets/img/logo.png" alt="logo">
</div>
<!--表单-->
<nuxt/>
</div>
</template>
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 二、创建页面
# 1、注册页
pages/register.vue
<template>
<div class="main">
<div class="title">
<a href="/login">登录</a>
<span>·</span>
<a class="active" href="/register">注册</a>
</div>
<div class="sign-up-container">
<form action="register">
<div class="input-prepend restyle">
<input type="text" placeholder="你的昵称">
<i class="iconfont icon-user"/>
</div>
<div class="input-prepend restyle no-radius">
<input type="text" placeholder="手机号">
<i class="iconfont icon-phone"/>
</div>
<div class="input-prepend">
<input type="password" placeholder="设置密码">
<i class="iconfont icon-password"/>
</div>
<div class="btn">
<input type="submit" class="sign-up-button" value="注册">
</div>
<p class="sign-up-msg">
点击 “注册” 即表示您同意并愿意遵守简书
<br>
<a target="_blank" href="http://www.jianshu.com/p/c44d171298ce">用户协议</a>
和
<a target="_blank" href="http://www.jianshu.com/p/2ov8x3">隐私政策</a> 。
</p>
</form>
<!-- 更多注册方式 -->
<div class="more-sign">
<h6>社交帐号直接注册</h6>
<ul>
<li><a id="weixin" class="weixin" target="_blank" href="http://localhost:8201/api/ucenter/wx/login"><i class="iconfont icon-weixin"/></a></li>
<li><a id="qq" class="qq" target="_blank" href="#"><i class="iconfont icon-qq"/></a></li>
</ul>
</div>
</div>
</div>
</template>
<script>
import '~/assets/css/sign.css'
import '~/assets/css/iconfont.css'
export default {
layout: 'sign'
}
</script>
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
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
# 2、登录页面
pages/login.vue
<template>
<div class="main">
<div class="title">
<a class="active" href="/login">登录</a>
<span>·</span>
<a href="/register">注册</a>
</div>
<div class="sign-up-container">
<form action="register">
<div class="input-prepend restyle">
<input type="text" placeholder="手机号">
<i class="iconfont icon-phone"/>
</div>
<div class="input-prepend">
<input type="password" placeholder="密码">
<i class="iconfont icon-password"/>
</div>
<div class="btn">
<input type="submit" class="sign-in-button" value="登录">
</div>
</form>
<!-- 更多登录方式 -->
<div class="more-sign">
<h6>社交帐号登录</h6>
<ul>
<li><a id="weixin" class="weixin" target="_blank" href="http://localhost:8201/api/ucenter/wx/login"><i class="iconfont icon-weixin"/></a></li>
<li><a id="qq" class="qq" target="_blank" href="#"><i class="iconfont icon-qq"/></a></li>
</ul>
</div>
</div>
</div>
</template>
<script>
import '~/assets/css/sign.css'
import '~/assets/css/iconfont.css'
export default {
layout: 'sign'
}
</script>
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
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
# 三、显示用户登录信息
# 1、解析jwt token
MemberController
package com.guli.ucenter.controller;
@CrossOrigin
@RestController
@RequestMapping("/ucenter/member")
public class MemberController {
@PostMapping("info/{token}")
public R getInfoByToken(@PathVariable String token){
Claims claims = JwtUtils.checkJWT(token);
String nickname = (String)claims.get("nickname");
String avatar = (String)claims.get("avatar");
String id = (String)claims.get("id");
LoginInfoVo loginInfoVo = new LoginInfoVo();
loginInfoVo.setId(id);
loginInfoVo.setAvatar(avatar);
loginInfoVo.setNickname(nickname);
return R.ok().data("loginInfo", loginInfoVo);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 2、前端获取用户信息
创建api/member.js
import request from '@/utils/request'
const api_name = '/ucenter/member'
export default {
getInfoByToken(token) {
return request({
url: `${api_name}/info/${token}`,
method: 'post'
})
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 3、default.vue脚本
import member from '@/api/member'
export default {
data() {
return {
token: '',
loginInfo: {}
}
},
created() {
console.log(this.$route.query.token)
this.token = this.$route.query.token
if (this.token) {
this.showInfo(this.token)
}
},
methods: {
showInfo() {
member.getInfoByToken(this.token).then(response => {
const loginInfo = response.data.data.loginInfo
if (loginInfo) { // token未过期
this.loginInfo = response.data.data.loginInfo
} else {
this.$router.push({ path: '/' })
}
})
}
}
}
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
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
# 4、default.vue显示用户信息
<ul class="h-r-login">
<li v-if="!loginInfo.id" id="no-login">
<a href="/login" title="登录">
<em class="icon18 login-icon"> </em>
<span class="vam ml5">登录</span>
</a>
|
<a href="/register" title="注册">
<span class="vam ml5">注册</span>
</a>
</li>
<li v-if="loginInfo.id" id="is-login-two" class="h-r-user">
<a href="#" title>
<img
:src="loginInfo.avatar"
width="30"
height="30"
class="vam picImg"
alt
>
<span id="userName" class="vam disIb" style="max-width:100%">{{ loginInfo.nickname }}</span>
</a>
<a href="/" title="退出" class="ml5">退出</a>
</li>
<!-- /未登录显示第1 li;登录后显示第2 li -->
</ul>
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
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
编辑 (opens new window)
上次更新: 2024/06/15, 15:12:25