mock.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. 本地模拟接口请求, 仅demo演示用.
  3. 实际项目以您服务器接口返回的数据为准,无需本地处理分页.
  4. 请参考官方写法: https://www.mescroll.com/uni.html?v=20200210#tagUpCallback
  5. * */
  6. // 模拟数据
  7. import goods from "./goods.js";
  8. // 获取新闻列表
  9. export function apiNewList(pageNum, pageSize) {
  10. return new Promise((resolute, reject)=>{
  11. //延时一秒,模拟联网
  12. setTimeout(function() {
  13. try {
  14. let list = [];
  15. if (!pageNum) {
  16. //模拟下拉刷新返回的数据
  17. let id=new Date().getTime();
  18. let newObj = {
  19. id:id,
  20. title: "【新增新闻" + id + "】 标题",
  21. content: "新增新闻的内容"
  22. };
  23. list.push(newObj);
  24. } else {
  25. //模拟上拉加载返回的数据
  26. for (let i = 0; i < pageSize; i++) {
  27. let upIndex = (pageNum - 1) * pageSize + i + 1;
  28. let newObj = {
  29. id:upIndex,
  30. title: "【新闻" + upIndex + "】 标题标题标题标题标题",
  31. content: "内容内容内容内容内容内容内容内容内容"
  32. };
  33. list.push(newObj);
  34. }
  35. console.log("page.num=" + pageNum + ", page.size=" + pageSize + ", curPageData.length=" + list.length);
  36. }
  37. //模拟接口请求成功
  38. resolute(list);
  39. } catch (e) {
  40. //模拟接口请求失败
  41. reject(e);
  42. }
  43. }, 1000)
  44. })
  45. }
  46. // 搜索商品
  47. export function apiGoods(pageNum, pageSize, keyword) {
  48. return new Promise((resolute, reject)=>{
  49. //延时一秒,模拟联网
  50. setTimeout(()=> {
  51. try{
  52. let data = {
  53. list: [], // 数据列表
  54. totalCount: 0, // 总数量
  55. totalPage: 0, // 总页数
  56. hasNext: false // 是否有下一页
  57. }
  58. // 符合关键词的记录
  59. let keywordList = [];
  60. if (!keyword || keyword == "推荐"||keyword == "全部") {
  61. // 搜索全部商品
  62. keywordList = goods;
  63. }else{
  64. // 关键词搜索
  65. if(keyword=="按时间"||keyword=="未发运") keyword="婴"; // 为这个关键词展示多几条数据
  66. for (let i = 0; i < goods.length; i++) {
  67. let good = goods[i]
  68. if (good.goodName.indexOf(keyword) !== -1) {
  69. keywordList.push(good)
  70. }
  71. }
  72. }
  73. // 分页
  74. for (let i = (pageNum - 1) * pageSize; i < pageNum * pageSize; i++) {
  75. if (i >= keywordList.length) break
  76. data.list.push(keywordList[i])
  77. }
  78. // 汇总数据
  79. data.totalCount = keywordList.length;
  80. data.totalPage = Math.ceil(data.totalCount/pageSize);
  81. data.hasNext = pageNum < data.totalPage
  82. //模拟接口请求成功
  83. console.log("pageNum=" + pageNum + ", pageSize=" + pageSize + ", data.list.length=" + data.list.length + ", totalCount=" + data.totalCount + ", totalPage=" + data.totalPage + ", hasNext=" + data.hasNext + (keyword ? ", keyword=" + keyword : ""));
  84. resolute(data);
  85. } catch (e) {
  86. //模拟接口请求失败
  87. reject(e);
  88. }
  89. },1000)
  90. })
  91. }
  92. // 获取微博列表
  93. export function apiWeiboList(pageNum, pageSize) {
  94. return new Promise((resolute, reject)=>{
  95. //延时2秒,模拟联网
  96. setTimeout(function() {
  97. try {
  98. let list = [];
  99. if(!pageNum){
  100. //此处模拟下拉刷新返回的数据
  101. let id=new Date().getTime();
  102. let newObj={id:id, title:"【新增微博"+id+"】 新增微博", content:"新增微博的内容,新增微博的内容"};
  103. list.push(newObj);
  104. }else{
  105. //此处模拟上拉加载返回的数据
  106. for (let i = 0; i < pageSize; i++) {
  107. let upIndex=(pageNum-1)*pageSize+i+1;
  108. let newObj={id:upIndex, title:"【微博"+upIndex+"】 标题标题标题标题标题标题", content:"内容内容内容内容内容内容内容内容内容内容"};
  109. list.push(newObj);
  110. }
  111. console.log("page.num=" + pageNum + ", page.size=" + pageSize + ", curPageData.length=" + list.length);
  112. }
  113. //模拟接口请求成功
  114. resolute(list);
  115. } catch (e) {
  116. //模拟接口请求失败
  117. reject(e);
  118. }
  119. }, 2000)
  120. })
  121. }
  122. // 获取消息列表(共5页消息)
  123. export function apiMsgList(pageNum, pageSize) {
  124. return new Promise((resolute, reject)=>{
  125. //延时一秒,模拟联网
  126. setTimeout(function() {
  127. try {
  128. let list = [];
  129. //模拟下拉加载更多记录
  130. for (let i = 0; i < pageSize; i++) {
  131. let msgId = (pageNum - 1) * pageSize + i + 1;
  132. let newObj = {
  133. id: msgId,
  134. title: "【消息" + msgId + "】",
  135. content: "内容: 下拉获取聊天记录"
  136. };
  137. // 此处模拟只有5页的消息 (第5页只有3条)
  138. if(pageNum>=5 && i>=3){}else{
  139. list.unshift(newObj);
  140. }
  141. }
  142. console.log("page.num=" + pageNum + ", page.size=" + pageSize + ", curPageData.length=" + list.length);
  143. //模拟接口请求成功
  144. resolute(list);
  145. } catch (e) {
  146. //模拟接口请求失败
  147. reject(e);
  148. }
  149. }, 1000)
  150. })
  151. }
  152. // 获取tabs类目
  153. export function apiGetTabs() {
  154. return new Promise((resolute, reject)=>{
  155. //延时,模拟联网
  156. setTimeout(function() {
  157. try {
  158. let tabs = ['推荐', '按时间', '按距离', '运费先付', '果汁', '奶瓶', '美素', '花王', '韩蜜', '口红', '毛巾', '玩具', '衣服'];
  159. //模拟接口请求成功
  160. resolute(tabs);
  161. } catch (e) {
  162. //模拟接口请求失败
  163. reject(e);
  164. }
  165. }, 10)
  166. })
  167. }