product-list.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. this.setData({
  66. goodsList:this.data.pageNum === 1?res.mercProdList:[...this.data.goodsList,...res.mercProdList]
  67. })
  68. }
  69. })
  70. },
  71. // 添加购物车
  72. addCart(prod){
  73. console.log(prod);
  74. let prodInfo = prod.currentTarget.dataset.prod;
  75. prodInfo.buy = true;
  76. this.triggerEvent('addCart',prodInfo);
  77. },
  78. // 观察滚动触底事件
  79. onReachBottom(e){
  80. this.setData({
  81. pageNum:this.data.pageNum+1
  82. });
  83. this.observerFunction();
  84. },
  85. observerFunction(){
  86. if(this.data.position === 'hot') this.getHotCommodity();
  87. if(this.data.position === 'new') this.getNewCommodity();
  88. if(this.data.position === 'presale') this.getPresaleCommodity();
  89. if(this.data.position === 'sort' || this.data.position === 'index') this.getClassifyCommodity();
  90. },
  91. },
  92. ready: function(){
  93. this.observerFunction();
  94. }
  95. })