Skip to content

Commit 91ecc00

Browse files
committed
vue2 验证码,增加开关、修复对社交登录的支持
1 parent 586f517 commit 91ecc00

File tree

8 files changed

+65
-37
lines changed

8 files changed

+65
-37
lines changed

.env.demo1024

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ VUE_APP_APP_NAME ='/admin-ui/'
1717
# 多租户的开关
1818
VUE_APP_TENANT_ENABLE = true
1919

20+
# 验证码的开关
21+
VUE_APP_CAPTCHA_ENABLE = true
22+
2023
# 文档的开关
2124
VUE_APP_DOC_ENABLE = true
2225

.env.dev

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ VUE_CLI_BABEL_TRANSPILE_MODULES = true
1313
# 多租户的开关
1414
VUE_APP_TENANT_ENABLE = true
1515

16+
# 验证码的开关
17+
VUE_APP_CAPTCHA_ENABLE = true
18+
1619
# 文档的开关
1720
VUE_APP_DOC_ENABLE = true
1821

.env.local

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ VUE_CLI_BABEL_TRANSPILE_MODULES = true
1414
# 多租户的开关
1515
VUE_APP_TENANT_ENABLE = true
1616

17+
# 验证码的开关
18+
VUE_APP_CAPTCHA_ENABLE = true
19+
1720
# 文档的开关
1821
VUE_APP_DOC_ENABLE = true
1922

.env.prod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ VUE_APP_APP_NAME ='yudao-admin'
1515
# 多租户的开关
1616
VUE_APP_TENANT_ENABLE = true
1717

18+
# 验证码的开关
19+
VUE_APP_CAPTCHA_ENABLE = true
20+
1821
# 文档的开关
1922
VUE_APP_DOC_ENABLE = false
2023

.env.stage

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ PUBLIC_PATH = 'http://static.yudao.iocoder.cn/'
1515
# 多租户的开关
1616
VUE_APP_TENANT_ENABLE = true
1717

18+
# 验证码的开关
19+
VUE_APP_CAPTCHA_ENABLE = true
20+
1821
# 文档的开关
1922
VUE_APP_DOC_ENABLE = false
2023

src/utils/ruoyi.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,19 @@ export function getTenantEnable() {
185185
return process.env.VUE_APP_TENANT_ENABLE || true;
186186
}
187187

188+
/**
189+
* 获得验证码功能是否开启
190+
*/
191+
export function getCaptchaEnable() {
192+
if (process.env.VUE_APP_CAPTCHA_ENABLE === "true") {
193+
return true;
194+
}
195+
if (process.env.VUE_APP_CAPTCHA_ENABLE === "false") {
196+
return false;
197+
}
198+
return process.env.VUE_APP_CAPTCHA_ENABLE || true;
199+
}
200+
188201
/**
189202
* 获得文档是否开启
190203
*/

