product-list.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. this.triggerEvent('addCart',prodInfo);
  76. },
  77. // 观察滚动触底事件
  78. onReachBottom(e){
  79. this.setData({
  80. pageNum:this.data.pageNum+1
  81. });
  82. this.observerFunction();
  83. },
  84. observerFunction(){
  85. if(this.data.position === 'hot') this.getHotCommodity();
  86. if(this.data.position === 'new') this.getNewCommodity();
  87. if(this.data.position === 'presale') this.getPresaleCommodity();
  88. if(this.data.position === 'sort' || this.data.position === 'index') this.getClassifyCommodity();
  89. },
  90. },
  91. ready: function(){
  92. this.observerFunction();
  93. }
  94. })