重新封装上传图片组件、代码生成图片使用组件

This commit is contained in:
不做码农 2021-12-03 21:58:52 +08:00
parent 057aecee55
commit 1de4685ee5
5 changed files with 77 additions and 48 deletions

View File

@ -102,10 +102,6 @@ $if((item.HtmlType == "radio" || item.HtmlType == "select"))
${item.ColumnName}Options: [], ${item.ColumnName}Options: [],
$end $end
$end $end
$if(replaceDto.UploadFile == 1)
//文件上传地址
uploadUrl: process.env.VUE_APP_BASE_API + "upload/SaveFile",
$end
$if(genTable.SortField != "") $if(genTable.SortField != "")
// 是否显示编辑排序 // 是否显示编辑排序
showEditSort: false, showEditSort: false,
@ -225,28 +221,15 @@ $if(genTable.SortField != "")
}, },
$end $end
$if(replaceDto.UploadFile == 1) $if(replaceDto.UploadFile == 1)
//文件上传前判断方法 //图片上传成功方法
beforeFileUpload(file) { handleUploadSuccess(res, columnName) {
const isJPG = file.type === "image/jpeg"; console.log(columnName, res)
const isLt2M = file.size / 1024 / 1024 < 2; this.form[columnName] = res.data;
if (!isJPG) { console.log(JSON.stringify(this.form))
this.msgError("上传图片只能是 JPG 格式!");
}
if (!isLt2M) {
this.msgError("上传图片大小不能超过 2MB!");
}
return isJPG && isLt2M;
}, },
$end $end
$foreach(item in genTable.Columns) $foreach(item in genTable.Columns)
$if((item.HtmlType == "imageUpload" || item.HtmlType == "fileUpload")) $if((item.HtmlType == "radio" || item.HtmlType == "select") && item.DictType != "")
//${item.ColumnComment}上传成功方法
handleUpload${item.CsharpField}Success(res, file) {
this.form.$item.ColumnName = res.data;
// this.form.$item.ColumnName = URL.createObjectURL(file.raw);
// this.${refs}refs.upload.clearFiles();
},
$elseif((item.HtmlType == "radio" || item.HtmlType == "select") && item.DictType != "")
// ${item.ColumnComment}字典翻译 // ${item.ColumnComment}字典翻译
${item.ColumnName}Format(row, column) { ${item.ColumnName}Format(row, column) {
return this.selectDictLabel(this.${item.ColumnName}Options, row.${item.ColumnName}); return this.selectDictLabel(this.${item.ColumnName}Options, row.${item.ColumnName});

View File

@ -104,11 +104,12 @@ namespace ZR.CodeGenerator
//图片 //图片
sb.AppendLine(" <el-col :span=\"24\">"); sb.AppendLine(" <el-col :span=\"24\">");
sb.AppendLine($" <el-form-item label=\"{labelName}\" :label-width=\"labelWidth\" prop=\"{columnName}\">"); sb.AppendLine($" <el-form-item label=\"{labelName}\" :label-width=\"labelWidth\" prop=\"{columnName}\">");
sb.AppendLine($" <el-upload class=\"avatar-uploader\" name=\"file\" :action=\"uploadUrl\" :show-file-list=\"false\" :on-success=\"handleUpload{dbFieldInfo.CsharpField}Success\" :before-upload=\"beforeFileUpload\">"); //sb.AppendLine($" <el-upload class=\"avatar-uploader\" name=\"file\" :action=\"uploadUrl\" :show-file-list=\"false\" :on-success=\"handleUpload{dbFieldInfo.CsharpField}Success\" :before-upload=\"beforeFileUpload\">");
sb.AppendLine($" <el-image v-if=\"form.{columnName}\" :src=\"form.{columnName}\" class=\"icon\"/>"); //sb.AppendLine($" <el-image v-if=\"form.{columnName}\" :src=\"form.{columnName}\" class=\"icon\"/>");
sb.AppendLine(" <i v-else class=\"el-icon-plus uploader-icon\"></i>"); //sb.AppendLine(" <i v-else class=\"el-icon-plus uploader-icon\"></i>");
sb.AppendLine(" </el-upload>"); //sb.AppendLine(" </el-upload>");
sb.AppendLine($" <el-input v-model=\"form.{columnName}\" placeholder=\"请上传文件或手动输入文件地址\"></el-input>"); //sb.AppendLine($" <el-input v-model=\"form.{columnName}\" placeholder=\"请上传文件或手动输入文件地址\"></el-input>");
sb.AppendLine($@" <UploadImage :icon=""form.{columnName}"" column='{columnName}' :key=""form.{columnName}"" @handleUploadSuccess=""handleUploadSuccess"" />");
sb.AppendLine(" </el-form-item>"); sb.AppendLine(" </el-form-item>");
sb.AppendLine(" </el-col>"); sb.AppendLine(" </el-col>");
} }

