Files
CompanyProject/衣莎项目/源码/YiShaXiYi/mall/placeOrder/placeOrder.js
2024-09-27 01:32:49 +08:00

373 lines
9.1 KiB
JavaScript

// mall/placeOrder/placeOrder.js
const app = getApp();
const regeneratorRuntime = app.loadAsync()
const model = app.loadModel("index")
const http = app.loadHttp()
Page({
/**
* 页面的初始数据
*/
data: {
system: app.globalData.system, //设备相关信息
pageTitle: "提交订单", //头部标题
isBack: true,//显示返回
mode: "1",//收货方式 1门店 2收货
remark: "",//订单备注
address: {
all: "请选择收货地址",
userName: "姓名",
telNumber: "手机号"
},//收货地址
store: {
name: "亲! 请选择门店地址",
address: "选择门店地址"
},//门店地址信息
temCart: {},//购物车勾选的商品信息
oldCart: null,//购物车勾选中的信息原始样本
coupon: {},//优惠券
all: "",//总价
userInfo: {//用户姓名手机号 包河区 新艺苑
name: "",
tel: ""
},
giftCard: {},//卡劵
isgiftCard: "",//拼接的卡劵信息方便操作
isPay: 2,//1卡支付 2微信支付
postage: {},//邮费配置
},
// 订单备注
remarkInput(e) {
this.setData({
remark: e.detail.value
})
},
// 选择支付方式
selectPay(e) {
let type = e.currentTarget.dataset.type
let url = e.currentTarget.dataset.url
if (type == 2) {
this.setData({
isgiftCard: "",
giftCard: {},
isPay: type
})
} else if (type == 1) {
wx.navigateTo({
url: url
})
}
this._calculation()
},
// 获取选择的门店
async getStore() {
let res = await model.storage.getSetting("storeChange")
if (!!res) {
model.storage.deleteStorage('storeChange')
console.log(res)
if (JSON.stringify(res.store) == '{}'){
this.setData({
store: {
name: "未选择地址",
address: "您未选择任何门店,请留意,客服电话!!!"
},
userInfo: {
name: res.name,
tel: res.tel
}
})
}else{
this.setData({
store: res.store,
userInfo: {
name: res.name,
tel: res.tel
}
})
}
}
},
// 获取默认收货人
async getConsignee() {
let res = await http.http.request2({ url: `${http.api.defaultUserAddress}`, method: "POST" })
let add = res.data.data
if (!!add.id) {
add.all = add.provinceName + add.cityName + add.countryName + add.detail
this.setData({
address: add
})
}
},
// 选择收货方式
wayDelivery(e) {
let mode = e.currentTarget.dataset.mode
this.setData({
mode: mode
})
},
// 选择收货地址
receivingAdd() {
if (this.data.mode == 1) {
wx.navigateTo({
url: "/mall/chooseStores/chooseStores"
})
return
}
wx.navigateTo({
url: "/user/addList/addList"
})
},
// 获取收货地址并且赋值到页面
async getAdd() {
let data = await model.storage.getSetting("address")
if (!!data) {
model.storage.deleteStorage('address')
data.all = data.provinceName + data.cityName + data.countyName + data.detailInfo
this.setData({
address: data || ""
})
// If the wechat address is chosen and the back end doesn't want to handle it,
// let me add the address interface and add it as the default address.
if (!data.id) {
let addData = {
userName: data.userName,
telNumber: data.telNumber,
provinceName: data.provinceName,
cityName: data.cityName,
countryName: data.countyName,
address: data.all,
detail: data.detailInfo,
is_default: 1
}
http.http.request2({ url: `${http.api.userAddress}`, method: "POST", data: addData })
}
}
},
// 获取优惠券
async getCoupon() {
let coupon = await model.storage.getSetting("coupon")
if (!!coupon) {
model.storage.deleteStorage('coupon')
this.setData({
coupon: coupon
})
}
this._calculation()
},
// 获取卡劵
async getGiftCard() {
let giftCard = await model.storage.getSetting("giftCard")
if (!!giftCard) {
model.storage.deleteStorage('giftCard')
this.setData({
giftCard: giftCard,
isgiftCard: giftCard.title + " " + giftCard.price + '折',
isPay: 1
})
}
this._calculation()
},
// 计算商品优惠完的总价格
// 总 - 优惠券 + 运费
_calculation() {
let temCart = this.data.temCart
let coupon = this.data.coupon.price || 0
let postage = this.data.postage
let all = temCart.total - coupon
if (all <= parseFloat(postage.satisfy_money)) {
all += parseFloat(postage.postage_money)
}
if (this.data.mode == 1){
all-=10
}
this.setData({
all: all.toFixed(2)
})
},
// 商城下单
async storeOrder(e) {
let formId = e.detail.formId
if (!this.data.userInfo.tel && this.data.mode == 1) {
model.msg.dataMsg({
type: "warning",
content: "请选择收货手机号"
})
return
}
if (!this.data.address.all && this.data.mode == 2) {
model.msg.dataMsg({
type: "warning",
content: "请选择收货地址"
})
return
}
let products = this.data.temCart.allCart
let oldCart = this.data.oldCart
let arr3 = []
products.map((v, i) => {
let data = {
store_product_id: v.store_product_id,
count: v.count,
attr: v.attr,
image: v.image
}
arr3[i] = data
})
let data = {
take_type: 1,
shop_id: 0,
username: "",
telnum: 0,
address_id: 0,
products: arr3,
remark: this.data.remark
}
if (this.data.mode == 1) {
data.take_type = 2
data.username = this.data.userInfo.name
data.telnum = this.data.userInfo.tel
if (this.data.store.id !== 'null' || this.data.store.id !== 'undefined'){
data.shop_id = this.data.store.id || 0
}
} else if (this.data.mode == 2) {
data.address_id = this.data.address.id
}
let res = await http.http.request2({ url: `${http.api.storeOrder}`, method: "POST", data: data })
// 下单成功时候就清除购物车
let rel = res.data.data
// 如果下单成功掉支付
if (rel.pass) {
model.shopCart.deleteCart(oldCart)
let payData = {
orderID: rel.store_order_id,
payType: this.data.isPay,
form_id: formId,
userCouponID: 0,
userCardID: 0
}
// 优惠券
if (!!this.data.coupon.id) {
payData.userCouponID = this.data.coupon.id
}
// 卡
if (this.data.isPay == 1) {
payData.userCardID = this.data.giftCard.id
}
// 吊起支付
let pay = await model.payment.pay(http.api.pre_order, payData)
if(pay == 3) return;
if (pay == 1) {
wx.redirectTo({
url: "/mall/paid/paid?name=支付成功"
})
return
}
setTimeout(() => {
wx.redirectTo({
url: "/mall/paid/paid?name=待支付"
})
}, 800)
}
},
// 获取当前邮费配置
async postage() {
let res = await http.http.request2({ url: `${http.api.postage}`, method: "POST", data: { type: 1 } })
if (res.data.status) {
this.setData({
postage: res.data.data
})
}
this._calculation()
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.getConsignee()
var that = this
app.setWatching('temCart', (n) => {
// 深拷贝,保留一份原始选中,方便删除
var _obj = JSON.stringify(n),
objClone = JSON.parse(_obj);
let temCart = n
let allCart = temCart.allCart
let attr = ""
allCart.map((v, i) => {
allCart[i].count = allCart[i].num
allCart[i].store_product_id = allCart[i].id
if (!(typeof v.attr == "string")) {
if (v.attr.length > 0) {
v.attr.map((v, i) => {
attr += v + ","
})
allCart[i].attr = attr.substr(0, attr.length - 1)
} else {
allCart[i].attr = ""
}
attr = ""
}
})
temCart.allCart = allCart
// temCart.total = (temCart.total).toFixed(2)
that.setData({
temCart: temCart,
oldCart: objClone
})
});
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
this.postage()
this.getAdd()
this.getCoupon()
this.getStore()
this.getGiftCard()
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})