history-msg.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. export const getHistoryMsg = (params)=>{
  2. const { page = 1,rows = 10 } = params ?? {};
  3. let join = ()=>{
  4. let arr = [];
  5. //通过当前页码及页数,模拟数据内容
  6. let startIndex = (page-1) * rows;
  7. let endIndex = startIndex + rows;
  8. for(let i = startIndex; i < endIndex; i++){
  9. arr.push({
  10. "id":i, // 消息的ID
  11. "content":`这是历史记录的第${i+1}条消息,第${page}页`, // 消息内容
  12. "type":Math.random() > 0.5 ? 1 : 0 ,// 此为消息类别,设 1 为发出去的消息,0 为收到对方的消息,
  13. "pic":"/static/logo.png" // 头像
  14. })
  15. }
  16. /*
  17. 颠倒数组中元素的顺序。将最新的数据排在本次接口返回数据的最后面。
  18. 后端接口按 消息的时间降序查找出当前页的数据后,再将本页数据按消息时间降序排序返回。
  19. 这是数据的重点,因为页面滚动条和上拉加载历史的问题。
  20. */
  21. // arr.reverse();
  22. return arr;
  23. }
  24. // 此处用到 ES6 的 Promise 知识,不懂的请自行学习。
  25. return new Promise((done,fail)=>{
  26. // 无数据请求接口,由 setTimeout 模拟,正式项目替换为 ajax 即可。
  27. setTimeout(()=>{
  28. let data = join();
  29. done(data);
  30. },1000);
  31. })
  32. }