msglist.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // comps/msglist/msglist.js
  2. let msgStorage = require("../../../utils/msgstorage");
  3. let disp = require("../../../utils/broadcast");
  4. let LIST_STATUS = {
  5. SHORT: "scroll_view_change",
  6. NORMAL: "scroll_view"
  7. };
  8. let page = 0;
  9. let Index = 0;
  10. let curMsgMid = ''
  11. let isFail = false
  12. Component({
  13. /**
  14. * 组件的属性列表
  15. */
  16. properties: {
  17. username: {
  18. type: Object,
  19. value: {},
  20. },
  21. },
  22. /**
  23. * 组件的初始数据
  24. */
  25. data: {
  26. imgPath: getApp().globalData.imgPath,
  27. view: LIST_STATUS.NORMAL,
  28. toView: "",
  29. chatMsg: [],
  30. __visibility__: false,
  31. },
  32. /**
  33. * 组件的方法列表
  34. */
  35. methods: {
  36. clickMsg(data) {
  37. if (data.currentTarget.dataset.msg.ext && data.currentTarget.dataset.msg.ext.action) {
  38. this.triggerEvent("clickMsg", data.currentTarget.dataset.msg)
  39. }
  40. },
  41. normalScroll() {
  42. this.setData({
  43. view: LIST_STATUS.NORMAL
  44. });
  45. },
  46. shortScroll() {
  47. this.setData({
  48. view: LIST_STATUS.SHORT
  49. });
  50. },
  51. onTap() {
  52. this.triggerEvent("msglistTap", null, { bubbles: true });
  53. },
  54. previewImage(event) {
  55. var url = event.target.dataset.url;
  56. wx.previewImage({
  57. urls: [url] // 需要预览的图片 http 链接列表
  58. });
  59. },
  60. getHistoryMsg() {
  61. let me = this
  62. let username = me.data.username;
  63. let myUsername = wx.getStorageSync("myUsername");
  64. let sessionKey = username.groupId ? username.groupId + myUsername : username.your + myUsername;
  65. let historyChatMsgs = wx.getStorageSync("rendered_" + sessionKey) || [];
  66. console.info(historyChatMsgs)
  67. if (Index < historyChatMsgs.length) {
  68. let timesMsgList = historyChatMsgs.slice(-Index - 10, -Index)
  69. this.setData({
  70. chatMsg: timesMsgList.concat(me.data.chatMsg),
  71. toView: timesMsgList[timesMsgList.length - 1].mid,
  72. });
  73. Index += timesMsgList.length;
  74. if (timesMsgList.length == 10) {
  75. page++
  76. }
  77. wx.stopPullDownRefresh()
  78. }
  79. console.info('chatMsg:', this.data.chatMsg);
  80. },
  81. renderMsg(renderableMsg, type, curChatMsg, sessionKey, isnew) {
  82. let me = this
  83. // 手动去重 这操作没必要
  84. // if (curChatMsg.length > 1) {
  85. // this.data.chatMsg.map(function(elem, index) {
  86. // curChatMsg.map(function(item, i) {
  87. // if(elem.mid == item.mid){
  88. // //me.data.chatMsg.splice(index, 1)
  89. // curChatMsg.splice(i, 1)
  90. // }
  91. // })
  92. // })
  93. // }
  94. var historyChatMsgs = wx.getStorageSync("rendered_" + sessionKey) || [];
  95. // if (curChatMsg.length) {
  96. // console.log(curMsgMid.substring(curMsgMid.length - 10) , curChatMsg[0].mid.substring(curChatMsg[0].mid.length - 10))
  97. // }
  98. // if(curChatMsg.length && curMsgMid.substring(curMsgMid.length - 10) == curChatMsg[curChatMsg.length - 1].mid.substring(curChatMsg[0].mid.length - 10)){
  99. // //curChatMsg[curChatMsg.length - 1].msg.data[0].isSuc = true
  100. // curChatMsg[curChatMsg.length - 1].isSuc = true
  101. // }
  102. historyChatMsgs = historyChatMsgs.concat(curChatMsg);
  103. //console.log('当前历史',renderableMsg)
  104. //console.log('历史消息', historyChatMsgs)
  105. if (!historyChatMsgs.length) return;
  106. if (isnew == 'newMsg') {
  107. this.setData({
  108. chatMsg: this.data.chatMsg.concat(curChatMsg),
  109. // 跳到最后一条
  110. toView: historyChatMsgs[historyChatMsgs.length - 1].mid,
  111. });
  112. } else {
  113. this.setData({
  114. chatMsg: historyChatMsgs.slice(-10),
  115. // 跳到最后一条
  116. toView: historyChatMsgs[historyChatMsgs.length - 1].mid,
  117. });
  118. }
  119. console.info('chatMsg:', this.data.chatMsg);
  120. wx.setStorageSync("rendered_" + sessionKey, historyChatMsgs);
  121. let chatMsg = wx.getStorageSync(sessionKey) || [];
  122. chatMsg.map(function (item, index) {
  123. curChatMsg.map(function (item2, index2) {
  124. if (item2.mid == item.mid) {
  125. chatMsg.splice(index, 1)
  126. }
  127. })
  128. })
  129. wx.setStorageSync(sessionKey, chatMsg);
  130. Index = historyChatMsgs.slice(-10).length;
  131. wx.pageScrollTo({
  132. scrollTop: 5000,
  133. duration: 300,
  134. })
  135. this.triggerEvent('render')
  136. if (isFail) {
  137. //this.renderFail(sessionKey)
  138. }
  139. },
  140. renderFail(sessionKey) {
  141. let me = this
  142. let msgList = me.data.chatMsg
  143. msgList.map((item) => {
  144. if (item.mid.substring(item.mid.length - 10) == curMsgMid.substring(curMsgMid.length - 10)) {
  145. item.msg.data[0].isFail = true
  146. item.isFail = true
  147. me.setData({
  148. chatMsg: msgList
  149. })
  150. }
  151. })
  152. if (me.curChatMsg[0].mid == curMsgMid) {
  153. me.curChatMsg[0].msg.data[0].isShow = false;
  154. me.curChatMsg[0].isShow = false
  155. }
  156. wx.setStorageSync("rendered_" + sessionKey, msgList);
  157. isFail = false
  158. },
  159. onFullscreenchange(e) {
  160. disp.fire('em.message.fullscreenchange', e.detail)
  161. }
  162. },
  163. // lifetimes
  164. created() {
  165. },
  166. attached() {
  167. this.__visibility__ = true;
  168. page = 0;
  169. Index = 0;
  170. },
  171. moved() { },
  172. detached() {
  173. this.__visibility__ = false;
  174. msgStorage.off("newChatMsg", this.dispMsg)
  175. },
  176. ready(event) {
  177. let me = this;
  178. if (getApp().globalData.isIPX) {
  179. this.setData({
  180. isIPX: true
  181. })
  182. }
  183. let username = this.data.username;
  184. let myUsername = wx.getStorageSync("myUsername");
  185. let sessionKey = username.groupId
  186. ? username.groupId + myUsername
  187. : username.your + myUsername;
  188. let chatMsg = wx.getStorageSync(sessionKey) || [];
  189. this.renderMsg(null, null, chatMsg, sessionKey);
  190. wx.setStorageSync(sessionKey, null);
  191. // disp.on("em.chat.sendSuccess", function(mid){
  192. // curMsgMid = mid
  193. // let msgList = me.data.chatMsg
  194. // msgList.map((item) =>{
  195. // if (item.mid.substring(item.mid.length - 10) == mid.substring(mid.length - 10)) {
  196. // item.msg.data[0].isSuc = true
  197. // item.isSuc = true
  198. // me.setData({
  199. // chatMsg: msgList
  200. // })
  201. // }
  202. // })
  203. // if (me.curChatMsg[0].mid == curMsgMid) {
  204. // me.curChatMsg[0].msg.data[0].isShow = true
  205. // me.curChatMsg[0].isShow = true
  206. // }
  207. // wx.setStorageSync("rendered_" + sessionKey, msgList);
  208. // console.log('msgList', msgList)
  209. // })
  210. disp.on('em.xmpp.error.sendMsgErr', function (errMsgs) {
  211. // curMsgMid = err.data.mid
  212. isFail = true
  213. // return
  214. let msgList = me.data.chatMsg
  215. msgList.map((item) => {
  216. for (let item2 in errMsgs) {
  217. if (item.mid.substring(item.mid.length - 10) == item2.substring(item2.length - 10)) {
  218. item.msg.data[0].isFail = true
  219. item.isFail = true
  220. me.setData({
  221. chatMsg: msgList
  222. })
  223. }
  224. }
  225. })
  226. // if (me.curChatMsg[0].mid == curMsgMid) {
  227. // me.curChatMsg[0].msg.data[0].isShow = false;
  228. // me.curChatMsg[0].isShow = false
  229. // }
  230. wx.setStorageSync("rendered_" + sessionKey, msgList);
  231. // disp.fire('em.megList.refresh')
  232. });
  233. msgStorage.on("newChatMsg", function dispMsg(renderableMsg, type, curChatMsg, sesskey) {
  234. me.curChatMsg = curChatMsg;
  235. if (!me.__visibility__) return;
  236. // 判断是否属于当前会话
  237. if (username.groupId) {
  238. // 群消息的 to 是 id,from 是 name
  239. if (renderableMsg.info.from == username.groupId || renderableMsg.info.to == username.groupId) {
  240. if (sesskey == sessionKey) {
  241. me.renderMsg(renderableMsg, type, curChatMsg, sessionKey, 'newMsg');
  242. }
  243. }
  244. }
  245. else if (renderableMsg.info.from == username.your || renderableMsg.info.to == username.your) {
  246. if (sesskey == sessionKey) {
  247. me.renderMsg(renderableMsg, type, curChatMsg, sessionKey, 'newMsg');
  248. }
  249. }
  250. });
  251. },
  252. })