Skip to content

Commit 5dbd6ab

Browse files
authored
Captcha (#514)
* fix: component cannot be rendered due to switch routing * feat(login): add captcha logic * feat: add captcha functionality
1 parent 13654d2 commit 5dbd6ab

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,15 @@ QQ群搜索:Lin CMS 官方交流群 或 814597236
100100

101101
## 版本日志
102102

103-
最新版本 `0.4.0`
103+
最新版本 `0.4.1`
104104

105+
### 0.4.1
106+
107+
1. `A` 新增验证码功能,默认关闭验证码
105108
### 0.4.0
106109

107110
1. `U` 升级到 Vue3 版本
111+
108112
### 0.3.5
109113

110114
1. `F` 统一前端规范,文件夹、文件名统一用单数和小写字母中划线形式

src/lin/model/user.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import store from '@/store'
2-
import _axios, { post, get, put } from '@/lin/plugin/axios'
2+
import _axios, { get, put } from '@/lin/plugin/axios'
33
import { saveTokens } from '../util/token'
44

55
export default class User {
@@ -24,13 +24,23 @@ export default class User {
2424

2525
/**
2626
* 登陆获取tokens
27-
* @param {string} username 用户名
28-
* @param {string} password 密码
27+
* @param { String } username 用户名
28+
* @param { String } password 密码
29+
* @param { String } captcha 验证码
30+
* @param { String } tag 验证码签名
2931
*/
30-
static async getToken(username, password) {
31-
const tokens = await post('cms/user/login', {
32-
username,
33-
password,
32+
static async getToken(username, password, captcha, tag) {
33+
const tokens = await _axios({
34+
url: 'cms/user/login',
35+
method: 'POST',
36+
data: {
37+
captcha,
38+
username,
39+
password,
40+
},
41+
headers: {
42+
tag,
43+
},
3444
})
3545
saveTokens(tokens.access_token, tokens.refresh_token)
3646
return tokens

src/view/login/login.vue

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
<span class="icon secret-icon"></span>
1313
<input type="password" v-model="account.password" autocomplete="off" placeholder="请填写用户登录密码" />
1414
</div>
15+
<div class="form-item password" v-if="captchaImage">
16+
<img class="captcha" :src="captchaImage" />
17+
<input type="password" v-model="account.captcha" autocomplete="off" placeholder="请填写验证码" />
18+
</div>
1519
<button class="submit-btn" type="submit">登录</button>
1620
</form>
1721
</div>
@@ -22,14 +26,17 @@
2226
import { reactive, ref, onMounted, getCurrentInstance } from 'vue'
2327
import { useStore } from 'vuex'
2428
import { useRouter } from 'vue-router'
29+
import axios from 'lin/plugin/axios'
2530
import UserModel from '@/lin/model/user'
2631
import Utils from '@/lin/util/util'
2732
import Config from '@/config'
2833
2934
export default {
3035
setup() {
36+
let tag = ''
3137
const wait = 2000 // 2000ms之内不能重复发起请求
3238
const loading = ref(false)
39+
const captchaImage = ref('')
3340
const store = useStore()
3441
const router = useRouter()
3542
const throttleLogin = ref(null)
@@ -38,16 +45,17 @@ export default {
3845
const account = reactive({
3946
username: '',
4047
password: '',
48+
captcha: '',
4149
})
4250
4351
/**
4452
* 根据账号密码登录,拿到 token 并储存
4553
*/
4654
const login = async () => {
47-
const { username, password } = account
55+
const { username, password, captcha } = account
4856
try {
4957
loading.value = true
50-
await UserModel.getToken(username, password)
58+
await UserModel.getToken(username, password, captcha, tag)
5159
await getInformation()
5260
loading.value = false
5361
router.push(Config.defaultRoute)
@@ -60,6 +68,16 @@ export default {
6068
}
6169
}
6270
71+
const getCaptcha = async () => {
72+
axios({
73+
method: 'POST',
74+
url: 'cms/user/captcha',
75+
}).then(result => {
76+
;({ tag } = result)
77+
captchaImage.value = result.image
78+
})
79+
}
80+
6381
/**
6482
* 获取并更新当前管理员信息
6583
*/
@@ -78,12 +96,14 @@ export default {
7896
* 节流登录
7997
*/
8098
onMounted(() => {
99+
getCaptcha()
81100
throttleLogin.value = Utils.throttle(login, wait)
82101
})
83102
84103
return {
85104
account,
86105
loading,
106+
captchaImage,
87107
throttleLogin,
88108
}
89109
},
@@ -130,6 +150,7 @@ export default {
130150
width: 100%;
131151
132152
.form-item {
153+
position: relative;
133154
width: 100%;
134155
height: 40px;
135156
box-sizing: border-box;
@@ -145,6 +166,13 @@ export default {
145166
padding-left: 74px;
146167
box-sizing: border-box;
147168
}
169+
170+
.captcha {
171+
position: absolute;
172+
width: 80px;
173+
right: 30px;
174+
top: -22px;
175+
}
148176
}
149177
150178
.form-item.nickname {

0 commit comments

Comments
 (0)