View File

@ -1,18 +1,9 @@
<template> <template>
<div class="component-upload-image"> <div class="component-upload-image">
<el-upload <el-upload :action="uploadImgUrl" :on-success="handleUploadSuccess" :before-upload="handleBeforeUpload" :on-error="handleUploadError" name="file"
:action="uploadImgUrl" :show-file-list="false" :headers="headers" style="display: inline-block; vertical-align: top">
list-type="picture-card" <el-image v-if="imageUrl" :src="imageUrl" class="icon" />
:on-success="handleUploadSuccess" <i v-else class="el-icon-plus uploader-icon"></i>
:before-upload="handleBeforeUpload"
:on-error="handleUploadError"
name="file"
:show-file-list="false"
:headers="headers"
style="display: inline-block; vertical-align: top"
>
<el-image v-if="value" :src="value" class="avatar" />
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload> </el-upload>
</div> </div>
</template> </template>
@ -21,27 +12,78 @@
import { getToken } from "@/utils/auth"; import { getToken } from "@/utils/auth";
export default { export default {
components: {}, name: "UploadImage",
data() { data() {
return { return {
uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // uploadImgUrl: process.env.VUE_APP_BASE_API + this.uploadUrl, //
headers: { headers: {
Authorization: "Bearer " + getToken(), Authorization: "Bearer " + getToken(),
}, },
imageUrl: "",
}; };
}, },
props: { props: {
value: { icon: {
type: String, type: String,
default: "",
}, },
// form
column: { type: String },
//
uploadUrl: {
type: String,
default: "Common/UploadFile",
},
// , ['png', 'jpg', 'jpeg']
fileType: {
type: Array,
default: () => ["png", "jpg", "jpeg"],
},
// (MB)
fileSize: {
type: Number,
default: 5,
},
},
mounted() {
this.imageUrl = this.icon;
}, },
methods: { methods: {
handleUploadSuccess(res) { handleUploadSuccess(res) {
this.$emit("input", res.url); this.$emit(`handleUploadSuccess`, res, this.column);
this.imageUrl = res.data;
this.loading.close(); this.loading.close();
}, },
handleBeforeUpload() { // loading
handleBeforeUpload(file) {
console.log(file)
let isImg = false;
if (this.fileType.length) {
let fileExtension = "";
if (file.name.lastIndexOf(".") > -1) {
fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
}
isImg = this.fileType.some((type) => {
if (file.type.indexOf(type) > -1) return true;
if (fileExtension && fileExtension.indexOf(type) > -1) return true;
return false;
});
} else {
isImg = file.type.indexOf("image") > -1;
}
if (!isImg) {
this.msgError(
`文件格式不正确, 请上传${this.fileType.join("/")}图片格式文件!`
);
return false;
}
if (this.fileSize) {
const isLt = file.size / 1024 / 1024 < this.fileSize;
if (!isLt) {
this.msgError(`上传头像图片大小不能超过 ${this.fileSize} MB!`);
return false;
}
}
this.loading = this.$loading({ this.loading = this.$loading({
lock: true, lock: true,
text: "上传中", text: "上传中",

View File

@ -27,6 +27,8 @@ import Editor from "@/components/Editor";
import DictTag from '@/components/DictTag' import DictTag from '@/components/DictTag'
// 字典数据组件 // 字典数据组件
// import DictData from '@/components/DictData' // import DictData from '@/components/DictData'
// 上传图片
import UploadImage from '@/components/UploadImage/index';
// 全局方法挂载 // 全局方法挂载
Vue.prototype.getDicts = getDicts Vue.prototype.getDicts = getDicts
@ -56,6 +58,7 @@ Vue.component('Pagination', Pagination)
Vue.component('RightToolbar', RightToolbar) Vue.component('RightToolbar', RightToolbar)
Vue.component('DictTag', DictTag) Vue.component('DictTag', DictTag)
Vue.component('Editor', Editor) Vue.component('Editor', Editor)
Vue.component('UploadImage', UploadImage)
Vue.use(permission) Vue.use(permission)
Vue.use(Element, { Vue.use(Element, {

View File

@ -8,7 +8,7 @@ export function getToken() {
} }
export function setToken(token) { export function setToken(token) {
console.log('set token=' + token) // console.log('set token=' + token)
return Cookies.set(TokenKey, token) return Cookies.set(TokenKey, token)
} }