vue實現隨機驗證碼功能

来源:https://www.cnblogs.com/web-aqin/archive/2019/04/30/10796326.html
-Advertisement-
Play Games

效果圖: 1.html代碼 1 <div class="form-group" style="display: flex;"> 2 <div> 3 <span>驗證碼:</span> 4 <input type="text" id="code" v-model="code" class="code" ...


效果圖:

 

1.html代碼

 1 <div class="form-group" style="display: flex;">
 2                     <div>
 3                         <span>驗證碼:</span>
 4                         <input type="text" id="code" v-model="code" class="code"  placeholder="請輸入您的驗證碼" />
 5                     </div>
 6                   <div class="login-code" @click="refreshCode">
 7             <!--驗證碼組件-->
 8             <s-identify :identifyCode="identifyCode"></s-identify>
 9             </div>
10                 </div>
HTML

 

2.css樣式

1 /*驗證碼樣式*/
2 .code{
3     width:124px;
4     height:31px;
5     border:1px solid rgba(186,186,186,1);
6 }
7 .login-code{
8      cursor: pointer;
9 }
CSS 代碼

 

3.js引入驗證碼組件,並且定義三個變數。

import SIdentify  from '../components/sidentify'

components: { SIdentify },
data () {
    return {
      identifyCodes: "1234567890",
      identifyCode: "",
      code:"",//text框輸入的驗證碼
    }
}, 
引入驗證碼組件,以及需要定義的變數

 

4.mounted里的代碼

1 mounted(){
2     this.identifyCode = "";
3     this.makeCode(this.identifyCodes, 4);
4 },
mounted代碼

 

5.在created里初始化驗證碼

 

6.methods里添加以下方法。

 1 //驗證碼
 2 randomNum(min, max) {
 3     return Math.floor(Math.random() * (max - min) + min);
 4 },
 5       
 6 refreshCode() {
 7     this.identifyCode = "";
 8     this.makeCode(this.identifyCodes, 4);
 9 },
10 makeCode(o, l) {
11     for (let i = 0; i < l; i++) {
12         this.identifyCode += this.identifyCodes[
13           this.randomNum(0, this.identifyCodes.length)
14         ];
15     }
16     console.log(this.identifyCode);
17 },
需要用到的方法

 

在提交表單的時候對驗證碼進行判斷。

 

sidentify.vue組件代碼:

