ChatImg.js
6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//*
// 聊天上传图片
// */
import Emiter from "Emiter";
import Loger from "Loger";
import $ from "jquery";
import ClassDataProxy from "proxy/ClassDataProxy";
import UIControlApe from './UIControlApe';
import ErrorApe from './ErrorApe';
import MD5 from "md5";
let loger = Loger.getLoger('PC-ChatImg');
let fileItem;
class ChatImg extends Emiter {
constructor() {
super();
this.callback=null;
this.init();
}
init(){
$("#fileImgInput").on("change", this._uploadFile.bind(this));//上传截图
}
_uploadFile(evt){
fileItem=document.getElementById('fileImgInput').files[0];
if(!fileItem){
loger.log("没有选择文件");
return;
}
// this._closeDocListHandler();
let fileStr=fileItem.name;
let fileValue=fileStr.split('.')[1];
loger.log("文件名:" + fileItem.name + "->文件类型:" + fileItem.type);
//目前最大支持50M 格式为jpg png pdf docx pptx xlsx
let fileName=fileItem.name.toLowerCase();
//判断选择的文件类型
if(fileName.lastIndexOf('.jpg')<0&&
fileName.lastIndexOf('.jpeg')<0&&
fileName.lastIndexOf('.png')<0&&
fileName.lastIndexOf('.gif')<0&&
fileName.lastIndexOf('.bmp')<0){
ErrorApe.showError(ErrorApe.TYPE_3,ClassDataProxy.localConfig.layoutImg);
return;
}
//判断文件的大小
if(fileItem.size>(5*1024*1024)){
ErrorApe.showError(ErrorApe.TYPE_3,ClassDataProxy.localConfig.chatError);
return;
}
if(!ClassDataProxy.DOCServerIP){
loger.warn("截图服务器地址无效,无法上传文件!");
ErrorApe.showError(ErrorApe.TYPE_3,ClassDataProxy.localConfig.serverError);
return
}
UIControlApe.disEnabledDocBar();
UIControlApe.disEnabledMediaBar();
//UIControlApe.disEnabledLeftBar();
//UIControlApe.disEnabledBottomBar();
//UIControlApe.hideBottomDocControlBar();
let timestamp=ClassDataProxy.creatTimestamp();
let authId=MD5(ClassDataProxy.siteId+""+timestamp);///authId String MD5(siteId+timestamp)
//console.log("authId:"+authId,"siteId:"+ClassDataProxy.siteId,"timestamp:"+timestamp);
let fd = new FormData();
fd.append("fileToUpload", fileItem);
fd.append("filename", escape(fileItem.name));
fd.append("size", fileItem.size);
fd.append("siteId", ClassDataProxy.siteId); //站点
fd.append("timestamp", String(timestamp)); //时间戳
fd.append("authId",authId); //上传时间
fd.append("classId", String(ClassDataProxy.classId)); //课堂号
/* let ipport=ClassDataProxy.DOCServerIP;
if( ClassDataProxy.DOCServerPort){
ipport=ipport+":"+ClassDataProxy.DOCServerPort;
}
let mediaUploadUrl= `http://${ipport}/DocSharing/imageUpload`;
*/
let ipport=ClassDataProxy.DOCServerIP;
if( ClassDataProxy.DOCServerPort){
ipport=ipport+":"+ClassDataProxy.DOCServerPort;
}
let mediaUploadUrl= `${ClassDataProxy.locationProtocol+ipport}/DocSharing/imageUpload`;
$.ajax({
type: "POST",
url: mediaUploadUrl,
processData: false,
contentType: false,
data: fd,
xhr:function(){
let xhr = $.ajaxSettings.xhr();
if( xhr.upload) {
xhr.upload.addEventListener("progress" ,function(evt){
let loaded = evt.loaded; //已经上传大小情况
let tot = evt.total; //附件总大小
let per = Math.floor(100*loaded/tot); //已经上传的百分比
} , false);
return xhr;
}
},
success: function (data) {
UIControlApe.enabledDocBar();
UIControlApe.enabledMediaBar();
//UIControlApe.enabledLeftBar();
//UIControlApe.enabledBottomBar();
//UIControlApe.showBottomDocControlBar();
this._uploadFileSuccess(data);
$("#fileImgInput").val('');
}.bind(this),
error: function (data) {
UIControlApe.enabledDocBar();
UIControlApe.enabledMediaBar();
loger.warn(name+"->上传文件失败", data);
ErrorApe.showError(ErrorApe.TYPE_3,name+"->"+ClassDataProxy.localConfig.serverError);
$("#fileImgInput").val('')
}
});
}
//将上传文件的信息,传递给底层
_uploadFileSuccess(msg) {
loger.log("服务器返回的截图信息->", msg);
//{"code":0,"list":[{"name":"sintel (2).mp4","id":"7","url":"http://101.200.150.192:9090/media/h5dev/201706/1478111117/76303834c69cd86f6c6212ae3019b1d0.mp4"}]}
/*
//code
0:成功
1:不是上传请求
2:编码错误
3:无法解析请求内容
4:没有上传文件,或者上传文件超过10个
5:siteId 不能为空
6:authId 错误
7:文件类型不支持
8:保存文件信息失败
10:保存文件失败*/
if(msg&&msg.code==0){
//返回的数据中是一个数组,支持多个上传,目前只处理一个上传的情况
if(!msg){
loger.warn('上传文件返回的数据中数组没有数据',msg.list);
return;
}
let fileInfo = {};
fileInfo.fileName = fileItem.name;//伴音名字
fileInfo.url = msg.url; //图片绝对地址 默认值: null
if(this.callback){
this.callback(fileInfo);
}
//console.log("fileInfo", fileInfo);
//$("#txt_message").val(fileInfo.fileName);
//ClassDataProxy.imgInfo=fileInfo;
//$("#txt_message").focus();
}else {
loger.warn("截图上传失败->", msg.code);
ErrorApe.showError(ErrorApe.TYPE_3,'截图上传失败->code:'+msg.code);
}
}
}
export default ChatImg;