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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
<template>
    <view class="page-wrapper">
        <div class="bg-top"></div>
 
        <div class="content-wrapper">
 
            <view class="text-2xl font-bold mb-4 text-center">投诉和建议</view>
 
            <!-- 投诉建议 -->
            <!-- 投诉内容 -->
            <view>
                <label class="text-sm font-semibold font-medium mb-1 block">问题描述</label>
                <textarea v-model="form.question" placeholder="请输入您的投诉或建议(5-500字)"
                    class="w-95-percent h-40 p-2 border border-gray-300 rounded-lg resize-none"
                    maxlength="500"></textarea>
                <view class="text-right text-xs text-gray-500">{{ form.question.length }}/500</view>
            </view>
 
            <!-- 上传图片 -->
            <view class="mb-4">
                <view class="text-sm font-semibold mb-1">上传图片</view>
                <view class="flex justify-start space-x-2">
                    <view v-for="(img, index) in form.images" :key="index" class="relative">
                        <image :src="img" class="w-20 h-20 rounded-md object-cover" />
                        <view
                            class="absolute top-0 right-0 bg-black bg-opacity-50 text-white text-xs px-1 cursor-pointer"
                            @click="removeImage(index)">
                            ×
                        </view>
                    </view>
                    <view v-if="form.images.length < 3"
                        class="w-20 h-20 border border-dashed border-gray-400 rounded-md text-gray-400 flex justify-center items-center cursor-pointer"
                        @click="chooseImage">
                        +
                    </view>
                    <!--
                <button v-if="form.images.length < 3"
                    class="w-20 h-20 border border-dashed border-gray-400 rounded-md text-gray-400"
                    @click="chooseImage">
                    +
                </button>
                -->
                </view>
                <view class="text-xs text-gray-500 mt-1">最多选择3张,每张不超过5MB</view>
            </view>
 
            <!-- 联系人 -->
            <view>
                <label class="text-sm font-semibold font-medium mb-1 block">联系人:</label>
                <input v-model="form.contactperson" type="text" placeholder="请输入联系人(最多10个字)"
                    class="w-95-percent border border-gray-300 rounded p-2 h-12" />
            </view>
 
            <!-- 联系电话 -->
            <view class="mt-10">
                <label class="text-sm font-semibold font-medium mb-1 block">联系电话:</label>
                <input v-model="form.contactphone" type="text" placeholder="请输入联系电话"
                    class="w-95-percent border border-gray-300 rounded p-2 h-12" />
            </view>
 
            <!-- 提交按钮 -->
            <button class="w-full bg-blue-500 text-white py-3 rounded mt-10" @click="submitForm">
                提交
            </button>
        </div>
        <div class="bg-bottom"></div>
 
    </view>
</template>
 
<script>
    import {
        useUserStore
    } from '@/stores/user'
    import {
        apiService
    } from '@/apiservice/apiService'
 
    const userStore = useUserStore()
 
    export default {
        data() {
            return {
                form: {
                    question: '',
                    images: [],
                    contactperson: userStore.username,
                    contactphone: userStore.phone
                }
            }
        },
        methods: {
            chooseImage() {
                uni.chooseImage({
                    count: 3 - this.form.images.length,
                    sizeType: ['original', 'compressed'],
                    success: (res) => {
                        const files = res.tempFilePaths.filter((f, i) => i + this.form.images.length < 3)
 
                        const maxSize = 5 * 1024 * 1024 // 5MB,单位字节
                        const oversized = files.filter(f => f.size > maxSize)
                        if (oversized.length > 0) {
                            uni.showToast({
                                title: '图片不能大于 5MB,请重新选择',
                                icon: 'none'
                            })
                            return
                        }
                        this.form.images.push(...files)
 
                    }
                })
            },
            removeImage(index) {
                this.form.images.splice(index, 1)
            },
            async uploadAllImages() {
                if (this.form.images.length === 0) return [];
 
                const uploadPromises = this.form.images.map(img => {
                    return new Promise((resolve, reject) => {
                        uni.uploadFile({
                            url: apiService.baseURL + '/advices/upload', // 后端上传接口
                            filePath: img,
                            name: 'file',
                            success: (res) => {
                                try {
                                    const data = JSON.parse(res.data);
                                    if (data.code === 1000 && data.data) {
                                        resolve(data.data);
                                    } else {
                                        reject(new Error('上传失败'));
                                    }
                                } catch (err) {
                                    reject(err);
                                }
                            },
                            fail: reject
                        });
                    });
                });
 
                return Promise.all(uploadPromises);
            },
            async submitForm() {
                // 内容校验
                if (this.form.question.length < 5) {
                    uni.showToast({
                        title: '投诉内容至少5个字',
                        icon: 'none'
                    })
                    return
                }
                if (this.form.question.length > 200) {
                    uni.showToast({
                        title: '投诉内容最多200字',
                        icon: 'none'
                    })
                    return
                }
 
                // 联系人校验
                const contact = this.form.contactperson.trim()
                if (!contact) {
                    uni.showToast({
                        title: '联系人不能为空',
                        icon: 'none'
                    })
                    return
                }
                if (contact.length > 10) {
                    uni.showToast({
                        title: '联系人最多10个字符',
                        icon: 'none'
                    })
                    return
                }
 
                // 电话校验
                const phone = this.form.contactphone.trim()
                const phoneRegex = /^(1\d{10}|([0-9\uFF10-\uFF19\--\s,,]{5,20}))$/
                if (!phoneRegex.test(phone)) {
                    uni.showToast({
                        title: '联系电话格式不正确',
                        icon: 'none'
                    })
                    return
                }
                try {
                    uni.showLoading({
                        title: '提交中...'
                    });
 
                    // 1️⃣ 并发上传图片
                    const imageUrls = await this.uploadAllImages();
                    const [img1, img2, img3] = imageUrls || [];
 
                    // 2️⃣ 组合提交数据
                    const payload = {
                        uuid: userStore.uuid,
                        question: this.form.question,
                        img1: img1,
                        img2: img2,
                        img3: img3,
                        contactperson: this.form.contactperson,
                        contactphone: this.form.contactphone
                    };
 
                    apiService.advicesAdd(payload).then((res) => {
                        if (res.code === 1000) {
                            uni.showToast({
                                title: '提交成功,我们将在24小时内回复,请在“我的”菜单查看。',
                                icon: 'success'
                            })
                            // 重置表单
                            this.form.question = ''
                            this.form.images = []
                            uni.navigateTo({
                                url: '/pages/ucenter/my-advice'
                            })
                        } else {
                            console.warn('游客更新失败:', res.message)
                            uni.showToast({
                                title: res.message || '提交失败',
                                icon: 'none'
                            })
                        }
                    }).catch((err) => {
                        console.error('游客更新错误:', err)
                        uni.showToast({
                            title: '发生错误,请联系管理员',
                            icon: 'none'
                        })
                    })
                } catch (err) {
                    console.error("提交错误:", err);
                    uni.showToast({
                        title: "网络错误",
                        icon: "none"
                    });
                } finally {
                    uni.hideLoading();
                }
            }
        },
    }
