| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- // components/product-list/product-list.js
- // 获取应用实例
- const {API} = require('../../service/api.js');
- Component({
- properties: {
- // header嵌入的位置,index:首页
- position:{
- type:String,
- value:''
- },
- filterObj:{
- type:Object,
- value:{}
- }
- },
- observers:{
- "filterObj": function(newVal){
- this.getClassifyCommodity();
- }
- },
- data: {
- goodsList: [],
- pageNum:1,
- },
- methods: {
- // 获取今日热门商品列表
- getHotCommodity(){
- API.getLeftProds().then(res=>{
- if(res){
- this.setData({
- goodsList:res.prodList
- })
- };
- })
- },
- // 获取上新商品列表
- getNewCommodity(){
- API.getMiddleProds().then(res=>{
- if(res){
- this.setData({
- goodsList:res.prodList
- })
- };
- })
- },
- // 获取预售商品列表
- getPresaleCommodity(){
- API.getRightProds().then(res=>{
- if(res){
- this.setData({
- goodsList:res.prodList
- })
- }
- })
- },
- // 获取分类商品
- getClassifyCommodity(){
- let data = {
- ...this.data.filterObj,
- pageNum:this.data.pageNum,
- pageSize:5
- }
- API.getProdsList(data).then(res=>{
- if(res){
- this.setData({
- goodsList:this.data.pageNum === 1?res.mercProdList:[...this.data.goodsList,...res.mercProdList]
- })
- }
- })
- },
- // 添加购物车
- addCart(prod){
- console.log(prod);
- let prodInfo = prod.currentTarget.dataset.prod;
- prodInfo.buy = true;
- this.triggerEvent('addCart',prodInfo);
- },
- // 观察滚动触底事件
- onReachBottom(e){
- this.setData({
- pageNum:this.data.pageNum+1
- });
- this.observerFunction();
- },
- observerFunction(){
- if(this.data.position === 'hot') this.getHotCommodity();
- if(this.data.position === 'new') this.getNewCommodity();
- if(this.data.position === 'presale') this.getPresaleCommodity();
- if(this.data.position === 'sort' || this.data.position === 'index') this.getClassifyCommodity();
- },
- },
- ready: function(){
- this.observerFunction();
- }
- })
|