hios-miniprogram/pages/order-check/index.js
shengliang 65f37e00b6 1、修复一个群友Solver提出的关于体验性改善的问题
2、详情页增加加载动画效果
3、去除一些console.log
2020-03-07 20:21:04 +08:00

196 lines
6.8 KiB
JavaScript
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

var util = require('../../utils/util.js');
var api = require('../../config/api.js');
const pay = require('../../services/pay.js');
const app = getApp()
Page({
data: {
checkedGoodsList: [],
checkedAddress: {},
goodsTotalPrice: 0.00, //商品总价
freightPrice: 0.00, //快递费
orderTotalPrice: 0.00, //订单总价
actualPrice: 0.00, //实际需要支付的总价
addressId: 0,
goodsCount: 0,
postscript: '',
outStock: 0
},
toGoodsList: function (e) {
wx.navigateTo({
url: '/pages/ucenter/goods-list/index?id=0',
});
},
toSelectAddress: function () {
wx.navigateTo({
url: '/pages/ucenter/address/index?type=1',
});
},
toAddAddress: function () {
wx.navigateTo({
url: '/pages/ucenter/address-add/index',
})
},
bindinputMemo(event) {
let postscript = event.detail.value;
this.setData({
postscript: postscript
});
},
onLoad: function (options) {
let addType = options.addtype;
let orderFrom = options.orderFrom;
if (addType != undefined) {
this.setData({
addType: addType
})
}
if (orderFrom != undefined) {
this.setData({
orderFrom: orderFrom
})
}
},
onUnload: function () {
wx.removeStorageSync('addressId');
},
onShow: function () {
// 页面显示
// TODO结算时显示默认地址而不是从storage中获取的地址值
try {
var addressId = wx.getStorageSync('addressId');
if (addressId == 0 || addressId == '') {
addressId = 0;
}
this.setData({
'addressId': addressId
});
} catch (e) {}
this.getCheckoutInfo();
},
onPullDownRefresh: function () {
wx.showNavigationBarLoading()
try {
var addressId = wx.getStorageSync('addressId');
if (addressId == 0 || addressId == '') {
addressId = 0;
}
this.setData({
'addressId': addressId
});
} catch (e) {
// Do something when catch error
}
this.getCheckoutInfo();
wx.hideNavigationBarLoading() //完成停止加载
wx.stopPullDownRefresh() //停止下拉刷新
},
getCheckoutInfo: function () {
let that = this;
let addressId = that.data.addressId;
let orderFrom = that.data.orderFrom;
let addType = that.data.addType;
util.request(api.CartCheckout, {
addressId: that.data.addressId,
addType: addType,
orderFrom: orderFrom,
type: 0
}).then(function (res) {
if (res.errno === 0) {
let addressId = 0;
if (res.data.checkedAddress != 0) {
addressId = res.data.checkedAddress.id;
}
that.setData({
checkedGoodsList: res.data.checkedGoodsList,
checkedAddress: res.data.checkedAddress,
actualPrice: res.data.actualPrice,
addressId: addressId,
freightPrice: res.data.freightPrice,
goodsTotalPrice: res.data.goodsTotalPrice,
orderTotalPrice: res.data.orderTotalPrice,
goodsCount: res.data.goodsCount,
outStock: res.data.outStock
});
let goods = res.data.checkedGoodsList;
wx.setStorageSync('addressId', addressId);
if (res.data.outStock == 1) {
util.showErrorToast('有部分商品缺货或已下架');
} else if (res.data.numberChange == 1) {
util.showErrorToast('部分商品库存有变动');
}
}
});
},
// TODO 有个bug用户没选择地址支付无法继续进行在切换过token的情况下
submitOrder: function (e) {
let formId = e.detail.formId;
if (this.data.addressId <= 0) {
util.showErrorToast('请选择收货地址');
return false;
}
let addressId = this.data.addressId;
let postscript = this.data.postscript;
let freightPrice = this.data.freightPrice;
let actualPrice = this.data.actualPrice;
util.request(api.OrderSubmit, {
addressId: addressId,
postscript: postscript,
freightPrice: freightPrice,
formId: formId,
actualPrice: actualPrice,
offlinePay:0
}, 'POST').then(res => {
// console.log(res);
if (res.errno === 0) {
wx.removeStorageSync('orderId');
wx.setStorageSync('addressId', 0);
const orderId = res.data.orderInfo.id;
pay.payOrder(parseInt(orderId)).then(res => {
wx.redirectTo({
url: '/pages/payResult/payResult?status=1&orderId=' + orderId
});
}).catch(res => {
wx.redirectTo({
url: '/pages/payResult/payResult?status=0&orderId=' + orderId
});
});
} else {
util.showErrorToast(res.errmsg);
}
});
},
offlineOrder: function (e) {
let formId = e.detail.formId;
let offlinePay = e.currentTarget.dataset.off;
if (this.data.addressId <= 0) {
util.showErrorToast('请选择收货地址');
return false;
}
let addressId = this.data.addressId;
let postscript = this.data.postscript;
let freightPrice = this.data.freightPrice;
let actualPrice = this.data.actualPrice;
util.request(api.OrderSubmit, {
addressId: addressId,
postscript: postscript,
freightPrice: freightPrice,
formId: formId,
actualPrice: actualPrice,
offlinePay:offlinePay
}, 'POST').then(res => {
if (res.errno === 0) {
wx.removeStorageSync('orderId');
wx.setStorageSync('addressId', 0);
wx.redirectTo({
url: '/pages/payOffline/index?status=1',
})
} else {
util.showErrorToast(res.errmsg);
wx.redirectTo({
url: '/pages/payOffline/index?status=0',
})
}
});
}
})