2024-10-23 09:55:22 +00:00
|
|
|
|
<template>
|
2025-03-16 15:22:08 +00:00
|
|
|
|
<div class="flexCard">
|
|
|
|
|
<el-card class="richTextPreview" shadow="never">
|
|
|
|
|
<div class="titleText">移动端预览</div>
|
|
|
|
|
<p v-html="previewRichText"></p>
|
|
|
|
|
</el-card>
|
2024-11-03 04:40:56 +00:00
|
|
|
|
<!-- 表单 ref 和 prop绑定 用于重置表单 -->
|
2025-03-16 15:22:08 +00:00
|
|
|
|
<el-card class="form-container" shadow="never">
|
|
|
|
|
<div class="form">
|
2025-03-20 13:04:28 +00:00
|
|
|
|
<el-form ref="resetFormData" :model="form" size="large" :rules="rules">
|
2024-12-17 06:59:05 +00:00
|
|
|
|
<div>
|
|
|
|
|
<el-form-item label="添加商品图片">
|
2025-03-20 13:04:28 +00:00
|
|
|
|
<ImgListUpload @update:goodImgArr="form.goodImg = $event"/>
|
2024-12-17 06:59:05 +00:00
|
|
|
|
</el-form-item>
|
2024-12-19 10:55:48 +00:00
|
|
|
|
</div>
|
2024-12-17 06:59:05 +00:00
|
|
|
|
<div class="NamePrice">
|
2024-12-19 10:55:48 +00:00
|
|
|
|
<el-form-item label="产品名称" prop="name">
|
|
|
|
|
<el-input v-model="form.name" maxlength="12" minlength="2" show-word-limit style="width: 260px;" />
|
|
|
|
|
</el-form-item>
|
2025-03-20 13:04:28 +00:00
|
|
|
|
<el-form-item label="产品价格" prop="price" label-width="100">
|
2025-03-02 03:29:54 +00:00
|
|
|
|
<el-input-number v-model="form.price" min="0" :precision="2" :step="0.5" />
|
2024-12-19 10:55:48 +00:00
|
|
|
|
<p>元</p>
|
|
|
|
|
</el-form-item>
|
2024-12-17 06:59:05 +00:00
|
|
|
|
</div>
|
|
|
|
|
<div class="IntroInven">
|
2024-12-19 10:55:48 +00:00
|
|
|
|
<el-form-item label="产品简介" prop="intro">
|
|
|
|
|
<el-input v-model="form.intro" type="textarea" placeholder="产品尺寸,服务等" maxlength="30" show-word-limit
|
|
|
|
|
style="width: 300px;" />
|
|
|
|
|
</el-form-item>
|
|
|
|
|
<div>
|
2025-03-20 13:04:28 +00:00
|
|
|
|
<el-form-item label="库存" prop="inventory" label-width="80">
|
2024-12-19 10:55:48 +00:00
|
|
|
|
<el-input-number v-model="form.inventory" min="0" :precision="0" :step="1" :disabled="invInput" />
|
|
|
|
|
</el-form-item>
|
|
|
|
|
</div>
|
2024-12-17 06:59:05 +00:00
|
|
|
|
</div>
|
|
|
|
|
<div class="festivalBox">
|
2024-12-19 10:55:48 +00:00
|
|
|
|
<el-form-item label="节日名称" prop="festivalName" style="width: 300px;">
|
2025-03-16 15:22:08 +00:00
|
|
|
|
<el-select v-model="form.festivalName" placeholder="请选择" >
|
|
|
|
|
<el-option v-for="item in festivalArr" :key="item" :label="item.name" :value="item.name" />
|
|
|
|
|
</el-select>
|
2024-12-19 10:55:48 +00:00
|
|
|
|
</el-form-item>
|
2025-03-20 13:04:28 +00:00
|
|
|
|
<el-form-item label="产品类别" prop="type" style="width: 240px;" label-width="80">
|
2024-12-19 10:55:48 +00:00
|
|
|
|
<el-select v-model="form.type" placeholder="请选择" @change="(event: any) => loadForm(event)" :disabled="typeSelect">
|
|
|
|
|
<el-option v-for="item in typeList" :key="item" :label="item" :value="item" />
|
|
|
|
|
</el-select>
|
|
|
|
|
</el-form-item>
|
|
|
|
|
<div>
|
|
|
|
|
<el-checkbox v-model="isqualify" label="定制商品" size="large" border style="margin-left: 10px;" @change="customTag" />
|
|
|
|
|
</div>
|
2024-12-17 06:59:05 +00:00
|
|
|
|
</div>
|
|
|
|
|
<el-form-item label="商品标签" prop="label">
|
|
|
|
|
<div v-for="(item, index) in labelList" :key="index">
|
|
|
|
|
<el-input v-model="labelList[index]" type="text" style="width: 150px;" maxlength="5" show-word-limit
|
|
|
|
|
@blur="addLabelList" />
|
|
|
|
|
</div>
|
2025-03-20 13:04:28 +00:00
|
|
|
|
<el-button type="primary" size="large" @click="addLabel">添加</el-button>
|
|
|
|
|
<el-button type="danger" size="large" @click="deleteLabel" v-if="labelList.length > 1">删除</el-button>
|
2024-12-17 06:59:05 +00:00
|
|
|
|
</el-form-item>
|
2025-03-16 15:22:08 +00:00
|
|
|
|
<el-form-item label="商品图文" prop="richText">
|
|
|
|
|
<richTextUtil @richTextContent="getInfo"/>
|
2024-12-17 06:59:05 +00:00
|
|
|
|
</el-form-item>
|
|
|
|
|
<div class="btnBox">
|
2024-12-19 10:55:48 +00:00
|
|
|
|
<el-form-item>
|
|
|
|
|
<el-button type="primary" @click="onSubmit" :disabled="saveBtn">保存</el-button>
|
|
|
|
|
</el-form-item>
|
|
|
|
|
<el-form-item>
|
|
|
|
|
<el-button type="primary" @click="resetForm">重置</el-button>
|
|
|
|
|
</el-form-item>
|
|
|
|
|
</div>
|
2024-12-17 06:59:05 +00:00
|
|
|
|
</el-form>
|
|
|
|
|
</div>
|
2025-03-16 15:22:08 +00:00
|
|
|
|
</el-card>
|
2024-10-23 09:55:22 +00:00
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script lang="ts" setup>
|
2025-03-20 13:04:28 +00:00
|
|
|
|
import {ref, onMounted, inject, reactive, onBeforeMount} from 'vue';
|
2025-03-16 15:22:08 +00:00
|
|
|
|
import { type FormRules } from 'element-plus';
|
2024-12-19 10:55:48 +00:00
|
|
|
|
import { SuccessInfo, WarnInfo, CommInfo } from '@/utils/messageInfo'; //封装ElMessage提示
|
2024-10-27 10:08:43 +00:00
|
|
|
|
import myAxios from "@/api/myAxios";
|
2024-12-19 10:55:48 +00:00
|
|
|
|
import { saveBtn, invInput } from '@/utils/entityProduct/globalVar';
|
|
|
|
|
import { validateName, validateIntro, validateDetail } from '@/utils/entityProduct/FormRules';
|
2025-03-16 15:22:08 +00:00
|
|
|
|
import richTextUtil from '@/layout/components/richTextUtil.vue';
|
2025-03-20 13:04:28 +00:00
|
|
|
|
import ImgListUpload from '@/utils/imgUpload/ImgListUpload.vue'
|
|
|
|
|
|
2024-11-03 04:40:56 +00:00
|
|
|
|
const resetFormData = ref()
|
2024-12-19 10:55:48 +00:00
|
|
|
|
const reload: any = inject("reload") //导入组件刷新
|
|
|
|
|
const typeList: any = ref([])
|
2024-12-17 06:59:05 +00:00
|
|
|
|
const labelList = ref([''])
|
2024-12-19 10:55:48 +00:00
|
|
|
|
const isqualify : any = ref(false)
|
|
|
|
|
const typeSelect : any = ref(false) //类别下拉选项栏的禁用状态
|
2025-03-16 15:22:08 +00:00
|
|
|
|
const previewRichText: any = ref<String>('')
|
|
|
|
|
const festivalArr : any = ref([]) //节日数组
|
2025-03-20 13:04:28 +00:00
|
|
|
|
const form : any = ref({ //添加实体类商品的表单
|
|
|
|
|
name: '', //商品名称
|
|
|
|
|
price: 0, //商品价格
|
|
|
|
|
intro: '',//产品简介
|
|
|
|
|
festivalName: '', //节日名称
|
|
|
|
|
type: '',//类别
|
|
|
|
|
label: '',//商品标签
|
|
|
|
|
goodImg: '', //商品图片url
|
|
|
|
|
inventory: 0, //库存
|
|
|
|
|
richText: ''
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
onBeforeMount(()=>{
|
|
|
|
|
form.value.festivalName = '无'
|
|
|
|
|
})
|
2025-03-16 15:22:08 +00:00
|
|
|
|
|
2024-11-03 04:40:56 +00:00
|
|
|
|
onMounted(() => {
|
|
|
|
|
getTypeList() //加载类别列表,渲染在产品列表的select选项里面
|
2025-03-16 15:22:08 +00:00
|
|
|
|
getFestivalArr() //查询节日列表
|
2024-11-01 05:04:02 +00:00
|
|
|
|
})
|
2025-03-16 15:22:08 +00:00
|
|
|
|
|
|
|
|
|
const getFestivalArr = async () => {
|
|
|
|
|
const res = await myAxios.get('/festival/get')
|
|
|
|
|
if(res.data.code === 1) {
|
|
|
|
|
festivalArr.value = res.data.data
|
|
|
|
|
} else {
|
|
|
|
|
WarnInfo('获取节日失败,检查服务')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-03 04:40:56 +00:00
|
|
|
|
const getTypeList = async () => { //获取类别列表作为可选项
|
2024-12-19 10:55:48 +00:00
|
|
|
|
const res = await myAxios.post('/category/list/web', {}) //获取商品列表
|
|
|
|
|
for(let key in res.data.data) {
|
|
|
|
|
typeList.value.push(res.data.data[key].typeName)
|
2024-11-03 04:40:56 +00:00
|
|
|
|
}
|
2025-03-22 08:48:20 +00:00
|
|
|
|
// console.log('typeList---->',typeList.value);
|
2024-11-01 05:04:02 +00:00
|
|
|
|
}
|
2024-11-03 04:40:56 +00:00
|
|
|
|
//提交表单
|
|
|
|
|
const onSubmit = async () => {
|
2025-03-20 13:04:28 +00:00
|
|
|
|
console.log(form.value)
|
2024-12-19 10:55:48 +00:00
|
|
|
|
const values = Object.values(form.value );
|
2024-11-03 04:40:56 +00:00
|
|
|
|
// 使用some()方法来检查是否有任何值为空
|
2025-03-20 13:04:28 +00:00
|
|
|
|
if (values.some((value: any) => value === null || value === undefined || value === '' || value === 0 || value === 'PHA+PGJyPjwvcD4=')) {
|
2024-12-17 06:59:05 +00:00
|
|
|
|
WarnInfo('请检查表单数据是否完整填写')
|
2024-11-03 04:40:56 +00:00
|
|
|
|
return; //空返回结束函数
|
|
|
|
|
}
|
2025-03-16 15:22:08 +00:00
|
|
|
|
const res = await myAxios.post('/goods/add', {
|
|
|
|
|
name: form.value.name,
|
|
|
|
|
type: form.value.type,
|
|
|
|
|
intro: form.value.intro,
|
|
|
|
|
price: form.value.price,
|
|
|
|
|
goodImg: form.value.goodImg,
|
|
|
|
|
richText: form.value.richText,
|
|
|
|
|
inventory: form.value.inventory,
|
|
|
|
|
festivalName: form.value.festivalName,
|
|
|
|
|
label: form.value.label
|
|
|
|
|
})
|
2024-11-03 04:40:56 +00:00
|
|
|
|
if (res.data.code === 1) {
|
2024-12-17 06:59:05 +00:00
|
|
|
|
SuccessInfo('提交成功')
|
2024-12-19 10:55:48 +00:00
|
|
|
|
form.value = {} //12.18改
|
2024-11-05 12:19:45 +00:00
|
|
|
|
reload() //上传完后重置表单
|
2024-11-03 04:40:56 +00:00
|
|
|
|
} else {
|
2024-12-17 06:59:05 +00:00
|
|
|
|
WarnInfo('服务错误')
|
2024-11-05 12:19:45 +00:00
|
|
|
|
return;
|
2024-11-03 04:40:56 +00:00
|
|
|
|
}
|
2024-11-01 05:04:02 +00:00
|
|
|
|
}
|
2024-11-03 04:40:56 +00:00
|
|
|
|
//将 商品类别 赋值到表格里
|
2024-12-19 10:55:48 +00:00
|
|
|
|
const loadForm = (value: any) => {
|
|
|
|
|
form.value.type = value //三元运算符 flag=1 是关于"商品类别"选项的值 flag=0 是关于“是否为节日限定”选项的值
|
2024-11-03 04:40:56 +00:00
|
|
|
|
}
|
|
|
|
|
//清除表单
|
2024-12-17 06:59:05 +00:00
|
|
|
|
const resetForm = () => {
|
2024-11-03 04:40:56 +00:00
|
|
|
|
resetFormData.value.resetFields()
|
2024-10-31 11:23:27 +00:00
|
|
|
|
}
|
2025-03-16 15:22:08 +00:00
|
|
|
|
|
2024-12-17 06:59:05 +00:00
|
|
|
|
const rules = reactive<FormRules<typeof form>>({ //表单校验规则
|
|
|
|
|
name: [{ validator: validateName, trigger: 'blur' }],
|
|
|
|
|
intro: [{ validator: validateIntro, trigger: 'blur' }],
|
|
|
|
|
introDetail: [{ validator: validateDetail, trigger: 'blur' }]
|
|
|
|
|
})
|
|
|
|
|
const addLabel = () => { //添加商品标签按钮的方法
|
|
|
|
|
if (labelList.value[labelList.value.length - 1] === '') {
|
|
|
|
|
WarnInfo('请填写完')
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (labelList.value.length < 4) {
|
|
|
|
|
labelList.value.push('')
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-20 13:04:28 +00:00
|
|
|
|
|
|
|
|
|
const deleteLabel = () => { //删除标签的方法
|
|
|
|
|
labelList.value.pop()
|
2025-03-22 08:48:20 +00:00
|
|
|
|
addLabelList() //格式化标签
|
|
|
|
|
// console.log('标签--->',labelList.value)
|
2025-03-20 13:04:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-17 06:59:05 +00:00
|
|
|
|
const addLabelList = () => { //格式化商品标签
|
|
|
|
|
var string = ''
|
|
|
|
|
if (!labelList.value.some((item: any) => item === '')) {
|
|
|
|
|
string = labelList.value.join(';')
|
|
|
|
|
} else return;
|
|
|
|
|
form.value.label = string
|
|
|
|
|
}
|
2024-12-19 10:55:48 +00:00
|
|
|
|
const customTag = (e:any)=>{ //点击定制
|
|
|
|
|
if(e) {
|
|
|
|
|
form.value.inventory = 1
|
|
|
|
|
invInput.value = true
|
|
|
|
|
typeSelect.value = true
|
|
|
|
|
form.value.type = '定制'
|
|
|
|
|
} else {
|
|
|
|
|
form.value.inventory = 0
|
|
|
|
|
invInput.value = false
|
|
|
|
|
typeSelect.value = false
|
|
|
|
|
form.value.type = ''
|
|
|
|
|
}
|
2024-12-17 06:59:05 +00:00
|
|
|
|
}
|
2025-03-16 15:22:08 +00:00
|
|
|
|
const getInfo = (info: any) => { //富文本赋值
|
|
|
|
|
// console.log('info', info)
|
|
|
|
|
previewRichText.value = info
|
|
|
|
|
form.value.richText = encode64(info)
|
|
|
|
|
}
|
|
|
|
|
function encode64(text: string): string {
|
|
|
|
|
return btoa(String.fromCharCode(...new TextEncoder().encode(text)))
|
|
|
|
|
}
|
2024-12-17 06:59:05 +00:00
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
2025-03-16 15:22:08 +00:00
|
|
|
|
.flexCard {
|
2024-12-17 06:59:05 +00:00
|
|
|
|
display: flex;
|
2025-03-16 15:22:08 +00:00
|
|
|
|
height: 100%;
|
2025-03-20 13:04:28 +00:00
|
|
|
|
min-width: 800px;
|
|
|
|
|
margin: 0 auto;
|
2024-12-17 06:59:05 +00:00
|
|
|
|
}
|
2024-12-19 10:55:48 +00:00
|
|
|
|
|
2025-03-16 15:22:08 +00:00
|
|
|
|
.richTextPreview {
|
|
|
|
|
height: 80vh;
|
|
|
|
|
overflow-y: auto;
|
|
|
|
|
word-break: break-all;
|
|
|
|
|
width: 400px;
|
2025-03-22 08:48:20 +00:00
|
|
|
|
min-width: 400px;
|
2025-03-16 15:22:08 +00:00
|
|
|
|
margin-right: 10px;
|
|
|
|
|
}
|
2025-03-22 08:48:20 +00:00
|
|
|
|
|
2025-03-16 15:22:08 +00:00
|
|
|
|
.form-container {
|
2025-03-20 13:04:28 +00:00
|
|
|
|
width: 900px;
|
|
|
|
|
height: 800px;
|
|
|
|
|
overflow: auto;
|
2025-03-16 15:22:08 +00:00
|
|
|
|
/* margin: 0 auto; */
|
|
|
|
|
}
|
|
|
|
|
.form {
|
|
|
|
|
/* width: 500px; */
|
2024-12-17 06:59:05 +00:00
|
|
|
|
margin: 0 auto;
|
|
|
|
|
}
|
2024-12-19 10:55:48 +00:00
|
|
|
|
|
2024-12-17 06:59:05 +00:00
|
|
|
|
.NamePrice {
|
|
|
|
|
display: flex;
|
|
|
|
|
}
|
2024-12-19 10:55:48 +00:00
|
|
|
|
|
2024-12-17 06:59:05 +00:00
|
|
|
|
.IntroInven {
|
|
|
|
|
display: flex;
|
|
|
|
|
}
|
2024-12-19 10:55:48 +00:00
|
|
|
|
|
2024-12-17 06:59:05 +00:00
|
|
|
|
.festivalBox {
|
|
|
|
|
display: flex;
|
|
|
|
|
}
|
2024-12-19 10:55:48 +00:00
|
|
|
|
|
2024-12-17 06:59:05 +00:00
|
|
|
|
.btnBox {
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: space-around;
|
2024-11-05 12:19:45 +00:00
|
|
|
|
}
|
2025-03-20 13:04:28 +00:00
|
|
|
|
|
|
|
|
|
.titleText {
|
|
|
|
|
text-align: center;
|
|
|
|
|
margin: 0 auto;
|
|
|
|
|
}
|
2024-12-17 06:59:05 +00:00
|
|
|
|
</style>
|