ChatImg.js 6.2 KB
//*
// 聊天上传图片
// */

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;