优化:商品及订单优惠价格;支付说明;
This commit is contained in:
@@ -26,53 +26,47 @@
|
||||
<!-- 商品总价 -->
|
||||
<view class="price-item">
|
||||
<text class="label">{{ $t('order.totalPrice') }}</text>
|
||||
<text class="value">{{ totalPrice.toFixed(2) }} 天医币</text>
|
||||
<text class="value">{{ totalAmount.toFixed(2) }} {{ t('global.coin') }}</text>
|
||||
</view>
|
||||
|
||||
<!-- 优惠券 -->
|
||||
<!-- <coupon v-model="selectedCoupon" /> -->
|
||||
|
||||
<!-- 活动立减 -->
|
||||
<view v-if="hasActivityDiscount" class="price-item">
|
||||
<text class="label">{{ $t('order.activityDiscount') }}</text>
|
||||
<text class="discount-value">-¥{{ activityDiscountAmount.toFixed(2) }}</text>
|
||||
<view v-if="promotionDiscounted > 0" class="price-item">
|
||||
<text class="label">{{ $t('order.promotionDiscounted') }}</text>
|
||||
<text class="discount-value">-{{ promotionDiscounted.toFixed(2) }} {{ t('global.coin') }}</text>
|
||||
</view>
|
||||
|
||||
<!-- VIP专享立减 -->
|
||||
<view v-if="vipPrice > 0" class="price-item">
|
||||
<view v-if="vipDiscounted > 0" class="price-item">
|
||||
<view class="label-row">
|
||||
<text class="vip-icon">VIP</text>
|
||||
<text class="label">{{ $t('order.vipDiscount') }}</text>
|
||||
</view>
|
||||
<text class="discount-value">-¥{{ vipPrice.toFixed(2) }}</text>
|
||||
</view>
|
||||
|
||||
<!-- 地区优惠 -->
|
||||
<view v-if="districtAmount > 0" class="price-item">
|
||||
<text class="label">{{ $t('order.districtDiscount') }}</text>
|
||||
<text class="discount-value">-¥{{ districtAmount.toFixed(2) }}</text>
|
||||
<text class="discount-value">-{{ vipDiscounted.toFixed(2) }} {{ t('global.coin') }}</text>
|
||||
</view>
|
||||
|
||||
<!-- 积分 -->
|
||||
<view v-if="allowPointPay && userInfo.jf > 0" class="price-item">
|
||||
<view v-if="allowPointPay && userInfo?.jf > 0" class="price-item">
|
||||
<view class="label-row">
|
||||
<image src="/static/icon/jifen.png" class="icon-img" />
|
||||
<text class="label">{{ $t('order.points') }}</text>
|
||||
<text class="points-total">
|
||||
({{ $t('order.allPoints') }}:{{ userInfo.jf }})
|
||||
({{ $t('order.allPoints') }}:{{ userInfo?.jf || 0 }})
|
||||
</text>
|
||||
</view>
|
||||
<text class="discount-value">-{{ jfNumber.toFixed(2) }}</text>
|
||||
<text class="discount-value">-{{ pointsDiscounted.toFixed(2) }}</text>
|
||||
</view>
|
||||
|
||||
<!-- 积分输入 -->
|
||||
<view v-if="allowPointPay && userInfo.jf > 0" class="points-input-section">
|
||||
<view v-if="allowPointPay && userInfo?.jf > 0" class="points-input-section">
|
||||
<text class="points-label">
|
||||
{{ $t('order.maxPoints', { max: jfNumberMax }) }}
|
||||
{{ $t('order.maxPoints', { max: pointsUsableMax }) }}
|
||||
</text>
|
||||
<view class="points-input-box">
|
||||
<input
|
||||
v-model="jfNumber"
|
||||
v-model="pointsDiscounted"
|
||||
type="number"
|
||||
clearable
|
||||
:placeholder="$t('order.pointsPlaceholder')"
|
||||
@@ -94,7 +88,7 @@
|
||||
<view class="bottom-bar">
|
||||
<view class="total-info">
|
||||
<text class="label">{{ $t('order.total') }}:</text>
|
||||
<text class="amount">{{ actualPayment.toFixed(2) }} 天医币</text>
|
||||
<text class="amount">{{ finalAmount }} {{ t('global.coin') }}</text>
|
||||
</view>
|
||||
<wd-button type="primary" :loading="submitting" @click="handleSubmit">
|
||||
{{ $t('order.submit') }}
|
||||
@@ -120,7 +114,7 @@
|
||||
</view>
|
||||
|
||||
<view class="popup-footer">
|
||||
<wd-button type="primary" block @click="handleRemarkConfirm">{{ $t('global.ok') }}</wd-button>
|
||||
<wd-button type="primary" block @click="showRemarkPopup = false">{{ $t('global.ok') }}</wd-button>
|
||||
</view>
|
||||
</view>
|
||||
</wd-popup>
|
||||
@@ -128,93 +122,66 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, computed, watch } from 'vue'
|
||||
import { ref, watch } from 'vue'
|
||||
import { orderApi } from '@/api/modules/order'
|
||||
import { t } from '@/utils/i18n'
|
||||
import type { IOrderGoods } from '@/types/order'
|
||||
import type { IGoods, IGoodsDiscountParams } from '@/types/order'
|
||||
import PayWay from '@/components/order/PayWay.vue'
|
||||
|
||||
// 使用页面传参
|
||||
interface Props {
|
||||
goodsList: IOrderGoods[],
|
||||
goodsList: IGoods[],
|
||||
userInfo: object,
|
||||
allowPointPay: boolean,
|
||||
orderType: string,
|
||||
}
|
||||
const props = withDefaults(defineProps<Props>(), {
|
||||
goodsList: () => [],
|
||||
userInfo: () => ({}),
|
||||
allowPointPay: () => true
|
||||
allowPointPay: () => true,
|
||||
orderType: () => 'order',
|
||||
})
|
||||
|
||||
// 价格相关
|
||||
const totalPrice = ref(0)
|
||||
const vipPrice = ref(0)
|
||||
const districtAmount = ref(0)
|
||||
const actualPayment = ref(0)
|
||||
|
||||
// 积分相关
|
||||
const jfNumber = ref(0)
|
||||
const jfNumberMax = ref(0)
|
||||
|
||||
// 优惠券相关
|
||||
|
||||
|
||||
// 订单备注
|
||||
const remark = ref('')
|
||||
const showRemarkPopup = ref(false)
|
||||
|
||||
// 价格相关
|
||||
const totalAmount = ref(0) // 商品总价
|
||||
const vipDiscounted = ref(0) // VIP专享立减
|
||||
const promotionDiscounted = ref(0) // 活动优惠金额
|
||||
const couponDiscounted = ref(0) // 优惠券优惠金额
|
||||
const finalAmount = ref<string | number>('--') // 最终支付金额
|
||||
|
||||
// 积分相关
|
||||
const pointsDiscounted = ref(0) // 积分支付金额
|
||||
const pointsUsableMax = ref(0) // 最大可用积分
|
||||
|
||||
// 优惠券相关
|
||||
|
||||
/**
|
||||
* 优惠价格的请求参数
|
||||
*/
|
||||
const goodsListParams = ref<IGoodsDiscountParams[]>([])
|
||||
const getDiscountParams = (goodsList: IGoods[]) => {
|
||||
goodsListParams.value = goodsList.map(item => ({
|
||||
productId: item.productId,
|
||||
quantity: item.productAmount || 1
|
||||
}))
|
||||
}
|
||||
|
||||
|
||||
// 支付相关
|
||||
|
||||
// UI状态
|
||||
const submitting = ref(false)
|
||||
|
||||
watch(() => props.goodsList, () => {
|
||||
getDiscountParams(props.goodsList)
|
||||
// 初始化价格
|
||||
calculateAllPrices()
|
||||
})
|
||||
|
||||
/**
|
||||
* 是否有活动优惠
|
||||
*/
|
||||
const hasActivityDiscount = computed(() => {
|
||||
return props.goodsList.some(item => item.activityPrice && item.activityPrice > 0)
|
||||
})
|
||||
|
||||
/**
|
||||
* 活动优惠金额
|
||||
*/
|
||||
const activityDiscountAmount = computed(() => {
|
||||
return props.goodsList.reduce((sum, item) => {
|
||||
if (item.activityPrice && item.activityPrice > 0) {
|
||||
return sum + (item.price - item.activityPrice) * item.productAmount
|
||||
}
|
||||
return sum
|
||||
}, 0)
|
||||
})
|
||||
|
||||
/**
|
||||
* 处理商品数量变化
|
||||
*/
|
||||
const handleQuantityChange = async (item: IOrderGoods, value: number) => {
|
||||
try {
|
||||
// 更新购物车
|
||||
await orderApi.updateCart({
|
||||
productId: item.productId,
|
||||
productAmount: value,
|
||||
price: item.price
|
||||
})
|
||||
|
||||
// 重新获取商品列表和计算价格
|
||||
// await getGoodsList()
|
||||
} catch (error) {
|
||||
console.error('更新数量失败:', error)
|
||||
uni.showToast({
|
||||
title: '更新失败',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算所有价格
|
||||
*/
|
||||
@@ -223,15 +190,17 @@ const calculateAllPrices = async () => {
|
||||
// 计算商品总价
|
||||
calculateTotalPrice()
|
||||
|
||||
// 获取VIP优惠
|
||||
// await calculateVipDiscount()
|
||||
|
||||
// 获取地区优惠
|
||||
// await calculateDistrictDiscount()
|
||||
|
||||
// 获取优惠券列表
|
||||
// await getAvailableCoupons()
|
||||
|
||||
await Promise.all([
|
||||
// 获取VIP优惠
|
||||
calculateVipDiscounted(),
|
||||
|
||||
// 获取活动优惠
|
||||
calculatePromotionDiscounted()
|
||||
|
||||
// 获取优惠券列表
|
||||
// await getAvailableCoupons()
|
||||
])
|
||||
|
||||
// 计算最终价格
|
||||
calculateFinalPrice()
|
||||
} catch (error) {
|
||||
@@ -243,8 +212,7 @@ const calculateAllPrices = async () => {
|
||||
* 计算商品总价
|
||||
*/
|
||||
const calculateTotalPrice = () => {
|
||||
console.log('商品列表:', props.goodsList)
|
||||
totalPrice.value = props.goodsList.reduce((sum, item) => {
|
||||
totalAmount.value = props.goodsList.reduce((sum: number, item: IGoods) => {
|
||||
// return sum + (item.price * item.productAmount)
|
||||
return sum + (item.price * 1)
|
||||
}, 0)
|
||||
@@ -253,17 +221,10 @@ const calculateTotalPrice = () => {
|
||||
/**
|
||||
* 计算VIP优惠
|
||||
*/
|
||||
const calculateVipDiscount = async () => {
|
||||
try {
|
||||
const productList = props.goodsList.map(item => ({
|
||||
productId: item.productId,
|
||||
quantity: item.productAmount
|
||||
}))
|
||||
|
||||
const res = await orderApi.getVipDiscountAmount(productList)
|
||||
if (res.code === 0 && res.data) {
|
||||
vipPrice.value = res.data.discountAmount
|
||||
}
|
||||
const calculateVipDiscounted = async () => {
|
||||
try {
|
||||
const res = await orderApi.getVipDiscountAmount(goodsListParams.value)
|
||||
vipDiscounted.value = res.discountAmount
|
||||
} catch (error) {
|
||||
console.error('获取VIP优惠失败:', error)
|
||||
}
|
||||
@@ -272,17 +233,10 @@ const calculateVipDiscount = async () => {
|
||||
/**
|
||||
* 计算活动优惠
|
||||
*/
|
||||
const calculateDistrictDiscount = async () => {
|
||||
try {
|
||||
const productList = props.goodsList.map(item => ({
|
||||
productId: item.productId,
|
||||
quantity: item.productAmount
|
||||
}))
|
||||
|
||||
const res = await orderApi.getDistrictAmount(productList)
|
||||
if (res.code === 0 && res.data) {
|
||||
districtAmount.value = res.data.districtAmount
|
||||
}
|
||||
const calculatePromotionDiscounted = async () => {
|
||||
try {
|
||||
const res = await orderApi.getDistrictAmount(goodsListParams.value)
|
||||
promotionDiscounted.value = res.districtAmount
|
||||
} catch (error) {
|
||||
console.error('获取地区优惠失败:', error)
|
||||
}
|
||||
@@ -291,57 +245,31 @@ const calculateDistrictDiscount = async () => {
|
||||
/**
|
||||
* 获取可用优惠券
|
||||
*/
|
||||
const getAvailableCoupons = async () => {
|
||||
try {
|
||||
const shopProductInfos = props.goodsList.map(item => {
|
||||
const price = item.activityPrice && item.activityPrice > 0 ? item.activityPrice : item.price
|
||||
return `${item.productId}:${price}:${item.productAmount}`
|
||||
}).join(',')
|
||||
// const getAvailableCoupons = async () => {
|
||||
// try {
|
||||
// const shopProductInfos = props.goodsList.map(item => {
|
||||
// const price = item.activityPrice && item.activityPrice > 0 ? item.activityPrice : item.price
|
||||
// return `${item.productId}:${price}:${item.productAmount}`
|
||||
// }).join(',')
|
||||
|
||||
const res = await orderApi.getCouponListPayment(shopProductInfos)
|
||||
if (res.code === 0 && res.data) {
|
||||
couponList.value = res.data.couponHistoryList || []
|
||||
// const res = await orderApi.getCouponListPayment(shopProductInfos)
|
||||
// if (res.code === 0 && res.data) {
|
||||
// couponList.value = res.data.couponHistoryList || []
|
||||
|
||||
// 如果当前选中的优惠券不可用,清除选择
|
||||
if (selectedCoupon.value) {
|
||||
const stillAvailable = couponList.value.find(
|
||||
c => c.couponId === selectedCoupon.value?.couponId && c.canUse === 1
|
||||
)
|
||||
if (!stillAvailable) {
|
||||
selectedCoupon.value = null
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取优惠券失败:', error)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算最终价格
|
||||
*/
|
||||
const calculateFinalPrice = () => {
|
||||
// const couponAmount = selectedCoupon.value?.couponEntity.couponAmount || 0
|
||||
const couponAmount = 0
|
||||
|
||||
// 计算最大可用积分
|
||||
const orderAmountAfterDiscount = totalPrice.value - districtAmount.value - vipPrice.value
|
||||
jfNumberMax.value = Math.min(
|
||||
props.userInfo.jf || 0,
|
||||
Math.floor(orderAmountAfterDiscount - couponAmount)
|
||||
)
|
||||
|
||||
// 限制当前积分不超过最大值
|
||||
if (jfNumber.value > jfNumberMax.value) {
|
||||
jfNumber.value = jfNumberMax.value
|
||||
}
|
||||
|
||||
// 计算实付款
|
||||
actualPayment.value = Math.max(
|
||||
0,
|
||||
totalPrice.value - couponAmount - jfNumber.value - districtAmount.value - vipPrice.value
|
||||
)
|
||||
}
|
||||
// // 如果当前选中的优惠券不可用,清除选择
|
||||
// if (selectedCoupon.value) {
|
||||
// const stillAvailable = couponList.value.find(
|
||||
// c => c.couponId === selectedCoupon.value?.couponId && c.canUse === 1
|
||||
// )
|
||||
// if (!stillAvailable) {
|
||||
// selectedCoupon.value = null
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// } catch (error) {
|
||||
// console.error('获取优惠券失败:', error)
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* 处理积分输入
|
||||
@@ -353,7 +281,7 @@ const handlePointsInput = (value: any) => {
|
||||
val = val.replace(/[^0-9]/g, '')
|
||||
|
||||
if (val === '0' || val === '') {
|
||||
jfNumber.value = 0
|
||||
pointsDiscounted.value = 0
|
||||
} else {
|
||||
let numericValue = parseInt(val, 10)
|
||||
if (numericValue < 0 || isNaN(numericValue)) {
|
||||
@@ -361,11 +289,11 @@ const handlePointsInput = (value: any) => {
|
||||
}
|
||||
|
||||
// 确保不超过最大值
|
||||
if (numericValue >= jfNumberMax.value) {
|
||||
numericValue = jfNumberMax.value
|
||||
if (numericValue >= pointsUsableMax.value) {
|
||||
numericValue = pointsUsableMax.value
|
||||
}
|
||||
|
||||
jfNumber.value = numericValue
|
||||
pointsDiscounted.value = numericValue
|
||||
}
|
||||
|
||||
// 重新计算实付款
|
||||
@@ -376,32 +304,52 @@ const handlePointsInput = (value: any) => {
|
||||
* 清除积分输入
|
||||
*/
|
||||
const handlePointsClear = () => {
|
||||
jfNumber.value = 0
|
||||
pointsDiscounted.value = 0
|
||||
calculateFinalPrice()
|
||||
}
|
||||
|
||||
/**
|
||||
* 跳转到充值页面
|
||||
* 计算最终价格
|
||||
*/
|
||||
const goToRecharge = () => {
|
||||
uni.navigateTo({
|
||||
url: '/pages/user/wallet/recharge/index?source=order'
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 确认备注
|
||||
*/
|
||||
const handleRemarkConfirm = () => {
|
||||
showRemarkPopup.value = false
|
||||
const calculateFinalPrice = () => {
|
||||
// const couponAmount = selectedCoupon.value?.couponEntity.couponAmount || 0
|
||||
const couponAmount = 0
|
||||
|
||||
// 计算最大可用积分
|
||||
const orderAmountAfterDiscount = totalAmount.value - promotionDiscounted.value - vipDiscounted.value
|
||||
pointsUsableMax.value = Math.min(
|
||||
props?.userInfo?.jf || 0,
|
||||
Math.floor(orderAmountAfterDiscount - couponAmount)
|
||||
)
|
||||
|
||||
// 限制当前积分不超过最大值
|
||||
if (pointsDiscounted.value > pointsUsableMax.value) {
|
||||
pointsDiscounted.value = pointsUsableMax.value
|
||||
}
|
||||
|
||||
// 计算实付款
|
||||
const result = Math.max(
|
||||
0,
|
||||
totalAmount.value - couponAmount - pointsDiscounted.value - promotionDiscounted.value - vipDiscounted.value
|
||||
)
|
||||
finalAmount.value = result.toFixed(2)
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证订单
|
||||
*/
|
||||
const validateOrder = (): boolean => {
|
||||
// 验证实付金额是否计算完成
|
||||
if (typeof finalAmount.value != 'number') {
|
||||
uni.showToast({
|
||||
title: t('order.invalidPaymentAmount'),
|
||||
icon: 'none'
|
||||
})
|
||||
return false
|
||||
}
|
||||
|
||||
// 验证天医币余额
|
||||
if (actualPayment.value > props.userInfo.peanutCoin) {
|
||||
if (finalAmount.value > props.userInfo.peanutCoin) {
|
||||
uni.showToast({
|
||||
title: t('order.insufficientBalance'),
|
||||
icon: 'none'
|
||||
@@ -425,9 +373,7 @@ const handleSubmit = async () => {
|
||||
}
|
||||
|
||||
// 验证订单
|
||||
if (!validateOrder()) {
|
||||
return
|
||||
}
|
||||
if (!validateOrder()) return
|
||||
|
||||
try {
|
||||
submitting.value = true
|
||||
@@ -459,31 +405,27 @@ const createOrder = async (): Promise<string | null> => {
|
||||
const orderParams = {
|
||||
userId: props.userInfo.id,
|
||||
paymentMethod: 4, // 天医币
|
||||
orderMoney: totalPrice.value,
|
||||
realMoney: actualPayment.value,
|
||||
jfDeduction: jfNumber.value,
|
||||
orderMoney: totalAmount.value,
|
||||
realMoney: finalAmount.value,
|
||||
pointsDeduction: pointsDiscounted.value,
|
||||
// couponId: selectedCoupon.value?.id,
|
||||
// couponName: selectedCoupon.value?.couponEntity.couponName,
|
||||
// vipDiscountAmount: vipPrice.value,
|
||||
// districtMoney: districtAmount.value,
|
||||
vipDiscountAmount: vipDiscounted.value,
|
||||
districtMoney: promotionDiscounted.value,
|
||||
remark: remark.value,
|
||||
productList: props.goodsList.map(item => ({
|
||||
productId: item.productId,
|
||||
quantity: item.productAmount || 1
|
||||
})),
|
||||
orderType: 'order',
|
||||
productList: goodsListParams.value,
|
||||
orderType: props.orderType,
|
||||
come: 10
|
||||
}
|
||||
|
||||
const res = await orderApi.placeCourseOrder(orderParams)
|
||||
|
||||
if (res.code === 0 && res.orderSn) {
|
||||
if (res.orderSn) {
|
||||
return res.orderSn
|
||||
}
|
||||
|
||||
return null
|
||||
} catch (error) {
|
||||
console.error('创建订单失败:', error)
|
||||
console.error('下单失败:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user