ui.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import * as chatInput from "../../modules/chat-input/chat-input";
  2. /**
  3. * 用户处理消息的收发UI更新
  4. */
  5. export default class UI {
  6. constructor(page) {
  7. this._page = page;
  8. }
  9. /**
  10. * 接收到消息时,更新UI
  11. * @param msg
  12. */
  13. updateViewWhenReceive(msg) {
  14. this._page.data.chatItems.push(msg);
  15. // .sort(UI.compare('create_at', 'id'))
  16. console.log(this._page.data.chatItems.length * 999);
  17. this._page.setData({
  18. chatItems: this._page.data.chatItems,
  19. scrollTopVal: this._page.data.chatItems.length * 999,
  20. });
  21. }
  22. /**
  23. * 发送消息时,渲染消息的发送状态为 发送中
  24. * @param sendMsg
  25. * @param cbOk
  26. */
  27. showItemForMoment(sendMsg, cbOk) {
  28. if (!sendMsg) return;
  29. this.updateDataWhenStartSending(sendMsg);
  30. cbOk && cbOk(this._page.data.chatItems.length - 1);
  31. }
  32. /**
  33. * 设置消息发送状态为 发送中
  34. * @param sendMsg
  35. * @param addToArr
  36. * @param needScroll
  37. */
  38. updateDataWhenStartSending(sendMsg, addToArr = true, needScroll = true) {
  39. chatInput.closeExtraView();
  40. sendMsg.sendStatus = 'sending';
  41. addToArr && this._page.data.chatItems.push(sendMsg);
  42. let obj = {};
  43. obj['textMessage'] = '';
  44. obj['chatItems'] = this._page.data.chatItems;
  45. needScroll && (obj['scrollTopVal'] = this._page.data.chatItems.length * 999);
  46. this._page.setData(obj);
  47. }
  48. /**
  49. * 设置消息发送状态为 发送成功
  50. * @param sendMsg
  51. * @param itemIndex
  52. */
  53. updateViewWhenSendSuccess(sendMsg, itemIndex) {
  54. console.log('发送成功', sendMsg);
  55. let that = this._page;
  56. let item = that.data.chatItems[itemIndex];
  57. item.timestamp = sendMsg.timestamp;
  58. this.updateSendStatusView('success', itemIndex);
  59. }
  60. updateListViewBySort() {
  61. this._page.setData({
  62. chatItems: this._page.data.chatItems.sort(UI._sortMsgListByTimestamp)
  63. })
  64. }
  65. /**
  66. * 设置消息发送状态为 发送失败
  67. * @param itemIndex
  68. */
  69. updateViewWhenSendFailed(itemIndex) {
  70. this.updateSendStatusView('failed', itemIndex);
  71. }
  72. updateSendStatusView(status, itemIndex) {
  73. let that = this._page;
  74. that.data.chatItems[itemIndex].sendStatus = status;
  75. let obj = {};
  76. obj[`chatItems[${itemIndex}].sendStatus`] = status;
  77. that.setData(obj);
  78. }
  79. updateChatStatus(content, open = true) {
  80. this._page.setData({
  81. chatStatue: open ? 'open' : 'close',
  82. chatStatusContent: content
  83. })
  84. }
  85. static _sortMsgListByTimestamp(item1, item2) {
  86. return item1.timestamp - item2.timestamp;
  87. }
  88. static compare(name1, name2) {
  89. // 这里的参数 a, b 是比较的第一个元素 a,第二个元素 b
  90. return function (a, b) {
  91. let fir1 = a[name1];
  92. let sec1 = b[name1];
  93. if (fir1 === sec1) {
  94. let fir2 = a[name2];
  95. let sec2 = b[name2];
  96. if (fir2 === sec2) {
  97. return 0; // 表示位置不变
  98. } else {
  99. return fir2 > sec2 ? -1 : 1; // 表示从大到小排序
  100. }
  101. } else {
  102. return fir1 > sec1 ? 1 : -1; // 表示从小到大排序
  103. }
  104. }
  105. }
  106. }