wx-starlock/pages/user/updateEmail.vue

230 lines
5.4 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<view>
<view class="tips">找回密码和登录新设备时可通过绑定的邮箱验证</view>
<input
class="input-email"
:value="email"
placeholder="请输入邮箱"
placeholder-class="input-placeholder"
:focus="true"
@input="updateInputEmail"
/>
<view class="view-top">
<input
type="number"
class="input-verify"
:value="verificationCode"
maxlength="6"
placeholder="请输入验证码"
placeholder-class="input-placeholder"
@input="updateInputCode"
/>
<view class="button-verify" @click="getEmailCode">{{ text }}</view>
</view>
<view class="button" @click="toUpdateEmail">确定</view>
</view>
</template>
<script>
import { mapActions, mapState } from 'pinia'
import { test } from 'uview-plus'
import { useUserStore } from '@/stores/user'
import { getEmailCodeRequest, updateEmailRequest } from '@/api/user'
import { useBasicStore } from '@/stores/basic'
export default {
data() {
return {
text: '获取验证码',
verificationCode: '',
token: '',
email: '',
pending: false
}
},
computed: {
...mapState(useUserStore, ['userInfo'])
},
onLoad(option) {
if (option.token) {
this.token = option.token
} else {
uni.setNavigationBarTitle({
title: '绑定邮箱'
})
}
},
methods: {
...mapActions(useUserStore, ['updateUserInfo']),
...mapActions(useBasicStore, ['routeJump', 'backAndToast', 'getNetworkType']),
updateInputEmail(data) {
this.email = data.detail.value
},
updateInputCode(data) {
this.verificationCode = data.detail.value
},
async getEmailCode() {
if (this.text !== '获取验证码') {
return
}
if (!test.email(this.email)) {
uni.showToast({
title: '请输入正确的邮箱',
icon: 'none'
})
return
}
const netWork = await this.getNetworkType()
if (!netWork) {
return
}
const { code } = await getEmailCodeRequest({
account: this.email,
channel: '2',
codeType: 6
})
if (code === 0) {
this.updateTime()
} else {
uni.showToast({
title: '验证码获取失败',
icon: 'none'
})
}
},
async toUpdateEmail() {
if (!test.email(this.email)) {
uni.showToast({
title: '请输入正确的邮箱',
icon: 'none'
})
return
}
if (this.verificationCode.length === 6 && test.digits(this.verificationCode)) {
const netWork = await this.getNetworkType()
if (!netWork) {
return
}
if (this.pending) {
return
}
this.pending = true
const params = {
verificationCode: this.verificationCode,
email: this.email
}
if (this.token !== '') {
params.unbindToken = this.token
}
const { code, message } = await updateEmailRequest(params)
if (code === 0) {
this.updateUserInfo({
...this.userInfo,
email: this.email
})
this.backAndToast('邮箱绑定成功')
} else {
uni.showToast({
title: message,
icon: 'none'
})
}
this.pending = false
} else {
uni.showToast({
title: '验证码为6位纯数字',
icon: 'none'
})
}
},
updateTime() {
let time = 120
this.text = `${time} s`
const now = new Date().getTime()
const timer = setInterval(() => {
const second = parseInt((new Date().getTime() - now) / 1000, 10)
this.text = `${time - second} s`
if (time <= second) {
clearInterval(timer)
this.text = '获取验证码'
}
}, 1000)
}
}
}
</script>
<style lang="scss">
page {
background-color: $uni-bg-color-grey;
}
</style>
<style scoped lang="scss">
.tips {
padding: 24rpx 32rpx 0 32rpx;
font-size: 28rpx;
color: #999999;
}
.input-email {
width: 616rpx;
height: 108rpx;
padding-right: 32rpx;
padding-left: 32rpx;
margin-top: 48rpx;
margin-left: 35rpx;
background: #ffffff;
border-radius: 16rpx;
}
.button-verify {
width: 265rpx;
height: 108rpx;
margin-top: 48rpx;
margin-left: 35rpx;
font-size: 32rpx;
line-height: 108rpx;
color: #ffffff;
text-align: center;
background: #63b8af;
border-radius: 16rpx;
}
.view-top {
display: flex;
align-items: center;
}
.input-verify {
width: 316rpx;
height: 108rpx;
padding-right: 32rpx;
padding-left: 32rpx;
margin-top: 48rpx;
margin-left: 35rpx;
background: #ffffff;
border-radius: 16rpx;
}
.input-placeholder {
height: 108rpx;
font-size: 36rpx;
font-weight: bold;
line-height: 108rpx;
}
.button {
width: 680rpx;
height: 96rpx;
margin-top: 32rpx;
margin-left: 35rpx;
font-size: 32rpx;
line-height: 96rpx;
color: #ffffff;
text-align: center;
background: #63b8af;
border-radius: 16rpx;
}
</style>