差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录 前一修订版
后一修订版
前一修订版
后一修订版 两侧同时换到之后的修订记录
im:applet:message [2019/07/22 10:58]
zhangdong [发送附件消息]
im:applet:message [2020/03/21 13:08]
zhangdong [接收消息]
行 274: 行 274:
 <code javascript>​ <code javascript>​
 // 单聊发送图片消息 // 单聊发送图片消息
-var sendPrivateImg = function ​() { +upLoadImage(res){ 
-    var id conn.getUniqueId()                  // 生成本地消息id +    var me this
-    var msg new WebIM.message('​img',​ id)       // 创建图片消息 +    var tempFilePaths ​res.tempFilePaths
-    var input document.getElementById('​image'​); ​ // 选择图片的input +    var token WebIM.conn.context.accessToken 
-    ​var file = WebIM.utils.getFileUrl(input);      // 将图片转化为二进制文件 +    ​wx.getImageInfo({ 
-    var allowType = { +        src: res.tempFilePaths[0],​ 
-        '​jpg': true, +        success(res){ 
-        '​gif': true, +     ​var allowType = { 
-        '​png': true, + jpg: true, 
-        '​bmp': true + gif: true, 
-    }; + png: true, 
-    ​if ​(file.filetype.toLowerCase() in allowType) { + bmp: true 
-        var option = +     ​}; 
-            ​apiUrlWebIM.config.apiURL+     var str = WebIM.config.appkey.split("#"​);​ 
-            filefile+     var width = res.width; 
-            to'​username'​                      // 接收消息对象 +     var height = res.height;​ 
-            ​roomTypefalse+     var index = res.path.lastIndexOf("​."​);​ 
-            ext{},                              // 扩展消息 +     var filetype = (~index && res.path.slice(index + 1)) || "";​ 
-            ​onFileUploadError:​ function ​() {      // 消息上传失败 +     if(filetype.toLowerCase() in allowType){ 
-                ​console.log('​onFileUploadError'​); + wx.uploadFile(
-            }, +     url"​https://​a1.easemob.com/" + str[0] + "/"​ + str[1] + "/​chatfiles"​
-            ​onFileUploadCompletefunction ​(  // 消息上传成功 +     filePathtempFilePaths[0]
-                ​console.log('​onFileUploadComplete'​); +     name"​file"​
-            }+     header
-            success: function () {                // 消息发送成功 +         "​Content-Type":​ "​multipart/​form-data"​
-                ​console.log('Success'); +         Authorization"​Bearer " + token 
-            }, +     ​}, 
-            ​flashUpload:​ WebIM.flashUpload +     success(res){ 
-        ​}; +         var data = res.data; 
-        msg.set(option); +         var dataObj = JSON.parse(data);​ 
-        conn.send(msg.body);​ +         var id = WebIM.conn.getUniqueId();​ // 生成本地消息 ​id 
-    +         var msg = new WebIM.message(msgType.IMAGE,​ id); 
-};+         var file = { 
 +     type: msgType.IMAGE,​ 
 +     size: { 
 +         width: width, 
 +         height: height 
 +     ​}, 
 +             urldataObj.uri + "/"​ + dataObj.entities[0].uuid,​ 
 +     filetype: filetype, 
 +     filename: tempFilePaths[0] 
 +         }; 
 +         msg.set({ 
 +     apiUrl: WebIM.config.apiURL,​ 
 +             body: file, 
 +     from: me.data.username.myName,​ 
 +     to: me.getSendToParam(), 
 +     roomType: false
 +     chatType: me.data.chatType,​ 
 +     ​success: function (argument) { 
 +         disp.fire('em.chat.sendSuccess', id); 
 +     ​
 +         ​})
 +         if(me.data.chatType == msgType.chatType.CHAT_ROOM){ 
 +     ​msg.setGroup("​groupchat"​); 
 +         } 
 +         WebIM.conn.send(msg.body);​ 
 +     ​
 +         ​}); 
 +     } 
 +        } 
 +    }); 
 +}
 </​code>​ </​code>​
  
行 312: 行 342:
 <code javascript>​ <code javascript>​
 // 单聊发送文件消息 // 单聊发送文件消息
