product-list.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // components/product-list/product-list.js
  2. // 获取应用实例
  3. const {API} = require('../../service/api.js');
  4. Component({
  5. properties: {
  6. // header嵌入的位置,index:首页
  7. position:{
  8. type:String,
  9. value:''
  10. },
  11. filterObj:{
  12. type:Object,
  13. value:{}
  14. }
  15. },
  16. observers:{
  17. "filterObj": function(newVal){
  18. this.getClassifyCommodity();
  19. }
  20. },
  21. data: {
  22. goodsList: [],
  23. pageNum:1,
  24. },
  25. methods: {
  26. // 获取今日热门商品列表
  27. getHotCommodity(){
  28. API.getLeftProds().then(res=>{
  29. if(res){
  30. this.setData({
  31. goodsList:res.prodList
  32. })
  33. };
  34. })
  35. },
  36. // 获取上新商品列表
  37. getNewCommodity(){
  38. API.getMiddleProds().then(res=>{
  39. if(res){
  40. this.setData({
  41. goodsList:res.prodList
  42. })
  43. };
  44. })
  45. },
  46. // 获取预售商品列表
  47. getPresaleCommodity(){
  48. API.getRightProds().then(res=>{
  49. if(res){
  50. this.setData({
  51. goodsList:res.prodList
  52. })
  53. }
  54. })
  55. },
  56. // 获取分类商品
  57. getClassifyCommodity(){
  58. let data = {
  59. ...this.data.filterObj,
  60. pageNum:this.data.pageNum,
  61. pageSize:5
  62. }
  63. API.getProdsList(data).then(res=>{
  64. if(res){
  65. res.mercProdList.forEach(item=>{
  66. if(!item.price && item.prodAttrList){
  67. item.price = item.prodAttrList[0].prodAttrPrice.price;
  68. }
  69. })
  70. this.setData({
  71. goodsList:this.data.pageNum === 1?res.mercProdList:[...this.data.goodsList,...res.mercProdList]
  72. })
  73. }
  74. })
  75. },
  76. // 添加购物车
  77. addCart(prod){
  78. console.log(prod);
  79. let prodInfo = prod.currentTarget.dataset.prod;
  80. prodInfo.buy = true;
  81. this.triggerEvent('addCart',prodInfo);
  82. },
  83. // 观察滚动触底事件
  84. onReachBottom(e){
  85. this.setData({
  86. pageNum:this.data.pageNum+1
  87. });
  88. this.observerFunction();
  89. },
  90. observerFunction(){
  91. if(this.data.position === 'hot') this.getHotCommodity();
  92. if(this.data.position === 'new') this.getNewCommodity();
  93. if(this.data.position === 'presale') this.getPresaleCommodity();
  94. if(this.data.position === 'sort' || this.data.position === 'index') this.getClassifyCommodity();
  95. },
  96. // 进入商品详情
  97. goProdDetail(e){
  98. wx.navigateTo({
  99. url: '/pages/prodDetail/prodDetail?prod='+ encodeURIComponent(JSON.stringify(e.currentTarget.dataset.prod)),
  100. })
  101. },
  102. },
  103. ready: function(){
  104. this.observerFunction();
  105. }
  106. })