msgstorage.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. let Disp = require("./Dispatcher.js");
  2. let msgPackager = require("./msgpackager.js");
  3. let msgType = require("./msgtype.js");
  4. let msgStorage = new Disp();
  5. let disp = require("./broadcast.js");
  6. msgStorage.saveReceiveMsg = function (receiveMsg, type) {
  7. let sendableMsg;
  8. if (type == msgType.IMAGE) {
  9. sendableMsg = {
  10. id: receiveMsg.id,
  11. type: type,
  12. body: {
  13. id: receiveMsg.id,
  14. from: receiveMsg.from,
  15. to: receiveMsg.to,
  16. type: receiveMsg.type,
  17. ext: receiveMsg.ext,
  18. chatType: receiveMsg.type,
  19. toJid: "",
  20. body: {
  21. type: type,
  22. url: receiveMsg.url,
  23. filename: receiveMsg.filename,
  24. filetype: receiveMsg.filetype,
  25. size: {
  26. width: receiveMsg.width,
  27. height: receiveMsg.height
  28. },
  29. },
  30. },
  31. };
  32. }
  33. else if (type == msgType.TEXT || type == msgType.EMOJI) {
  34. sendableMsg = {
  35. id: receiveMsg.id,
  36. type: type,
  37. body: {
  38. id: receiveMsg.id,
  39. from: receiveMsg.from,
  40. to: receiveMsg.to,
  41. type: receiveMsg.type,
  42. ext: receiveMsg.ext,
  43. chatType: receiveMsg.type,
  44. toJid: "",
  45. body: {
  46. type: type,
  47. msg: receiveMsg.data,
  48. },
  49. },
  50. value: receiveMsg.data
  51. };
  52. }
  53. else if (type == msgType.FILE) {
  54. sendableMsg = {
  55. id: receiveMsg.id,
  56. type: type,
  57. body: {
  58. id: receiveMsg.id,
  59. length: receiveMsg.file_length,
  60. from: receiveMsg.from,
  61. to: receiveMsg.to,
  62. type: receiveMsg.type,
  63. ext: receiveMsg.ext,
  64. chatType: receiveMsg.type,
  65. toJid: "",
  66. body: {
  67. type: type,
  68. url: receiveMsg.url,
  69. filename: receiveMsg.filename,
  70. msg: "当前不支持此格式消息展示",
  71. },
  72. },
  73. value: receiveMsg.data
  74. };
  75. }
  76. else if (type == msgType.AUDIO) {
  77. sendableMsg = {
  78. id: receiveMsg.id,
  79. type: type,
  80. accessToken: receiveMsg.token || receiveMsg.accessToken,
  81. body: {
  82. id: receiveMsg.id,
  83. length: receiveMsg.length,
  84. from: receiveMsg.from,
  85. to: receiveMsg.to,
  86. type: receiveMsg.type,
  87. ext: receiveMsg.ext,
  88. chatType: receiveMsg.type,
  89. toJid: "",
  90. body: {
  91. type: type,
  92. url: receiveMsg.url,
  93. filename: receiveMsg.filename,
  94. filetype: receiveMsg.filetype,
  95. from: receiveMsg.from,
  96. to: receiveMsg.to
  97. },
  98. },
  99. };
  100. }
  101. else if (type == msgType.VIDEO) {
  102. sendableMsg = {
  103. id: receiveMsg.id,
  104. type: type,
  105. accessToken: receiveMsg.token || receiveMsg.accessToken,
  106. body: {
  107. id: receiveMsg.id,
  108. length: receiveMsg.length,
  109. from: receiveMsg.from,
  110. to: receiveMsg.to,
  111. type: receiveMsg.type,
  112. ext: receiveMsg.ext,
  113. chatType: receiveMsg.type,
  114. toJid: "",
  115. body: {
  116. type: type,
  117. url: receiveMsg.url,
  118. filename: receiveMsg.filename,
  119. filetype: receiveMsg.filetype,
  120. from: receiveMsg.from,
  121. to: receiveMsg.to
  122. },
  123. },
  124. };
  125. }
  126. else {
  127. return;
  128. }
  129. this.saveMsg(sendableMsg, type, receiveMsg);
  130. };
  131. msgStorage.saveMsg = function (sendableMsg, type, receiveMsg) {
  132. //console.log('sendableMsgsendableMsg', sendableMsg)
  133. let me = this;
  134. let myName = wx.getStorageSync("myUsername");
  135. let sessionKey;
  136. // 仅用作群聊收消息,发消息没有 receiveMsg
  137. if (receiveMsg && receiveMsg.type == "groupchat") {
  138. sessionKey = receiveMsg.to + myName;
  139. }
  140. // 群聊发 & 单发 & 单收
  141. else {
  142. sessionKey = sendableMsg.body.from == myName
  143. ? sendableMsg.body.to + myName
  144. : sendableMsg.body.from + myName;
  145. }
  146. let curChatMsg = wx.getStorageSync(sessionKey) || [];
  147. let renderableMsg = msgPackager(sendableMsg, type, myName);
  148. if (type == msgType.AUDIO) {
  149. renderableMsg.msg.length = sendableMsg.body.length;
  150. renderableMsg.msg.token = sendableMsg.accessToken;
  151. }
  152. curChatMsg.push(renderableMsg);
  153. //console.log('renderableMsgrenderableMsg', renderableMsg)
  154. if (type == msgType.VIDEO) {
  155. renderableMsg.msg.token = sendableMsg.accessToken;
  156. //如果是音频则请求服务器转码
  157. // wx.downloadFile({
  158. // url: sendableMsg.body.body.url,
  159. // header: {
  160. // "X-Requested-With": "XMLHttpRequest",
  161. // Accept: "audio/mp3",
  162. // Authorization: "Bearer " + sendableMsg.accessToken
  163. // },
  164. // success(res){
  165. // // wx.playVoice({
  166. // // filePath: res.tempFilePath
  167. // // });
  168. // renderableMsg.msg.url = res.tempFilePath;
  169. // save();
  170. // },
  171. // fail(e){
  172. // console.log("downloadFile failed", e);
  173. // }
  174. // });
  175. }
  176. // else{
  177. // save();
  178. // }
  179. save();
  180. function save() {
  181. wx.setStorage({
  182. key: sessionKey,
  183. data: curChatMsg,
  184. success() {
  185. if (type == msgType.AUDIO || type == msgType.VIDEO) {
  186. disp.fire('em.chat.audio.fileLoaded');
  187. }
  188. me.fire("newChatMsg", renderableMsg, type, curChatMsg, sessionKey);
  189. }
  190. });
  191. }
  192. };
  193. module.exports = msgStorage;