order.js 5.21 KB
import App from '../../../utils/api.js'
import util from '../../../utils/util.js'
var QQMapWX = require('../../../lib/qqmap-wx-jssdk.min.js');
let qqmapsdk;
Page({
	data:{
		hasmore:true
	},
	onLoad(opt){
		this.setData({
			totalprice:opt.totalprice
		})
		qqmapsdk = new QQMapWX({
		    key: 'TFKBZ-QUVL3-FHK3X-YJWHP-B35GT-JNF4J'
		});
		this.opt=opt;
		if(util.cache('remark')){
			wx.removeStorageSync('remark')
		}
		console.log(opt);
		this.getsellerinfo();
		this.getcartlist();
	},
	onShow(){
		if(util.cache('user')){
			this.setData({
				user:util.cache('user')
			})
			
		}
		if(util.cache('remark')){
			this.setData({
				remark:util.cache('remark')
			})
		}
	},
	getdistance(){
		qqmapsdk.calculateDistance({
				mode:'straight',
		        from: {
					latitude: this.data.seller.lat,
					longitude: this.data.seller.long
				}, //若起点有数据则采用起点坐标,若为空默认当前地址
		        to: [
					{
						latitude: this.data.user.lat,
						longitude: this.data.user.long
					},
				], //终点坐标
		        success: (res) =>{//成功后的回调
		          console.log(res.result.elements[0].distance,'hahaha');
				  this.distance=res.result.elements[0].distance;
				  if(Number(this.config.is_full_range)*1000>=this.distance){
					  this.realcreateorder();
				  }else{
					  wx.showModal({
						  content:'收货地址超出配送范围',
						  showCancel:false,
						  confirmText:'我知道了',
						  confirmColor:'#F0242E',
						  success:(res)=>{
						  	
						  }
						  
					  })
				  }
		        },
		        fail: function(error) {
		          console.error(error);
		        },
		        complete: function(res) {
		          console.log(res);
		        }
		    });
	},
	getcartlist(){
		App.Orders.GetCartByOpenid({sellerid:this.opt.id,openid:util.cache('openid')}).then((json)=>{
			console.log(json,'yyyyyyy')
			if(json.ret==200){
				this.setData({
					cartlist:json.data
				})
				
			}
		})
	},
	getsellerinfo(){
		App.Seller.GetSellerById({sellerid:this.opt.id}).then((json)=>{
			if(json.ret==200){
				this.setData({
					seller:json.data.seller
				})
				this.getsellerconfig();
			}
			
		})
	},
	getsellerconfig(){
		App.Seller.GetSellerConfig({sellerid:this.data.seller.id}).then((json)=>{
			console.log(json)
			if(json.ret==200){
				this.setData({
					config:json.data[0]
				})
				this.config=json.data[0]
			}
		})
	},
	showtime(){
		this.selectComponent('#time').show();
	},
	getsendtime(e){
		this.setData({
			sendtime:e.detail
		})
	},
	toaddress(){
		// if(this.data.user){
		// 	wx.navigateTo({
		// 		url:'../address/address?id='+this.data.user.id
		// 	})
		// }else{
		// 	wx.navigateTo({
		// 		url:'../address/address'
		// 	})
		// }
		wx.navigateTo({
			url:'../address/list?type=select'
		})
		
	},
	submit(){
		if(this.config){
			let keylist=['is_full_amount','is_full_range','is_place_order','is_receiving','prints'],count=0;
			for(let i=0;i<keylist.length;i++){
				for(let key in this.config){
					if(keylist[i]==key){
						if(this.config[key] && this.config[key]!=''){
							count+=1;
						}
					}
				}
			}
			if(this.config.is_place_order==0){
				return wx.showModal({
					content:'商家已停止接单',
					showCancel:false,
					confirmText:'我知道了',
					confirmColor:'#F0242E',
					success:(res)=>{
						
					}
					
				})
			}
			if(this.config.dinner_time && this.config.dinner_time!='' || (this.config.lunch_time && this.config.lunch_time!='') && count==keylist.length){
				
				this.createorder();
			}else{
				return wx.showModal({
					content:'商家暂未配置接单配置项,才可以下单',
					showCancel:false,
					confirmText:'我知道了',
					confirmColor:'#F0242E',
					success:(res)=>{
						
					}
					
				})
			}
		}
	},
	createorder(){
		if(!this.data.user.id){
			return wx.showToast({
				title:'请填写配送地址',
				icon:'none',
				mask:true,
				duration:1500
			})
		}
		if(!this.data.sendtime){
			return wx.showToast({
				title:'请选择配送时间',
				icon:'none',
				mask:true,
				duration:1500
			})
		}
		this.getdistance();
	},
	realcreateorder(){
		let parmdata={
			sellerid:this.opt.id,
			openid:util.cache('openid'),
			address_id:this.data.user.id,
		}
		if(this.data.remark){
			parmdata.remark=this.data.remark;
		}
		if(this.data.sendtime){
			let timestamp=Date.parse(new Date())+24*60*60*this.data.sendtime.dayidx*1000;
			let sdate=util.unixtoTime(timestamp/1000,'YYYY/MM/DD')+' '+this.data.sendtime.stime+':00';
			let edate=util.unixtoTime(timestamp/1000,'YYYY/MM/DD')+' '+this.data.sendtime.etime+':00';
			console.log(Date.parse(new Date()),Number(util.timetouUnix(edate))*1000)
			if(Number(util.timetouUnix(edate))*1000<Date.parse(new Date())){
				return wx.showToast({
					title:'请重新选择配送时间',
					icon:'none',
					mask:true,
					duration:1500
				})
			}
			parmdata.sd_time=util.timetouUnix(sdate)+'-'+util.timetouUnix(edate);
		}
		console.log(parmdata)
		App.Orders.CreateOrder(parmdata).then((json)=>{
			console.log(json)
			if(json.ret==200){
				wx.redirectTo({
					url:'./detail?id='+json.data+'&sellerid='+this.data.seller.id+'&totalprice='+this.data.totalprice
				})
				// this.pay();
			}
			
		})
	},
	showmore(){
		this.setData({
			hasmore:!this.data.hasmore
		})
	}
})