vue+element 獲取驗證碼


我們在做一個項目,登錄注冊頁面是少不了的,為了人機校驗,驗證碼也是必須的

我的這個項目獲取驗證碼,前端發送一個隨機四位數給后端,后端返回一張圖片,前端渲染就可以

template代碼:

 1 <el-form-item label="" prop="captcha_code">
 2  <el-input
 3   v-model="loginForm.captcha_code"
 4   placeholder="驗證碼"
 5   prefix-icon="lj-icon-yanzhengma"
 6   autocomplete="off"
 7   autocapitalize="off"
 8   spellcheck="false"
 9   maxlength="4"
10   @keyup.enter.native="handleLogin"
11   style="float: left; width: 122px;"
12   ></el-input>
13    <div class="captcha_code">
14     <img src="" ref="code" @click="changeCode">
15    </div>
16  </el-form-item>
17  <el-button
18   :loading="loading"
19   type="primary"
20   style="width: 100%;"
21   @click="handleLogin"
22 >登錄</el-button>

data數據聲明:

 1 data() {
 2     return {
 3       loginForm: {
 4         username: "",
 5         password: "",
 6         captcha_key: "",
 7         captcha_code: ""
 8       },
 9     }
10 }

mounted數據加載完執行方法:

 1 mounted() { 2 // 得到驗證碼圖片 3 this.changeCode(); 4 }, 

methods方法:

 1 getCaptchaKey() {
 2       let captchaKey = Math.random()
 3         .toString(36)
 4         .substring(2);
 5       return captchaKey;
 6     },
 7 changeCode() {
 8       let captcha_key = this.getCaptchaKey();
 9       this.loginForm.captcha_key = captcha_key;
10       this.$refs.code.setAttribute(
11         "src",
12         process.env.VUE_APP_API_URL +
13           "auth/get_captcha?captcha_key=" +
14           captcha_key
15       );
16     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM