纸通宝小程序
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.
 

203 lines
5.5 KiB

// pages/bidding/create/index.js
const request = require('../../../utils/request') //导入模块
const event = require('../../../utils/event.js')
const util = require('../../../utils/util')
const math = require('../../../utils/math')
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
})
request.get('/recycle-service/get/user-product/' + 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(){
request.get('/recycle-service/get/shipping-address-list').then(res => {
res.data.forEach((element) => {
if(element.isDefault===1){
this.setData({
addressInfo: element,
shippingAddressId:element.shippingAddressId
})
}
})
})
},
onUnload: function () {
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
})
request.post('/recycle-service/create/order-trade', model).then(result => {
wx.hideLoading()
event.emit('EventMessage', {
what: 112,
desc: 'OrderCreate'
})
// var url = '/pages/morder/index/index&status=2'
wx.redirectTo({
// url: '/pages/payment/index?type=1&orderId=' + result.data.orderId + '&url=' + url
url: '/pages/morder/detail/index?orderId=' + result.data.orderId
})
}).catch(err => {
wx.hideLoading()
util.showToast(err)
})
}
})