code_copy.vue
3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<template>
<view class="code-login">
<view class="code-title" style="margin-bottom: 30px;">
<view class="code-title-text" style="font-size: 23px;color: #202328">输入4位验证码</view>
<view class="code-title-tips" style="display: flex; flex-direction: row; justify-content: space-between; font-size: 12px;font-weight: 400;color: rgba(109,119,143,0.82);">
<view>验证码已发送</view>
<view>
<view v-if="waitTime">重新发送({{waitTime}}s)</view>
<uv-button v-else type="primary" size="mini" text="重新发送" @click="handleRetry"></uv-button>
</view>
</view>
</view>
<view class="forget-form">
<uv-form
labelPosition="left"
ref="formRef"
labelWidth="0"
:borderBottom="false"
>
<uv-form-item label="">
<view class="code-list" style="display: flex;flex-direction: row;">
<!-- num是从1开始 -->
<uv-input
v-for="(num, index) in formNum"
:key="num"
ref="inputRef"
:value="formModel[index]"
type="number"
maxlength="1"
color="#202328"
inputAlign="center"
fontSize="22px"
border="bottom"
class="code-input"
@input="(value) => handleInput(value, index)"
></uv-input>
</view>
</uv-form-item>
</uv-form>
</view>
</view>
</template>
<script setup>
import {
ref,
} from 'vue';
import {
onLoad
} from '@dcloudio/uni-app';
import { doLoginCheckCodeApi } from '@/api';
const formNum = ref(4)
const formModel = ref(['', '', '', ''])
const loginName = ref('')
const contactWay = ref('')
// const formModel = ref(['1', '1', '2', '3'])
const inputRef = ref(null)
onLoad((options) => {
console.log('options', options)
loginName.value = options.loginName || ''
contactWay.value = options.contactWay || ''
})
const handleInput = (value, index) => {
console.log('handleInput', value, index)
if (parseInt(value) || parseInt(value) === 0) {
formModel.value[index] = value
// 跳到下一个input
// TODO: 此功能,浏览器无法验证
console.log('inputElList', inputRef)
const targetIndex = index + 1
if (targetIndex < formNum.value ) {
const targetItem = inputRef.value[targetIndex]
console.log('targetItem', targetItem)
if (targetItem.value) {
targetItem.value.$el.focus()
}
}
// 判断是否已有4个有效值
const isValid = formModel.value.every(item => {
return (parseInt(item) || parseInt(item) === 0)
})
console.log('handleInput-isValid', isValid)
if (isValid) {
// TODO: 进行登录验证
const params = {
loginName: loginName.value,
code: formModel.value.join(''),
}
doLoginCheckCodeApi(params).then((res) => {
console.log('code', res)
const checkCode = res.data || ''
// 成功
uni.navigateTo({
url: `/subPackages/accountGroup/pages/login/forget/reset?loginName=${params.loginName}&code=${checkCode}`,
})
})
}
}
console.log('handleInput-formModel', formModel.value)
}
// 发送验证码
const waitTime = ref(60)
// const waitTime = ref(10)
const startTime = () => {
const timer = setInterval(() => {
if (waitTime.value) {
waitTime.value -= 1
} else {
// 结束计时
clearInterval(timer)
}
}, 1000)
}
const handleRetry = () => {
const params = {
loginName: loginName.value
}
if (contactWay.value.includes('@')) {
// 邮箱
params.email = contactWay.value
} else {
// 手机号
params.phone = contactWay.value
}
doLoginGetCodeApi(params).then(() => {
uni.showToast({icon: 'success', title: '发送成功'})
}).catch(() => {
uni.showToast({icon: 'error', title: '发送失败'})
})
}
startTime()
</script>
<style scoped>
.code-login {
padding: 40px 25px 10px 25px;
}
.code-input {
flex: 1;
margin: 0px 10px;
border-color: #6D778F !important
}
</style>