123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- <template>
- <view
- scroll-y="true"
- :class="view + ' wrap ' + (isIPX?'scroll_view_X': '')"
- @tap="onTap"
- upper-threshold="-50"
- :scroll-into-view="toView"
- >
- <view class="message" v-for="item in chatMsg" :key="item.mid" :id="item.mid">
-
- {{ item.time }}
- <view class="main" :class="item.style">
- <view class="user">
-
- <text class="user-text">{{ item.time}}</text>
- </view>
- <image class="avatar" v-if="item.style=='self'" :src="item.user.avatarurl" />
- <image class="avatar" v-else :src="item.your.avatarurl" />
- <view class="msg">
- <image
- class="err"
- :class="(item.style == 'self' && item.isFail) ? 'show' : 'hide'"
- src="/static/images/msgerr.png"
- />
- <image
- v-if="item.style == 'self'"
- src="/static/images/poprightarrow2x.png"
- class="msg_poprightarrow"
- />
- <image
- v-if="item.style == ''"
- src="/static/images/popleftarrow2x.png"
- class="msg_popleftarrow"
- />
- <view v-if="item.msg.type == 'img' || item.msg.type == 'video'">
- <image
- v-if="item.msg.type == 'img'"
- class="avatar"
- :src="item.msg.data"
- style="width:90px; height:120px; margin:2px auto;"
- mode="aspectFit"
- @tap="previewImage"
- :data-url="item.msg.data"
- />
-
- </view>
- <audio-msg v-if="item.msg.type == 'audio'" :msg="item"></audio-msg>
- <view v-else-if="item.msg.type == 'txt' || item.msg.type == 'emoji'">
- <view class="template" v-for="(d_item, d_index) in item.msg.data" :key="d_index">
- <text
- :data-msg="item"
- @tap="clickMsg"
- v-if="d_item.type == 'txt'"
- class="msg-text"
- style="float:left;"
- selectable="true"
- >{{ d_item.data }}</text>
- <image
- v-if="d_item.type == 'emoji'"
- class="avatar"
- :src="'/static/images/faces/' + d_item.data"
- style="width:25px; height:25px; margin:0 0 2px 0; float:left;"
- />
- </view>
- </view>
- </view>
- </view>
- </view>
- </view>
-
- </template>
- <script>
- let msgStorage = require("../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;
- import audioMsg from "./type/audio/audio";
- export default {
- data() {
- return {
- view: LIST_STATUS.NORMAL,
- toView: "",
- chatMsg: [],
- __visibility__: false,
- isIPX: false
- };
- },
- components: {
- audioMsg
- },
- props: {
- username: {
- type: Object,
- default: () => ({})
- }
- },
-
- created() {},
- beforeMount() {
- this.__visibility__ = true;
- page = 0;
- Index = 0;
- },
- moved() {},
- destroyed() {
- this.__visibility__ = false;
- msgStorage.off("newChatMsg", this.dispMsg)
- },
- mounted(event) {
- let me = this;
- if (getApp().globalData.isIPX) {
- this.isIPX= true
- }
-
- this.username = uni.username;
- let username = this.username;
- let myUsername = uni.getStorageSync("myUsername");
- let sessionKey = username.groupId ? username.groupId + myUsername : username.your + myUsername;
- let chatMsg = uni.getStorageSync(sessionKey) || [];
- this.renderMsg(null, null, chatMsg, sessionKey);
- uni.setStorageSync(sessionKey, null);
- disp.on('em.error.sendMsgErr', function (err) {
-
- isFail = true;
-
- console.log('发送失败了');
- return;
- let msgList = me.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.chatMsg= msgList
- }
- });
-
-
-
-
- uni.setStorageSync("rendered_" + sessionKey, msgList);
- });
- msgStorage.on("newChatMsg", this.dispMsg);
- },
- methods: {
- normalScroll() {
- this.view= LIST_STATUS.NORMAL
- },
- dispMsg(renderableMsg, type, curChatMsg, sesskey) {
- let me = this;
- let username = this.username;
- let myUsername = uni.getStorageSync("myUsername");
- let sessionKey = username.groupId ? username.groupId + myUsername : username.your + myUsername;
- me.curChatMsg = curChatMsg;
-
- if (!me.__visibility__) return;
-
- if (username.groupId) {
-
- 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');
- }
- }
- },
- shortScroll() {
- this.view= LIST_STATUS.SHORT
- },
- onTap() {
- this.$emit("msglistTap", null, {
- bubbles: true
- });
- },
- previewImage(event) {
- var url = event.target.dataset.url;
- uni.previewImage({
- urls: [url]
- });
- },
- getHistoryMsg() {
- let me = this;
- let username = this.username;
- let myUsername = uni.getStorageSync("myUsername");
- let sessionKey = username.groupId ? username.groupId + myUsername : username.your + myUsername;
- let historyChatMsgs = uni.getStorageSync("rendered_" + sessionKey) || [];
- if (Index < historyChatMsgs.length) {
- let timesMsgList = historyChatMsgs.slice(-Index - 10, -Index);
- this.chatMsg= timesMsgList.concat(me.chatMsg),
- this.toView= timesMsgList[timesMsgList.length - 1].mid
- Index += timesMsgList.length;
- if (timesMsgList.length == 10) {
- page++;
- }
- uni.stopPullDownRefresh();
- }
- },
- renderMsg(renderableMsg, type, curChatMsg, sessionKey, isnew) {
- let me = this;
- var historyChatMsgs = uni.getStorageSync("rendered_" + sessionKey) || [];
- historyChatMsgs = historyChatMsgs.concat(curChatMsg);
-
- if (!historyChatMsgs.length) return;
- let user = {}
- let your = {}
- let myUsername = uni.getStorageSync('myUsername')
- uni.WebIM.conn.fetchUserInfoById([myUsername,me.username.your], ['avatarurl','nickname']).then((res) => {
- user = res.data[myUsername]
- your = res.data[me.username.your]
- console.log('用户信息',res);
- historyChatMsgs.map(function(item,index) {
- item.user = user
- item.your = your
- })
- if (isnew == 'newMsg') {
- this.chatMsg= this.chatMsg.concat(curChatMsg)
-
- this.toView= historyChatMsgs[historyChatMsgs.length - 1].mid
- } else {
- this.chatMsg= historyChatMsgs.slice(-10)
-
- this.toView= historyChatMsgs[historyChatMsgs.length - 1].mid
- }
-
- uni.setStorageSync("rendered_" + sessionKey, historyChatMsgs);
- let chatMsg = uni.getStorageSync(sessionKey) || [];
- chatMsg.map(function (item, index) {
- curChatMsg.map(function (item2, index2) {
- if (item2.mid == item.mid) {
- chatMsg.splice(index, 1);
- }
- });
- });
- uni.setStorageSync(sessionKey, chatMsg);
- Index = historyChatMsgs.slice(-10).length;
-
- setTimeout(() => {
- uni.pageScrollTo({
- scrollTop: 5000,
- duration: 300,
- fail: (e) => {
-
- }
- });
- }, 100)
-
- if (isFail) {
- this.renderFail(sessionKey);
- }
- })
-
-
-
- },
- renderFail(sessionKey) {
- let me = this;
- let msgList = me.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.chatMsg= msgList
- }
- });
- if (me.curChatMsg[0].mid == curMsgMid) {
- me.curChatMsg[0].msg.data[0].isShow = false;
- me.curChatMsg[0].isShow = false;
- }
- uni.setStorageSync("rendered_" + sessionKey, msgList);
- isFail = false;
- },
-
- clickMsg(event){
- if(typeof(event.target.dataset.msg) == 'object' &&
- event.target.dataset.msg.msg.ext &&
- event.target.dataset.msg.msg.ext.msg_extension){
- this.$emit("clickMsg", event.target.dataset.msg.msg.ext)
- }
- }
- }
- };
- </script>
- <style>
- @import "./msglist.css";
- </style>
|