94 lines
2.5 KiB
JavaScript
94 lines
2.5 KiB
JavaScript
const { getProductDetail, createGoodsOrder } = require('../../api/productApi')
|
|
const { listAddresses } = require('../../api/addressApi')
|
|
|
|
Page({
|
|
data: {
|
|
product: null,
|
|
addresses: [],
|
|
addressIndex: 0,
|
|
skuIndex: 0,
|
|
submitting: false,
|
|
form: {
|
|
quantity: 1,
|
|
deliveryMethod: 'IMMEDIATE',
|
|
scheduledTime: '',
|
|
remark: ''
|
|
}
|
|
},
|
|
|
|
onLoad(options) {
|
|
this.productId = Number(options.id)
|
|
this.load()
|
|
},
|
|
|
|
load() {
|
|
Promise.all([getProductDetail(this.productId), listAddresses().catch(() => [])])
|
|
.then(([product, addresses]) => {
|
|
this.setData({
|
|
product,
|
|
addresses: addresses || []
|
|
})
|
|
})
|
|
.catch((error) => wx.showToast({ title: error.message, icon: 'none' }))
|
|
},
|
|
|
|
onSkuChange(event) {
|
|
this.setData({ skuIndex: Number(event.detail.value) })
|
|
},
|
|
|
|
onAddressChange(event) {
|
|
this.setData({ addressIndex: Number(event.detail.value) })
|
|
},
|
|
|
|
onQuantityChange(event) {
|
|
this.setData({ 'form.quantity': Number(event.detail.value || 1) })
|
|
},
|
|
|
|
onMethodChange(event) {
|
|
this.setData({ 'form.deliveryMethod': event.detail.value })
|
|
},
|
|
|
|
onScheduledTimeChange(event) {
|
|
this.setData({ 'form.scheduledTime': event.detail.value })
|
|
},
|
|
|
|
onRemarkInput(event) {
|
|
this.setData({ 'form.remark': event.detail.value })
|
|
},
|
|
|
|
submit() {
|
|
if (!this.data.product || this.data.submitting) {
|
|
return
|
|
}
|
|
const sku = this.data.product.skus[this.data.skuIndex]
|
|
if (!sku) {
|
|
wx.showToast({ title: '请选择规格', icon: 'none' })
|
|
return
|
|
}
|
|
const address = this.data.addresses[this.data.addressIndex]
|
|
if (this.data.form.deliveryMethod !== 'SELF_PICKUP' && !address) {
|
|
wx.showToast({ title: '请先添加地址', icon: 'none' })
|
|
return
|
|
}
|
|
this.setData({ submitting: true })
|
|
createGoodsOrder({
|
|
merchantId: this.data.product.merchantId,
|
|
addressId: address ? address.id : null,
|
|
deliveryMethod: this.data.form.deliveryMethod,
|
|
scheduledTime: this.data.form.scheduledTime || null,
|
|
remark: this.data.form.remark,
|
|
items: [{
|
|
productId: this.data.product.id,
|
|
skuId: sku.id,
|
|
quantity: this.data.form.quantity
|
|
}]
|
|
})
|
|
.then(() => {
|
|
wx.showToast({ title: '已提交' })
|
|
wx.navigateTo({ url: '/pages/orders/index' })
|
|
})
|
|
.catch((error) => wx.showToast({ title: error.message, icon: 'none' }))
|
|
.finally(() => this.setData({ submitting: false }))
|
|
}
|
|
})
|