index.vue 15.5 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 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
<!--
 * @Date: 2022-09-19 14:11:06
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2025-07-03 14:27:41
 * @FilePath: /jgdl/src/pages/setAuthCar/index.vue
 * @Description: 申请认证
-->
<template>
    <view class="auth-car-page">
        <!-- 表单内容 -->
        <view class="form-container">
            <!-- 车辆照片上传 -->
            <view class="form-section">
                <text class="section-title">添加车辆照片</text>
                <view class="upload-grid">
                    <!-- 正面照 -->
                    <view class="upload-item">
                        <!-- 上传按钮 -->
                        <view v-if="!uploadedImages.front" class="upload-button" @click="triggerUpload('front')">
                            <Plus class="upload-icon" />
                        </view>
                        <!-- 图片预览 -->
                        <view v-else class="image-preview" @click="previewImage(uploadedImages.front)">
                            <image :src="uploadedImages.front" class="preview-image" mode="aspectFill" />
                            <view class="delete-btn" @click.stop="deleteImage('front')">
                                <Close class="delete-icon" />
                            </view>
                        </view>
                        <text class="upload-label">正面照</text>
                    </view>

                    <!-- 左侧照 -->
                    <view class="upload-item">
                        <!-- 上传按钮 -->
                        <view v-if="!uploadedImages.left" class="upload-button" @click="triggerUpload('left')">
                            <Plus class="upload-icon" />
                        </view>
                        <!-- 图片预览 -->
                        <view v-else class="image-preview" @click="previewImage(uploadedImages.left)">
                            <image :src="uploadedImages.left" class="preview-image" mode="aspectFill" />
                            <view class="delete-btn" @click.stop="deleteImage('left')">
                                <Close class="delete-icon" />
                            </view>
                        </view>
                        <text class="upload-label">左侧照</text>
                    </view>

                    <!-- 右侧照 -->
                    <view class="upload-item">
                        <!-- 上传按钮 -->
                        <view v-if="!uploadedImages.right" class="upload-button" @click="triggerUpload('right')">
                            <Plus class="upload-icon" />
                        </view>
                        <!-- 图片预览 -->
                        <view v-else class="image-preview" @click="previewImage(uploadedImages.right)">
                            <image :src="uploadedImages.right" class="preview-image" mode="aspectFill" />
                            <view class="delete-btn" @click.stop="deleteImage('right')">
                                <Close class="delete-icon" />
                            </view>
                        </view>
                        <text class="upload-label">右侧照</text>
                    </view>

                    <!-- 其他 -->
                    <view class="upload-item">
                        <!-- 上传按钮 -->
                        <view v-if="!uploadedImages.other" class="upload-button" @click="triggerUpload('other')">
                            <Plus class="upload-icon" />
                        </view>
                        <!-- 图片预览 -->
                        <view v-else class="image-preview" @click="previewImage(uploadedImages.other)">
                            <image :src="uploadedImages.other" class="preview-image" mode="aspectFill" />
                            <view class="delete-btn" @click.stop="deleteImage('other')">
                                <Close class="delete-icon" />
                            </view>
                        </view>
                        <text class="upload-label">其他</text>
                    </view>
                </view>

                <!-- 图片预览组件 -->
                <nut-image-preview v-model:show="previewVisible" :images="previewImages" :init-no="previewIndex"  @close="closePreview" />
            </view>

            <!-- 车辆详情表单 -->
            <nut-form ref="formRef" :model-value="formData">
                <view class="form-section">
                    <!-- 车型品牌 -->
                    <nut-form-item label-position="top" label="车型品牌" prop="brand" required :rules="[{ required: true, message: '请选择车型品牌' }]">
                        <view class="form-item-content" @click="showBrandPicker">
                            <text class="form-value">{{ formData.brand || '请选择' }}</text>
                            <Right class="arrow-icon" />
                        </view>
                    </nut-form-item>

                    <!-- 车辆型号 -->
                    <nut-form-item label-position="top" label="车辆型号" prop="model" required :rules="[{ required: true, message: '请选择车辆型号' }]">
                        <view class="form-item-content" @click="showModelPicker">
                            <text class="form-value">{{ formData.model || '请选择' }}</text>
                            <Right class="arrow-icon" />
                        </view>
                    </nut-form-item>

                    <!-- 续航里程 -->
                    <nut-form-item label="续航里程" prop="range" required :rules="[{ required: true, message: '请输入续航里程' }]">
                        <nut-input v-model="formData.range" placeholder="60" type="number" input-align="right">
                            <template #right>
                                <text class="unit">公里</text>
                            </template>
                        </nut-input>
                    </nut-form-item>

                    <!-- 最高时速 -->
                    <nut-form-item label="最高时速" prop="maxSpeed" required :rules="[{ required: true, message: '请输入最高时速' }]">
                        <nut-input v-model="formData.maxSpeed" placeholder="25" type="number" input-align="right">
                            <template #right>
                                <text class="unit">km/h</text>
                            </template>
                        </nut-input>
                    </nut-form-item>
                </view>
            </nut-form>

            <!-- 车辆描述 -->
            <view class="form-section">
                <text class="section-title">车辆描述</text>
                <nut-textarea v-model="formData.description" placeholder="请描述车辆详情,如使用感受、车况特点等" :max-length="200" :rows="4" show-word-limit />
            </view>
        </view>

        <!-- 底部按钮 -->
        <view class="bottom-actions">
            <nut-button color="#f97316" size="large" block @click="onSubmit">
                {{ isEditMode ? '保存修改' : '提交申请' }}
            </nut-button>
        </view>

        <!-- 选择器弹窗 -->
        <!-- 品牌选择 -->
        <nut-popup v-model:visible="brandPickerVisible" position="bottom">
            <nut-picker v-model="brandValue" :columns="brandOptions" title="选择车型品牌" @confirm="onBrandConfirm" @cancel="brandPickerVisible = false" />
        </nut-popup>

        <!-- 型号选择 -->
        <nut-popup v-model:visible="modelPickerVisible" position="bottom">
            <nut-picker v-model="modelValue" :columns="modelOptions" title="选择车辆型号" @confirm="onModelConfirm" @cancel="modelPickerVisible = false" />
        </nut-popup>
    </view>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { Plus, Right, RectLeft, Close } from '@nutui/icons-vue-taro'
import Taro from '@tarojs/taro'
import './index.less'

// 获取页面参数
const instance = Taro.getCurrentInstance()
const { id, mode } = instance.router?.params || {}
const isEditMode = ref(mode === 'edit' && id)
const carId = ref(id || '')

const themeVars = ref({
    navbarBackground: '#fb923c',
    navbarColor: '#ffffff',
})

// 已上传图片的URL
const uploadedImages = reactive({
    front: '',
    left: '',
    right: '',
    other: ''
})

// 图片预览相关
const previewVisible = ref(false)
const previewImages = ref([])
const previewIndex = ref(0)

const closePreview = () => {
    previewVisible.value = false
}

// 表单数据
const formData = reactive({
    brand: '',
    model: '',
    range: '',
    maxSpeed: '',
    description: ''
})

// 选择器显示状态
const brandPickerVisible = ref(false)
const modelPickerVisible = ref(false)

// 选择器值
const brandValue = ref([])
const modelValue = ref([])

// 选择器选项数据
const brandOptions = ref([
    { text: '小牛电动', value: '小牛电动' },
    { text: '雅迪', value: '雅迪' },
    { text: '爱玛', value: '爱玛' },
    { text: '台铃', value: '台铃' },
    { text: '绿源', value: '绿源' },
    { text: '新日', value: '新日' },
    { text: '立马', value: '立马' },
    { text: '其他', value: '其他' }
])

const modelOptions = ref([
    { text: 'NGT', value: 'NGT' },
    { text: 'UQi+', value: 'UQi+' },
    { text: 'Gova G2', value: 'Gova G2' },
    { text: 'MQi+', value: 'MQi+' },
    { text: 'NQi GTS', value: 'NQi GTS' },
    { text: 'RQi', value: 'RQi' },
    { text: '其他型号', value: '其他型号' }
])

/**
 * 返回上一页
 */
const goBack = () => {
    Taro.navigateBack()
}

/**
 * 触发图片上传
 * @param {String} type - 图片类型 (front/left/right/other)
 */
const triggerUpload = (type) => {
    Taro.chooseImage({
        count: 1,
        sizeType: ['compressed'],
        sourceType: ['album', 'camera'],
        success: function (res) {
            const tempFilePath = res.tempFilePaths[0]
            uploadImage(tempFilePath, type)
        },
        fail: function () {
            Taro.showToast({
                title: '选择图片失败',
                icon: 'none'
            })
        }
    })
}

/**
 * 上传图片到服务器
 * @param {String} filePath - 图片文件路径
 * @param {String} type - 图片类型 (front/left/right/other)
 */
const uploadImage = (filePath, type) => {
    // 显示上传中提示
    Taro.showLoading({
        title: '上传中',
        mask: true
    })

    // 模拟上传成功(实际项目中替换为真实的上传逻辑)
    setTimeout(() => {
        Taro.hideLoading()

        // 模拟服务器返回的图片URL
        const mockImageUrl = filePath // 在实际项目中,这里应该是服务器返回的URL

        // 更新对应类型的图片
        uploadedImages[type] = mockImageUrl

        Taro.showToast({
            title: '上传成功',
            icon: 'success'
        })
    }, 1500)
}

/**
 * 删除图片
 * @param {String} type - 图片类型
 */
const deleteImage = (type) => {
    uploadedImages[type] = ''
    Taro.showToast({
        title: '删除成功',
        icon: 'success'
    })
}

/**
 * 预览图片
 * @param {String} imageUrl - 图片URL
 */
const previewImage = (imageUrl) => {
    previewImages.value = [imageUrl]
    previewIndex.value = 0
    previewVisible.value = true
}

/**
 * 显示品牌选择器
 */
const showBrandPicker = () => {
    brandPickerVisible.value = true
}

/**
 * 显示型号选择器
 */
const showModelPicker = () => {
    modelPickerVisible.value = true
}

/**
 * 品牌选择确认
 * @param {Object} options - 选择的选项
 */
const onBrandConfirm = (options) => {
    formData.brand = options.selectedOptions[0]?.text || ''
    brandPickerVisible.value = false
}

/**
 * 型号选择确认
 * @param {Object} options - 选择的选项
 */
const onModelConfirm = (options) => {
    formData.model = options.selectedOptions[0]?.text || ''
    modelPickerVisible.value = false
}

/**
 * 提交申请/保存修改
 */
const onSubmit = () => {
    // 表单验证
    if (!formData.brand) {
        Taro.showToast({
            title: '请选择车型品牌',
            icon: 'none'
        })
        return
    }

    if (!formData.model) {
        Taro.showToast({
            title: '请选择车辆型号',
            icon: 'none'
        })
        return
    }

    if (!formData.range) {
        Taro.showToast({
            title: '请输入续航里程',
            icon: 'none'
        })
        return
    }

    if (!formData.maxSpeed) {
        Taro.showToast({
            title: '请输入最高时速',
            icon: 'none'
        })
        return
    }

    // 检查是否至少上传了一张照片
    const hasImage = Object.values(uploadedImages).some(img => img)
    if (!hasImage) {
        Taro.showToast({
            title: '请至少上传一张车辆照片',
            icon: 'none'
        })
        return
    }

    // 提交数据
    const submitData = {
        ...formData,
        images: uploadedImages
    }

    if (isEditMode.value) {
        submitData.id = carId.value
    }

    const loadingTitle = isEditMode.value ? '保存中' : '提交中'
    Taro.showLoading({
        title: loadingTitle,
        mask: true
    })

    // TODO: 调用真实API
    // if (isEditMode.value) {
    //   updateAuthApplication(carId.value, submitData)
    // } else {
    //   submitAuthApplication(submitData)
    // }

    // 模拟提交成功
    setTimeout(() => {
        Taro.hideLoading()
        const successTitle = isEditMode.value ? '保存成功' : '申请提交成功'
        Taro.showToast({
            title: successTitle,
            icon: 'success'
        })

        // 返回上一页
        setTimeout(() => {
            Taro.navigateBack()
        }, 1500)
    }, 2000)
}

/**
 * 加载认证数据(编辑模式)
 */
const loadAuthData = async () => {
    if (!isEditMode.value || !carId.value) return

    try {
        Taro.showLoading({ title: '加载中...' })

        // TODO: 调用真实API获取认证数据
        // const authData = await getAuthById(carId.value)

        // 暂时不模拟数据,按用户要求
        // 如果需要模拟数据,可以取消下面的注释

        const mockAuthData = {
            brand: '小牛电动',
            model: 'NGT',
            range: '60',
            maxSpeed: '25',
            description: '车况良好,电池续航正常,无重大事故。',
            images: {
                front: 'https://picsum.photos/300/200?random=5',
                left: 'https://picsum.photos/300/200?random=6',
                right: 'https://picsum.photos/300/200?random=7',
                other: 'https://picsum.photos/300/200?random=8'
            }
        }

        // 填充表单数据
        Object.assign(formData, {
            brand: mockAuthData.brand,
            model: mockAuthData.model,
            range: mockAuthData.range,
            maxSpeed: mockAuthData.maxSpeed,
            description: mockAuthData.description
        })

        // 填充图片数据
        Object.assign(uploadedImages, mockAuthData.images)


        Taro.hideLoading()
    } catch (error) {
        console.error('加载认证数据失败:', error)
        Taro.hideLoading()
        Taro.showToast({
            title: '加载数据失败',
            icon: 'none'
        })
    }
}

// 页面加载时执行
onMounted(() => {
    if (isEditMode.value) {
        loadAuthData()
    }
    // 动态修改标题
    wx.setNavigationBarTitle({
        title: isEditMode.value ? '编辑认证' : '发布认证'
    });
})
</script>

<script>
export default {
    name: "setAuthCarPage",
};
</script>