Vue_注册登录(短信验证码登录)
?
一、前言? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
1、動態獲取圖片驗證碼
2、實現手機驗證碼登錄(工具準備)
3、手機驗證碼登錄(后臺實現)
3、前臺實現
?
二、主要內容? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
1、動態獲取圖片驗證碼
? ? (1)請求的接口如下,返回的是一張svg的圖片
## 獲取一次性驗證碼### 請求URL:http://localhost:3000/captcha ### 請求方式:? ? ?(2)初次顯示圖片,可以直接在image中的src中請求路徑直接得到
<!--第一次顯示直接請求http://localhost:4000下面的--> <!--點擊圖片的時候要更新圖片,注冊一個點擊事件--> <input type="text" maxlength="11" placeholder="驗證碼" v-model="captche"> <img class="get_verification" src="http://localhost:4000/captcha" alt="captcha" @click='getCaptcha'>? ? ? (3)點擊圖片的時候更新,methods中調用方法
//獲取圖片驗證碼 getCaptcha(event){console.log(this) console.log(event.target)event.target.src="http://localhost:4000/captcha?Time="+Date.now()}?
?2、實現手機驗證碼登錄(工具準備)
? ? (1)這里用到“容聯通信云”:https://www.yuntongxun.com注冊登錄好之后,每個人都有不同的id號
? ? ? ? ? ?
? ?(2)然后選擇你要的驗證類型
? ? ? ? ? ??
?
? ? (3)你需要添加一個測試號碼來接收驗證短信
? ? ?
? ? ?(4)接下來就可以根據官方文檔寫代碼了
?
3、手機驗證碼登錄(后臺實現)
? ? (1)后臺項目結構如圖所示
? ? ?
? ? (2)發送shengchen
var md5 = require('blueimp-md5') var moment = require('moment') var Base64 = require('js-base64').Base64; var request = require('request');/*生成指定長度的隨機數*/ function randomCode(length) {var chars = ['0','1','2','3','4','5','6','7','8','9'];var result = ""; //統一改名: alt + shift + Rfor(var i = 0; i < length ; i ++) {var index = Math.ceil(Math.random()*9);result += chars[index];}return result; } // console.log(randomCode(6)); exports.randomCode = randomCode;/* 向指定號碼發送指定驗證碼*/ function sendCode(phone, code, callback) {var ACCOUNT_SID = '8a216da86a960fd9016a96d0eb580180';var AUTH_TOKEN = '3abf248c565446d0bf10d46eb62dee07';var Rest_URL = 'https://app.cloopen.com:8883';var AppID = '8a216da86a960fd9016a96d0eba80186';//1. 準備請求url/*1.使用MD5加密(賬戶Id + 賬戶授權令牌 + 時間戳)。其中賬戶Id和賬戶授權令牌根據url的驗證級別對應主賬戶。時間戳是當前系統時間,格式"yyyyMMddHHmmss"。時間戳有效時間為24小時,如:201404161420302.SigParameter參數需要大寫,如不能寫成sig=abcdefg而應該寫成sig=ABCDEFG*/var sigParameter = '';var time = moment().format('YYYYMMDDHHmmss');sigParameter = md5(ACCOUNT_SID+AUTH_TOKEN+time);var url = Rest_URL+'/2013-12-26/Accounts/'+ACCOUNT_SID+'/SMS/TemplateSMS?sig='+sigParameter;//2. 準備請求體var body = {to : phone,appId : AppID,templateId : '1',"datas":[code,"1"]}//body = JSON.stringify(body);//3. 準備請求頭/*1.使用Base64編碼(賬戶Id + 冒號 + 時間戳)其中賬戶Id根據url的驗證級別對應主賬戶2.冒號為英文冒號3.時間戳是當前系統時間,格式"yyyyMMddHHmmss",需與SigParameter中時間戳相同。*/var authorization = ACCOUNT_SID + ':' + time;authorization = Base64.encode(authorization);var headers = {'Accept' :'application/json','Content-Type' :'application/json;charset=utf-8','Content-Length': JSON.stringify(body).length+'','Authorization' : authorization}//4. 發送請求, 并得到返回的結果, 調用callback// callback(true); request({method : 'POST',url : url,headers : headers,body : body,json : true}, function (error, response, body) {console.log(error, response, body);callback(body.statusCode==='000000');// callback(true); }); } exports.sendCode = sendCode;/*調用方式 sendCode('13716**2779', randomCode(6), function (success) {console.log(success); })*/?
? (2)在index.js中調用
var express = require('express'); var router = express.Router(); const md5 = require('blueimp-md5') const models = require('../db/models') const UserModel = models.getModel('user') const _filter = {'pwd': 0, '__v': 0} // 查詢時過濾掉 const sms_util = require('../util/sms_util') const users = {} const ajax = require('../api/ajax') var svgCaptcha = require('svg-captcha')/* 發送驗證碼短信 */ router.get('/sendcode', function (req, res, next) {//1. 獲取請求參數數據var phone = req.query.phone;//2. 處理數據//生成驗證碼(6位隨機數)var code = sms_util.randomCode(6);//發送給指定的手機號 console.log(`向${phone}發送驗證碼短信: ${code}`);sms_util.sendCode(phone, code, function (success) {//success表示是否成功if (success) {users[phone] = codeconsole.log(users[phone])console.log('保存驗證碼: ', phone, code)res.send({"code": 0})} else {//3. 返回響應數據res.send({"code": 1, msg: '短信驗證碼發送失敗'})}}) })/* 短信登陸 */ router.post('/login_sms', function (req, res, next) {var phone = req.body.phone;var code = req.body.code;console.log('/login_sms', phone, code);if (users[code] != code) {res.send({code: 1, msg: '手機號或驗證碼不正確'});return;}//刪除保存的codedelete users[phone];UserModel.findOne({phone}, function (err, user) {if (user) {req.session.userid = user._idres.send({code: 0, data: user})} else {//存儲數據const userModel = new UserModel({phone})userModel.save(function (err, user) {req.session.userid = user._idres.send({code: 0, data: user})})}})})?
3、前臺實現
? ? (1)輸入正確的手機號之后,點擊“獲取驗證碼”,會異步調用getCode()方法
a:頁面行為
? ? ?
b 調用getCode方法獲取驗證碼
?
c在methods中定義獲取驗證碼函數
// 異步獲取短信驗證碼 async getCode () {// 如果當前沒有計時if(!this.computeTime) {// 啟動倒計時this.computeTime = 60this.intervalId = setInterval(() => {this.computeTime--if(this.computeTime<=0) {// 停止計時clearInterval(this.intervalId)}}, 1000)// 發送ajax請求(向指定手機號發送驗證碼短信)const result = await reqSendCode(this.phone)if(result.code===1) {// 顯示提示this.showAlert(result.msg)// 停止計時if(this.computeTime) {this.computeTime = 0clearInterval(this.intervalId)this.intervalId = undefined}}}},d.異步請求時會調用自己封裝的請求函數
//6.發送短信驗證碼 export const reqSendCode = (phone)=>ajax('/api/sendcode', {phone}) import axios from 'axios' export default function ajax(url = '', data = {}, type = 'GET') {return new Promise(function (resolve, reject) {let promiseif (type === 'GET') {// 準備url query 參數數據let dataStr = '' //數據拼接字符串Object.keys(data).forEach(key => {dataStr += key + '=' + data[key] + '&'})if (dataStr !== '') {dataStr = dataStr.substring(0, dataStr.lastIndexOf('&'))url = url + '?' + dataStr}// 發送get 請求promise = axios.get(url)} else {// 發送post 請求promise = axios.post(url, data)}promise.then(response => {resolve(response.data)}).catch(error => {reject(error)})}) } ajax函數封裝?
e.后臺接受到get請求,會執行后臺index.js中的get請求方式,先生成驗證碼,并且提示到手機上
router.get('/sendcode', function (req, res, next) {//1. 獲取請求參數數據var phone = req.query.phone;//2. 處理數據//生成驗證碼(6位隨機數)var code = sms_util.randomCode(6);//發送給指定的手機號 console.log(`向${phone}發送驗證碼短信: ${code}`);sms_util.sendCode(phone, code, function (success) {//success表示是否成功if (success) {users[phone] = codeconsole.log(users[phone])console.log('保存驗證碼: ', phone, code)res.send({"code": 0})} else {//3. 返回響應數據res.send({"code": 1, msg: '短信驗證碼發送失敗'})}}) })f.用戶收到驗證碼,并且輸入點擊“登錄”按鈕,提交表單,再次對服務器發起post請求,服務器在驗證
router.post('/login_sms', function (req, res, next) {var phone = req.body.phone;var code = req.body.code;console.log('/login_sms', phone, code);if (users[code] != code) {res.send({code: 1, msg: '手機號或驗證碼不正確'});return;}//刪除保存的codedelete users[phone];UserModel.findOne({phone}, function (err, user) {if (user) {req.session.userid = user._idres.send({code: 0, data: user})} else {//存儲數據const userModel = new UserModel({phone})userModel.save(function (err, user) {req.session.userid = user._idres.send({code: 0, data: user})})}})})g.前臺接受到后臺的響應數據之后,還需要進行以下操作
1)將電話號碼保存到vuex的state中去
2)進行路由跳轉
if(this.loginWay) { // 短信登陸const {rightPhone, phone, code} = thisif(!this.rightPhone) {// 手機號不正確this.showAlert('手機號不正確')return} else if(!/^\d{6}$/.test(code)) {// 驗證必須是6位數字this.showAlert('驗證必須是6位數字')return}// 發送ajax請求短信登陸result = await reqSmsLogin(phone, code)}// 停止計時if(this.computeTime) {this.computeTime = 0clearInterval(this.intervalId)this.intervalId = undefined}// 根據結果數據處理if(result.code===0) {const user = result.datacosole.log(user)// 將user保存到vuex的statethis.$store.dispatch('recordUser', user)// 去個人中心界面this.$router.replace('/profile')} else {// 顯示新的圖片驗證碼this.getCaptcha()// 顯示警告提示const msg = result.msgthis.showAlert(msg)}}?
?
?
?
三、總結? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
轉載于:https://www.cnblogs.com/xxm980617/p/10822624.html
總結
以上是生活随笔為你收集整理的Vue_注册登录(短信验证码登录)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python-语言播报
- 下一篇: vue路由切换和用location切换u