msgstorage.js 7.3 KB

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