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.
 

592 lines
16 KiB

// pages/order/post/index.js
let app = getApp()
import commonApi from "../../../utils/https/common"
Page({
/**
* 页面的初始数据
*/
data: {
pageUseType: 0, // 0 邮寄 1 自提 3邮寄或自提 购物车用 不考虑3
allProduct: [],
product: [],
showPrice: 0,
from: null,
address: null,
postFee: 0,
coupon:app.globalData.couponInfo,
sku_id: '',
kjId: null,
gp_id: null,
prizeId: null,
select_allowance: false,
allowance_data: null,
allowance_price: 0,
showAllowance: false,
flag:null,
isCar:'single',
showYhq:null,
pickupIndex: null,
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.setData({
from: options.from,
kjId: app.globalData.kjId,
gp_id: app.globalData.gp_id || app.globalData.team_id,
prizeId: wx.getStorageSync('prizeId'),
flag:options.flag,
isCar:options.isCar,
showYhq:app.globalData.listName?false:true
})
this.handleProduct()
},
handleProduct () {
app.globalData.postProduct.forEach((item,index) => {
item.keyIndex = index
item.sku.batch_shipment = item.sku.batch_shipment?item.sku.batch_shipment:0;
// 用户选择的配送方式
item.user_select_type = item.sku.use_type === 1 ? 1 : 0
})
this.setData({
allProduct: app.globalData.postProduct,
})
if (this.data.from == "cart") {
if (this.data.pageUseType == 0) {
let pageUseType = app.globalData.postProduct.some(x=>x.user_select_type==0)?0:1
let productList = app.globalData.postProduct.filter(x=>x.user_select_type == pageUseType)
this.setData({
product: productList,
pageUseType: pageUseType,
})
} else {
let productList = app.globalData.postProduct.filter(x=>x.user_select_type == 1)
this.setData({
product: productList,
pageUseType: 1,
})
}
} else {
this.setData({
product: app.globalData.postProduct,
})
}
console.log(this.data.product, "页面数据")
let price = 0, sku_id = [];
this.data.product.forEach(item=>{
if (!app.globalData.kjId && !this.data.gp_id) {
price = price + item.sku.price * item.productNum;
}
else if (this.data.gp_id) {
price = price + item.sku.event_price * item.productNum;
}
sku_id.push(item.sku.id)
})
this.setData({
showPrice: app.globalData.kjId ? 0 : price,
sku_id: sku_id.join(","),
})
if (!this.data.kjId && !this.data.gp_id) {
this.couponCom = this.selectAllComponents("#coupon")[0];
}
},
changeUserPost (e) {
let item = e.currentTarget.dataset.item
let value = e.currentTarget.dataset.value
let index = e.currentTarget.dataset.index
item.user_select_type = value
this.data.product[index] = item
this.setData({
product: this.data.product
})
this.getPostFee()
this.changePrice()
},
price() {
let allPrice
if (this.data.coupon) {
if (this.data.coupon.activity.discount_type == 'pricebreak') {
return allPrice = this.data.showPrice + this.data.postFee - this.data.coupon.activity.money>0?this.data.showPrice + this.data.postFee - this.data.coupon.activity.money/100:0
} else {
return allPrice = (this.data.showPrice* this.data.coupon.activity.fold)/ 10 + this.data.postFee
}
}
},
changeAllowance: function () {
this.setData({
showAllowance: !this.data.showAllowance
})
},
inputRemark: function (e) {
let index = e.currentTarget.dataset.index, product = this.data.product;
product[index].remark = e.detail.value;
this.setData({
product: product
})
},
phoneInput: function (e) {
let index = e.currentTarget.dataset.index, product = this.data.product;
product[index].phone = e.detail.value;
this.setData({
product: product
})
},
delTel: function(e) {
let index = e.currentTarget.dataset.index, product = this.data.product;
product[index].phone = "";
this.setData({
product: product
})
},
setAddress: function (e) {
console.log('我触发了');
this.setData({
address: e.detail
})
this.getPostFee()
},
changePickup:function (e) {
let index = e.currentTarget.dataset.index;
let item = e.currentTarget.dataset.item
this.setData({
pickupIndex: index
})
wx.navigateTo({
url:`/subPackages/techanNew/selfPickPoint/index?pickupId=null&goodsId=${item.product.id}&skuId=${item.sku.id}`
})
},
setPickUpPoint: function (data) {
console.log(data)
let goods = this.data.product[this.data.pickupIndex]
if (goods) {
goods.pickupAddress = data
}
this.setData({
product: this.data.product
})
},
minus: function (e) {
let index = e.currentTarget.dataset.index, product = this.data.product;
let productNum = product[index].productNum;
if (productNum <= 1) return;
product[index].productNum--;
this.setData({
product: product,
coupon: null
})
this.getPostFee()
this.changePrice()
},
add: function (e) {
let index = e.currentTarget.dataset.index, product = this.data.product;
product[index].productNum = product[index].productNum + 1;
this.setData({
product: product,
coupon: null
})
this.getPostFee()
this.changePrice()
},
changePrice: function () {
let product = this.data.product, price = 0;
product.map(item => {
price = price + item.sku.price * item.productNum;
if (this.data.allowance_data && this.data.select_allowance && this.data.allowance_price) {
let spread_price = Number(this.data.allowance_data.discount_limit_price - this.data.allowance_data.user_used_price);
price -= this.data.allowance_price * item.productNum < spread_price ? this.data.allowance_price * item.productNum : spread_price
}
})
if (this.data.coupon && this.data.showPrice != price) {
wx.showToast({
title: '订单价格发生变化,请重新选择优惠券',
icon: 'none'
})
app.globalData.couponInfo = null;
if (!this.data.kjId && !this.data.gp_id) {
this.couponCom.setNullCoupon()
}
this.setData({
coupon: null
})
}
this.setData({
showPrice: app.globalData.kjId ? 0 : price
})
},
order: function () {
for(let info of this.data.product) {
if (info.user_select_type == 0) {
if (!this.data.address) {
wx.showToast({title: '请选择收货地址',icon: 'none'});
return;
}
}
if (info.user_select_type == 1) {
if (!info.pickupAddress||!info.pickupAddress.id) {
wx.showToast({title: '请选择自提点',icon: 'none'});
return;
}
if (!info.phone) {
wx.showToast({title: '请输入预留手机号',icon: 'none'});
return;
}
}
let allProductIndex = this.data.allProduct.findIndex(x=>x.keyIndex == info.keyIndex)
if (allProductIndex>=0) {
this.data.allProduct[allProductIndex] = info
}
}
// 判断邮寄自提下单结束
let canEnd = this.data.from == "cart"?false:true;
if (!canEnd) {
// 还有自提的产品
if (this.data.pageUseType==0 && this.data.allProduct.some(x=>x.user_select_type==1)) {
canEnd = false
} else {
canEnd = true
}
}
if (!canEnd) {
this.setData({
pageUseType: 1
})
this.handleProduct()
return
}
// 真下单
let product_list = [];
for(let info of this.data.allProduct) {
let param = {
type: info.product.type,
product_id: info.product.id,
sku_id: info.sku.id,
// post: this.data.address.id,
product_num: info.productNum,
remark: info.remark,
is_batch_shipment: info.sku.batch_shipment,
compose_sku_id: info.sku.compose_sku_id,
supplier_id: info.product.supplier_id,
}
if (info.user_select_type == 0) {
param.use_type = 0
param.post = this.data.address.id
}
if (info.user_select_type == 1) {
param.use_type = 1
param.pickup_shop_id = info.pickupAddress.id
param.pickup_shop_info = info.pickupAddress
param.phone = info.phone
}
product_list.push(param)
}
let data = {
source: "WECHATXCX",
product_list: product_list,
coupon_id: this.data.coupon ? this.data.coupon.id : null,
originate_order_id: this.data.kjId,
gp_id: app.globalData.gp_id,
team_id: app.globalData.team_id
};
if (this.data.select_allowance) data.is_allowance = 1
if (app.globalData.from) {
data.system_name = app.globalData.from;
}
if (app.globalData.retailId) {
data.commission_code = app.globalData.retailId;
app.globalData.retailId = null;
}
if (this.data.prizeId) {
data.lottery_id = this.data.prizeId
}
if (app.globalData.category_id) {
data.category_id = app.globalData.category_id;
}
let ZTPointProduct = this.data.product.find(v=>v.product&&v.product.ZTPoint)
if (ZTPointProduct) {
data.partner_code = ZTPointProduct.product.ZTPoint
data.channel = ZTPointProduct.product.ZTPoint
}
console.log(app.globalData.list,this.data.flag)
if(!app.globalData.list || app.globalData.list.length == 0){
// 判断混合下单
let use_type = product_list[0].use_type
if (product_list.some(x=>x.use_type!=use_type)) {
app.globalData.productPrice = (this.data.showPrice + this.data.postFee - (this.data.coupon?this.data.coupon.activity.money:0))/100;
app.globalData.createDate = data
wx.redirectTo({
url: '/pages/order/orderList/index?flag=mix'
})
return
}
commonApi.user_post("order/create", {
data: JSON.stringify(data)
}).then(res => {
if (app.globalData.kjId) {
app.globalData.kjId = null;
this.setData({
kjId: null
})
// this.onLoad({})
}
app.globalData.gp_id = null;
app.globalData.team_id = null;
this.setData({
gp_id: null
})
// this.onLoad({});
})
}else{
app.globalData.productPrice = (this.data.showPrice + this.data.postFee - (this.data.coupon?this.data.coupon.activity.money:0))/100;
app.globalData.createDate = data
console.log(app.globalData.product)
if (!app.globalData.product) {
app.globalData.product = app.globalData.list[0]
}
wx.redirectTo({
url: '/pages/order/scene/index?flag='+this.data.flag+'&isCar='+this.data.isCar,
})
}
},
getPostFee: function () {
if (!this.data.address) return;
let list = [], indexList = [],that = this;
let product = this.data.product.filter((x,i)=>{
if (x.user_select_type == 0) {
indexList.push(i)
return x
}
});
if (product.length<=0) {
let postFee = 0;
this.data.product.forEach(x=>{
x.postFee = 0;
})
if (this.data.coupon && this.data.postFee != postFee) {
wx.showToast({
title: '订单价格发生变化,请重新选择优惠券',
icon: 'none'
})
app.globalData.couponInfo = null;
if (!this.data.kjId && !this.data.gp_id) {
this.couponCom.setNullCoupon()
}
this.setData({
coupon: null
})
}
this.setData({
postFee: postFee,
product: this.data.product,
})
return
}
product.map(item => {
list.push(commonApi.user_post("order/get_post_price", {
sku_id: item.sku.id,
num: item.productNum,
consignee_id: that.data.address.id
}))
})
// 获取邮费
Promise.all(list).then(res => {
let postFee = 0, flag = true;
res.map((item, index) => {
if (item.data.price < 0) flag = false;
product[index].postFee = item.data.price;
// 累计总邮费
let postPrice = Number(item.data.price)
if(product[index].sku.batch_shipment == 1) {
postPrice = product[index].sku.batch_count * postPrice
}
postFee = postFee + postPrice;
})
if (!flag) {
wx.showToast({
title: '该地区不发货',
icon: 'none'
})
product.map(item => {
item.postFee = 0;
})
postFee = 0;
}
if (this.data.coupon && this.data.postFee != postFee) {
wx.showToast({
title: '订单价格发生变化,请重新选择优惠券',
icon: 'none'
})
app.globalData.couponInfo = null;
if (!this.data.kjId && !this.data.gp_id) {
this.couponCom.setNullCoupon()
}
this.setData({
coupon: null
})
}
for(let i=0;i<indexList.length;i++) {
this.data.product[indexList[i]] = product[i]
}
this.setData({
postFee: postFee,
product: this.data.product
})
})
},
changeBatchShip (e) {
let product = this.data.product;
let value = e.currentTarget.dataset.value;
let index = e.currentTarget.dataset.index;
product[index].sku.batch_shipment = value;
console.log(index)
this.setData({
product: product
})
this.getPostFee()
this.changePrice()
},
/**
* 选择补贴
*/
selectAllowance: function () {
this.setData({
select_allowance: !this.data.select_allowance
})
this.changePrice()
},
//获取最高优惠券
getNewCoupon(e){
this.setData({
coupon:e.detail
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
// this.setData({
// coupon: app.globalData.couponInfo
// })
// 获取补贴
// if (this.data.product.length == 1) {
// commonApi.user_post("product/getProductAllowancePrice", {
// product_code: this.data.product[0].product.product_code
// }).then(resTwo => {
// console.log(resTwo)
// if (resTwo && resTwo.code == 1) {
// this.setData({
// allowance_data: resTwo.data
// })
// let spread_price = Number(this.data.allowance_data.discount_limit_price - this.data.allowance_data.user_used_price)
// let sInfo = this.data.product[0].sku
// console.log(9999, this.data.allowance_data.discount_rate / 100 * sInfo.price)
// console.log(9999, spread_price)
// if (spread_price > 0) {
// if (spread_price < this.data.allowance_data.discount_rate / 100 * sInfo.price) {
// this.setData({
// allowance_price: spread_price
// })
// } else {
// this.setData({
// allowance_price: this.data.allowance_data.discount_rate / 100 * sInfo.price
// })
// }
// this.changePrice()
// } else {
// this.setData({
// allowance_price: 0
// })
// }
// }
// })
// }
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
this.setData({
kjId: null,
gp_id: null
});
app.globalData.kjId = null;
app.globalData.gp_id = null;
app.globalData.team_id = null;
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
}
})