-var sendPrivateFile = function ​() { +uploadRecord(tempFilePath){ 
-    var id conn.getUniqueId(); ​                  // 生成本地消息id +    var str = WebIM.config.appkey.split("#"​); 
-    var msg = new WebIM.message('​file',​ id);        // 创建文件消息 +    var me this
-    var input document.getElementById('​file'​) // 选择文件的input +    var token = WebIM.conn.context.accessToken 
-    var file = WebIM.utils.getFileUrl(input); ​     // 将文件转化为二进制文件 +    ​wx.uploadFile(
-    ​var allowType = +        ​url: "https://​a1.easemob.com/"​ + str[0] + "/"​ + str[1] + "/​chatfiles"​
-        ​'​jpg'​true+        ​filePathtempFilePath
-        ​'​gif'​true+        ​name"​file"​
-        ​'​png'​true+        ​header{ 
-        ​'​bmp'​true, +            "​Content-Type"​"​multipart/​form-data"​
-        '​zip'​true+            ​Authorization"​Bearer " + token 
-        '​txt'​true, +        ​}
-        ​'​doc'​:true+        ​success(res){ 
-        ​'​pdf':​ true +            var id = WebIM.conn.getUniqueId()
-    }+            var msg = new WebIM.message(msgType.AUDIO, id); 
-    ​if ​(file.filetype.toLowerCase(in allowType) { +            var dataObj ​JSON.parse(res.data);​ 
-        var option ​{ +            ​msg.set({ 
-            apiUrl: WebIM.config.apiURL,​ +                ​apiUrl: WebIM.config.apiURL,​ 
-            filefile+                ​accessTokentoken
-            to'​username'​                      /接收消息对象 +                body
-            ​roomTypefalse+                    type: msgType.FILE, 
-            ext{},                              // 扩展消息 +                    url: dataObj.uri + "/" + dataObj.entities[0].uuid,​ 
-            ​onFileUploadErrorfunction () {      // 消息上传失败 +                    ​filetype""​
-                ​console.log('​onFileUploadError'​); +                    ​filenametempFilePath,​ 
-            }+                    accessToken:​ token, 
-            ​onFileUploadComplete: function () {   // 消息上传成功 +                    length: ""​ 
-                ​console.log('onFileUploadComplete'); +                ​}, 
-            }, +                fromme.data.username.myName,​ 
-            ​success:​ function ​() {                // 消息发送成功 +                ​to: me.getSendToParam(), 
-                ​console.log('​Success'​); +                ​roomType:​ false
-            }, +                ​chatType:​ me.data.chatType,​ 
-            ​flashUpload:​ WebIM.flashUpload +                success: function (argument) { 
-        }+                    disp.fire('em.chat.sendSuccess', id); 
-        msg.set(option);​ +                
-        ​conn.send(msg.body);​ +           }); 
-    }+           ​if(me.isGroupChat()){ 
 +               msg.setGroup("​groupchat"​); 
 +           ​
 +           msg.body.length = ""​
 +           WebIM.conn.send(msg.body);​ 
 +       ​} 
 +   });
 }; };
 </​code>​ </​code>​
行 354: 行 390:
 <code javascript>​ <code javascript>​
 // 单聊发送音频消息 // 单聊发送音频消息
