You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

281 lines
6.7 KiB

// pages/order/road/index.js
import commonApi from "../../../utils/https/common"
import util from "../../../utils/util"
let app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
monthDays1:[0,0],
monthDays2:[0,0],
month:[],
monthIndex:0,
prices:{},
selectDate:null,
id:21,
sku:[],
nums:[],
price:0,
infoFlag:false,
productInfo:null,
ZTPoint:''
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
console.log(options.skuid);
this.setData({
id:options.id
})
if (options.ZTPoint) {this.setData({ZTPoint: options.ZTPoint})}
// 获取产品下的规格
commonApi._post("product/get_product_detail",{
id:this.data.id
}).then(res=>{
let nums = [];
if(options.skuid){
let sku = res.data.sku.find(item=>item.id==options.skuid);
res.data.sku = [sku];
}
res.data.sku.map(item=>{
console.log(item);
item.stock>0?nums.push(1):nums.push(0)
})
for(let i=0;i<res.data.sku.length;i++){
for(let j=i+1;j<res.data.sku.length;j++){
if(res.data.sku[i].id>res.data.sku[j].id){
[res.data.sku[i],res.data.sku[j]] = [res.data.sku[j],res.data.sku[i]]
}
}
}
this.setData({
productInfo:res.data,
sku:res.data.sku,
nums:nums
})
})
this.initDate()
},
changeMonth:function(e){
this.setData({
monthIndex:e.currentTarget.dataset.index
})
},
initDate(){
let today = new Date(),month1 = today.getMonth()+1,month2 = (month1 + 1)>12? (month1 + 1)% 12:(month1+1),year = today.getFullYear(),year2 = month1<month2?year:(year+1);
let monthDays1 = this.getMonthDays(month1,year);
let monthDays2 = this.getMonthDays(month2,year2);
this.setData({
month:[month1,month2],
monthDays1:monthDays1,
monthDays2:monthDays2
})
let selectDate,selectMonth;
commonApi.user_post("product/get_product_sku_price_by_date",{
product_id:this.data.id,
start_date:util.formatDate(today),
end_date:year2+"/"+month2+"/"+monthDays2[0]
}).then(res=>{
if (!res || res.code != 1) return
let prices={};
res.data.map(item=>{
// 给规格排序
for(let i=0;i<item.info.length;i++){
for(let j=i+1;j<item.info.length;j++){
if(item.info[i].sku_info.id>item.info[j].sku_info.id){
[item.info[i],item.info[j]] = [item.info[j],item.info[i]]
}
}
}
let stock = 0;
item.info.map(i=>{
stock = stock+i.stock;
})
item.stock = stock;
if(item.stock>0){
let date = item.date.split("-").splice(1,2),newDate=[];
console.log(date)
date.map(d=>{
d = parseInt(d);
newDate.push(d)
})
if(!selectDate){
selectDate = newDate[1];
selectMonth = newDate[0];
}
prices[newDate.join("-")] = item;
}
})
this.setData({
prices:prices
})
if(!selectDate) return;
let info = prices[selectMonth+'-'+selectDate];
info.selectMonth = selectMonth;
info.selectDate = selectDate;
this.setData({
selectDate:info,
monthIndex:selectMonth==month1?0:1
})
this.changePrice()
})
},
// 获取这个月有几天
getMonthDays:function(month,year){
let date = new Date(year+"/"+month+"/01").getDay();
if(month==2){
if(year % 100 !==0 && year % 4==0 || year % 400 ==0){
return [29,date];
}
else {
return [28,date];
}
}
else if(month<8 && month % 2 ==1 || month>=8 && month % 2==0){
return [31,date];
}
else {
return [30,date];
}
},
selectDate:function(e){
// 选择日期
let item = e.currentTarget.dataset.item,selectMonth = this.data.month[this.data.monthIndex];
if(!selectMonth) return
let info = this.data.prices[selectMonth+'-'+(item+1)];
console.log(info)
if(info && info.product_price && info.product_price!==null && info.stock!=0){
info.selectMonth = selectMonth;
info.selectDate = item+1;
this.setData({
selectDate:info
})
this.changePrice()
}
},
minus:function(e){
if(!this.data.selectDate){
wx.showToast({
title: '请先选择出行日期',
icon:'none'
})
return;
}
let index = e.currentTarget.dataset.index,nums = this.data.nums;
if(nums[index]==0) return;
nums[index] = nums[index] - 1;
this.setData({
nums:nums
})
this.changePrice()
},
add:function(e){
let index = e.currentTarget.dataset.index,nums = this.data.nums,selectDate = this.data.selectDate,sku = this.data.sku;
if(!selectDate){
wx.showToast({
title: '请先选择出行日期',
icon:'none'
})
return;
}
if(selectDate.info[index].stock==nums[index]) return;
nums[index] = nums[index] + 1;
this.setData({
nums:nums
})
this.changePrice()
},
showInfo:function(){
this.setData({
infoFlag:!this.data.infoFlag
})
},
changePrice:function(){
let selectDate = this.data.selectDate,nums = this.data.nums,price=0;
nums.map((num,index)=>{
price = price + num * selectDate.info[index].price;
})
this.setData({
price:price
})
},
order:function(){
let selectDate = this.data.selectDate,sku = this.data.sku,nums = this.data.nums,allNum = 0;
if(!selectDate){
wx.showToast({
title: '请先选择日期',
icon:'none'
})
return;
}
sku.map((item,index)=>{
item.num = nums[index];
allNum = allNum + nums[index];
item.price = selectDate.info[index].price;
})
if(allNum==0){
wx.showToast({
title: '至少选择一个规格',
icon:'none'
})
return;
};
app.globalData.product = {
product:{...this.data.productInfo,ZTPoint:this.data.ZTPoint},
sku:sku,
selectDate:selectDate,
price:this.data.price
}
wx.navigateTo({
url: '/pages/order/roadOrder/index',
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
}
})