src/views/login.vue

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,11 @@
8787
</div>
8888
</div>
8989
</div>
90-
<Verify
91-
ref="verify"
92-
:captcha-type="'blockPuzzle'"
93-
:img-size="{width:'400px',height:'200px'}"
94-
@success="handleLogin"
95-
/>
90+
91+
<!-- 图形验证码 -->
92+
<Verify ref="verify" :captcha-type="'blockPuzzle'" :img-size="{width:'400px',height:'200px'}"
93+
@success="handleLogin" />
94+
9695
<!-- footer -->
9796
<div class="footer">
9897
Copyright © 2020-2022 iocoder.cn All Rights Reserved.
@@ -104,7 +103,7 @@
104103
import {sendSmsCode, socialAuthRedirect} from "@/api/login";
105104
import {getTenantIdByName} from "@/api/system/tenant";
106105
import {SystemUserSocialTypeEnum} from "@/utils/constants";
107-
import {getTenantEnable} from "@/utils/ruoyi";
106+
import {getCaptchaEnable, getTenantEnable} from "@/utils/ruoyi";
108107
import {
109108
getPassword,
110109
getRememberMe, getTenantName,
@@ -188,16 +187,21 @@ export default {
188187
created() {
189188
// 租户开关
190189
this.tenantEnable = getTenantEnable();
190+
// 验证码开关
191+
this.captchaEnable = getCaptchaEnable();
191192
// 重定向地址
192193
this.redirect = this.$route.query.redirect;
193194
this.getCookie();
194195
},
195196
methods: {
196197
getCode() {
197-
// 只有开启的状态,才加载验证码。默认开启
198+
// 情况一,未开启:则直接登录
198199
if (!this.captchaEnable) {
200+
this.handleLogin({})
199201
return;
200202
}
203+
204+
// 情况二,已开启:则展示验证码;只有完成验证码的情况,才进行登录
201205
// 弹出验证码
202206
this.$refs.verify.show()
203207
},
@@ -214,7 +218,7 @@ export default {
214218
tenantName: tenantName ? tenantName : this.loginForm.tenantName,
215219
};
216220
},
217-
handleLogin(params) {
221+
handleLogin(captchaParams) {
218222
this.$refs.loginForm.validate(valid => {
219223
if (valid) {
220224
this.loading = true;
@@ -230,7 +234,7 @@ export default {
230234
removeRememberMe()
231235
removeTenantName()
232236
}
233-
this.loginForm.captchaVerification = params.captchaVerification
237+
this.loginForm.captchaVerification = captchaParams.captchaVerification
234238
// 发起登陆
235239
// console.log("发起登录", this.loginForm);
236240
this.$store.dispatch(this.loginForm.loginType === "sms" ? "SmsLogin" : "Login", this.loginForm).then(() => {

src/views/socialLogin.vue

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,11 @@
3232
<svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
3333
</el-input>
3434
</el-form-item>
35-
<el-form-item prop="code" v-if="captchaEnable">
36-
<el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%"
37-
@keyup.enter.native="handleLogin">
38-
<svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon"/>
39-
</el-input>
40-
<div class="login-code">
41-
<img :src="codeUrl" @click="getCode" class="login-code-img"/>
42-
</div>
43-
</el-form-item>
4435
<el-checkbox v-model="loginForm.rememberMe" style="margin:0 0 25px 0;">记住密码</el-checkbox>
4536
<!-- 下方的登录按钮 -->
4637
<el-form-item style="width:100%;">
4738
<el-button :loading="loading" size="medium" type="primary" style="width:100%;"
48-
@click.native.prevent="handleLogin">
39+
@click.native.prevent="getCode">
4940
<span v-if="!loading">登 录</span>
5041
<span v-else>登 录 中...</span>
5142
</el-button>
@@ -55,6 +46,11 @@
5546
</div>
5647
</div>
5748
</div>
49+
50+
<!-- 图形验证码 -->
51+
<Verify ref="verify" :captcha-type="'blockPuzzle'" :img-size="{width:'400px',height:'200px'}"
52+
@success="handleLogin" />
53+
5854
<!-- footer -->
5955
<div class="footer">
6056
Copyright © 2020-2022 iocoder.cn All Rights Reserved.
@@ -72,10 +68,15 @@ import {
7268
setRememberMe,
7369
setUsername
7470
} from "@/utils/auth";
75-
import {getCodeImg} from "@/api/login";
71+
72+
import Verify from '@/components/Verifition/Verify';
73+
import {getCaptchaEnable} from "@/utils/ruoyi";
7674
7775
export default {
7876
name: "ThirdLogin",
77+
components: {
78+
Verify
79+
},
7980
data() {
8081
return {
8182
codeUrl: "",
@@ -85,8 +86,7 @@ export default {
8586
username: "admin",
8687
password: "admin123",
8788
rememberMe: false,
88-
code: "",
89-
uuid: "",
89+
captchaVerification: "",
9090
},
9191
loginRules: {
9292
username: [
@@ -114,9 +114,10 @@ export default {
114114
// },
115115
created() {
116116
this.getCookie();
117+
// 验证码开关
118+
this.captchaEnable = getCaptchaEnable();
117119
// 重定向地址
118120
this.redirect = this.$route.query.redirect;
119-
this.getCode();
120121
// 社交登录相关
121122
this.type = this.$route.query.type;
122123
this.code = this.$route.query.code;
@@ -133,19 +134,15 @@ export default {
133134
},
134135
methods: {
135136
getCode() {
136-
// 只有开启的状态,才加载验证码。默认开启
137+
// 情况一,未开启:则直接登录
137138
if (!this.captchaEnable) {
139+
this.handleLogin({})
138140
return;
139141
}
140-
// 请求远程,获得验证码
141-
getCodeImg().then(res => {
142-
res = res.data;
143-
this.captchaEnable = res.enable;
144-
if (this.captchaEnable) {
145-
this.codeUrl = "data:image/gif;base64," + res.img;
146-
this.loginForm.uuid = res.uuid;
147-
}
148-
});
142+
143+
// 情况二,已开启:则展示验证码;只有完成验证码的情况,才进行登录
144+
// 弹出验证码
145+
this.$refs.verify.show()
149146
},
150147
getCookie() {
151148
const username = getUsername();
@@ -158,7 +155,7 @@ export default {
158155
loginType: this.loginForm.loginType,
159156
};
160157
},
161-
handleLogin() {
158+
handleLogin(captchaParams) {
162159
this.$refs.loginForm.validate(valid => {
163160
if (valid) {
164161
this.loading = true;
@@ -177,8 +174,7 @@ export default {
177174
// 账号密码登录
178175
username: this.loginForm.username,
179176
password: this.loginForm.password,
180-
code: this.loginForm.code,
181-
uuid: this.loginForm.uuid,
177+
captchaVerification: captchaParams.captchaVerification
182178
}).then(() => {
183179
this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
184180
}).catch(() => {

0 commit comments

Comments
 (0)