index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import {
  2. getReplyList,
  3. getReplyConfig
  4. } from '../../api/store.js';
  5. const app = getApp();
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. parameter: {
  12. 'navbar': '1',
  13. 'return': '1',
  14. 'title': '商品评分',
  15. 'color': false,
  16. },
  17. replyData: {},
  18. product_id: 0,
  19. reply: [],
  20. type: 0,
  21. loading: false,
  22. loadend: false,
  23. loadTitle: '加载更多',
  24. page: 1,
  25. limit: 8
  26. },
  27. /**
  28. * 授权回调
  29. */
  30. onLoadFun: function () {
  31. this.getProductReplyCount();
  32. this.getProductReplyList();
  33. },
  34. /**
  35. * 生命周期函数--监听页面加载
  36. */
  37. onLoad: function (options) {
  38. if (!options.product_id) return app.Tips({
  39. title: '缺少参数'
  40. }, {
  41. tab: 3,
  42. url: 1
  43. });
  44. this.setData({
  45. product_id: options.product_id
  46. });
  47. },
  48. /**
  49. * 获取评论统计数据
  50. */
  51. getProductReplyCount: function () {
  52. var that = this;
  53. getReplyConfig(that.data.product_id).then(res => {
  54. that.setData({
  55. replyData: res.data
  56. });
  57. });
  58. },
  59. /**
  60. * 分页获取评论
  61. */
  62. getProductReplyList: function () {
  63. var that = this;
  64. if (that.data.loadend) return;
  65. if (that.data.loading) return;
  66. that.setData({
  67. loading: true,
  68. loadTitle: ''
  69. });
  70. getReplyList(that.data.product_id, {
  71. page: that.data.page,
  72. limit: that.data.limit,
  73. type: that.data.type,
  74. }).then(res => {
  75. var list = res.data,
  76. loadend = list.length < that.data.limit;
  77. that.data.reply = app.SplitArray(list, that.data.reply);
  78. that.setData({
  79. reply: that.data.reply,
  80. loading: false,
  81. loadend: loadend,
  82. loadTitle: loadend ? "😕人家是有底线的~~" : "加载更多",
  83. page: that.data.page + 1
  84. });
  85. }).catch(err => {
  86. that.setData({
  87. loading: false,
  88. loadTitle: '加载更多'
  89. });
  90. });
  91. },
  92. /**
  93. * 点击事件切换
  94. */
  95. changeType: function (e) {
  96. var type = e.target.dataset.type;
  97. type = parseInt(type);
  98. if (type == this.data.type) return;
  99. this.setData({
  100. type: type,
  101. page: 1,
  102. loadend: false,
  103. reply: []
  104. });
  105. this.getProductReplyList();
  106. },
  107. /**
  108. * 页面上拉触底事件的处理函数
  109. */
  110. onReachBottom: function () {
  111. this.getProductReplyList();
  112. },
  113. })