wangzhibo
2025-10-23 cad64c1de2c45856b0774933c5801b05b64984e8
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<template>
    <view class="page-wrapper">
        <div class="bg-top"></div>
 
        <div class="content-wrapper">
            <view class="center h-32">
                <image :src="selectedavatarUrl" class="w-16 h-16 rounded-full" />
                <button type="balanced" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">选择头像</button>
            </view>
            <!-- 性别选择 -->
            <view class="mb-4">
                <text class="font-bold mb-4">性别</text>
                <view class="h-2"></view>
                <radio-group @change="onSexChange" :value="sex" class="mt-10">
                    <label class="mr-4">
                        <radio value="male" :checked="sex === 'male'" /> 男
                    </label>
                    <label>
                        <radio value="female" :checked="sex === 'female'" /> 女
                    </label>
                </radio-group>
            </view>
 
            <!-- 姓名 -->
            <view class="flex items-center space-x-2">
                <label class="w-30 text-sm font-medium font-bold"><text class="font-bold">姓名/昵称:</text></label>
                <input class="flex-1 border border-gray-300 rounded p-2 h-8 w-full" type="nickname"
                    style="font-size:42rpx;" :value="username" @input="userNameInput" @blur="userNameInput" />
            </view>
            <view class="h-8"></view>
 
            <!-- 手机号码 -->
            <view class="flex items-center space-x-2">
                <label class="w-30 text-sm font-medium font-bold"><text class="font-bold">手机号码:</text></label>
                <input v-model="phone" placeholder="请输入手机号码" type="number" maxlength="11"
                    class="flex-1 border border-gray-300 rounded p-2 h-8 w-full" />
            </view>
 
            <!-- 保存按钮 -->
            <button class="bg-blue-500 text-white p-2 rounded w-full mt-100 w-12" @click="saveUser">
                保存
            </button>
        </div>
        <div class="bg-bottom"></div>
    </view>
</template>
 
<script setup>
    import {
        ref,
        onMounted
    } from 'vue'
    import {
        useUserStore
    } from '@/stores/user'
 
    import {
        apiService
    } from '@/apiservice/apiService'
 
    // store
    const userStore = useUserStore()
 
    // 表单数据
    const selectedavatarUrl = ref(0)
    const sex = ref('male')
    const username = ref('')
    const phone = ref('')
 
 
 
    //获取微信昵称
    function userNameInput(e) {
        console.log(e.detail)
        username.value = e.detail.value
    }
    //获取微信图像
    function onChooseAvatar(e) {
        console.log(e.detail)
        selectedavatarUrl.value = e.detail.avatarUrl
    }
 
 
 
    // 性别切换
    function onSexChange(e) {
        sex.value = e.detail.value
    }
 
    // 校验规则
    function validateForm() {
        if (!username.value.trim()) {
            uni.showToast({
                title: '姓名不能为空',
                icon: 'none'
            })
            return false
        }
        if (username.value.length > 10) {
            uni.showToast({
                title: '姓名不能超过10个字',
                icon: 'none'
            })
            return false
        }
        if (phone.value) {
            const phoneReg = /^1\d{10}$/
            if (!phoneReg.test(phone.value)) {
                uni.showToast({
                    title: '手机号格式不正确',
                    icon: 'none'
                })
                return false
            }
        }
        return true
    }
 
    // 保存
    async function saveUser() {
        if (!validateForm()) return
 
        try {
            userStore.saveUserInfo({
                username: username.value,
                avatarUrl: selectedavatarUrl.value,
                sex: sex.value,
                phone: phone.value
            });
 
            if (userStore.id) {
                apiService.touristUpdate({
                        id: userStore.id,
                        username: userStore.username,
                        avatarurl: userStore.avatarUrl,
                        sex: userStore.sex,
                        phone: userStore.phone
                    })
                    .then((res) => {
                        if (res.code === 1000) {
                            console.log('游客更新成功:');
                            uni.showToast({
                                title: '保存成功',
                                icon: 'success',
                                duration: 1500,
                                success: () => {
                                    // toast 完成后跳转
                                    setTimeout(() => {
                                        uni.switchTab({
                                            url: '/pages/index/index'
                                        });
                                    }, 1500);
                                }
                            });
                        } else {
                            console.warn('游客更新失败:', res.message);
                            uni.showToast({
                                title: '保存失败',
                                icon: 'none',
                                duration: 1500
                            });
                        }
                    })
                    .catch((err) => {
                        console.error('游客更新错误:', err);
                        uni.showToast({
                            title: '网络错误',
                            icon: 'none',
                            duration: 1500
                        });
                    });
            }else{
                uni.showToast({
                    title: '网络错误',
                    icon: 'none',
                    duration: 1500
                });
            }
 
        } catch (err) {
            console.error(err);
            uni.showToast({
                title: '保存失败',
                icon: 'none'
            });
        }
    }
 
    // 初始化
    onMounted(() => {
        // 读取已有数据
        if (userStore.username) {
            username.value = userStore.username
        }
        if (userStore.phone) {
            phone.value = userStore.phone
        }
        if (userStore.sex) {
            sex.value = userStore.sex
        }
        if (userStore.avatarUrl) {
            selectedavatarUrl.value = userStore.avatarUrl
        }
    })