代碼:

  1 <template>
  2     <div class="s-canvas">
  3         <canvas id="s-canvas" :width="contentWidth" :height="contentHeight"></canvas>
  4     </div>
  5 </template>
  6 <script>
  7 export default {
  8     name: 'SIdentify',
  9     props: {
 10         identifyCode: {
 11             type: String,
 12             default: '1234'
 13         },
 14         fontSizeMin: {
 15             type: Number,
 16             default: 25
 17         },
 18         fontSizeMax: {
 19             type: Number,
 20             default: 30
 21         },
 22         backgroundColorMin: {
 23             type: Number,
 24             default: 255
 25         },
 26         backgroundColorMax: {
 27             type: Number,
 28             default: 255
 29         },
 30         colorMin: {
 31             type: Number,
 32             default: 0
 33         },
 34         colorMax: {
 35             type: Number,
 36             default: 160
 37         },
 38         lineColorMin: {
 39             type: Number,
 40             default: 100
 41         },
 42         lineColorMax: {
 43             type: Number,
 44             default: 255
 45         },
 46         dotColorMin: {
 47             type: Number,
 48             default: 0
 49         },
 50         dotColorMax: {
 51             type: Number,
 52             default: 255
 53         },
 54         contentWidth: {
 55             type: Number,
 56             default: 112
 57         },
 58         contentHeight: {
 59             type: Number,
 60             default: 31
 61         }
 62     },
 63     methods: {
 64         // 生成一個隨機數
 65         randomNum(min, max) {
 66             return Math.floor(Math.random() * (max - min) + min)
 67         },
 68         // 生成一個隨機的顏色
 69         randomColor(min, max) {
 70             let r = this.randomNum(min, max)
 71             let g = this.randomNum(min, max)
 72             let b = this.randomNum(min, max)
 73             return 'rgb(' + r + ',' + g + ',' + b + ')'
 74         },
 75         drawPic() {
 76             let canvas = document.getElementById('s-canvas')
 77             let ctx = canvas.getContext('2d')
 78             ctx.textBaseline = 'bottom'
 79             // 繪製背景
 80             ctx.fillStyle = this.randomColor(this.backgroundColorMin, this.backgroundColorMax)
 81             ctx.fillRect(0, 0, this.contentWidth, this.contentHeight)
 82             // 繪製文字
 83             for (let i = 0; i < this.identifyCode.length; i++) {
 84                 this.drawText(ctx, this.identifyCode[i], i)
 85             }
 86             this.drawLine(ctx)
 87             this.drawDot(ctx)
 88         },
 89         drawText(ctx, txt, i) {
 90             ctx.fillStyle = this.randomColor(this.colorMin, this.colorMax)
 91             ctx.font = this.randomNum(this.fontSizeMin, this.fontSizeMax) + 'px SimHei'
 92             let x = (i + 1) * (this.contentWidth / (this.identifyCode.length + 1))
 93             let y = this.randomNum(this.fontSizeMax, this.contentHeight - 5)
 94             var deg = this.randomNum(-45, 45)
 95             // 修改坐標原點和旋轉角度
 96             ctx.translate(x, y)
 97             ctx.rotate(deg * Math.PI / 180)
 98             ctx.fillText(txt, 0, 0)
 99             // 恢復坐標原點和旋轉角度
100             ctx.rotate(-deg * Math.PI / 180)
101             ctx.translate(-x, -y)
102         },
103         drawLine(ctx) {
104             // 繪製干擾線
105             for (let i = 0; i < 5; i++) {
106                 ctx.strokeStyle = this.randomColor(this.lineColorMin, this.lineColorMax)
107                 ctx.beginPath()
108                 ctx.moveTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight))
109                 ctx.lineTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight))
110                 ctx.stroke()
111             }
112         },
113         drawDot(ctx) {
114             // 繪製干擾點
115             for (let i = 0; i < 80; i++) {
116                 ctx.fillStyle = this.randomColor(0, 255)
117                 ctx.beginPath()
118                 ctx.arc(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight), 1, 0, 2 * Math.PI)
119                 ctx.fill()
120             }
121         }
122     },
123     watch: {
124         identifyCode() {
125             this.drawPic()
126         }
127     },
128     mounted() {
129         this.drawPic()
130     }
131 }
132 </script>
133 <style scoped>
134 .s-canvas {
135     height: 38px;
136    
137 }
138 .s-canvas canvas{
139     margin-top: 1px;
140     margin-left: 8px;
141 }
142 </style>

這篇文章是我參考別人寫的,很感謝那個博主。

 登錄功能帶驗證碼的例子:https://www.jianshu.com/p/99c6e2f3e457

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • https://www.cnblogs.com/wisewrong/archive/2018/12/28/10186611.html 參考這個就行。 而我這篇文章主要是對裡面的相關步驟作一些簡單的說明。 1:安裝Vue-cli3 2:目錄創建:packages 目錄用於存放自己定義的組件 examp ...
  • 參考Promise 的 官方規範 https://promisesaplus.com/ Promise 其實就是一個狀態機 它只有兩種狀態變化 pending =》 fulfilled pending =》 rejected 並且狀態一旦發生變化後就不會再改變 我們用es5來實現下 先寫個架子, 並 ...
  • http/1.0、http/1.1、http/2都有哪些不同的機制,經歷了哪些變革,對前端優化有什麼樣的影響? ...
  • css: js: main.js代碼: 1 import Vuex from 'vuex' 2 Vue.use(Vuex); 3 const store = new Vuex.Store({ 4 state: { 5 majorDetail: false, 6 Index: document.loc ...
  • 映射(Map) 映射(Map)是十分常見的一種數據結構,由一系列鍵(key)和值(value)組成的。每個key對應一個value,根據key可以獲取和設定value,也可以根據key來查詢value。 上面那個圖展示了一個映射,該映射以每個值得id作為鍵,每個鍵對應一個值。 上面那個圖展示了一個映 ...
  • 按鈕樣式: 需要載入jq <script> (function(a){a.fn.scrollToTop=function(c){var d={speed:800};c&&a.extend(d,{speed:c});return this.each(function(){var b=a(this);a ...
  • js練習計算器,支持滑鼠點擊、鍵盤操作 ...
  • 學習筆記 1.JQuery添加節點相關方法 2.JQuery刪除節點 3.JQuery替換節點 4.JQuery節點的複製 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...