Files
guofu-admin/modules/views/bak/goods/components/goodsEdit.vue
2024-08-23 22:28:24 +08:00

362 lines
10 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>
<ele-modal
:width="800"
:visible="visible"
:maskClosable="false"
:maxable="maxable"
:title="isUpdate ? '编辑广告' : '添加广告'"
:body-style="{ paddingBottom: '28px' }"
@update:visible="updateVisible"
@ok="save"
>
<a-form
ref="formRef"
:model="form"
:rules="rules"
:label-col="styleResponsive ? { md: 4, sm: 5, xs: 24 } : { flex: '90px' }"
:wrapper-col="
styleResponsive ? { md: 19, sm: 19, xs: 24 } : { flex: '1' }
"
>
<a-form-item label="广告位类型" name="adType">
<a-select
ref="select"
v-model:value="form.adType"
style="width: 120px"
>
<a-select-option value="图片广告">图片广告</a-select-option>
<a-select-option value="幻灯片">幻灯片</a-select-option>
<a-select-option value="视频广告">视频广告</a-select-option>
</a-select>
</a-form-item>
<template v-if="form.adType == '幻灯片'">
<a-form-item label="广告图片" name="images" extra="请上传广告图片,最多可上传9张图">
<SelectFile
:placeholder="`请选择图片`"
:limit="9"
:data="images"
@done="chooseFile"
@del="onDeleteItem"
/>
<!-- <ele-image-upload-->
<!-- v-model:value="images"-->
<!-- :limit="9"-->
<!-- :drag="true"-->
<!-- :accept="'image/png,image/jpeg'"-->
<!-- :item-style="{ maxWidth: '160px', maxHeight: '160px' }"-->
<!-- :upload-handler="uploadHandler"-->
<!-- @upload="onUpload"-->
<!-- />-->
</a-form-item>
</template>
<template v-if="form.adType == '图片广告'">
<a-form-item label="广告图片" name="images" extra="请上传广告图片">
<SelectFile
:placeholder="`请选择图片`"
:limit="1"
:data="images"
@done="chooseFile"
@del="onDeleteItem"
/>
<!-- <ele-image-upload-->
<!-- v-model:value="images"-->
<!-- :limit="1"-->
<!-- :accept="'image/png,image/jpeg'"-->
<!-- :drag="true"-->
<!-- :item-style="{ maxWidth: '160px', maxHeight: '160px' }"-->
<!-- :upload-handler="uploadHandler"-->
<!-- @upload="onUpload"-->
<!-- />-->
</a-form-item>
</template>
<template v-if="form.adType == '视频广告'">
<a-form-item label="上传视频" name="images" extra="请上传视频文件仅支持mp4格式大小200M以内">
<SelectFile
:placeholder="`请选择视频文件`"
:limit="1"
:data="images"
@done="chooseFile"
@del="onDeleteItem"
/>
<!-- <ele-image-upload-->
<!-- v-model:value="images"-->
<!-- :limit="1"-->
<!-- :accept="'video/mp4'"-->
<!-- :drag="true"-->
<!-- :item-style="{ maxWidth: '160px', maxHeight: '160px' }"-->
<!-- :upload-handler="uploadHandler"-->
<!-- @upload="onUpload"-->
<!-- />-->
</a-form-item>
</template>
<a-form-item label="广告位名称" name="name">
<a-input
allow-clear
:maxlength="100"
placeholder="请输入广告位名称"
v-model:value="form.name"
/>
</a-form-item>
<a-form-item label="广告位描述" name="comments">
<a-textarea
:rows="4"
:maxlength="200"
placeholder="请输入广告位描述"
v-model:value="form.comments"
/>
</a-form-item>
<a-form-item label="路由/链接地址" name="path">
<a-input
allow-clear
:maxlength="100"
placeholder="请输入路由/链接地址"
v-model:value="form.path"
/>
</a-form-item>
<a-form-item label="排序号" name="sortNumber">
<a-input-number
:min="0"
:max="9999"
class="ele-fluid"
placeholder="请输入排序号"
v-model:value="form.sortNumber"
/>
</a-form-item>
<a-form-item label="状态" name="status">
<a-radio-group v-model:value="form.status">
<a-radio :value="0">开启</a-radio>
<a-radio :value="1">关闭</a-radio>
</a-radio-group>
</a-form-item>
</a-form>
</ele-modal>
</template>
<script lang="ts" setup>
import { ref, reactive, watch } from 'vue';
import { Form, message } from 'ant-design-vue';
import {assignObject, isChinese} from 'ele-admin-pro';
import { addAd, updateAd } from '@/api/cms/ad';
import { Ad } from '@/api/cms/ad/model';
import { useThemeStore } from '@/store/modules/theme';
import { storeToRefs } from 'pinia';
import {FormInstance, RuleObject} from 'ant-design-vue/es/form';
import { ItemType } from 'ele-admin-pro/es/ele-image-upload/types';
import { uploadFile } from '@/api/system/file';
import {FileRecord} from "@/api/system/file/model";
// 是否是修改
const isUpdate = ref(false);
const useForm = Form.useForm;
// 是否开启响应式布局
const themeStore = useThemeStore();
const { styleResponsive } = storeToRefs(themeStore);
const props = defineProps<{
// 弹窗是否打开
visible: boolean;
// 修改回显的数据
data?: Ad | null;
}>();
const emit = defineEmits<{
(e: 'done'): void;
(e: 'update:visible', visible: boolean): void;
}>();
// 提交状态
const loading = ref(false);
// 已上传数据
const images = ref<ItemType[]>([]);
// 是否显示最大化切换按钮
const maxable = ref(true);
// 表格选中数据
const formRef = ref<FormInstance | null>(null);
// 用户信息
const form = reactive<Ad>({
adId: undefined,
name: '',
adType: '图片广告',
images: '',
width: '',
height: '',
path: '',
type: '',
status: 0,
comments: '',
sortNumber: 100
});
/* 更新visible */
const updateVisible = (value: boolean) => {
emit('update:visible', value);
};
// 表单验证规则
const rules = reactive({
name: [
{
required: true,
type: 'string',
message: '请填写广告位名称',
trigger: 'blur'
}
],
adType: [
{
required: true,
type: 'string',
message: '请选择广告类型',
trigger: 'blur'
}
],
images: [
{
required: true,
type: 'string',
message: '请上传图片或视频',
trigger: 'blur',
validator: async (_rule: RuleObject, value: string) => {
if (images.value.length == 0) {
return Promise.reject('请上传图片或视频');
}
return Promise.resolve();
}
}
]
});
const { resetFields } = useForm(form, rules);
/* 上传事件 */
const uploadHandler = (file: File) => {
const item: ItemType = {
file,
uid: (file as any).uid,
name: file.name
};
if (file.type.startsWith('video')) {
if (file.size / 1024 / 1024 > 200) {
message.error('大小不能超过 200MB');
return;
}
}
if (file.type.startsWith('image')) {
if (file.size / 1024 / 1024 > 5) {
message.error('大小不能超过 5MB');
return;
}
}
onUpload(item);
};
// 上传文件
const onUpload = (item: any) => {
const { file } = item;
uploadFile(file)
.then((data) => {
images.value.push({
uid: data.id,
url: file.type == 'video/mp4' ? 'https://oss.wsdns.cn/20240301/6e4e32cb808245d4be336b9486961145.png' : data.path,
status: 'done'
});
})
.catch((e) => {
message.error(e.message);
});
};
const chooseFile = (data: FileRecord) => {
images.value.push({
uid: data.id,
url: data.path,
status: 'done'
});
form.images = data.path;
}
const onDeleteItem = (index: number) => {
images.value.splice(index,1)
form.images = '';
}
/* 保存编辑 */
const save = () => {
if (!formRef.value) {
return;
}
formRef.value
.validate()
.then(() => {
loading.value = true;
const formData = {
...form,
images: JSON.stringify(images.value)
};
const saveOrUpdate = isUpdate.value ? updateAd : addAd;
saveOrUpdate(formData)
.then((msg) => {
loading.value = false;
message.success(msg);
updateVisible(false);
emit('done');
})
.catch((e) => {
loading.value = false;
message.error(e.message);
});
})
.catch(() => {});
};
watch(
() => props.visible,
(visible) => {
if (visible) {
if (props.data) {
assignObject(form, props.data);
images.value = [];
if (props.data.images) {
const arr = JSON.parse(props.data.images);
arr.map((d) => {
images.value.push({
uid: d.uid,
url: d.url,
status: 'done'
});
});
}
isUpdate.value = true;
} else {
images.value = [];
isUpdate.value = false;
}
} else {
resetFields();
}
},
{ immediate: true }
);
</script>
<style lang="less">
.tab-pane {
min-height: 300px;
}
.ml-10 {
margin-left: 5px;
}
.upload-text {
margin-right: 70px;
}
.icon-bg {
width: 50px;
height: 50px;
display: block;
border-radius: 50px;
background: url('data:image/svg+xml;charset=utf8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20version%3D%221.1%22%3E%3Cdefs%3E%3ClinearGradient%20id%3D%221%22%20x1%3D%220%22%20x2%3D%221%22%20y1%3D%220%22%20y2%3D%220%22%20gradientTransform%3D%22matrix(6.123233995736766e-17%2C%201%2C%20-0.024693877551020406%2C%206.123233995736766e-17%2C%200.5%2C%200)%22%3E%3Cstop%20stop-color%3D%22%230a060d%22%20stop-opacity%3D%221%22%20offset%3D%220%22%3E%3C%2Fstop%3E%3Cstop%20stop-color%3D%22%23660061%22%20stop-opacity%3D%221%22%20offset%3D%220.95%22%3E%3C%2Fstop%3E%3C%2FlinearGradient%3E%3C%2Fdefs%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22url(%231)%22%3E%3C%2Frect%3E%3C%2Fsvg%3E');
}
</style>