-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest.html
More file actions
77 lines (76 loc) · 2.13 KB
/
test.html
File metadata and controls
77 lines (76 loc) · 2.13 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>SSO TEST</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="app">
<div v-if="!userinfo.username">
<input type="text" placeholder="用户名" v-model="username" />
<input type="password" placeholder="密码" v-model="password" />
<button @click="login">登录</button>
</div>
<div v-else>
用户已经登录: {{ userinfo.username }}
</div>
<button @click="getUserInfo">获取用户信息</button>
<button @click="logout">退出登录</button>
</div>
<script>
const passportUrl = 'http://passport.xxx.com:1280'
const axiosInstance = axios.create({
headers: {'Content-Type': 'application/json;charset=utf-8'},
withCredentials: true
})
const app = new Vue({
el: '#app',
data () {
return {
username: '',
password: '',
isLogin: false,
userinfo: {}
}
},
mounted () {
this.getUserInfo()
},
methods: {
async getUserInfo () {
let res = await axiosInstance.get('/users')
if (res.data.code === 0) {
this.userinfo = res.data.data
}
},
async login () {
try {
console.log('login')
let res = await axiosInstance.post(`${passportUrl}/login`, {
username: this.username,
password: this.password
})
console.log(res)
if (res.data.code === 0) {
alert('登录成功')
this.getUserInfo()
}
} catch (err) {
console.log(err.message)
}
},
async logout () {
let res = await axiosInstance.post(`${passportUrl}/logout`)
if (res.data.code === 0) {
this.userinfo = {}
}
}
}
})
</script>
</body>
</html>