verifyUser.vue
5.86 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<template>
<div class="verify-user-page">
<p class="title" style="">请录入真实的信息</p>
<div class="input-content">
<van-config-provider :theme-vars="themeVars">
<van-form ref="form" @submit="onSubmit">
<div class="van-hairline--bottom">
<van-field v-model="user_name" name="user_name" label="家长姓名" placeholder="请输入你真实的姓名"
:rules="[{ required: true, message: '请填写家长姓名' }]" />
</div>
<div class="van-hairline--bottom">
<van-field v-if="use_widget" v-model="phone" name="phone" label="手机号" placeholder="手机号" readonly clickable
:rules="[{ validator, message: '请输入正确手机号' }]"
@touchstart.stop="showKeyboard" />
<van-field v-else v-model="phone" name="phone" label="手机号" placeholder="手机号"
:rules="[{ validator, message: '请输入正确手机号' }]" />
</div>
<div class="van-hairline--bottom">
<van-field v-model="pin" center clearable name="pin" type="digit" label="短信验证码" placeholder="请输入短信验证码"
:formatter="formatter" :rules="[{ required: true, message: '请填写验证码' }]">
<template #button>
<van-button @click="sendCode" size="small" type="primary" :disabled="disabled">
<span v-if="countDown === 60">发送验证码</span>
<span v-else>{{ countDown }} 秒重新发送</span>
</van-button>
</template>
</van-field>
</div>
</van-form>
</van-config-provider>
</div>
</div>
<div style="padding: 2rem;">
<my-button @on-click="submit" type="primary">保存</my-button>
</div>
<van-number-keyboard v-model="phone" :show="keyboard_show" :maxlength="11" @blur="onBlur" />
</template>
<script setup>
import MyButton from '@/components/MyButton/index.vue'
import { wxInfo } from '@/utils/tools';
import { ref, onMounted, nextTick } from 'vue'
import { useRouter } from 'vue-router'
import axios from '@/utils/axios';
import { Toast } from 'vant';
import { styleColor } from '@/constant.js';
const $router = useRouter();
const themeVars = {
buttonPrimaryBackground: styleColor.baseColor,
buttonPrimaryBorderColor: styleColor.baseColor,
buttonPrimaryColor: styleColor.baseFontColor,
};
/******************** 输入框控件 START ********************/
/**
* 判断微信环境看是否弹出控件框
* 桌面微信直接输入
* 其他环境弹出输入框
*/
let use_widget = ref(true);
if (wxInfo().isiOS || wxInfo().isAndroid) {
use_widget.value = true;
} else {
use_widget.value = false;
}
// 手机号输入控件
const keyboard_show = ref(false);
const showKeyboard = () => { // 弹出数字弹框
keyboard_show.value = true;
};
const onBlur = () => { // 数字键盘失焦回调
keyboard_show.value = false;
if (phone.value.length === 11) { // 手机号码位数正确时可以发送验证码
disabled.value = false;
}
};
/******************** 发送验证码模块 START ********************/
// TODO:发送验证码接口待调,验证码写默认8888
const countDown = ref(60);
const countDownHandle = () => {
// 倒计时
if (countDown.value === 0) {
countDown.value = 60;
} else {
countDown.value--;
setTimeout(() => {
countDownHandle()
}, 1000)
}
}
const sendCode = () => { // 发送验证码
if (countDown.value === 60) {
axios.post('/srv/?a=bind_phone&t=get_code', {
phone: phone.value
})
.then(res => {
if (res.data.code === 1) {
Toast.success('发送成功');
countDownHandle()
} else {
console.warn(res.data);
Toast({
message: res.data.msg,
icon: 'close',
});
}
})
.catch(err => {
console.error(err);
})
}
};
/******************** 表单业务逻辑 START ********************/
const user_name = ref('')
const phone = ref('');
const pin = ref('');
const disabled = ref(true);
// 表单填写验证
const form = ref(null);
let valid = null;
let submit = () => {
valid = form.value.validate();
valid
.then(() => {
form.value.submit();
})
.catch(error => {
console.error(error);
Toast({
message: '请检查后再次提交',
icon: 'cross',
});
})
};
/**
* 手机号码校验
* 函数返回 true 表示校验通过,false 表示不通过
* @param {*} val
*/
const validator = (val) => {
let flag = false;
// 简单判断手机号位数
if (/1\d{10}/.test(val) && phone.value.length === 11) {
disabled.value = false;
flag = true;
} else {
disabled.value = true;
flag = false;
}
return flag
};
// 过滤输入的验证码 只能四位
const formatter = (value) => value.substring(0, 4);
// 保存用户信息
const onSubmit = (values) => {
console.warn(values);
axios.post('/srv/?a=c_auth', {
user_name: values.user_name,
phone: values.phone,
pin: values.pin,
})
.then(res => {
if (res.data.code === 1) {
Toast.success('录入成功')
$router.go(-1);
} else {
console.warn(res.data);
if (!res.data.show) return false;
Toast({
message: res.data.msg,
icon: 'close',
});
}
})
.catch(err => {
console.error(err);
})
};
/****************************************/
</script>
<script>
import mixin from 'common/mixin';
export default {
mixins: [mixin.init],
data() {
return {
}
},
mounted() {
},
methods: {
}
}
</script>
<style lang="less" scoped>
.verify-user-page {
padding: 1rem;
.title {
color: @base-color;
font-size: 1.25rem;
padding: 1rem;
text-align: center;
font-weight: bold;
}
.input-content {
padding: 1rem;
}
}
</style>