index.js 2.4 KB

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