(component): upload 组件 i18n 兼容

This commit is contained in:
就眠儀式 2022-10-26 01:40:33 +08:00
parent 48991abdc0
commit 0ab1ae66ca
4 changed files with 87 additions and 94 deletions

View File

@ -22,6 +22,7 @@ import { templateRef } from "@vueuse/core";
import { LayLayer } from "@layui/layer-vue"; import { LayLayer } from "@layui/layer-vue";
import LayButton from "../button/index.vue"; import LayButton from "../button/index.vue";
import Cropper from "cropperjs"; import Cropper from "cropperjs";
import { useI18n } from "../../language";
export interface LayerButton { export interface LayerButton {
text: string; text: string;
@ -83,7 +84,7 @@ const getCutDownResult = () => {
commonUploadTransaction([newFile]); commonUploadTransaction([newFile]);
nextTick(() => clearAllCutEffect()); nextTick(() => clearAllCutEffect());
} else { } else {
errorF(cutInitErrorMsg); errorF(cutInitErrorMsg.value);
} }
}; };
@ -99,21 +100,34 @@ const clearAllCutEffect = () => {
innerCutVisible.value = false; innerCutVisible.value = false;
}; };
let defaultCutLayerOption: LayerModal = { const { t } = useI18n();
title: "标题", const text = computed(() => t('upload.text'));
move: true, const dragText = computed(() => t('upload.dragText'));
maxmin: false, const defaultErrorMsg = computed(() => t('upload.defaultErrorMsg'));
offset: [], const urlErrorMsg = computed(() => t('upload.urlErrorMsg'));
btn: [ const numberErrorMsg = computed(() => t('upload.numberErrorMsg'));
{ text: "导出", callback: getCutDownResult }, const occurFileSizeErrorMsg = computed(() => t('upload.occurFileSizeErrorMsg'));
{ text: "取消", callback: closeCutDownModal }, const cutInitErrorMsg = computed(() => t('upload.cutInitErrorMsg'));
], const uploadSuccess = computed(() => t('upload.uploadSuccess'));
area: ["640px", "640px"], const startUploadMsg = computed(() => t('upload.startUploadMsg'));
content: "11", const cannotSupportCutMsg = computed(() => t('upload.cannotSupportCutMsg'))
shade: true, const title = computed(() => t('upload.title'))
shadeClose: true, const confirmBtn = computed(() => t('upload.confirmBtn'))
type: "component", const cancelBtn = computed(() => t('upload.cancelBtn'))
};
let defaultCutLayerOption = computed<LayerModal>(() => {
return {
type: "component",
title: title.value,
shade: true,
shadeClose: true,
area: ["640px", "640px"],
btn: [
{ text: confirmBtn.value, callback: getCutDownResult },
{ text: cancelBtn.value, callback: closeCutDownModal },
],
}
});
const props = withDefaults(defineProps<UploadProps>(), { const props = withDefaults(defineProps<UploadProps>(), {
field: "file", field: "file",
@ -153,16 +167,9 @@ if (props.cutOptions && props.cutOptions.layerOption) {
Object.assign(defaultCutLayerOption, props.cutOptions.layerOption) Object.assign(defaultCutLayerOption, props.cutOptions.layerOption)
); );
} else { } else {
computedCutLayerOption = computed(() => defaultCutLayerOption); computedCutLayerOption = computed(() => defaultCutLayerOption.value);
} }
const defaultErrorMsg = "上传失败";
const urlErrorMsg = "上传地址格式不合法";
const numberErrorMsg = "文件上传超过规定的个数";
const sizeErrorMsg = "文件大小超过限制";
const cutInitErrorMsg = "剪裁插件初始化失败";
const uploadSuccess = "上传成功";
interface localUploadTransaction { interface localUploadTransaction {
url: string; url: string;
files: File[] | Blob[]; files: File[] | Blob[];
@ -180,7 +187,7 @@ const localUploadTransaction = (option: localUploadTransaction) => {
const { url, files } = option; const { url, files } = option;
let formData = new FormData(); let formData = new FormData();
if (url.length <= 5) { if (url.length <= 5) {
errorF(urlErrorMsg); errorF(urlErrorMsg.value);
return; return;
} }
if (Array.isArray(files) && files.length > 0) { if (Array.isArray(files) && files.length > 0) {
@ -222,7 +229,7 @@ const errorF = (errorText: string) => {
let errorMsg = errorText ? errorText : defaultErrorMsg; let errorMsg = errorText ? errorText : defaultErrorMsg;
errorMsg = `layui-vue:${errorMsg}`; errorMsg = `layui-vue:${errorMsg}`;
console.warn(errorMsg); console.warn(errorMsg);
layer.msg(errorMsg, { icon: 2, time: 1000 }, function (res: unknown) {}); layer.msg(errorMsg, { icon: 2, time: 1000 }, function (res: unknown) { });
emit("error", Object.assign({ currentTimeStamp, msg: errorMsg })); emit("error", Object.assign({ currentTimeStamp, msg: errorMsg }));
}; };
@ -240,7 +247,7 @@ const localUpload = (option: localUploadOption, callback: Function) => {
xhr.status === 304 || xhr.status === 304 ||
xhr.status == 0 xhr.status == 0
) { ) {
let successText = "上传开始"; let successText = startUploadMsg.value;
emit( emit(
"before", "before",
Object.assign({ currentTimeStamp, msg: successText, ...option }) Object.assign({ currentTimeStamp, msg: successText, ...option })
@ -286,7 +293,7 @@ const uploadChange = (e: any) => {
e.preventDefault(); e.preventDefault();
const _files = [...(e.target.files || e.dataTransfer.files)]; const _files = [...(e.target.files || e.dataTransfer.files)];
if (props.multiple && props.number != 0 && props.number < _files.length) { if (props.multiple && props.number != 0 && props.number < _files.length) {
errorF(numberErrorMsg); errorF(numberErrorMsg.value);
return; return;
} }
if (props.size && props.size != 0) { if (props.size && props.size != 0) {
@ -294,9 +301,7 @@ const uploadChange = (e: any) => {
let _file = _files[i]; let _file = _files[i];
let _size = _file.size; let _size = _file.size;
if (_size > props.size * 1024) { if (_size > props.size * 1024) {
errorF( errorF(occurFileSizeErrorMsg.value);
`文件 ${_file.name} ${sizeErrorMsg},文件最大不可超过${props.size}KB`
);
return; return;
} }
} }
@ -327,9 +332,7 @@ const uploadChange = (e: any) => {
}, 400); }, 400);
} else { } else {
if (arm2) { if (arm2) {
console.warn( console.warn(cannotSupportCutMsg.value);
"当前版本暂不支持单次多文件剪裁,尝试设置 multiple 为 false, 通过 @done 获取返回文件对象"
);
} }
commonUploadTransaction(_files); commonUploadTransaction(_files);
} }
@ -392,84 +395,44 @@ onUnmounted(() => {
</script> </script>
<template> <template>
<div <div class="layui-upload layui-upload-wrap" :class="disabledPreview ? 'layui-upload-file-disabled' : ''">
class="layui-upload layui-upload-wrap" <input type="file" class="layui-upload-file" ref="orgFileInput" :name="field" :field="field" :multiple="multiple"
:class="disabledPreview ? 'layui-upload-file-disabled' : ''" :accept="acceptMime" :disabled="disabled" @click="clickOrgInput" @change="uploadChange" />
>
<input
type="file"
class="layui-upload-file"
ref="orgFileInput"
:name="field"
:field="field"
:multiple="multiple"
:accept="acceptMime"
:disabled="disabled"
@click="clickOrgInput"
@change="uploadChange"
/>
<div v-if="!drag"> <div v-if="!drag">
<div class="layui-upload-btn-box" @click.stop="chooseFile"> <div class="layui-upload-btn-box" @click.stop="chooseFile">
<template v-if="slot.default"> <template v-if="slot.default">
<slot :disabled="disabled"></slot> <slot :disabled="disabled"></slot>
</template> </template>
<template v-else> <template v-else>
<lay-button type="primary" :disabled="disabled">上传文件</lay-button> <lay-button type="primary" :disabled="disabled">{{ text }}</lay-button>
</template> </template>
</div> </div>
</div> </div>
<div <div v-else ref="dragRef" class="layui-upload-drag" :class="
v-else disabled
ref="dragRef" ? 'layui-upload-drag-disable'
class="layui-upload-drag" : isDragEnter
:class="
disabled
? 'layui-upload-drag-disable'
: isDragEnter
? 'layui-upload-drag-draging' ? 'layui-upload-drag-draging'
: '' : ''
" " @click.stop="chooseFile">
@click.stop="chooseFile"
>
<i class="layui-icon"></i> <i class="layui-icon"></i>
<p>点击上传或将文件拖拽到此处</p> <p>{{ dragText }}</p>
<div class="layui-hide" id="uploadDemoView"> <div class="layui-hide" id="uploadDemoView">
<hr /> <hr />
<img src="" alt="上传成功后渲染" style="max-width: 196px" /> <img src="" alt="上传成功后渲染" style="max-width: 196px" />
</div> </div>
</div> </div>
<lay-layer <lay-layer v-model="innerCutVisible" :title="computedCutLayerOption.title" :move="computedCutLayerOption.move"
v-model="innerCutVisible" :resize="computedCutLayerOption.resize" :shade="computedCutLayerOption.shade"
:title="computedCutLayerOption.title" :shadeClose="computedCutLayerOption.shadeClose" :shadeOpacity="computedCutLayerOption.shadeOpacity"
:move="computedCutLayerOption.move" :zIndex="computedCutLayerOption.zIndex" :btnAlign="computedCutLayerOption.btnAlign"
:resize="computedCutLayerOption.resize" :area="computedCutLayerOption.area" :anim="computedCutLayerOption.anim"
:shade="computedCutLayerOption.shade" :isOutAnim="computedCutLayerOption.isOutAnim" :btn="computedCutLayerOption.btn" @close="clearAllCutEffect">
:shadeClose="computedCutLayerOption.shadeClose" <div class="copper-container" v-for="(base64str, index) in activeUploadFilesImgs" :key="`file${index}`">
:shadeOpacity="computedCutLayerOption.shadeOpacity" <img :src="base64str" :id="`_lay_upload_img${index}`" class="_lay_upload_img" />
:zIndex="computedCutLayerOption.zIndex"
:btnAlign="computedCutLayerOption.btnAlign"
:area="computedCutLayerOption.area"
:anim="computedCutLayerOption.anim"
:isOutAnim="computedCutLayerOption.isOutAnim"
:btn="computedCutLayerOption.btn"
@close="clearAllCutEffect"
>
<div
class="copper-container"
v-for="(base64str, index) in activeUploadFilesImgs"
:key="`file${index}`"
>
<img
:src="base64str"
:id="`_lay_upload_img${index}`"
class="_lay_upload_img"
/>
</div> </div>
</lay-layer> </lay-layer>
<div <div class="layui-upload-list" :class="disabledPreview ? 'layui-upload-list-disabled' : ''">
class="layui-upload-list"
:class="disabledPreview ? 'layui-upload-list-disabled' : ''"
>
<slot name="preview"></slot> <slot name="preview"></slot>
</div> </div>
</div> </div>

View File

@ -45,4 +45,19 @@ export default {
empty: { empty: {
description: "No data", description: "No data",
}, },
upload: {
text: "Upload files",
dragText: "Click Upload or drag the file here",
defaultErrorMsg: "Upload failed",
urlErrorMsg: "The upload address format is illegal",
numberErrorMsg: "The number of files uploaded exceeds the specified number",
cutInitErrorMsg: "Clipping plug-in initialization failed",
uploadSuccess: "Upload succeeded",
cannotSupportCutMsg: "The current version does not support single multiple file clipping. Try to set multiple to false, and get the returned file object through @ done",
occurFileSizeErrorMsg: "File size warning,The maximum file size cannot exceed target KB",
startUploadMsg: "Upload Start",
confirmBtn: "confirm",
cancelBtn: "cancel",
title: "title",
},
}; };

View File

@ -45,4 +45,19 @@ export default {
empty: { empty: {
description: "无数据", description: "无数据",
}, },
upload: {
text: "上传文件",
dragText: "点击上传,或将文件拖拽到此处",
defaultErrorMsg: "上传失败",
urlErrorMsg: "上传地址格式不合法",
numberErrorMsg: "文件上传超过规定的个数",
cutInitErrorMsg: "剪裁插件初始化失败",
uploadSuccess: "上传成功",
cannotSupportCutMsg: "当前版本暂不支持单次多文件剪裁,尝试设置 multiple 为 false, 通过 @done 获取返回文件对象",
occurFileSizeErrorMsg: "文件大小超过限制,文件最大不可超过传入的指定size属性的KB数",
startUploadMsg: "开始上传",
confirmBtn: "确认",
cancelBtn: "取消",
title: "标题",
},
}; };

View File

@ -203,7 +203,7 @@ export default {
::: demo 使用 `lay-upload` 标签, 添加 `cut` 开启 选择文件后剪裁功能 ::: demo 使用 `lay-upload` 标签, 添加 `cut` 开启 选择文件后剪裁功能
<template> <template>
  <lay-upload @cutdone="getCutDone" @cutcancel="getCutCancel" :cut="true" :multiple="false" @done="getFileDone">   <lay-upload @cutdone="getCutDone" acceptMime="images" @cutcancel="getCutCancel" :cut="true" :multiple="false" @done="getFileDone">
<template #preview> <template #preview>
<div class="easy-wrap" v-if="cutUrl"> <div class="easy-wrap" v-if="cutUrl">
<img :src="cutUrl"/> <img :src="cutUrl"/>