This commit is contained in:
quantulr
2022-10-19 00:22:39 +08:00
parent e2da1fd45b
commit 88e3b00298
44 changed files with 1827 additions and 1324 deletions

View File

@ -20,18 +20,36 @@
<!-- 上传提示 -->
<div class="el-upload__tip" v-if="showTip">
请上传
<template v-if="fileSize"> 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b> </template>
<template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b> </template>
<template v-if="fileSize">
大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b>
</template>
<template v-if="fileType">
格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b>
</template>
的文件
</div>
<!-- 文件列表 -->
<transition-group class="upload-file-list el-upload-list el-upload-list--text" name="el-fade-in-linear" tag="ul">
<li :key="file.uid" class="el-upload-list__item ele-upload-list__item-content" v-for="(file, index) in fileList">
<el-link :href="`${baseUrl}${file.url}`" :underline="false" target="_blank">
<transition-group
class="upload-file-list el-upload-list el-upload-list--text"
name="el-fade-in-linear"
tag="ul"
>
<li
:key="file.uid"
class="el-upload-list__item ele-upload-list__item-content"
v-for="(file, index) in fileList"
>
<el-link
:href="`${baseUrl}${file.url}`"
:underline="false"
target="_blank"
>
<span class="el-icon-document"> {{ getFileName(file.name) }} </span>
</el-link>
<div class="ele-upload-list__item-content-action">
<el-link :underline="false" @click="handleDelete(index)" type="danger">删除</el-link>
<el-link :underline="false" @click="handleDelete(index)" type="danger"
>删除</el-link
>
</div>
</li>
</transition-group>
@ -61,8 +79,8 @@ const props = defineProps({
// 是否显示提示
isShowTip: {
type: Boolean,
default: true
}
default: true,
},
});
const { proxy } = getCurrentInstance();
@ -71,30 +89,35 @@ const number = ref(0);
const uploadList = ref([]);
const baseUrl = import.meta.env.VITE_APP_BASE_API;
const uploadFileUrl = ref(import.meta.env.VITE_APP_BASE_API + "/common/upload"); // 上传的图片服务器地址
const headers = ref({ Authorization: "Bearer " + getToken() });
// const headers = ref({ Authorization: "Bearer " + getToken() });
const headers = ref({});
const fileList = ref([]);
const showTip = computed(
() => props.isShowTip && (props.fileType || props.fileSize)
);
watch(() => props.modelValue, val => {
if (val) {
let temp = 1;
// 首先将值转为数组
const list = Array.isArray(val) ? val : props.modelValue.split(',');
// 然后将数组转为对象数组
fileList.value = list.map(item => {
if (typeof item === "string") {
item = { name: item, url: item };
}
item.uid = item.uid || new Date().getTime() + temp++;
return item;
});
} else {
fileList.value = [];
return [];
}
},{ deep: true, immediate: true });
watch(
() => props.modelValue,
(val) => {
if (val) {
let temp = 1;
// 首先将值转为数组
const list = Array.isArray(val) ? val : props.modelValue.split(",");
// 然后将数组转为对象数组
fileList.value = list.map((item) => {
if (typeof item === "string") {
item = { name: item, url: item };
}
item.uid = item.uid || new Date().getTime() + temp++;
return item;
});
} else {
fileList.value = [];
return [];
}
},
{ deep: true, immediate: true }
);
// 上传前校检格式和大小
function handleBeforeUpload(file) {
@ -110,7 +133,9 @@ function handleBeforeUpload(file) {
return false;
});
if (!isTypeOk) {
proxy.$modal.msgError(`文件格式不正确, 请上传${props.fileType.join("/")}格式文件!`);
proxy.$modal.msgError(
`文件格式不正确, 请上传${props.fileType.join("/")}格式文件!`
);
return false;
}
}
@ -141,7 +166,9 @@ function handleUploadError(err) {
function handleUploadSuccess(res, file) {
uploadList.value.push({ name: res.fileName, url: res.fileName });
if (uploadList.value.length === number.value) {
fileList.value = fileList.value.filter(f => f.url !== undefined).concat(uploadList.value);
fileList.value = fileList.value
.filter((f) => f.url !== undefined)
.concat(uploadList.value);
uploadList.value = [];
number.value = 0;
emit("update:modelValue", listToString(fileList.value));
@ -169,11 +196,11 @@ function listToString(list, separator) {
let strs = "";
separator = separator || ",";
for (let i in list) {
if(undefined !== list[i].url) {
if (undefined !== list[i].url) {
strs += list[i].url + separator;
}
}
return strs != '' ? strs.substr(0, strs.length - 1) : '';
return strs != "" ? strs.substr(0, strs.length - 1) : "";
}
</script>

View File

@ -49,8 +49,10 @@
</el-form>
</template>
<script setup>
import { launch } from "@/api/config";
const { proxy } = getCurrentInstance();
// import { launch } from "@/api/config";
import { pubMessage } from "@/api/website/message";
import { ElMessage } from "element-plus";
// const { proxy } = getCurrentInstance();
const data = reactive({
form: {},
rules: {
@ -77,14 +79,18 @@ function reset() {
mobile: undefined,
email: undefined,
};
proxy.resetForm("formRef");
if (formRef.value) {
formRef.value.resetFields();
}
}
const formRef = ref();
/** 提交按钮 */
function submitForm() {
proxy.$refs["formRef"].validate((valid) => {
formRef.value.validate((valid) => {
if (valid) {
launch(form.value).then((res) => {
proxy.$modal.msgSuccess(res.message);
pubMessage(form.value).then((res) => {
// proxy.$modal.msgSuccess(res.message);
ElMessage.success("发送成功");
reset();
});
}