123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- // comps/msglist/msglist.js
- let msgStorage = require("../../../utils/msgstorage");
- let disp = require("../../../utils/broadcast");
- let LIST_STATUS = {
- SHORT: "scroll_view_change",
- NORMAL: "scroll_view"
- };
- let page = 0;
- let Index = 0;
- let curMsgMid = ''
- let isFail = false
- Component({
- /**
- * 组件的属性列表
- */
- properties: {
- username: {
- type: Object,
- value: {},
- },
- },
- /**
- * 组件的初始数据
- */
- data: {
- imgPath: getApp().globalData.imgPath,
- view: LIST_STATUS.NORMAL,
- toView: "",
- chatMsg: [],
- __visibility__: false,
- },
- /**
- * 组件的方法列表
- */
- methods: {
- clickMsg(data) {
- if (data.currentTarget.dataset.msg.ext && data.currentTarget.dataset.msg.ext.action) {
- this.triggerEvent("clickMsg", data.currentTarget.dataset.msg)
- }
- },
- normalScroll() {
- this.setData({
- view: LIST_STATUS.NORMAL
- });
- },
- shortScroll() {
- this.setData({
- view: LIST_STATUS.SHORT
- });
- },
- onTap() {
- this.triggerEvent("msglistTap", null, { bubbles: true });
- },
- previewImage(event) {
- var url = event.target.dataset.url;
- wx.previewImage({
- urls: [url] // 需要预览的图片 http 链接列表
- });
- },
- getHistoryMsg() {
- let me = this
- let username = me.data.username;
- let myUsername = wx.getStorageSync("myUsername");
- let sessionKey = username.groupId ? username.groupId + myUsername : username.your + myUsername;
- let historyChatMsgs = wx.getStorageSync("rendered_" + sessionKey) || [];
- console.info(historyChatMsgs)
- if (Index < historyChatMsgs.length) {
- let timesMsgList = historyChatMsgs.slice(-Index - 10, -Index)
- this.setData({
- chatMsg: timesMsgList.concat(me.data.chatMsg),
- toView: timesMsgList[timesMsgList.length - 1].mid,
- });
- Index += timesMsgList.length;
- if (timesMsgList.length == 10) {
- page++
- }
- wx.stopPullDownRefresh()
- }
- console.info('chatMsg:', this.data.chatMsg);
- },
- renderMsg(renderableMsg, type, curChatMsg, sessionKey, isnew) {
- let me = this
- // 手动去重 这操作没必要
- // if (curChatMsg.length > 1) {
- // this.data.chatMsg.map(function(elem, index) {
- // curChatMsg.map(function(item, i) {
- // if(elem.mid == item.mid){
- // //me.data.chatMsg.splice(index, 1)
- // curChatMsg.splice(i, 1)
- // }
- // })
- // })
- // }
- var historyChatMsgs = wx.getStorageSync("rendered_" + sessionKey) || [];
- // if (curChatMsg.length) {
- // console.log(curMsgMid.substring(curMsgMid.length - 10) , curChatMsg[0].mid.substring(curChatMsg[0].mid.length - 10))
- // }
- // if(curChatMsg.length && curMsgMid.substring(curMsgMid.length - 10) == curChatMsg[curChatMsg.length - 1].mid.substring(curChatMsg[0].mid.length - 10)){
- // //curChatMsg[curChatMsg.length - 1].msg.data[0].isSuc = true
- // curChatMsg[curChatMsg.length - 1].isSuc = true
- // }
- historyChatMsgs = historyChatMsgs.concat(curChatMsg);
- //console.log('当前历史',renderableMsg)
- //console.log('历史消息', historyChatMsgs)
- if (!historyChatMsgs.length) return;
- let historyChatMsgsShow = historyChatMsgs;
- // 排除消息中的定位信息
- for (let i = 0; i < historyChatMsgsShow.length; i++) {
- if (historyChatMsgsShow[i].msg.data[0].data == 'location') {
- console.log('msg', historyChatMsgsShow[i].msg.data[0].data);
- historyChatMsgsShow.splice(i, 1);
- i--;
- }
- }
- console.info('historyChatMsgsShow', historyChatMsgsShow);
- let curChatMsgShow = curChatMsg;
- for (let i = 0; i < curChatMsgShow.length; i++) {
- if (curChatMsgShow[i].msg.data[0].data == 'location') {
- console.log('msg', curChatMsgShow[i].msg.data[0].data);
- curChatMsgShow.splice(i, 1);
- i--;
- }
- }
- console.info('curChatMsgShow', curChatMsgShow);
- if (isnew == 'newMsg') {
- this.setData({
- chatMsg: this.data.chatMsg.concat(curChatMsgShow),
- // 跳到最后一条
- toView: historyChatMsgsShow[historyChatMsgsShow.length - 1].mid,
- });
- } else {
- this.setData({
- chatMsg: historyChatMsgsShow.slice(-10),
- // 跳到最后一条
- toView: historyChatMsgsShow[historyChatMsgsShow.length - 1].mid,
- });
- }
- console.info('chatMsg:', this.data.chatMsg);
- wx.setStorageSync("rendered_" + sessionKey, historyChatMsgs);
- let chatMsg = wx.getStorageSync(sessionKey) || [];
- chatMsg.map(function (item, index) {
- curChatMsg.map(function (item2, index2) {
- if (item2.mid == item.mid) {
- chatMsg.splice(index, 1)
- }
- })
- })
- wx.setStorageSync(sessionKey, chatMsg);
- Index = historyChatMsgs.slice(-10).length;
- wx.pageScrollTo({
- scrollTop: 5000,
- duration: 300,
- })
- this.triggerEvent('render')
- if (isFail) {
- //this.renderFail(sessionKey)
- }
- },
- renderFail(sessionKey) {
- let me = this
- let msgList = me.data.chatMsg
- msgList.map((item) => {
- if (item.mid.substring(item.mid.length - 10) == curMsgMid.substring(curMsgMid.length - 10)) {
- item.msg.data[0].isFail = true
- item.isFail = true
- me.setData({
- chatMsg: msgList
- })
- }
- })
- if (me.curChatMsg[0].mid == curMsgMid) {
- me.curChatMsg[0].msg.data[0].isShow = false;
- me.curChatMsg[0].isShow = false
- }
- wx.setStorageSync("rendered_" + sessionKey, msgList);
- isFail = false
- },
- onFullscreenchange(e) {
- disp.fire('em.message.fullscreenchange', e.detail)
- }
- },
- // lifetimes
- created() {
- },
- attached() {
- this.__visibility__ = true;
- page = 0;
- Index = 0;
- },
- moved() { },
- detached() {
- this.__visibility__ = false;
- msgStorage.off("newChatMsg", this.dispMsg)
- },
- ready(event) {
- let me = this;
- if (getApp().globalData.isIPX) {
- this.setData({
- isIPX: true
- })
- }
- let username = this.data.username;
- let myUsername = wx.getStorageSync("myUsername");
- let sessionKey = username.groupId
- ? username.groupId + myUsername
- : username.your + myUsername;
- let chatMsg = wx.getStorageSync(sessionKey) || [];
- this.renderMsg(null, null, chatMsg, sessionKey);
- wx.setStorageSync(sessionKey, null);
- // disp.on("em.chat.sendSuccess", function(mid){
- // curMsgMid = mid
- // let msgList = me.data.chatMsg
- // msgList.map((item) =>{
- // if (item.mid.substring(item.mid.length - 10) == mid.substring(mid.length - 10)) {
- // item.msg.data[0].isSuc = true
- // item.isSuc = true
- // me.setData({
- // chatMsg: msgList
- // })
- // }
- // })
- // if (me.curChatMsg[0].mid == curMsgMid) {
- // me.curChatMsg[0].msg.data[0].isShow = true
- // me.curChatMsg[0].isShow = true
- // }
- // wx.setStorageSync("rendered_" + sessionKey, msgList);
- // console.log('msgList', msgList)
- // })
- disp.on('em.xmpp.error.sendMsgErr', function (errMsgs) {
- // curMsgMid = err.data.mid
- isFail = true
- // return
- let msgList = me.data.chatMsg
- msgList.map((item) => {
- for (let item2 in errMsgs) {
- if (item.mid.substring(item.mid.length - 10) == item2.substring(item2.length - 10)) {
- item.msg.data[0].isFail = true
- item.isFail = true
- me.setData({
- chatMsg: msgList
- })
- }
- }
- })
- // if (me.curChatMsg[0].mid == curMsgMid) {
- // me.curChatMsg[0].msg.data[0].isShow = false;
- // me.curChatMsg[0].isShow = false
- // }
- wx.setStorageSync("rendered_" + sessionKey, msgList);
- // disp.fire('em.megList.refresh')
- });
- msgStorage.on("newChatMsg", function dispMsg(renderableMsg, type, curChatMsg, sesskey) {
- me.curChatMsg = curChatMsg;
- if (!me.__visibility__) return;
- // 判断是否属于当前会话
- if (username.groupId) {
- // 群消息的 to 是 id,from 是 name
- if (renderableMsg.info.from == username.groupId || renderableMsg.info.to == username.groupId) {
- if (sesskey == sessionKey) {
- me.renderMsg(renderableMsg, type, curChatMsg, sessionKey, 'newMsg');
- }
- }
- }
- else if (renderableMsg.info.from == username.your || renderableMsg.info.to == username.your) {
- if (sesskey == sessionKey) {
- me.renderMsg(renderableMsg, type, curChatMsg, sessionKey, 'newMsg');
- }
- }
- });
- },
- })
|