上传附件并展示出扩展图标大小

master
taoshihan 2 years ago
parent 2e97afec06
commit 727704ab14

@ -211,14 +211,6 @@ func UploadImg(c *gin.Context) {
} }
} }
func UploadFile(c *gin.Context) { func UploadFile(c *gin.Context) {
SendAttachment, err := strconv.ParseBool(models.FindConfig("SendAttachment"))
if !SendAttachment || err != nil {
c.JSON(200, gin.H{
"code": 400,
"msg": "禁止上传附件!",
})
return
}
f, err := c.FormFile("realfile") f, err := c.FormFile("realfile")
if err != nil { if err != nil {
c.JSON(200, gin.H{ c.JSON(200, gin.H{
@ -250,6 +242,9 @@ func UploadFile(c *gin.Context) {
"msg": "上传成功!", "msg": "上传成功!",
"result": gin.H{ "result": gin.H{
"path": filepath, "path": filepath,
"ext": fileExt,
"size": f.Size,
"name": f.Filename,
}, },
}) })
} }

@ -378,6 +378,29 @@ a{color: #07a9fe;text-decoration: none;}
margin-right: 10px; margin-right: 10px;
cursor: pointer; cursor: pointer;
} }
.productCard{
cursor: pointer;
padding: 5px;
border-radius: 2px;
width: auto;
max-width: 730px;
display: flex;
border-radius: 5px;
}
.productCard img{
width: 100px;
height: 100px;
margin-right: 15px;
}
.productCard .productCardPrice{
color: #ff7736;
font-weight: bold;
font-size: 16px;
margin-top: 10px;
}
.productCard .productCardTitle{
color: #333;
}
@media screen and (min-width: 900px) { @media screen and (min-width: 900px) {
.chatCenter { .chatCenter {
max-height: 650px; max-height: 650px;

@ -430,7 +430,13 @@ new Vue({
type: 'error' type: 'error'
}); });
}else{ }else{
_this.messageContent+='file[/' + res.result.path + ']'; var data=JSON.stringify({
name:res.result.name,
ext:res.result.ext,
size:res.result.size,
path:'/' + res.result.path,
})
_this.messageContent+='attachment['+data+']';
_this.chatToUser(); _this.chatToUser();
} }
}, },

@ -103,33 +103,104 @@ function replaceContent (content,baseUrl) {// 转义聊天内容中的特殊字
baseUrl=""; baseUrl="";
} }
var faces=placeFace(); var faces=placeFace();
var html = function (end) { content = (content || '')
return new RegExp('\\n*\\[' + (end || '') + '(pre|div|span|p|table|thead|th|tbody|tr|td|ul|li|ol|li|dl|dt|dd|h2|h3|h4|h5)([\\s\\S]*?)\\]\\n*', 'g'); .replace(/face\[(.*?)\]/g, function (face) { // 转义表情
};
content = (content || '').replace(/&(?!#?[a-zA-Z0-9]+;)/g, '&')
.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/'/g, '&#39;').replace(/"/g, '&quot;') // XSS
.replace(/face\[([^\s\[\]]+?)\]/g, function (face) { // 转义表情
var alt = face.replace(/^face/g, ''); var alt = face.replace(/^face/g, '');
return '<img alt="' + alt + '" title="' + alt + '" src="'+baseUrl + faces[alt] + '">'; return '<img alt="' + alt + '" title="' + alt + '" src="'+baseUrl + faces[alt] + '">';
}) })
.replace(/img\[([^\s\[\]]+?)\]/g, function (face) { // 转义图片 .replace(/img\[(.*?)\]/g, function (face) { // 转义图片
var src = face.replace(/^img\[/g, '').replace(/\]/g, '');; var src = face.replace(/^img\[/g, '').replace(/\]/g, '');;
return '<img onclick="bigPic(src,true)" src="' +baseUrl+ src + '" style="max-width: 100%"/></div>'; return '<img onclick="bigPic(src,true)" src="' +baseUrl+ src + '" style="max-width: 150px"/></div>';
}) })
.replace(/file\[([^\s\[\]]+?)\]/g, function (face) { // 转义图片 .replace(/\n/g, '<br>'); // 转义换行
var src = face.replace(/^file\[/g, '').replace(/\]/g, '');; content=replaceAttachment(content);
return '<div class="folderBtn" onclick="window.open(\''+baseUrl+src+'\')" style="font-size:25px;"/></div>';
})
.replace(/\[([^\s\[\]]+?)\]+link\[([^\s\[\]]+?)\]/g, function (face) { // 转义超链接
var text = face.replace(/link\[.*?\]/g, '').replace(/\[|\]/g, '');
var src = face.replace(/^\[([^\s\[\]]+?)\]+link\[/g, '').replace(/\]/g, '');
return '<a href="'+src+'" target="_blank" />【'+text+'】</a>';
})
.replace(html(), '\<$1 $2\>').replace(html('/'), '\</$1\>') // 转移HTML代码
.replace(/\n/g, '<br>') // 转义换行
return content; return content;
} }
//替换附件展示
function replaceAttachment(str){
return str.replace(/attachment\[(.*?)\]/g, function (result) {
var mutiFiles=result.match(/attachment\[(.*?)\]/)
if (mutiFiles.length<2){
return result;
}
//return result;
var info=JSON.parse(mutiFiles[1])
var imgSrc="";
switch(info.ext){
case ".mp3":
imgSrc="/static/images/ext/MP3.png";
break;
case ".zip":
imgSrc="/static/images/ext/ZIP.png";
break;
case ".txt":
imgSrc="/static/images/ext/TXT.png";
break;
case ".7z":
imgSrc="/static/images/ext/7z.png";
break;
case ".bpm":
imgSrc="/static/images/ext/BMP.png";
break;
case ".png":
imgSrc="/static/images/ext/PNG.png";
break;
case ".jpg":
imgSrc="/static/images/ext/JPG.png";
break;
case ".jpeg":
imgSrc="/static/images/ext/JPEG.png";
break;
case ".pdf":
imgSrc="/static/images/ext/PDF.png";
break;
case ".doc":
imgSrc="/static/images/ext/DOC.png";
break;
case ".docx":
imgSrc="/static/images/ext/DOCX.png";
break;
case ".rar":
imgSrc="/static/images/ext/RAR.png";
break;
case ".xlsx":
imgSrc="/static/images/ext/XLSX.png";
break;
case ".csv":
imgSrc="/static/images/ext/XLSX.png";
break;
default:
imgSrc="/static/images/ext/default.png";
break;
}
var html= `<div onclick="window.open('`+info.path+`')" class="productCard">
<div><img src='`+imgSrc+`' style='width: 38px;height: 38px;' /></div>
<div class="productCardTitle">
<div class="productCardTitle">`+info.name+`</div>
<div style="font-size: 12px;color: #666">`+formatFileSize(info.size)+`</div>
</div>
</div>`;
return html;
})
}
function formatFileSize(fileSize) {
if (fileSize < 1024) {
return fileSize + 'B';
} else if (fileSize < (1024*1024)) {
var temp = fileSize / 1024;
temp = temp.toFixed(2);
return temp + 'KB';
} else if (fileSize < (1024*1024*1024)) {
var temp = fileSize / (1024*1024);
temp = temp.toFixed(2);
return temp + 'MB';
} else {
var temp = fileSize / (1024*1024*1024);
temp = temp.toFixed(2);
return temp + 'GB';
}
}
function bigPic(src,isVisitor){ function bigPic(src,isVisitor){
if (isVisitor) { if (isVisitor) {
window.open(src); window.open(src);

Loading…
Cancel
Save