</script>
<style>
    /* 布局与容器 */
    .p-4 {
        padding: 1rem;
    }
 
    .bg-gray-100 {
        background-color: #f3f4f6;
    }
 
    .min-h-screen {
        min-height: 100vh;
    }
 
    /* 字体与文本 */
    .text-2xl {
        font-size: 1.5rem;
        line-height: 2rem;
    }
 
    .font-bold {
        font-weight: 700;
    }
 
    .font-medium {
        font-weight: 500;
    }
 
    .font-semibold {
        font-weight: 600;
    }
 
    .text-center {
        text-align: center;
    }
 
    .text-sm {
        font-size: 0.875rem;
        line-height: 1.25rem;
    }
 
    .mt-10 {
        margin-top: 50rpx;
    }
 
    .justify-start {
        justify-content: flex-start;
    }
 
    .text-xs {
        font-size: 0.75rem;
        line-height: 1rem;
    }
 
    .text-gray-400 {
        color: #9ca3af;
    }
 
    .text-gray-500 {
        color: #6b7280;
    }
 
    .text-white {
        color: #ffffff;
    }
 
    /* 边距 */
    .mb-1 {
        margin-bottom: 0.25rem;
    }
 
    .mb-4 {
        margin-bottom: 1rem;
    }
 
    .mt-1 {
        margin-top: 0.25rem;
    }
 
    /* 尺寸与边框 */
    .w-full {
        width: 100%;
    }
 
    .w-95-percent {
        width: 95%;
    }
 
    .h-40 {
        height: 8rem;
    }
 
    .h-12 {
        height: 2.5rem;
    }
 
    .w-20 {
        width: 5rem;
    }
 
    .h-20 {
        height: 5rem;
    }
 
    .border {
        border-width: 1px;
        border-style: solid;
        border-color: #d1d5db;
    }
 
    .border-gray-300 {
        border-color: #d1d5db;
    }
 
    .border-gray-400 {
        border-color: #9ca3af;
    }
 
    .border-dashed {
        border-style: dashed;
    }
 
    .rounded {
        border-radius: 0.25rem;
    }
 
    .rounded-md {
        border-radius: 0.375rem;
    }
 
    .rounded-lg {
        border-radius: 0.5rem;
    }
 
    /* 背景色与透明度 */
    .bg-black {
        background-color: #000000;
    }
 
    .bg-opacity-50 {
        opacity: 0.5;
    }
 
    .bg-blue-500 {
        background-color: #3b82f6;
    }
 
    /* 位置 */
    .relative {
        position: relative;
    }
 
    .absolute {
        position: absolute;
    }
 
    .top-0 {
        top: 0;
    }
 
    .right-0 {
        right: 0;
    }
 
    .justify-center {
        justify-content: center;
    }
 
    .items-center {
        align-items: center;
    }
 
    /* 布局与对齐 */
    .flex {
        display: flex;
    }
 
    .space-x-2 {
        margin-left: 0.5rem;
    }
 
    .text-right {
        text-align: right;
    }
 
    .block {
        display: block;
    }
 
    .cursor-pointer {
        cursor: pointer;
    }
 
    /* 填充 */
    .p-2 {
        padding: 0.5rem;
    }
 
    .py-3 {
        padding-top: 0.75rem;
        padding-bottom: 0.75rem;
    }
 
    .px-1 {
        padding-left: 0.25rem;
        padding-right: 0.25rem;
    }
 
    /* 图片与视频 */
    .object-cover {
        object-fit: cover;
    }
 
    /* 其他 */
    .resize-none {
        resize: none;
    }
 
    .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>