123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- let Disp = require("./Dispatcher.js");
- let msgPackager = require("./msgpackager.js");
- let msgType = require("./msgtype.js");
- let msgStorage = new Disp();
- let disp = require("./broadcast.js");
- msgStorage.saveReceiveMsg = function (receiveMsg, type) {
- let sendableMsg;
- if (type == msgType.IMAGE) {
- sendableMsg = {
- id: receiveMsg.id,
- type: type,
- body: {
- id: receiveMsg.id,
- from: receiveMsg.from,
- to: receiveMsg.to,
- type: receiveMsg.type,
- ext: receiveMsg.ext,
- chatType: receiveMsg.type,
- toJid: "",
- body: {
- type: type,
- url: receiveMsg.url,
- filename: receiveMsg.filename,
- filetype: receiveMsg.filetype,
- size: {
- width: receiveMsg.width,
- height: receiveMsg.height
- },
- },
- },
- };
- }
- else if (type == msgType.TEXT || type == msgType.EMOJI) {
- sendableMsg = {
- id: receiveMsg.id,
- type: type,
- body: {
- id: receiveMsg.id,
- from: receiveMsg.from,
- to: receiveMsg.to,
- type: receiveMsg.type,
- ext: receiveMsg.ext,
- chatType: receiveMsg.type,
- toJid: "",
- body: {
- type: type,
- msg: receiveMsg.data,
- },
- },
- value: receiveMsg.data
- };
- }
- else if (type == msgType.FILE) {
- sendableMsg = {
- id: receiveMsg.id,
- type: type,
- body: {
- id: receiveMsg.id,
- length: receiveMsg.file_length,
- from: receiveMsg.from,
- to: receiveMsg.to,
- type: receiveMsg.type,
- ext: receiveMsg.ext,
- chatType: receiveMsg.type,
- toJid: "",
- body: {
- type: type,
- url: receiveMsg.url,
- filename: receiveMsg.filename,
- msg: "当前不支持此格式消息展示",
- },
- },
- value: receiveMsg.data
- };
- }
- else if (type == msgType.AUDIO) {
- sendableMsg = {
- id: receiveMsg.id,
- type: type,
- accessToken: receiveMsg.token || receiveMsg.accessToken,
- body: {
- id: receiveMsg.id,
- length: receiveMsg.length,
- from: receiveMsg.from,
- to: receiveMsg.to,
- type: receiveMsg.type,
- ext: receiveMsg.ext,
- chatType: receiveMsg.type,
- toJid: "",
- body: {
- type: type,
- url: receiveMsg.url,
- filename: receiveMsg.filename,
- filetype: receiveMsg.filetype,
- from: receiveMsg.from,
- to: receiveMsg.to
- },
- },
- };
- }
- else if (type == msgType.VIDEO) {
- sendableMsg = {
- id: receiveMsg.id,
- type: type,
- accessToken: receiveMsg.token || receiveMsg.accessToken,
- body: {
- id: receiveMsg.id,
- length: receiveMsg.length,
- from: receiveMsg.from,
- to: receiveMsg.to,
- type: receiveMsg.type,
- ext: receiveMsg.ext,
- chatType: receiveMsg.type,
- toJid: "",
- body: {
- type: type,
- url: receiveMsg.url,
- filename: receiveMsg.filename,
- filetype: receiveMsg.filetype,
- from: receiveMsg.from,
- to: receiveMsg.to
- },
- },
- };
- }
- else {
- return;
- }
- this.saveMsg(sendableMsg, type, receiveMsg);
- };
- msgStorage.saveMsg = function (sendableMsg, type, receiveMsg) {
- //console.log('sendableMsgsendableMsg', sendableMsg)
- let me = this;
- let myName = wx.getStorageSync("myUsername");
- let sessionKey;
- // 仅用作群聊收消息,发消息没有 receiveMsg
- if (receiveMsg && receiveMsg.type == "groupchat") {
- sessionKey = receiveMsg.to + myName;
- }
- // 群聊发 & 单发 & 单收
- else {
- sessionKey = sendableMsg.body.from == myName
- ? sendableMsg.body.to + myName
- : sendableMsg.body.from + myName;
- }
- let curChatMsg = wx.getStorageSync(sessionKey) || [];
- let renderableMsg = msgPackager(sendableMsg, type, myName);
- if (type == msgType.AUDIO) {
- renderableMsg.msg.length = sendableMsg.body.length;
- renderableMsg.msg.token = sendableMsg.accessToken;
- }
- curChatMsg.push(renderableMsg);
- //console.log('renderableMsgrenderableMsg', renderableMsg)
- if (type == msgType.VIDEO) {
- renderableMsg.msg.token = sendableMsg.accessToken;
- //如果是音频则请求服务器转码
- // wx.downloadFile({
- // url: sendableMsg.body.body.url,
- // header: {
- // "X-Requested-With": "XMLHttpRequest",
- // Accept: "audio/mp3",
- // Authorization: "Bearer " + sendableMsg.accessToken
- // },
- // success(res){
- // // wx.playVoice({
- // // filePath: res.tempFilePath
- // // });
- // renderableMsg.msg.url = res.tempFilePath;
- // save();
- // },
- // fail(e){
- // console.log("downloadFile failed", e);
- // }
- // });
- }
- // else{
- // save();
- // }
- save();
- function save() {
- wx.setStorage({
- key: sessionKey,
- data: curChatMsg,
- success() {
- if (type == msgType.AUDIO || type == msgType.VIDEO) {
- disp.fire('em.chat.audio.fileLoaded');
- }
- me.fire("newChatMsg", renderableMsg, type, curChatMsg, sessionKey);
- }
- });
- }
- };
- module.exports = msgStorage;
|