-var sendPrivateAudio = function ​() { +uploadRecord(tempFilePath,​ dur){ 
-    var id conn.getUniqueId();                   // 生成本地消息id +    var str WebIM.config.appkey.split("#"​); 
-    var msg new WebIM.message('​audio',​ id)     // 创建音频消息 +    var me this
-    var input document.getElementById('​audio'​);  ​// 选择音频的input +    var token WebIM.conn.context.accessToken 
-    var file = WebIM.utils.getFileUrl(input); ​     ​// 将音频转化为二进制文件 +    wx.uploadFile(
-    var allowType = { +        url: "​https:​//a1.easemob.com/" + str[0] + "/" + str[1] + "/​chatfiles",​ 
-        ​'​mp3'​true+        ​filePathtempFilePath
-        ​'​amr'​true+        ​name"​file"​
-        ​'​wmv'​true +        ​header{ 
-    }; +            "​Content-Type":​ "​multipart/​form-data",​ 
-    ​if ​(file.filetype.toLowerCase() in allowType{ +            Authorization:​ "​Bearer " + token 
-        var option ​{ +        ​}, 
-            apiUrl: WebIM.config.apiURL,​ +        ​success(res){ 
-            filefile+            var id = WebIM.conn.getUniqueId()
-            to'​username'​                      ​// 接收消息对象 +            var msg = new WebIM.message(msgType.AUDIO,​ id); 
-            ​roomTypefalse+            var dataObj ​JSON.parse(res.data);​ 
-            ext{}                             // 扩展消息 +            ​msg.set({ 
-            ​onFileUploadErrorfunction () {      // 消息上传失败 +                ​apiUrl: WebIM.config.apiURL,​ 
-                ​console.log('​onFileUploadError'​); +                ​accessTokentoken
-            }, +                body
-            ​onFileUploadCompletefunction () {   // 消息上传成功 +                    type: msgType.AUDIO, 
-                ​console.log('​onFileUploadComplete'​); +                    url: dataObj.uri + "/" + dataObj.entities[0].uuid,​ 
-            }+                    ​filetype""​
-            success: function () {                // 消息发送成功 +                    ​filenametempFilePath
-                ​console.log('Success'); +                    ​accessTokentoken, 
-            }, +                    ​length:​ Math.ceil(dur / 1000
-            ​flashUpload:​ WebIM.flashUpload +                }, 
-        }+                fromme.data.username.myName,​ 
-        msg.set(option); +                ​to: me.getSendToParam(), 
-        conn.send(msg.body);​ +                ​roomType:​ false
-    }+                ​chatType:​ me.data.chatType,​ 
 +                ​success: function (argument) { 
 +                    disp.fire('em.chat.sendSuccess', id); 
 +                
 +           }); 
 +           if(me.isGroupChat()){ 
 +               msg.setGroup("​groupchat"​)
 +           } 
 +           msg.body.length = Math.ceil(dur / 1000); 
 +           //​console.log('​发送的语音消息',​ msg.body) 
 +           ​WebIM.conn.send(msg.body);​ 
 +       ​} 
 +   });
 }; };
 </​code>​ </​code>​
  
-单聊发送视频消息示例如下: 
-<code javascript>​ 
-// 单聊发送视频消息 
-var sendPrivateVideo = function () { 
-    var id = conn.getUniqueId(); ​                  // 生成本地消息id 
-    var msg = new WebIM.message('​video',​ id);      // 创建视频消息 
-    var input = document.getElementById('​video'​); ​ // 选择视频的input 
-    var file = WebIM.utils.getFileUrl(input); ​     // 将视频转化为二进制文件 
-    var allowType = { 
-        '​mp4':​ true, 
-        '​wmv':​ true, 
-        '​avi':​ true, 
-        '​rmvb':​true,​ 
-        '​mkv':​true 
-    }; 
-    if (file.filetype.toLowerCase() in allowType) { 
-        var option = { 
-            apiUrl: WebIM.config.apiURL,​ 
-            file: file, 
-            to: '​username', ​                      // 接收消息对象 
-            roomType: false, 
-            ext: {},                              // 扩展消息 
-            onFileUploadError:​ function () {      // 消息上传失败 
-                console.log('​onFileUploadError'​);​ 
-            }, 
-            onFileUploadComplete:​ function () {   // 消息上传成功 
-                console.log('​onFileUploadComplete'​);​ 
-            }, 
-            success: function () {                // 消息发送成功 
-                console.log('​Success'​);​ 
-            }, 
-            flashUpload:​ WebIM.flashUpload 
-        }; 
-        msg.set(option);​ 
-        conn.send(msg.body);​ 
-    } 
-}; 
-</​code>​ 
  
 === API === === API ===
行 482: 行 492:
 }); });
 </​code>​ </​code>​
- 
-**注意:** 
-  * 如果 ''​isAutoLogin''​ 设置为 false ,那么必须手动设置上线,否则无法收消息。手动上线指的是调用 ''​conn.setPresence()''​ ; 
-  ​ 
-  * 如果 conn 初始化时已将 ''​isAutoLogin''​ 设置为 true ,则无需调用 ''​conn.setPresence()''​。 
  
 === API === === API ===