msgstorage.js 4.11 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
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
let Disp = require("../../../utils/Dispatcher");
let msgPackager = require("msgpackager");
let msgType = require("msgtype");
let msgStorage = new Disp();
let disp = require("../../../utils/broadcast");
msgStorage.saveReceiveMsg = function(receiveMsg, type){
let sendableMsg;
console.log('测试测试MSGStorage',receiveMsg)
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: 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("hxName");
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) || [];
console.log('缓存内容',curChatMsg)
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.AUDIO){
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(){
console.log(curChatMsg)
if (type == msgType.AUDIO || type == msgType.VIDEO) {
disp.fire('em.chat.audio.fileLoaded');
}
me.fire("newChatMsg", renderableMsg, type, curChatMsg, sessionKey);
}
});
}
};

module.exports = msgStorage;