msglist.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. let historyChatMsgsShow = historyChatMsgs;
  107. // 排除消息中的定位信息
  108. for (let i = 0; i < historyChatMsgsShow.length; i++) {
  109. if (historyChatMsgsShow[i].msg.data[0].data == 'location') {
  110. console.log('msg', historyChatMsgsShow[i].msg.data[0].data);
  111. historyChatMsgsShow.splice(i, 1);
  112. i--;
  113. }
  114. }
  115. console.info('historyChatMsgsShow', historyChatMsgsShow);
  116. let curChatMsgShow = curChatMsg;
  117. for (let i = 0; i < curChatMsgShow.length; i++) {
  118. if (curChatMsgShow[i].msg.data[0].data == 'location') {
  119. console.log('msg', curChatMsgShow[i].msg.data[0].data);
  120. curChatMsgShow.splice(i, 1);
  121. i--;
  122. }
  123. }
  124. console.info('curChatMsgShow', curChatMsgShow);
  125. if (isnew == 'newMsg') {
  126. this.setData({
  127. chatMsg: this.data.chatMsg.concat(curChatMsgShow),
  128. // 跳到最后一条
  129. toView: historyChatMsgsShow[historyChatMsgsShow.length - 1].mid,
  130. });
  131. } else {
  132. this.setData({
  133. chatMsg: historyChatMsgsShow.slice(-10),
  134. // 跳到最后一条
  135. toView: historyChatMsgsShow[historyChatMsgsShow.length - 1].mid,
  136. });
  137. }
  138. console.info('chatMsg:', this.data.chatMsg);
  139. wx.setStorageSync("rendered_" + sessionKey, historyChatMsgs);
  140. let chatMsg = wx.getStorageSync(sessionKey) || [];
  141. chatMsg.map(function (item, index) {
  142. curChatMsg.map(function (item2, index2) {
  143. if (item2.mid == item.mid) {
  144. chatMsg.splice(index, 1)
  145. }
  146. })
  147. })
  148. wx.setStorageSync(sessionKey, chatMsg);
  149. Index = historyChatMsgs.slice(-10).length;
  150. wx.pageScrollTo({
  151. scrollTop: 5000,
  152. duration: 300,
  153. })
  154. this.triggerEvent('render')
  155. if (isFail) {
  156. //this.renderFail(sessionKey)
  157. }
  158. },
  159. renderFail(sessionKey) {
  160. let me = this
  161. let msgList = me.data.chatMsg
  162. msgList.map((item) => {
  163. if (item.mid.substring(item.mid.length - 10) == curMsgMid.substring(curMsgMid.length - 10)) {
  164. item.msg.data[0].isFail = true
  165. item.isFail = true
  166. me.setData({
  167. chatMsg: msgList
  168. })
  169. }
  170. })
  171. if (me.curChatMsg[0].mid == curMsgMid) {
  172. me.curChatMsg[0].msg.data[0].isShow = false;
  173. me.curChatMsg[0].isShow = false
  174. }
  175. wx.setStorageSync("rendered_" + sessionKey, msgList);
  176. isFail = false
  177. },
  178. onFullscreenchange(e) {
  179. disp.fire('em.message.fullscreenchange', e.detail)
  180. }
  181. },
  182. // lifetimes
  183. created() {
  184. },
  185. attached() {
  186. this.__visibility__ = true;
  187. page = 0;
  188. Index = 0;
  189. },
  190. moved() { },
  191. detached() {
  192. this.__visibility__ = false;
  193. msgStorage.off("newChatMsg", this.dispMsg)
  194. },
  195. ready(event) {
  196. let me = this;
  197. if (getApp().globalData.isIPX) {
  198. this.setData({
  199. isIPX: true
  200. })
  201. }
  202. let username = this.data.username;
  203. let myUsername = wx.getStorageSync("myUsername");
  204. let sessionKey = username.groupId
  205. ? username.groupId + myUsername
  206. : username.your + myUsername;
  207. let chatMsg = wx.getStorageSync(sessionKey) || [];
  208. this.renderMsg(null, null, chatMsg, sessionKey);
  209. wx.setStorageSync(sessionKey, null);
  210. // disp.on("em.chat.sendSuccess", function(mid){
  211. // curMsgMid = mid
  212. // let msgList = me.data.chatMsg
  213. // msgList.map((item) =>{
  214. // if (item.mid.substring(item.mid.length - 10) == mid.substring(mid.length - 10)) {
  215. // item.msg.data[0].isSuc = true
  216. // item.isSuc = true
  217. // me.setData({
  218. // chatMsg: msgList
  219. // })
  220. // }
  221. // })
  222. // if (me.curChatMsg[0].mid == curMsgMid) {
  223. // me.curChatMsg[0].msg.data[0].isShow = true
  224. // me.curChatMsg[0].isShow = true
  225. // }
  226. // wx.setStorageSync("rendered_" + sessionKey, msgList);
  227. // console.log('msgList', msgList)
  228. // })
  229. disp.on('em.xmpp.error.sendMsgErr', function (errMsgs) {
  230. // curMsgMid = err.data.mid
  231. isFail = true
  232. // return
  233. let msgList = me.data.chatMsg
  234. msgList.map((item) => {
  235. for (let item2 in errMsgs) {
  236. if (item.mid.substring(item.mid.length - 10) == item2.substring(item2.length - 10)) {
  237. item.msg.data[0].isFail = true
  238. item.isFail = true
  239. me.setData({
  240. chatMsg: msgList
  241. })
  242. }
  243. }
  244. })
  245. // if (me.curChatMsg[0].mid == curMsgMid) {
  246. // me.curChatMsg[0].msg.data[0].isShow = false;
  247. // me.curChatMsg[0].isShow = false
  248. // }
  249. wx.setStorageSync("rendered_" + sessionKey, msgList);
  250. // disp.fire('em.megList.refresh')
  251. });
  252. msgStorage.on("newChatMsg", function dispMsg(renderableMsg, type, curChatMsg, sesskey) {
  253. me.curChatMsg = curChatMsg;
  254. if (!me.__visibility__) return;
  255. // 判断是否属于当前会话
  256. if (username.groupId) {
  257. // 群消息的 to 是 id,from 是 name
  258. if (renderableMsg.info.from == username.groupId || renderableMsg.info.to == username.groupId) {
  259. if (sesskey == sessionKey) {
  260. me.renderMsg(renderableMsg, type, curChatMsg, sessionKey, 'newMsg');
  261. }
  262. }
  263. }
  264. else if (renderableMsg.info.from == username.your || renderableMsg.info.to == username.your) {
  265. if (sesskey == sessionKey) {
  266. me.renderMsg(renderableMsg, type, curChatMsg, sessionKey, 'newMsg');
  267. }
  268. }
  269. });
  270. },
  271. })