product.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // pages/productinfo/productinfo.js
  2. import productApi from '../../api/product'
  3. import commentsApi from '../../api/comments'
  4. const app = getApp()
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. shopId: 0,
  11. productId: 0,
  12. product: {},
  13. commentsList: [],
  14. commentsNum: 0
  15. },
  16. /**
  17. * 生命周期函数--监听页面加载
  18. */
  19. onLoad: async function (options) {
  20. wx.showLoading({
  21. title: '加载中',
  22. })
  23. var shopId = options.shopId;
  24. var productId = options.productId;
  25. var product = await productApi.getProductById(productId);
  26. var comments = await commentsApi.getCommentsByProduct(1, 10, shopId, productId);
  27. //对返回的评论列表进行处理
  28. var list = this.standardCommentsList(comments.list);
  29. console.log(list)
  30. this.setData({
  31. shopId,
  32. productId,
  33. product: product.info,
  34. commentsList: list,
  35. commentsNum: comments.count
  36. })
  37. },
  38. standardCommentsList: function (list) {
  39. list.forEach(function (item, i, array) {
  40. let urls = item.imgUrls;
  41. let imgs = urls.split(',');
  42. item.imgs = imgs;
  43. let time = item.createTime.trim().split(" ")[0];
  44. item.commentTime = time
  45. })
  46. return list;
  47. },
  48. /**
  49. * 生命周期函数--监听页面初次渲染完成
  50. */
  51. onReady: function () {
  52. },
  53. /**
  54. * 生命周期函数--监听页面显示
  55. */
  56. onShow: function () {
  57. },
  58. /**
  59. * 生命周期函数--监听页面隐藏
  60. */
  61. onHide: function () {
  62. },
  63. /**
  64. * 生命周期函数--监听页面卸载
  65. */
  66. onUnload: function () {
  67. },
  68. /**
  69. * 页面相关事件处理函数--监听用户下拉动作
  70. */
  71. onPullDownRefresh: function () {
  72. },
  73. /**
  74. * 页面上拉触底事件的处理函数
  75. */
  76. onReachBottom: function () {
  77. },
  78. /**
  79. * 用户点击右上角分享
  80. */
  81. onShareAppMessage: function () {
  82. },
  83. gotoAllComments: function (e) {
  84. var shopId = this.data.shopId;
  85. var productId = this.data.productId;
  86. wx.navigateTo({
  87. url: '../comments/comments?title=全部评论&commentstype=all&shopId=' + shopId + '&productId=' + productId,
  88. })
  89. },
  90. gotoAppointment: async function (e) {
  91. const self = this
  92. console.log(this)
  93. var shopId = this.data.shopId;
  94. var productId = this.data.productId;
  95. const isAuth = await app.isAuth()
  96. if (!isAuth) {
  97. wx.redirectTo({
  98. url: '/pages/prompt/prompt?page=' + this.route+`&shopId=${shopId}&productId=${productId}`,
  99. })
  100. return
  101. }
  102. wx.navigateTo({
  103. url: '../appointment/appointment?' + 'shopId=' + shopId + '&productId=' + productId,
  104. })
  105. }
  106. })