</script>
<style>
    /* 居中容器 */
    .center {
        display: flex;
        flex-direction: column;
        align-items: center;
    }
 
    /* 外层容器 padding */
    .p-4 {
        padding: 1rem;
    }
 
    /* 下边距 */
    .mb-4 {
        margin-bottom: 1rem;
    }
 
    /* 粗体文本 */
    .font-bold {
        font-weight: bold;
    }
 
    /* flex 布局 */
    .flex {
        display: flex;
    }
 
    .flex-wrap {
        flex-wrap: wrap;
    }
 
    .items-center {
        align-items: center;
    }
 
    .justify-center {
        justify-content: center;
    }
 
    /* 宽度控制 */
    .w-12 {
        width: 3rem;
    }
 
    .h-12 {
        height: 3rem;
    }
 
    .h-2 {
        height: 0.5rem;
    }
 
    .w-24 {
        width: 6rem;
    }
 
    .w-30 {
        width: 7rem;
    }
 
    .mt-10 {
        margin-top: 10rpx;
    }
 
    .mt-100 {
        margin-top: 100rpx;
    }
 
    /* 头像 */
    .w-16 {
        width: 4rem;
    }
 
    .h-16 {
        height: 4rem;
    }
 
    .h-32 {
        height: 8rem;
    }
 
    /* 边框 */
    .border {
        border-width: 1px;
        border-style: solid;
    }
 
    .border-2 {
        border-width: 2px;
    }
 
    .border-gray-300 {
        border-color: #D1D5DB;
    }
 
    .border-blue-500 {
        border-color: #3B82F6;
    }
 
    /* 圆角 */
    .rounded {
        border-radius: 0.25rem;
    }
 
    .rounded-full {
        border-radius: 9999px;
    }
 
    /* 输入框 padding 和高度 */
    .p-2 {
        padding: 0.5rem;
    }
 
    .h-8 {
        height: 2rem;
    }
 
    .h-12 {
        height: 3rem;
    }
 
    /* 文本颜色与背景 */
    .text-white {
        color: #ffffff;
    }
 
    .bg-blue-500 {
        background-color: #3B82F6;
    }
 
    /* label 样式 */
    .text-sm {
        font-size: 0.875rem;
    }
 
    .font-medium {
        font-weight: 500;
    }
 
    /* 按钮全宽 */
    .w-full {
        width: 100%;
    }
</style>
<style>
    /* 外层容器 padding */
    .p-4 {
        padding: 1rem;
    }
 
    /* 下边距 */
    .mb-4 {
        margin-bottom: 1rem;
    }
 
    /* 粗体文本 */
    .font-bold {
        font-weight: bold;
    }
 
    /* flex 布局 */
    .flex {
        display: flex;
    }
 
    .flex-wrap {
        flex-wrap: wrap;
    }
 
    .items-center {
        align-items: center;
    }
 
    .justify-center {
        justify-content: center;
    }
 
    /* 水平间距 */
    .space-x-2 {
        margin-left: 0.5rem;
    }
 
    /* 宽度控制 */
    .w-12 {
        width: 3rem;
    }
 
    .h-12 {
        height: 3rem;
    }
 
    .w-24 {
        width: 6rem;
    }
 
    /* 边框 */
    .border {
        border-width: 1px;
        border-style: solid;
    }
 
    .border-2 {
        border-width: 2px;
    }
 
    .border-gray-300 {
        border-color: #D1D5DB;
    }
 
    .border-blue-500 {
        border-color: #3B82F6;
    }
 
    /* 圆角 */
    .rounded {
        border-radius: 0.25rem;
    }
 
    .rounded-full {
        border-radius: 9999px;
    }
 
    /* 文本颜色与背景 */
    .text-white {
        color: #ffffff;
    }
 
    .bg-blue-500 {
        background-color: #3B82F6;
    }
 
    /* label 样式 */
    .text-sm {
        font-size: 0.875rem;
    }
 
    .font-medium {
        font-weight: 500;
    }
 
    /* 按钮全宽 */
    .w-full {
        width: 100%;
    }
 
    .page-wrapper {
        position: relative;
        min-height: 100vh;
        /* 确保小内容时填满视口 */
        overflow: hidden;
    }
 
    /* 顶部背景 */
    .bg-top {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 500rpx;
        /* 可用固定像素或图片高度 */
        background: url('https://farm.evcc.top/Uploads/yuanboyuan/background/bg_top.jpg') no-repeat top center;
        background-size: cover;
        z-index: 0;
    }
 
    /* 底部背景 */
    .bg-bottom {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 500rpx;
        /* 可用固定像素或图片高度 */
        background: url('https://farm.evcc.top/Uploads/yuanboyuan/background/bg_bottom.jpg') no-repeat bottom center;
        background-size: cover;
        z-index: 0;
    }
 
    /* 内容层 */
    .content-wrapper {
        position: relative;
        z-index: 1;
        padding: 2rem 1rem;
    }
</style>