index.vue
8.95 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<template>
<view class="register-page">
<!-- 头像区域 -->
<view class="avatar-section">
<view class="avatar-container">
<image
:src="formData.avatar || defaultAvatar"
class="avatar-image"
mode="aspectFill"
@click="previewAvatar"
/>
</view>
<view class="change-avatar-btn" @click="changeAvatar">
<text class="change-avatar-text">上传头像</text>
</view>
</view>
<!-- 表单内容 -->
<nut-form ref="formRef" :model-value="formData">
<view class="form-container">
<!-- 昵称 -->
<nut-form-item label="昵称" prop="nickname" required :rules="[{ required: true, message: '请输入昵称' }]">
<nut-input
v-model="formData.nickname"
placeholder="请输入昵称"
input-align="right"
clearable
/>
</nut-form-item>
<!-- 手机号 -->
<nut-form-item label="手机号" prop="phone" required :rules="phoneRules">
<view class="phone-input-container">
<nut-input
v-model="formData.phone"
placeholder="请输入手机号"
type="tel"
maxlength="11"
input-align="right"
clearable
class="phone-input"
/>
<nut-button
size="small"
:disabled="codeCountdown > 0 || !isPhoneValid"
@click="sendCode"
class="code-btn"
>
{{ codeCountdown > 0 ? `${codeCountdown}s` : '获取验证码' }}
</nut-button>
</view>
</nut-form-item>
<!-- 验证码 -->
<nut-form-item label="验证码" prop="verifyCode" required :rules="[{ required: true, message: '请输入验证码' }]">
<nut-input
v-model="formData.verifyCode"
placeholder="请输入验证码"
type="number"
maxlength="6"
input-align="right"
clearable
/>
</nut-form-item>
<!-- 性别 -->
<nut-form-item label="性别" prop="gender" body-align="right" required :rules="[{ required: true, message: '请选择性别' }]">
<nut-radio-group v-model="formData.gender" direction="horizontal">
<nut-radio label="男">男</nut-radio>
<nut-radio label="女">女</nut-radio>
</nut-radio-group>
</nut-form-item>
<!-- 生日 -->
<nut-form-item label="生日" prop="birthday" label-position="top">
<view class="birthday-item" @click="showDatePicker">
<text class="birthday-value">{{ formData.birthday || '请选择生日' }}</text>
<Right class="arrow-icon" />
</view>
</nut-form-item>
<!-- 所在学校 -->
<nut-form-item label="所在学校" prop="school" required :rules="[{ required: true, message: '请选择学校' }]" label-position="top">
<view class="school-item" @click="showSchoolPicker">
<text class="school-value">{{ formData.school || '请选择学校' }}</text>
<Right class="arrow-icon" />
</view>
</nut-form-item>
</view>
</nut-form>
<!-- 注册按钮 -->
<view class="register-section">
<nut-button
color="#f97316"
size="large"
block
@click="handleRegister"
:loading="isRegistering"
>
{{ isRegistering ? '保存中...' : '保存' }}
</nut-button>
</view>
<!-- 日期选择器 -->
<nut-popup v-model:visible="datePickerVisible" position="bottom">
<nut-date-picker
v-model="dateValue"
title="选择生日"
@confirm="onDateConfirm"
@cancel="datePickerVisible = false"
/>
</nut-popup>
<!-- 学校选择器 -->
<nut-popup v-model:visible="schoolPickerVisible" position="bottom">
<nut-picker
v-model="schoolValue"
:columns="schoolOptions"
title="选择学校"
@confirm="onSchoolConfirm"
@cancel="schoolPickerVisible = false"
/>
</nut-popup>
<!-- 头像预览 -->
<nut-image-preview
v-model:show="avatarPreviewVisible"
:images="[formData.avatar || defaultAvatar]"
/>
<!-- 成功提示 -->
<nut-toast
v-model:visible="toastVisible"
:msg="toastMessage"
:type="toastType"
/>
</view>
</template>
<script setup>
import { ref, reactive, computed, onMounted } from 'vue'
import Taro from '@tarojs/taro'
import { RectLeft, Right } from '@nutui/icons-vue-taro'
import './index.less'
// 主题配置
const themeVars = {
navbarBackground: '#f97316',
navbarColor: '#fff'
}
// 默认头像
const defaultAvatar = 'https://randomuser.me/api/portraits/men/32.jpg'
// 表单数据
const formData = reactive({
avatar: '',
nickname: '',
phone: '',
verifyCode: '',
gender: '',
birthday: '',
school: ''
})
// 弹框控制
const datePickerVisible = ref(false)
const schoolPickerVisible = ref(false)
const avatarPreviewVisible = ref(false)
const toastVisible = ref(false)
const toastMessage = ref('')
const toastType = ref('success')
// 验证码相关
const codeCountdown = ref(0)
const isRegistering = ref(false)
// 日期选择
const dateValue = ref(new Date())
// 学校选择
const schoolValue = ref([])
const schoolOptions = ref([
[
{ text: '上海理工大学', value: '上海理工大学' },
{ text: '上海大学', value: '上海大学' },
{ text: '华东理工大学', value: '华东理工大学' },
{ text: '上海交通大学', value: '上海交通大学' },
{ text: '复旦大学', value: '复旦大学' },
{ text: '同济大学', value: '同济大学' },
{ text: '华东师范大学', value: '华东师范大学' },
{ text: '上海财经大学', value: '上海财经大学' }
]
])
// 手机号验证规则
const phoneRules = [
{ required: true, message: '请输入手机号' },
{
pattern: /^1[3-9]\d{9}$/,
message: '请输入正确的手机号格式'
}
]
// 计算属性:手机号是否有效
const isPhoneValid = computed(() => {
return /^1[3-9]\d{9}$/.test(formData.phone)
})
/**
* 返回上一页
*/
const goBack = () => {
Taro.navigateBack()
}
/**
* 更换头像
*/
const changeAvatar = () => {
Taro.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: (res) => {
formData.avatar = res.tempFilePaths[0]
}
})
}
/**
* 预览头像
*/
const previewAvatar = () => {
avatarPreviewVisible.value = true
}
/**
* 发送验证码
*/
const sendCode = () => {
if (!isPhoneValid.value) {
showToast('请输入正确的手机号', 'error')
return
}
// 模拟发送验证码
codeCountdown.value = 60
const timer = setInterval(() => {
codeCountdown.value--
if (codeCountdown.value <= 0) {
clearInterval(timer)
}
}, 1000)
showToast('验证码已发送', 'success')
}
/**
* 显示日期选择器
*/
const showDatePicker = () => {
datePickerVisible.value = true
}
/**
* 确认日期选择
*/
const onDateConfirm = ({ selectedValue }) => {
formData.birthday = `${selectedValue[0]}-${String(selectedValue[1]).padStart(2, '0')}-${String(selectedValue[2]).padStart(2, '0')}`
datePickerVisible.value = false
}
/**
* 显示学校选择器
*/
const showSchoolPicker = () => {
schoolPickerVisible.value = true
}
/**
* 确认学校选择
*/
const onSchoolConfirm = ({ selectedOptions }) => {
formData.school = selectedOptions[0].text
schoolPickerVisible.value = false
}
/**
* 显示提示信息
*/
const showToast = (message, type = 'success') => {
toastMessage.value = message
toastType.value = type
toastVisible.value = true
}
/**
* 表单验证
*/
const validateForm = () => {
if (!formData.nickname.trim()) {
showToast('请输入昵称', 'error')
return false
}
if (!isPhoneValid.value) {
showToast('请输入正确的手机号', 'error')
return false
}
if (!formData.verifyCode.trim()) {
showToast('请输入验证码', 'error')
return false
}
if (!formData.gender) {
showToast('请选择性别', 'error')
return false
}
if (!formData.school) {
showToast('请选择学校', 'error')
return false
}
return true
}
/**
* 处理注册
*/
const handleRegister = async () => {
if (!validateForm()) {
return
}
isRegistering.value = true
try {
// 模拟注册API调用
await new Promise(resolve => setTimeout(resolve, 2000))
// TODO: 这里应该调用实际的注册API
// const result = await registerAPI(formData)
showToast('注册成功', 'success')
setTimeout(() => {
// 注册成功后跳转到登录页面或首页
Taro.navigateBack()
}, 1500)
} catch (error) {
showToast('注册失败,请重试', 'error')
} finally {
isRegistering.value = false
}
}
// 初始化
onMounted(() => {
// 可以在这里进行初始化操作
})
</script>
<script>
export default {
name: 'RegisterPage'
}
</script>