133 lines
3.6 KiB
JavaScript
133 lines
3.6 KiB
JavaScript
const orderApi = require('../../api/order')
|
|
const afterSaleApi = require('../../api/after-sale')
|
|
const auth = require('../../utils/auth')
|
|
const navigation = require('../../utils/navigation')
|
|
const theme = require('../../utils/theme')
|
|
const { formatPrice } = require('../../utils/format')
|
|
const { runWithLoading } = require('../../utils/loading')
|
|
|
|
function safeList(value) {
|
|
return Array.isArray(value) ? value : []
|
|
}
|
|
|
|
Page({
|
|
data: Object.assign(theme.pageThemeData(getApp().globalData.shopTheme), {
|
|
orderId: '',
|
|
loading: false,
|
|
submitting: false,
|
|
isLoggedIn: false,
|
|
order: null,
|
|
items: [],
|
|
typeOptions: ['退款', '售后服务'],
|
|
form: {
|
|
orderItemId: '',
|
|
type: 'REFUND',
|
|
typeIndex: 0,
|
|
reason: '',
|
|
refundAmount: ''
|
|
}
|
|
}),
|
|
|
|
onLoad(options) {
|
|
this.setData({
|
|
orderId: (options && options.orderId) || '',
|
|
'form.orderItemId': (options && options.orderItemId) || ''
|
|
})
|
|
},
|
|
|
|
onShow() {
|
|
this.loadOrder()
|
|
},
|
|
|
|
handleBack() {
|
|
navigation.goBack('/pages/orders/index')
|
|
},
|
|
|
|
async loadOrder() {
|
|
const isLoggedIn = auth.isLoggedIn()
|
|
this.setData({ isLoggedIn })
|
|
if (!isLoggedIn) {
|
|
navigation.openAuth(navigation.getCurrentPageUrl())
|
|
return
|
|
}
|
|
if (!this.data.orderId) return
|
|
|
|
this.setData({ loading: true })
|
|
try {
|
|
const order = await orderApi.getOrderDetail(this.data.orderId)
|
|
const items = safeList(order && order.items)
|
|
const selectedItem = items.find(item => String(item.id) === String(this.data.form.orderItemId)) || items[0]
|
|
this.setData({
|
|
order,
|
|
items,
|
|
'form.orderItemId': selectedItem ? selectedItem.id : '',
|
|
'form.refundAmount': String(order && (order.actualAmount || order.totalAmount || '')),
|
|
amountText: `¥${formatPrice(order && (order.actualAmount || order.totalAmount))}`
|
|
})
|
|
} catch (error) {
|
|
wx.showToast({
|
|
title: error.message || '订单信息加载失败',
|
|
icon: 'none'
|
|
})
|
|
} finally {
|
|
this.setData({ loading: false })
|
|
}
|
|
},
|
|
|
|
handleItemChange(event) {
|
|
const index = Number(event.detail.value)
|
|
const item = this.data.items[index]
|
|
this.setData({
|
|
'form.orderItemId': item ? item.id : ''
|
|
})
|
|
},
|
|
|
|
handleTypeChange(event) {
|
|
const index = Number(event.detail.value)
|
|
this.setData({
|
|
'form.typeIndex': index,
|
|
'form.type': index === 0 ? 'REFUND' : 'AFTER_SALE'
|
|
})
|
|
},
|
|
|
|
handleInput(event) {
|
|
const field = event.currentTarget.dataset.field
|
|
this.setData({
|
|
[`form.${field}`]: event.detail.value
|
|
})
|
|
},
|
|
|
|
async submit() {
|
|
if (!this.data.orderId) {
|
|
wx.showToast({ title: '缺少订单信息', icon: 'none' })
|
|
return
|
|
}
|
|
const reason = String(this.data.form.reason || '').trim()
|
|
if (!reason) {
|
|
wx.showToast({ title: '请填写售后原因', icon: 'none' })
|
|
return
|
|
}
|
|
this.setData({ submitting: true })
|
|
try {
|
|
await runWithLoading(() => afterSaleApi.applyAfterSale({
|
|
orderId: this.data.orderId,
|
|
orderItemId: this.data.form.orderItemId || undefined,
|
|
type: this.data.form.type,
|
|
reason,
|
|
refundAmount: this.data.form.refundAmount ? Number(this.data.form.refundAmount) : undefined
|
|
}), { title: '提交售后' })
|
|
wx.showToast({ title: '售后申请已提交', icon: 'none' })
|
|
wx.redirectTo({
|
|
url: `/pages/after-sales/index?orderId=${encodeURIComponent(this.data.orderId)}`
|
|
})
|
|
} catch (error) {
|
|
wx.showToast({
|
|
title: error.message || '售后申请提交失败',
|
|
icon: 'none'
|
|
})
|
|
} finally {
|
|
this.setData({ submitting: false })
|
|
}
|
|
}
|
|
})
|