一.短信验证功能
1.短信验证倒计时基础效果:
实现点击获取验证码开启倒计时,60秒后恢复获取验证码
javascript
<button @click="getCode">
{{ second === totalSecond ? '获取验证码' : second + '秒后重新发送' }}
</button>
<script>
export default {
data () {
return {
totalSecond: 60, // 短信验证码倒计时总秒数
second: 60, // 当前倒计时秒数
timer: null // 倒计时定时器id
}
},
methods: {
// 获取短信验证码
getCode () {
if (!this.timer && this.second === this.totalSecond) {
this.timer = setInterval(() => {
this.second--
if (this.second <= 0) {
clearInterval(this.timer)
this.timer = null
this.second = this.totalSecond
}
}, 1000)
}
}
},
destroyed () {
clearInterval(this.timer) // 离开时清除定时器
}
}
</script>
2.倒计时之前校验处理手机号、验证码:
javascript
<div class="form-item">
<input v-model="mobile" class="inp" maxlength="11" placeholder="请输入手机号码" type="text">
</div>
<div class="form-item">
<input v-model="picCode" class="inp" maxlength="5" placeholder="请输入图形验证码" type="text">
<img v-if="picUrl" :src="picUrl" @click="getPicCode" alt="">
</div>
<script>
export default {
data () {
return {
mobile: '', // 手机号
picCode: '', // 用户输入的图形验证码
}
},
methods: {
// 校验手机号和图形验证码是否合法,通过则返回true,否则返回false
checkForm () {
if (!/^1[3-9]\d{9}$/.test(this.mobile)) {
this.$toast('请输入正确的手机号')
return false
}
if (!/^\w{4}$/.test(this.picCode)) {
this.$toast('请输入正确的图形验证码')
return false
}
return true
},
// 获取短信验证码
getCode () {
if (!this.checkForm()) return
if (!this.timer && this.second === this.totalSecond) {
this.timer = setInterval(() => {
this.second--
if (this.second <= 0) {
clearInterval(this.timer)
this.timer = null
this.second = this.totalSecond
}
}, 1000)
}
}
}
}
</script>
3.封装短信验证请求接口,发送请求添加提示:

在login.js中封装获取短信验证码接口,接口请求和传递参数按照接口文档来:
javascript
// 获取短信验证码
export const getMsgCode = (captchaCode, captchaKey, mobile) => {
return request.post('/captcha/sendSmsCaptcha', {
form: {
captchaCode,
captchaKey,
mobile
}
})
}
在页面中发送请求:
javascript
<script>
import { getMsgCode } from '@/api/login'
export default {
methods: {
// 获取短信验证码
async getCode () {
if (!this.checkForm()) return
if (!this.timer && this.second === this.totalSecond) {
// 发送请求
await getMsgCode(this.picCode, this.picKey, this.mobile)
this.$toast('短信验证码发送成功')
this.timer = setInterval(() => {
this.second--
if (this.second <= 0) {
clearInterval(this.timer)
this.timer = null
this.second = this.totalSecond
}
}, 1000)
}
}
}
}
</script>
二.登录功能
1.阅读接口文档,封装登录接口
2.登录前的校验(手机号,图形验证码,短信验证码)
3.调用方法发送请求,成功添加提示并跳转

在login.js中封装登录接口:
javascript
// 登录接口
export const codeLogin = (mobile, smsCode) => {
return request.post('/passport/login', {
form: {
isParty: false,
partyData: {},
mobile,
smsCode
}
})
}
在页面中注册点击事件,绑定短信验证码,发送登录请求进行登录:
javascript
<div class="form-item">
<input v-model="smsCode" class="inp" placeholder="请输入短信验证码" type="text">
<button @click="getCode">
{{ second === totalSecond ? '获取验证码' : second + '秒后重新发送' }}
</button>
</div>
<div class="login-btn" @click="login">登录</div>
<script>
import { codeLogin } from '@/api/login'
export default {
data () {
return {
mobile: '', // 手机号
smsCode: '', // 短信验证码
}
},
async created () {
this.getPicCode()
},
methods: {
// 登录
async login () {
if (!this.checkForm()) return
if (!/^\d{6}$/.test(this.smsCode)) {
this.$toast('请输入正确的手机验证码')
}
const res = await codeLogin(this.mobile, this.smsCode)
console.log(res)
}
}
}
</script>

三.统一处理错误(响应拦截器)
每次请求,都会有可能会错误,就都需要错误提示
响应拦截器是咱们拿到数据的第一个数据流转站,可以在里面统一处理错误
javascript
import { Toast } from 'vant' // 这里不是在组件内部,需要进行引入
// 添加响应拦截器
instance.interceptors.response.use(function (response) {
const res = response.data // 默认axios会多包装一层data,需要响应拦截器处理一下
// 处理非成功响应
if (res.status !== 200) {
Toast(res.message)
return Promise.reject(res.message) // 抛出一个错误的promise
}
// 处理成功响应
return res
}, function (error) {
return Promise.reject(error)
})
四.将token权证信息存入vuex

token存入vuex的好处,易获取,响应式
Vuex需要分模块,user模块
1.创建user模块
在store 文件夹下新建一个文件夹modules ,在里面新建文件user.js
user.js中的代码:
javascript
export default {
namespaced: true,
state () {
return {
// 个人信息相关
userInfo: {
token: '',
userId: ''
}
}
},
mutations: {},
actions: {},
getters: {}
}
2.将user.js文件挂载到vuex

3.在user.js中提供mutations
javascript
export default {
namespaced: true,
state () {
return {
// 个人信息相关
userInfo: {
token: '',
userId: ''
}
}
},
mutations: {
// mutations第一个参数都是state
setUserInfo (state, obj) {
state.userInfo = obj
}
},
actions: {},
getters: {}
}
4.在登录页面中commit调用

五.vuex持久化处理
封装storage存储模块,利用本地存储,进行vuex持久化处理
在utils 文件夹下新建storage.js文件,里面存放的代码:
javascript
// 约定一个通用的键名
const INFO_KEY = 'vue2_shopping_info'
// 获取个人信息
export const userInfo = () => {
const defaultObj = { token: '', userId: '' }
const result = localStorage.getItem(INFO_KEY)
return result ? JSON.parse(result) : defaultObj
}
// 设置个人信息
export const setInfo = (info) => {
localStorage.setItem(INFO_KEY, JSON.stringify(info))
}
// 移除个人信息
export const removeInfo = () => {
localStorage.removeItem(INFO_KEY)
在user.js中引入并使用:

