jiangchengfeiyi-Web/src/views/CostumeAppointments/AddCostumes.vue
2024-12-10 19:27:49 +08:00

164 lines
4.9 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<div>
<el-form ref="resetFormData" :model="form" label-width="auto" style="width: 750px" size="large">
<el-form-item label="添加服装图片">
<el-upload
ref="uploadProductImg"
action="#"
list-type="picture-card"
:auto-upload="false"
multiple="true"
:on-remove="handleRemove"
@change="(event: any) => handleChange(event, 0)"
:on-exceed="Exceed_ProductImg"
limit="1" >
<el-icon>
<Plus />
</el-icon>
</el-upload>
</el-form-item>
<el-form-item label="添加效果图">
<el-upload
ref="uploadProductDetail"
action="#"
list-type="picture-card"
:auto-upload="false"
multiple="true"
:on-change="(event: any) => handleChange(event, 1)"
:on-exceed="Exceed_ProductDetail"
limit="1"
:on-remove="handleRemove"
>
<el-icon>
<Plus />
</el-icon>
</el-upload>
</el-form-item>
<el-form-item label="服装名称" prop="name">
<el-input v-model="form.name" maxlength="30" placeholder="30字以内"/>
</el-form-item>
<el-form-item label="服装价格" prop="price">
<el-input v-model="form.price" />
</el-form-item>
<el-form-item label="服装类别" prop="clothesType">
<el-select v-model="clothesType.value" placeholder="请选择" @change="(event:any) => loadForm(event)">
<el-option v-for="item in clothesType" :key="item.value" :label="item.label" :value="item.values" />
</el-select>
</el-form-item>
<el-form-item label="服装简介" prop="intro">
<el-input v-model="form.intro" maxlength="40" placeholder="40字以内"/>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="onSubmit">提交</el-button>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="resetForm">重置</el-button>
</el-form-item>
</el-form>
</div>
</template>
<script setup lang="ts">
import {Plus} from "@element-plus/icons-vue";
import {ElMessage, genFileId, type UploadProps, type UploadRawFile} from "element-plus";
import myAxios from "@/api/myAxios";
import {inject, ref ,onMounted} from "vue";
const fileSimple = ref()
const uploadProductImg : any = ref()
const uploadProductDetail : any = ref()
const form = ref({
name:'',
image:'',
effectImg:'',
intro:'',
price:'',
clothesType:''
})
const clothesType: any = ref([
{
values: '',
label: ''
}
])
onMounted(()=>{
getGrade()
})
const getGrade = async ()=>{
const res = await myAxios.post('/clothesGrade/list',{})
for(let i in res.data.data){
clothesType.value[i] = {
values: res.data.data[i].clothesType,
label:res.data.data[i].clothesType,
}
}
}
const loadForm = (value: any) => {
form.value.clothesType = value
}
const reload : any = inject("reload")
const onSubmit = async () => {
const values = Object.values(form.value);
if (values.some(value => value === null || value === undefined || value === '')) {
ElMessage({
type:'warning',
message:'请检查表单数据是否完整填写'
})
return;
}
const res = await myAxios.post('/clothesInfo/add', { ...form.value })
if (res.data.code === 1) {
ElMessage({
type: 'success',
message: "提交成功"
})
reload() //上传完后重置表单
} else {
ElMessage.error("服务错误")
return;
}
}
const resetFormData = ref()
const resetForm = () =>{
resetFormData.value.resetFields()
}
const handleRemove: UploadProps['onRemove'] = (uploadFile, uploadFiles) => {
console.log(uploadFile, uploadFiles)
}
const handleChange = async (file: any, flag: number) => {
fileSimple.value = file
let formData = new FormData()
formData.append("file", fileSimple.value.raw)
const res = await myAxios({
url: '/file/upload/server/not_login',
method: 'post',
headers: {
'content-Type': 'multipart/form-data'
},
data: {
biz: "test",
file: formData.get("file")
}
})
if (res.data.code === 1) {
flag ? form.value.effectImg = res.data.data : form.value.image = res.data.data
}
}
const Exceed_ProductImg: UploadProps['onExceed'] = (files) => { //覆盖商品照片
uploadProductImg.value!.clearFiles()
const file = files[0] as UploadRawFile
file.uid = genFileId()
uploadProductImg.value!.handleStart(file)
}
const Exceed_ProductDetail: UploadProps['onExceed'] = (files) => { //覆盖商品详情图片
uploadProductDetail.value!.clearFiles()
const file = files[0] as UploadRawFile
file.uid = genFileId()
uploadProductDetail.value!.handleStart(file)
}
</script>
<style scoped>
</style>