msglist.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <template>
  2. <view
  3. scroll-y="true"
  4. :class="view + ' wrap ' + (isIPX?'scroll_view_X': '')"
  5. @tap="onTap"
  6. upper-threshold="-50"
  7. :scroll-into-view="toView"
  8. >
  9. <view class="message" v-for="item in chatMsg" :key="item.mid" :id="item.mid">
  10. <!-- <view class="time">
  11. <text class="time-text">{{ item.time }}</text>
  12. </view>-->
  13. <view class="main" :class="item.style">
  14. <view class="user">
  15. <!-- yourname:就是消息的 from -->
  16. <text class="user-text">{{ item.time}}</text>
  17. </view>
  18. <image class="avatar" v-if="item.style=='self'" :src="item.user.avatarurl" />
  19. <image class="avatar" v-else :src="item.your.avatarurl" />
  20. <view class="msg">
  21. <image
  22. class="err"
  23. :class="(item.style == 'self' && item.isFail) ? 'show' : 'hide'"
  24. src="/static/images/msgerr.png"
  25. />
  26. <image
  27. v-if="item.style == 'self'"
  28. src="/static/images/poprightarrow2x.png"
  29. class="msg_poprightarrow"
  30. />
  31. <image
  32. v-if="item.style == ''"
  33. src="/static/images/popleftarrow2x.png"
  34. class="msg_popleftarrow"
  35. />
  36. <view v-if="item.msg.type == 'img' || item.msg.type == 'video'">
  37. <image
  38. v-if="item.msg.type == 'img'"
  39. class="avatar"
  40. :src="item.msg.data"
  41. style="width:90px; height:120px; margin:2px auto;"
  42. mode="aspectFit"
  43. @tap="previewImage"
  44. :data-url="item.msg.data"
  45. />
  46. <!-- <video v-if="item.msg.type == 'video'" :src="item.msg.data" controls style="width:300rpx;"/> -->
  47. </view>
  48. <audio-msg v-if="item.msg.type == 'audio'" :msg="item"></audio-msg>
  49. <view v-else-if="item.msg.type == 'txt' || item.msg.type == 'emoji'">
  50. <view class="template" v-for="(d_item, d_index) in item.msg.data" :key="d_index">
  51. <text
  52. :data-msg="item"
  53. @tap="clickMsg"
  54. v-if="d_item.type == 'txt'"
  55. class="msg-text"
  56. style="float:left;"
  57. selectable="true"
  58. >{{ d_item.data }}</text>
  59. <image
  60. v-if="d_item.type == 'emoji'"
  61. class="avatar"
  62. :src="'/static/images/faces/' + d_item.data"
  63. style="width:25px; height:25px; margin:0 0 2px 0; float:left;"
  64. />
  65. </view>
  66. </view>
  67. </view>
  68. </view>
  69. </view>
  70. </view>
  71. <!-- <view style="height: 1px;"></view> -->
  72. </template>
  73. <script>
  74. let msgStorage = require("../msgstorage");
  75. let disp = require("../../../utils/broadcast");
  76. let LIST_STATUS = {
  77. SHORT: "scroll_view_change",
  78. NORMAL: "scroll_view"
  79. };
  80. let page = 0;
  81. let Index = 0;
  82. let curMsgMid = '';
  83. let isFail = false;
  84. import audioMsg from "./type/audio/audio";
  85. export default {
  86. data() {
  87. return {
  88. view: LIST_STATUS.NORMAL,
  89. toView: "",
  90. chatMsg: [],
  91. __visibility__: false,
  92. isIPX: false
  93. };
  94. },
  95. components: {
  96. audioMsg
  97. },
  98. props: {
  99. username: {
  100. type: Object,
  101. default: () => ({})
  102. }
  103. },
  104. // lifetimes
  105. created() {},
  106. beforeMount() {
  107. this.__visibility__ = true;
  108. page = 0;
  109. Index = 0;
  110. },
  111. moved() {},
  112. destroyed() {
  113. this.__visibility__ = false;
  114. msgStorage.off("newChatMsg", this.dispMsg)
  115. },
  116. mounted(event) {
  117. let me = this;
  118. if (getApp().globalData.isIPX) {
  119. this.isIPX= true
  120. }
  121. this.username = uni.username;
  122. let username = this.username;
  123. let myUsername = uni.getStorageSync("myUsername");
  124. let sessionKey = username.groupId ? username.groupId + myUsername : username.your + myUsername;
  125. let chatMsg = uni.getStorageSync(sessionKey) || [];
  126. this.renderMsg(null, null, chatMsg, sessionKey);
  127. uni.setStorageSync(sessionKey, null);
  128. disp.on('em.error.sendMsgErr', function (err) {
  129. // curMsgMid = err.data.mid;
  130. isFail = true;
  131. // return;
  132. console.log('发送失败了');
  133. return;
  134. let msgList = me.chatMsg;
  135. msgList.map(item => {
  136. if (item.mid.substring(item.mid.length - 10) == curMsgMid.substring(curMsgMid.length - 10)) {
  137. item.msg.data[0].isFail = true;
  138. item.isFail = true;
  139. me.chatMsg= msgList
  140. }
  141. });
  142. // if (me.curChatMsg[0].mid == curMsgMid) {
  143. // me.curChatMsg[0].msg.data[0].isShow = false;
  144. // me.curChatMsg[0].isShow = false;
  145. // }
  146. uni.setStorageSync("rendered_" + sessionKey, msgList);
  147. });
  148. msgStorage.on("newChatMsg", this.dispMsg);
  149. },
  150. methods: {
  151. normalScroll() {
  152. this.view= LIST_STATUS.NORMAL
  153. },
  154. dispMsg(renderableMsg, type, curChatMsg, sesskey) {
  155. let me = this;
  156. let username = this.username;
  157. let myUsername = uni.getStorageSync("myUsername");
  158. let sessionKey = username.groupId ? username.groupId + myUsername : username.your + myUsername;
  159. me.curChatMsg = curChatMsg;
  160. if (!me.__visibility__) return; // 判断是否属于当前会话
  161. if (username.groupId) {
  162. // 群消息的 to 是 id,from 是 name
  163. if (renderableMsg.info.from == username.groupId || renderableMsg.info.to == username.groupId) {
  164. if (sesskey == sessionKey) {
  165. me.renderMsg(renderableMsg, type, curChatMsg, sessionKey, 'newMsg');
  166. }
  167. }
  168. } else if (renderableMsg.info.from == username.your || renderableMsg.info.to == username.your) {
  169. if (sesskey == sessionKey) {
  170. me.renderMsg(renderableMsg, type, curChatMsg, sessionKey, 'newMsg');
  171. }
  172. }
  173. },
  174. shortScroll() {
  175. this.view= LIST_STATUS.SHORT
  176. },
  177. onTap() {
  178. this.$emit("msglistTap", null, {
  179. bubbles: true
  180. });
  181. },
  182. previewImage(event) {
  183. var url = event.target.dataset.url;
  184. uni.previewImage({
  185. urls: [url] // 需要预览的图片 http 链接列表
  186. });
  187. },
  188. getHistoryMsg() {
  189. let me = this;
  190. let username = this.username;
  191. let myUsername = uni.getStorageSync("myUsername");
  192. let sessionKey = username.groupId ? username.groupId + myUsername : username.your + myUsername;
  193. let historyChatMsgs = uni.getStorageSync("rendered_" + sessionKey) || [];
  194. if (Index < historyChatMsgs.length) {
  195. let timesMsgList = historyChatMsgs.slice(-Index - 10, -Index);
  196. this.chatMsg= timesMsgList.concat(me.chatMsg),
  197. this.toView= timesMsgList[timesMsgList.length - 1].mid
  198. Index += timesMsgList.length;
  199. if (timesMsgList.length == 10) {
  200. page++;
  201. }
  202. uni.stopPullDownRefresh();
  203. }
  204. },
  205. renderMsg(renderableMsg, type, curChatMsg, sessionKey, isnew) {
  206. let me = this;
  207. var historyChatMsgs = uni.getStorageSync("rendered_" + sessionKey) || [];
  208. historyChatMsgs = historyChatMsgs.concat(curChatMsg);
  209. if (!historyChatMsgs.length) return;
  210. let user = {}
  211. let your = {}
  212. let myUsername = uni.getStorageSync('myUsername')
  213. uni.WebIM.conn.fetchUserInfoById([myUsername,me.username.your], ['avatarurl','nickname']).then((res) => {
  214. user = res.data[myUsername]
  215. your = res.data[me.username.your]
  216. console.log('用户信息',res);
  217. historyChatMsgs.map(function(item,index) {
  218. item.user = user
  219. item.your = your
  220. })
  221. if (isnew == 'newMsg') {
  222. this.chatMsg= this.chatMsg.concat(curChatMsg)
  223. // 跳到最后一条
  224. this.toView= historyChatMsgs[historyChatMsgs.length - 1].mid
  225. } else {
  226. this.chatMsg= historyChatMsgs.slice(-10)
  227. // 跳到最后一条
  228. this.toView= historyChatMsgs[historyChatMsgs.length - 1].mid
  229. }
  230. uni.setStorageSync("rendered_" + sessionKey, historyChatMsgs);
  231. let chatMsg = uni.getStorageSync(sessionKey) || [];
  232. chatMsg.map(function (item, index) {
  233. curChatMsg.map(function (item2, index2) {
  234. if (item2.mid == item.mid) {
  235. chatMsg.splice(index, 1);
  236. }
  237. });
  238. });
  239. uni.setStorageSync(sessionKey, chatMsg);
  240. Index = historyChatMsgs.slice(-10).length;
  241. // setTimeout 兼容支付宝小程序
  242. setTimeout(() => {
  243. uni.pageScrollTo({
  244. scrollTop: 5000,
  245. duration: 300,
  246. fail: (e) => {
  247. //console.log('滚失败了', e)
  248. }
  249. });
  250. }, 100)
  251. if (isFail) {
  252. this.renderFail(sessionKey);
  253. }
  254. })
  255. },
  256. renderFail(sessionKey) {
  257. let me = this;
  258. let msgList = me.chatMsg;
  259. msgList.map(item => {
  260. if (item.mid.substring(item.mid.length - 10) == curMsgMid.substring(curMsgMid.length - 10)) {
  261. item.msg.data[0].isFail = true;
  262. item.isFail = true;
  263. me.chatMsg= msgList
  264. }
  265. });
  266. if (me.curChatMsg[0].mid == curMsgMid) {
  267. me.curChatMsg[0].msg.data[0].isShow = false;
  268. me.curChatMsg[0].isShow = false;
  269. }
  270. uni.setStorageSync("rendered_" + sessionKey, msgList);
  271. isFail = false;
  272. },
  273. clickMsg(event){
  274. if(typeof(event.target.dataset.msg) == 'object' &&
  275. event.target.dataset.msg.msg.ext &&
  276. event.target.dataset.msg.msg.ext.msg_extension){
  277. this.$emit("clickMsg", event.target.dataset.msg.msg.ext)
  278. }
  279. }
  280. }
  281. };
  282. </script>
  283. <style>
  284. @import "./msglist.css";
  285. </style>