import { createTradeOrder, getUserProduct, getAddressList } from "../../../../api/ztb" const event = require('../../../../utils/event') const util = require('../../../../utils/util') const math = require('../../../../utils/math') const tdsdk = require('../../../../libs/tdweapp') const app = getApp() Page({ data: { kg: app.globalData.kg, productId: '', productInfo: null, totalMoney: 0, quantity: 1, shippingAddressId:'', adressList:[], nuitPrice: null, addressInfo: null, stock: 0, }, onLoad: function (options) { if(options.productId){ this.data.productId = options.productId if(!util.isEmpty(options.price)){ this.data.unitPrice = Number(options.price) } this.getProductDetails() this.getAdressList() } event.on('EventMessage', this, this.onEvent) }, onEvent: function (message) { if (message.what == 301) { // console.log(message.obj) this.setData({ addressInfo: message.obj, shippingAddressId:message.obj.shippingAddressId }) } }, getProductDetails: function () { wx.showLoading({ title: '加载中', mask: true }) getUserProduct(this.data.productId).then(result => { if(!this.data.unitPrice){ this.data.unitPrice = result.data.unitPrice } var money = 0 var stock = app.globalData.kg ? result.data.stock : math.divide(result.data.stock, 1000) if(util.isEmpty(result.data.lowestNum)){ result.data.lowestNum = app.globalData.kg ? 1000 : 1 } else { if(!app.globalData.kg){ result.data.lowestNum = Number(math.divide(result.data.lowestNum, 1000)).toFixed(2) } } var quantity = result.data.bidType == 2 ? stock : result.data.lowestNum if(result.data.bidType == 2){ if(app.globalData.kg){ money = math.times(stock, this.data.unitPrice) } else { money = math.times(stock, math.times(1000, this.data.unitPrice)) } } else { if(app.globalData.kg){ money = math.times(quantity, this.data.unitPrice) } else { money = math.times(quantity, math.times(1000, this.data.unitPrice)) } } this.setData({ kg: app.globalData.kg, productInfo: result.data, stock: stock, unitPrice: this.data.unitPrice, quantity: quantity, totalMoney: money }) wx.hideLoading() if (!app.globalData.userInfo.isVIP) { this.wuxDialog.open({ resetOnClose: true, title: '温馨提示', content: '您还不是纸通宝的会员,无法下单,现在去开通会员?', buttons: [{ text: '确定', type: 'primary', onTap(e) { wx.navigateTo({ url: '/pages/ztbvip/index' }) } }] }) } }).catch(err => { //异常回调 wx.hideLoading() util.showToast(err) }) }, // 数量变化的函数监听 onNumChange: function (e) { if (e.detail && this.data.productInfo.bidType == 1) { var money = 0 if(app.globalData.kg){ money = math.times(e.detail.value, this.data.unitPrice) } else { money = math.times(e.detail.value, math.times(1000, this.data.unitPrice)) } this.setData({ quantity: e.detail.value, totalMoney: money }) } }, getAdressList:function(){ getAddressList().then(res => { res.data.forEach((element) => { if(element.isDefault===1){ this.setData({ addressInfo: element, shippingAddressId:element.shippingAddressId }) } }) }) }, onShow: function () { tdsdk.Page.onShow() }, onHide: function(){ tdsdk.Page.onHide() }, onUnload: function () { tdsdk.Page.onUnload() event.remove('EventMessage', this) }, toCreateOrder:function(){ if (!app.globalData.userInfo.isVIP) { this.wuxDialog.open({ resetOnClose: true, title: '温馨提示', content: '您还不是纸通宝的会员,无法下单,现在去开通会员?', buttons: [{ text: '确定', type: 'primary', onTap(e) { wx.navigateTo({ url: '/pages/ztbvip/index' }) } }] }) return } if(this.data.quantity <= 0){ util.showToast('请输入购买数量') return } if(this.data.addressInfo===null){ util.showToast('请输入收货地址') return } var model = { productId: this.data.productInfo.productId, quantity: app.globalData.kg ? this.data.quantity : math.times(this.data.quantity, 1000), shippingAddressId: this.data.shippingAddressId } wx.showLoading({ title: '处理中', mask: true }) createTradeOrder(model).then(result => { wx.hideLoading() event.emit('EventMessage', { what: 112, desc: 'OrderCreate' }) wx.redirectTo({ url: '/submodel/pages/morder/detail/index?orderId=' + result.data.orderId }) }).catch(err => { wx.hideLoading() util.showToast(err) }) } })