menu.js 5.37 KB
var util = require('../../../utils/util.js')
import App from '../../../utils/api.js'
Page({
	data:{
		tablist:[
			{
				name:'点菜',
			},
			{
				name:'商家',
			},
		],
		tabidx:0,
		refresh:false,
	},
	onLoad(opt){
		let systeminfo=util.cache('system.info');
		let ratio=750/systeminfo.windowWidth;
		let headerHeight=util.cache('headerHeight')
		let contentheight=systeminfo.windowHeight-headerHeight-(418/ratio);
		this.setData({
			contentheight:Math.floor(contentheight),
			seller:opt,
			headerHeight:headerHeight,
		})
		console.log(opt)
		this.opt=opt;
		this.getsellerinfo();
	},
	onShow(){
		this.getfoodlist();
	},
	onReady(){
		
	},
	getsellerinfo(){
		App.Seller.GetSellerById({sellerid:this.opt.id}).then((json)=>{
			if(json.ret==200){
				this.setData({
					seller:json.data.seller
				})
			}
			
		})
	},
	getfoodlist(){
		App.Cookbook.NewGetCookbookByClass({sellerid:this.opt.id,openid:util.cache('openid')}).then((json)=>{
			console.log(json);
			if(json.ret==200){
				let classidx=0;
				json.data.forEach((v,i)=>{
					if(v.cookbookList[0].length>0){
						v.cookbookList[0].forEach((r,n)=>{
							r.count=Number(r.count)
						})
					}
				})
				this.setData({
					cooklist:json.data,
					classidx:classidx,
					refresh:true,
					currentidx:classidx
				})
				getApp().timer=setTimeout(()=>{
					this.gettoplist();
				},800);
				console.log(this.data.cooklist[this.data.classidx])
			}
		})
	},
	getcurrentclass(e){
		console.log(e);
		if(!this.toplist){
			this.gettoplist();
		}
		if(this.toplist){
			let toplist=this.toplist,scrolltop=e.detail.scrollTop;
			if(this.action=='click'){
				let classidx=this.data.classidx,currentidx=this.data.classidx;
				if(classidx<toplist.length-1){
					if(scrolltop>=toplist[classidx] && scrolltop<toplist[classidx+1]){
						this.setData({
							currentidx:classidx
						})
					}else{
						while(scrolltop<toplist[currentidx] && currentidx>0){
							currentidx--
						}
						this.setData({
							currentidx:currentidx
						})
					}
				}else{
					if(scrolltop>=toplist[classidx]){
						this.setData({
							currentidx:classidx
						})
					}else{
						while(scrolltop<toplist[currentidx] && currentidx>0){
							currentidx--
						}
						this.setData({
							currentidx:currentidx
						})
					}
				}
				this.action='auto';
			}else{
				toplist.forEach((v,i)=>{
					if(i<toplist.length-1){
						if(scrolltop>=v && scrolltop<toplist[i+1]){
							console.log('rrrrrrrr',i)
							let currentclass=this.data.cooklist[i];
							if(this.data.classidx!=i){
								this.setData({
									classidx:i,
									currentidx:i,
								})
							}
							
						}
						
					}else{
						if(scrolltop>=v){
							if(this.data.classidx!=i){
								this.setData({
									classidx:i,
									currentidx:i,
								})
							}
						}
					}
				})
			}
		}
		
		
	},
	gettoplist(){
		let cooklist=this.data.cooklist,toplist=[];
		cooklist.forEach((v,i)=>{
			if(i==0){
				toplist.push(0);
			}else{
				wx.createSelectorQuery().select('#view_'+v.id).boundingClientRect().exec((res)=>{
				  console.log(res,'view_'+v.id)
				  let systeminfo=util.cache('system.info');
				  let ratio=750/systeminfo.windowWidth;
				  let headerHeight=util.cache('headerHeight')
				  let top=headerHeight+(418/ratio)
				  toplist.push(res[0].top-top);
				  console.log(toplist);
				})
			}
			
		})
		this.toplist=toplist;
		console.log(toplist);
	},
	getclassidx(e){
		console.log(e)
		this.setData({
			classidx:e.detail.classidx,
		})
		this.action='click';
	},
	selecttab(e){
		this.setData({
			tabidx:e.currentTarget.dataset.index
		})
	},
	refreshprice(){
		console.log('rrrr')
		this.setData({
			refresh:true
		})
	},
	showrmodal(e){
		console.log(e,'rmodal')
		this.setData({
			reduceitem:this.data.cooklist[e.detail.classidx].cookbookList[0][e.detail.cookidx]
		})
		this.selectComponent('#rmodal').showrmodal();
		// this.refreshprice();
	},
	showspec(e){
		console.log(e,'showspec',this.selectComponent('#spec'));
		this.setData({
			additem:this.data.cooklist[e.detail.classidx].cookbookList[0][e.detail.cookidx]
		})
		this.selectComponent('#spec').show();
		// this.refreshprice();
	},
	tellfood(e){
		console.log(e,'actfood')
		let cooklist=this.data.cooklist,classidx,cookidx,count=0;
		cooklist.forEach((v,i)=>{
			if(v.id==e.detail.classid){
				classidx=i;
				if(v.cookbookList[0].length>0){
					v.cookbookList[0].forEach((r,n)=>{
						if(r.id==e.detail.id){
							cookidx=n;
							if(e.detail.action=='reduce'){
								if(r.count>0){
									count=r.count-1;
								}
							}else{
								count=r.count+1;
							}
							
						}
					})
				}
			}
			
		})
		let setkey='cooklist['+classidx+'].cookbookList[0]['+cookidx+'].count';
		this.setData({
			[setkey]:count,
			refresh:true,
		})
		
	},
	tome(){
		wx.navigateTo({
			url:'../me/index'
		})
	},
	onHide(){
		clearTimeout(getApp().timer);
	},
	onShareAppMessage(){
		return {
			title:'"'+this.data.seller.sellername+'"店铺邀请您点餐',
			path:'pages/custom/enter/enter?id='+this.data.seller.id+'&share='+true,
			imageUrl:this.data.seller.seller_image ? this.data.seller.seller_image : ''
		}
	},
	openaddress(){
		wx.openLocation({
		    latitude:Number(this.data.seller.lat),
		    longitude:Number(this.data.seller.long),
		    scale: 18,
			address:this.data.seller.map_address,
			success:(res)=>{
				console.log(res,'success')
			},
			fail:(fail)=>{
				console.log(fail,'fail')
			}
		})
	}
	
})