Compare commits
7 Commits
4ffe3a8f4b
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
| 5ff710c6a0 | |||
| 6b1e506f43 | |||
| 4a45bc5242 | |||
| 0628a0f6b4 | |||
| 8b902be603 | |||
| 37ab933849 | |||
| e58a2fd915 |
@@ -2,21 +2,21 @@
|
|||||||
export const ENV_CONFIG = {
|
export const ENV_CONFIG = {
|
||||||
// 开发环境
|
// 开发环境
|
||||||
development: {
|
development: {
|
||||||
// API_BASE_URL: 'http://127.0.0.1:9200/api',
|
API_BASE_URL: 'http://127.0.0.1:9200/api',
|
||||||
API_BASE_URL: 'https://glt-api.websoft.top/api',
|
// API_BASE_URL: 'https://glt-api2.websoft.top/api',
|
||||||
APP_NAME: '开发环境',
|
APP_NAME: '开发环境',
|
||||||
DEBUG: 'true',
|
DEBUG: 'true',
|
||||||
},
|
},
|
||||||
// 生产环境
|
// 生产环境
|
||||||
production: {
|
production: {
|
||||||
API_BASE_URL: 'https://glt-api.websoft.top/api',
|
API_BASE_URL: 'https://glt-api2.websoft.top/api',
|
||||||
APP_NAME: '桂乐淘',
|
APP_NAME: '桂乐淘',
|
||||||
DEBUG: 'false',
|
DEBUG: 'false',
|
||||||
},
|
},
|
||||||
// 测试环境
|
// 测试环境
|
||||||
test: {
|
test: {
|
||||||
// API_BASE_URL: 'http://127.0.0.1:9200/api',
|
// API_BASE_URL: 'http://127.0.0.1:9200/api',
|
||||||
API_BASE_URL: 'https://glt-api.websoft.top/api',
|
API_BASE_URL: 'https://glt-api2.websoft.top/api',
|
||||||
APP_NAME: '测试环境',
|
APP_NAME: '测试环境',
|
||||||
DEBUG: 'true',
|
DEBUG: 'true',
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ export interface GltTicketTemplate {
|
|||||||
unitName?: string;
|
unitName?: string;
|
||||||
// 最小购买数量
|
// 最小购买数量
|
||||||
minBuyQty?: number;
|
minBuyQty?: number;
|
||||||
|
// 购买步长(如:5 的倍数)
|
||||||
|
step?: number;
|
||||||
// 起始发送数量
|
// 起始发送数量
|
||||||
startSendQty?: number;
|
startSendQty?: number;
|
||||||
// 买赠:买1送4 => gift_multiplier=4
|
// 买赠:买1送4 => gift_multiplier=4
|
||||||
|
|||||||
@@ -81,6 +81,8 @@ export interface ShopGoods {
|
|||||||
isNew?: number;
|
isNew?: number;
|
||||||
// 库存
|
// 库存
|
||||||
stock?: number;
|
stock?: number;
|
||||||
|
// 步长
|
||||||
|
step?: number;
|
||||||
// 商品重量
|
// 商品重量
|
||||||
goodsWeight?: number;
|
goodsWeight?: number;
|
||||||
// 消费赚取积分
|
// 消费赚取积分
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import {useEffect, useState} from "react";
|
import {useEffect, useMemo, useState} from "react";
|
||||||
import {
|
import {
|
||||||
Image,
|
Image,
|
||||||
Button,
|
Button,
|
||||||
@@ -82,17 +82,62 @@ const OrderConfirm = () => {
|
|||||||
const [selectedStore, setSelectedStore] = useState<ShopStore | null>(getSelectedStoreFromStorage())
|
const [selectedStore, setSelectedStore] = useState<ShopStore | null>(getSelectedStoreFromStorage())
|
||||||
|
|
||||||
const router = Taro.getCurrentInstance().router;
|
const router = Taro.getCurrentInstance().router;
|
||||||
const goodsId = router?.params?.goodsId;
|
const params = router?.params || ({} as Record<string, any>)
|
||||||
|
const goodsIdParam = params?.goodsId
|
||||||
|
const orderDataRaw = params?.orderData
|
||||||
|
|
||||||
|
type OrderDataParam = {
|
||||||
|
goodsId?: number | string
|
||||||
|
skuId?: number | string
|
||||||
|
quantity?: number | string
|
||||||
|
price?: number | string
|
||||||
|
specInfo?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const orderDataParam: OrderDataParam | null = useMemo(() => {
|
||||||
|
if (!orderDataRaw) return null
|
||||||
|
const rawText = String(orderDataRaw)
|
||||||
|
try {
|
||||||
|
return JSON.parse(decodeURIComponent(rawText)) as OrderDataParam
|
||||||
|
} catch (_e1) {
|
||||||
|
try {
|
||||||
|
return JSON.parse(rawText) as OrderDataParam
|
||||||
|
} catch (_e2) {
|
||||||
|
console.error('orderData 参数解析失败:', orderDataRaw)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [orderDataRaw])
|
||||||
|
|
||||||
|
const resolvedGoodsId = (() => {
|
||||||
|
const id1 = Number(goodsIdParam)
|
||||||
|
if (Number.isFinite(id1) && id1 > 0) return id1
|
||||||
|
const id2 = Number(orderDataParam?.goodsId)
|
||||||
|
if (Number.isFinite(id2) && id2 > 0) return id2
|
||||||
|
return undefined
|
||||||
|
})()
|
||||||
|
|
||||||
|
const resolvedSkuId = (() => {
|
||||||
|
const n = Number(orderDataParam?.skuId)
|
||||||
|
return Number.isFinite(n) && n > 0 ? n : undefined
|
||||||
|
})()
|
||||||
|
|
||||||
|
const quantityFromParam = (() => {
|
||||||
|
const n = Number(orderDataParam?.quantity)
|
||||||
|
return Number.isFinite(n) && n > 0 ? Math.floor(n) : undefined
|
||||||
|
})()
|
||||||
|
|
||||||
// 页面级兜底:未登录直接进入下单页时,引导去注册/登录并回跳
|
// 页面级兜底:未登录直接进入下单页时,引导去注册/登录并回跳
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!goodsId) {
|
// 兼容 goodsId / orderData 两种进入方式(goodsDetail 有规格时会走 orderData)
|
||||||
// 也可能是 orderData 模式;这里只做最小兜底
|
const backUrl =
|
||||||
if (!ensureLoggedIn('/shop/orderConfirm/index')) return
|
orderDataRaw
|
||||||
return
|
? `/shop/orderConfirm/index?orderData=${orderDataRaw}`
|
||||||
}
|
: resolvedGoodsId
|
||||||
if (!ensureLoggedIn(`/shop/orderConfirm/index?goodsId=${goodsId}`)) return
|
? `/shop/orderConfirm/index?goodsId=${resolvedGoodsId}`
|
||||||
}, [goodsId])
|
: '/shop/orderConfirm/index'
|
||||||
|
if (!ensureLoggedIn(backUrl)) return
|
||||||
|
}, [resolvedGoodsId, orderDataRaw])
|
||||||
|
|
||||||
const isTicketTemplateActive =
|
const isTicketTemplateActive =
|
||||||
!!ticketTemplate &&
|
!!ticketTemplate &&
|
||||||
@@ -142,7 +187,9 @@ const OrderConfirm = () => {
|
|||||||
// 计算商品总价
|
// 计算商品总价
|
||||||
const getGoodsTotal = () => {
|
const getGoodsTotal = () => {
|
||||||
if (!goods) return 0
|
if (!goods) return 0
|
||||||
const price = parseFloat(goods.price || '0')
|
const rawPrice = String(orderDataParam?.price ?? goods.price ?? '0')
|
||||||
|
const priceNum = parseFloat(rawPrice)
|
||||||
|
const price = Number.isFinite(priceNum) ? priceNum : 0
|
||||||
// const total = price * quantity
|
// const total = price * quantity
|
||||||
|
|
||||||
// 🔍 详细日志,用于排查数值精度问题
|
// 🔍 详细日志,用于排查数值精度问题
|
||||||
@@ -183,12 +230,21 @@ const OrderConfirm = () => {
|
|||||||
const handleQuantityChange = (value: string | number) => {
|
const handleQuantityChange = (value: string | number) => {
|
||||||
const fallback = isTicketTemplateActive ? minBuyQty : 1
|
const fallback = isTicketTemplateActive ? minBuyQty : 1
|
||||||
const newQuantity = typeof value === 'string' ? parseInt(value, 10) || fallback : value
|
const newQuantity = typeof value === 'string' ? parseInt(value, 10) || fallback : value
|
||||||
const finalQuantity = Math.max(fallback, Math.min(newQuantity, goods?.stock || 999))
|
const step = goods?.step || 1
|
||||||
|
const stockMax = goods?.stock ?? 999
|
||||||
|
const maxMultiple = step > 1 ? Math.floor(stockMax / step) * step : stockMax
|
||||||
|
const maxAllowed = maxMultiple > 0 ? maxMultiple : stockMax
|
||||||
|
const effectiveMin = Math.min(fallback, maxAllowed)
|
||||||
|
const clamped = Math.max(effectiveMin, Math.min(Number(newQuantity) || fallback, maxAllowed))
|
||||||
|
const snapped = step > 1 ? Math.ceil(clamped / step) * step : clamped
|
||||||
|
const finalQuantity = Math.max(effectiveMin, Math.min(snapped, maxAllowed))
|
||||||
setQuantity(finalQuantity)
|
setQuantity(finalQuantity)
|
||||||
|
|
||||||
// 数量变化时,重新排序优惠券并检查当前选中的优惠券是否还可用
|
// 数量变化时,重新排序优惠券并检查当前选中的优惠券是否还可用
|
||||||
if (availableCoupons.length > 0) {
|
if (availableCoupons.length > 0) {
|
||||||
const newTotal = parseFloat(goods?.price || '0') * finalQuantity
|
const priceNum = parseFloat(String(orderDataParam?.price ?? goods?.price ?? '0'))
|
||||||
|
const unitPrice = Number.isFinite(priceNum) ? priceNum : 0
|
||||||
|
const newTotal = unitPrice * finalQuantity
|
||||||
const sortedCoupons = sortCoupons(availableCoupons, newTotal)
|
const sortedCoupons = sortCoupons(availableCoupons, newTotal)
|
||||||
const usableCoupons = filterUsableCoupons(sortedCoupons, newTotal)
|
const usableCoupons = filterUsableCoupons(sortedCoupons, newTotal)
|
||||||
setAvailableCoupons(sortedCoupons)
|
setAvailableCoupons(sortedCoupons)
|
||||||
@@ -497,7 +553,9 @@ const OrderConfirm = () => {
|
|||||||
comments: goods.name,
|
comments: goods.name,
|
||||||
deliveryType: 0,
|
deliveryType: 0,
|
||||||
buyerRemarks: orderRemark,
|
buyerRemarks: orderRemark,
|
||||||
couponId: parseInt(String(bestCoupon.id), 10)
|
couponId: parseInt(String(bestCoupon.id), 10),
|
||||||
|
skuId: resolvedSkuId,
|
||||||
|
specInfo: orderDataParam?.specInfo
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -507,6 +565,7 @@ const OrderConfirm = () => {
|
|||||||
await PaymentHandler.pay(updatedOrderData, currentPaymentType, hasTicketTemplate ? {
|
await PaymentHandler.pay(updatedOrderData, currentPaymentType, hasTicketTemplate ? {
|
||||||
onSuccess: async () => {
|
onSuccess: async () => {
|
||||||
const id = goods.goodsId
|
const id = goods.goodsId
|
||||||
|
const ticketIndexUrl = `/user/ticket/index?fromPayAt=${Date.now()}`
|
||||||
try {
|
try {
|
||||||
const res = await Taro.showModal({
|
const res = await Taro.showModal({
|
||||||
title: '提示',
|
title: '提示',
|
||||||
@@ -518,13 +577,13 @@ const OrderConfirm = () => {
|
|||||||
if (id) {
|
if (id) {
|
||||||
await Taro.redirectTo({ url: `/user/ticket/use?goodsId=${id}` })
|
await Taro.redirectTo({ url: `/user/ticket/use?goodsId=${id}` })
|
||||||
} else {
|
} else {
|
||||||
await Taro.redirectTo({ url: '/user/ticket/index' })
|
await Taro.redirectTo({ url: ticketIndexUrl })
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
await Taro.redirectTo({ url: '/user/ticket/index' })
|
await Taro.redirectTo({ url: ticketIndexUrl })
|
||||||
}
|
}
|
||||||
} catch (_e) {
|
} catch (_e) {
|
||||||
await Taro.redirectTo({ url: '/user/ticket/index' })
|
await Taro.redirectTo({ url: ticketIndexUrl })
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -547,7 +606,9 @@ const OrderConfirm = () => {
|
|||||||
deliveryType: 0,
|
deliveryType: 0,
|
||||||
buyerRemarks: orderRemark,
|
buyerRemarks: orderRemark,
|
||||||
// 🔧 确保 couponId 是正确的数字类型,且不传递 undefined
|
// 🔧 确保 couponId 是正确的数字类型,且不传递 undefined
|
||||||
couponId: selectedCoupon ? parseInt(String(selectedCoupon.id), 10) : undefined
|
couponId: selectedCoupon ? parseInt(String(selectedCoupon.id), 10) : undefined,
|
||||||
|
skuId: resolvedSkuId,
|
||||||
|
specInfo: orderDataParam?.specInfo
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -583,6 +644,7 @@ const OrderConfirm = () => {
|
|||||||
await PaymentHandler.pay(orderData, paymentType, hasTicketTemplate ? {
|
await PaymentHandler.pay(orderData, paymentType, hasTicketTemplate ? {
|
||||||
onSuccess: async () => {
|
onSuccess: async () => {
|
||||||
const id = goods.goodsId
|
const id = goods.goodsId
|
||||||
|
const ticketIndexUrl = `/user/ticket/index?fromPayAt=${Date.now()}`
|
||||||
try {
|
try {
|
||||||
const res = await Taro.showModal({
|
const res = await Taro.showModal({
|
||||||
title: '提示',
|
title: '提示',
|
||||||
@@ -594,13 +656,13 @@ const OrderConfirm = () => {
|
|||||||
if (id) {
|
if (id) {
|
||||||
await Taro.redirectTo({ url: `/user/ticket/use?goodsId=${id}` })
|
await Taro.redirectTo({ url: `/user/ticket/use?goodsId=${id}` })
|
||||||
} else {
|
} else {
|
||||||
await Taro.redirectTo({ url: '/user/ticket/index' })
|
await Taro.redirectTo({ url: ticketIndexUrl })
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
await Taro.redirectTo({ url: '/user/ticket/index' })
|
await Taro.redirectTo({ url: ticketIndexUrl })
|
||||||
}
|
}
|
||||||
} catch (_e) {
|
} catch (_e) {
|
||||||
await Taro.redirectTo({ url: '/user/ticket/index' })
|
await Taro.redirectTo({ url: ticketIndexUrl })
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -682,8 +744,8 @@ const OrderConfirm = () => {
|
|||||||
|
|
||||||
// 分别加载数据,避免类型推断问题
|
// 分别加载数据,避免类型推断问题
|
||||||
let goodsRes: ShopGoods | null = null
|
let goodsRes: ShopGoods | null = null
|
||||||
if (goodsId) {
|
if (resolvedGoodsId) {
|
||||||
goodsRes = await getShopGoods(Number(goodsId))
|
goodsRes = await getShopGoods(resolvedGoodsId)
|
||||||
}
|
}
|
||||||
|
|
||||||
const [addressRes, paymentRes] = await Promise.all([
|
const [addressRes, paymentRes] = await Promise.all([
|
||||||
@@ -694,9 +756,9 @@ const OrderConfirm = () => {
|
|||||||
// 设置商品信息
|
// 设置商品信息
|
||||||
// 查询当前商品是否存在水票套票活动(失败/无数据时不影响正常下单)
|
// 查询当前商品是否存在水票套票活动(失败/无数据时不影响正常下单)
|
||||||
let tpl: GltTicketTemplate | null = null
|
let tpl: GltTicketTemplate | null = null
|
||||||
if (goodsId) {
|
if (resolvedGoodsId) {
|
||||||
try {
|
try {
|
||||||
tpl = await getGltTicketTemplateByGoodsId(Number(goodsId))
|
tpl = await getGltTicketTemplateByGoodsId(resolvedGoodsId)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
tpl = null
|
tpl = null
|
||||||
}
|
}
|
||||||
@@ -712,18 +774,41 @@ const OrderConfirm = () => {
|
|||||||
const n = Number(tpl?.minBuyQty)
|
const n = Number(tpl?.minBuyQty)
|
||||||
return Number.isFinite(n) && n > 0 ? Math.floor(n) : 1
|
return Number.isFinite(n) && n > 0 ? Math.floor(n) : 1
|
||||||
})()
|
})()
|
||||||
|
const tplStep = (() => {
|
||||||
|
const n = Number(tpl?.step)
|
||||||
|
return Number.isFinite(n) && n > 0 ? Math.floor(n) : undefined
|
||||||
|
})()
|
||||||
|
|
||||||
// 设置商品信息(若存在套票模板,则默认 canBuyNumber 使用模板最小购买量)
|
// 设置商品信息(若存在套票模板,则默认 canBuyNumber 使用模板最小购买量)
|
||||||
if (goodsRes) {
|
if (goodsRes) {
|
||||||
const patchedGoods: ShopGoods = { ...goodsRes }
|
const patchedGoods: ShopGoods = { ...goodsRes }
|
||||||
|
// 兜底:确保 step 为合法正整数;若存在套票模板则优先使用模板 step
|
||||||
|
const goodsStepNum = Number((patchedGoods as any)?.step)
|
||||||
|
const goodsStep = Number.isFinite(goodsStepNum) && goodsStepNum > 0 ? Math.floor(goodsStepNum) : 1
|
||||||
|
patchedGoods.step = tplActive && tplStep ? tplStep : goodsStep
|
||||||
|
|
||||||
|
// 规格商品(orderData 模式)下单时,用 sku 价格覆盖展示与计算金额
|
||||||
|
if (orderDataParam?.price !== undefined && orderDataParam?.price !== null && orderDataParam?.price !== '') {
|
||||||
|
patchedGoods.price = String(orderDataParam.price)
|
||||||
|
}
|
||||||
|
|
||||||
if (tplActive && ((patchedGoods.canBuyNumber ?? 0) === 0)) {
|
if (tplActive && ((patchedGoods.canBuyNumber ?? 0) === 0)) {
|
||||||
patchedGoods.canBuyNumber = tplMinBuyQty
|
patchedGoods.canBuyNumber = tplMinBuyQty
|
||||||
}
|
}
|
||||||
setGoods(patchedGoods)
|
setGoods(patchedGoods)
|
||||||
|
|
||||||
// 设置默认购买数量:优先使用 canBuyNumber,否则使用 1
|
// 设置默认购买数量:优先使用 canBuyNumber,其次使用路由参数 quantity,否则使用 1
|
||||||
const initQty = (patchedGoods.canBuyNumber ?? 0) > 0 ? (patchedGoods.canBuyNumber as number) : 1
|
const fixedQty = (patchedGoods.canBuyNumber ?? 0) > 0 ? Number(patchedGoods.canBuyNumber) : undefined
|
||||||
setQuantity(initQty)
|
const rawQty = fixedQty ?? quantityFromParam ?? 1
|
||||||
|
const minQty = tplActive ? tplMinBuyQty : 1
|
||||||
|
const step = patchedGoods.step || 1
|
||||||
|
const stockMax = patchedGoods.stock ?? 999
|
||||||
|
const maxMultiple = step > 1 ? Math.floor(stockMax / step) * step : stockMax
|
||||||
|
const maxAllowed = maxMultiple > 0 ? maxMultiple : stockMax
|
||||||
|
const effectiveMin = Math.min(minQty, maxAllowed)
|
||||||
|
const clamped = Math.max(effectiveMin, Math.min(Math.floor(rawQty), maxAllowed))
|
||||||
|
const stepped = step > 1 ? Math.ceil(clamped / step) * step : clamped
|
||||||
|
setQuantity(Math.min(maxAllowed, Math.max(effectiveMin, stepped)))
|
||||||
}
|
}
|
||||||
|
|
||||||
setTicketTemplate(tpl)
|
setTicketTemplate(tpl)
|
||||||
@@ -748,9 +833,20 @@ const OrderConfirm = () => {
|
|||||||
const n = Number(goodsRes?.canBuyNumber)
|
const n = Number(goodsRes?.canBuyNumber)
|
||||||
if (Number.isFinite(n) && n > 0) return Math.floor(n)
|
if (Number.isFinite(n) && n > 0) return Math.floor(n)
|
||||||
if (tplActive) return tplMinBuyQty
|
if (tplActive) return tplMinBuyQty
|
||||||
return 1
|
return quantityFromParam || 1
|
||||||
})()
|
})()
|
||||||
const total = parseFloat(goodsRes.price || '0') * initQty
|
const stepForInit = tplActive && tplStep ? tplStep : (() => {
|
||||||
|
const n = Number((goodsRes as any)?.step)
|
||||||
|
return Number.isFinite(n) && n > 0 ? Math.floor(n) : 1
|
||||||
|
})()
|
||||||
|
const stockMax = goodsRes.stock ?? 999
|
||||||
|
const maxMultiple = stepForInit > 1 ? Math.floor(stockMax / stepForInit) * stepForInit : stockMax
|
||||||
|
const maxAllowed = maxMultiple > 0 ? maxMultiple : stockMax
|
||||||
|
const initQtySnapped = stepForInit > 1 ? Math.ceil(initQty / stepForInit) * stepForInit : initQty
|
||||||
|
const effectiveMin = Math.min(tplActive ? tplMinBuyQty : 1, maxAllowed)
|
||||||
|
const safeInitQty = Math.max(effectiveMin, Math.min(initQtySnapped, maxAllowed))
|
||||||
|
const unitPrice = parseFloat(String(orderDataParam?.price ?? goodsRes.price ?? '0'))
|
||||||
|
const total = unitPrice * safeInitQty
|
||||||
await loadUserCoupons(total)
|
await loadUserCoupons(total)
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -771,7 +867,7 @@ const OrderConfirm = () => {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isLoggedIn()) return
|
if (!isLoggedIn()) return
|
||||||
loadAllData()
|
loadAllData()
|
||||||
}, [goodsId]);
|
}, [resolvedGoodsId, orderDataRaw]);
|
||||||
|
|
||||||
// 重新加载数据
|
// 重新加载数据
|
||||||
const handleRetry = () => {
|
const handleRetry = () => {
|
||||||
@@ -868,7 +964,7 @@ const OrderConfirm = () => {
|
|||||||
value={quantity}
|
value={quantity}
|
||||||
min={isTicketTemplateActive ? minBuyQty : 1}
|
min={isTicketTemplateActive ? minBuyQty : 1}
|
||||||
max={goods.stock || 999}
|
max={goods.stock || 999}
|
||||||
step={minBuyQty === 1 ? 1 : 10}
|
step={goods.step || 1}
|
||||||
readOnly
|
readOnly
|
||||||
disabled={((goods.canBuyNumber ?? 0) !== 0) && !isTicketTemplateActive}
|
disabled={((goods.canBuyNumber ?? 0) !== 0) && !isTicketTemplateActive}
|
||||||
onChange={handleQuantityChange}
|
onChange={handleQuantityChange}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useState } from 'react';
|
import { useRef, useState } from 'react';
|
||||||
import Taro, { useDidShow } from '@tarojs/taro';
|
import Taro, { useDidShow } from '@tarojs/taro';
|
||||||
import {
|
import {
|
||||||
Button,
|
Button,
|
||||||
@@ -23,6 +23,7 @@ import dayjs from "dayjs";
|
|||||||
import { ensureLoggedIn } from '@/utils/auth';
|
import { ensureLoggedIn } from '@/utils/auth';
|
||||||
|
|
||||||
const PAGE_SIZE = 10;
|
const PAGE_SIZE = 10;
|
||||||
|
const PAY_REFRESH_HANDLED_KEY = 'user_ticket_from_pay_at_handled';
|
||||||
|
|
||||||
const UserTicketList = () => {
|
const UserTicketList = () => {
|
||||||
const [ticketList, setTicketList] = useState<GltUserTicket[]>([]);
|
const [ticketList, setTicketList] = useState<GltUserTicket[]>([]);
|
||||||
@@ -47,6 +48,25 @@ const UserTicketList = () => {
|
|||||||
const [qrVisible, setQrVisible] = useState(false);
|
const [qrVisible, setQrVisible] = useState(false);
|
||||||
const [qrTicket, setQrTicket] = useState<GltUserTicket | null>(null);
|
const [qrTicket, setQrTicket] = useState<GltUserTicket | null>(null);
|
||||||
const [qrImageUrl, setQrImageUrl] = useState('');
|
const [qrImageUrl, setQrImageUrl] = useState('');
|
||||||
|
const payAutoRefreshRunningRef = useRef(false);
|
||||||
|
|
||||||
|
const sleep = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms));
|
||||||
|
|
||||||
|
const parsePositiveNumberParam = (v: unknown) => {
|
||||||
|
const n = Number(v);
|
||||||
|
return Number.isFinite(n) && n > 0 ? n : undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getFromPayAtParam = () => {
|
||||||
|
const params = Taro.getCurrentInstance().router?.params;
|
||||||
|
return parsePositiveNumberParam((params as any)?.fromPayAt);
|
||||||
|
};
|
||||||
|
|
||||||
|
const shouldAutoRefreshAfterPay = (fromPayAt?: number) => {
|
||||||
|
if (!fromPayAt) return false;
|
||||||
|
const handled = parsePositiveNumberParam(Taro.getStorageSync(PAY_REFRESH_HANDLED_KEY)) || 0;
|
||||||
|
return handled !== fromPayAt;
|
||||||
|
};
|
||||||
|
|
||||||
const getUserId = () => {
|
const getUserId = () => {
|
||||||
const raw = Taro.getStorageSync('UserId');
|
const raw = Taro.getStorageSync('UserId');
|
||||||
@@ -460,7 +480,7 @@ const UserTicketList = () => {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
await rollbackUserTicketAfterOrderCancel(order, beforeTicket);
|
await rollbackUserTicketAfterOrderCancel(order, beforeTicket);
|
||||||
Taro.showToast({ title: '订单已取消,水票已退回', icon: 'success' });
|
Taro.showToast({ title: '订单已取消,水票已退回', icon: 'none' });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('取消订单后退回水票失败:', e);
|
console.error('取消订单后退回水票失败:', e);
|
||||||
await Taro.showModal({
|
await Taro.showModal({
|
||||||
@@ -530,22 +550,36 @@ const UserTicketList = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
useDidShow(() => {
|
useDidShow(() => {
|
||||||
const tabParam = Taro.getCurrentInstance().router?.params?.tab
|
void (async () => {
|
||||||
const nextTab =
|
const tabParam = Taro.getCurrentInstance().router?.params?.tab;
|
||||||
tabParam === 'ticket' || tabParam === 'order'
|
const nextTab = tabParam === 'ticket' || tabParam === 'order' ? tabParam : undefined;
|
||||||
? tabParam
|
|
||||||
: undefined
|
|
||||||
|
|
||||||
if (nextTab && nextTab !== activeTab) {
|
if (nextTab && nextTab !== activeTab) {
|
||||||
setActiveTab(nextTab)
|
setActiveTab(nextTab);
|
||||||
}
|
}
|
||||||
|
|
||||||
const tabToLoad = nextTab || activeTab
|
const tabToLoad = nextTab || activeTab;
|
||||||
if (tabToLoad === 'ticket') {
|
if (tabToLoad === 'ticket') {
|
||||||
reloadTickets(true).then()
|
await reloadTickets(true);
|
||||||
} else {
|
|
||||||
reloadOrders(true).then()
|
const fromPayAt = getFromPayAtParam();
|
||||||
|
if (shouldAutoRefreshAfterPay(fromPayAt) && !payAutoRefreshRunningRef.current) {
|
||||||
|
payAutoRefreshRunningRef.current = true;
|
||||||
|
try {
|
||||||
|
Taro.setStorageSync(PAY_REFRESH_HANDLED_KEY, fromPayAt);
|
||||||
|
// 支付后水票可能异步入账:自动再刷新几次,避免用户手动下拉刷新。
|
||||||
|
for (const delayMs of [800, 1500, 2500]) {
|
||||||
|
await sleep(delayMs);
|
||||||
|
await reloadTickets(true);
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
payAutoRefreshRunningRef.current = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
await reloadOrders(true);
|
||||||
|
}
|
||||||
|
})();
|
||||||
})
|
})
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -75,6 +75,8 @@ const OrderConfirm = () => {
|
|||||||
const [ticketLoading, setTicketLoading] = useState(false)
|
const [ticketLoading, setTicketLoading] = useState(false)
|
||||||
const [ticketLoaded, setTicketLoaded] = useState(false)
|
const [ticketLoaded, setTicketLoaded] = useState(false)
|
||||||
const noTicketPromptedRef = useRef(false)
|
const noTicketPromptedRef = useRef(false)
|
||||||
|
const ticketAutoRetryCountRef = useRef(0)
|
||||||
|
const ticketAutoRetryTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||||
|
|
||||||
// Delivery range (geofence): block ordering if address/current location is outside.
|
// Delivery range (geofence): block ordering if address/current location is outside.
|
||||||
const [fences, setFences] = useState<ShopStoreFence[]>([])
|
const [fences, setFences] = useState<ShopStoreFence[]>([])
|
||||||
@@ -207,6 +209,39 @@ const OrderConfirm = () => {
|
|||||||
return !!userId && ticketLoaded && !ticketLoading && usableTickets.length === 0
|
return !!userId && ticketLoaded && !ticketLoading && usableTickets.length === 0
|
||||||
}, [ticketLoaded, ticketLoading, usableTickets.length, userId])
|
}, [ticketLoaded, ticketLoading, usableTickets.length, userId])
|
||||||
|
|
||||||
|
// After buying tickets and redirecting here, some backends may issue tickets asynchronously.
|
||||||
|
// If opened with a `goodsId`, retry a few times to refresh tickets.
|
||||||
|
useEffect(() => {
|
||||||
|
if (isEditMode) return
|
||||||
|
if (!numericGoodsId) return
|
||||||
|
if (!ticketLoaded || ticketLoading) return
|
||||||
|
|
||||||
|
if (usableTickets.length > 0) {
|
||||||
|
ticketAutoRetryCountRef.current = 0
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ticketAutoRetryCountRef.current >= 4) return
|
||||||
|
if (ticketAutoRetryTimerRef.current) return
|
||||||
|
|
||||||
|
const delays = [800, 1500, 2500, 4000]
|
||||||
|
const delay = delays[ticketAutoRetryCountRef.current] ?? 2500
|
||||||
|
ticketAutoRetryCountRef.current += 1
|
||||||
|
ticketAutoRetryTimerRef.current = setTimeout(async () => {
|
||||||
|
ticketAutoRetryTimerRef.current = null
|
||||||
|
await loadUserTickets()
|
||||||
|
}, delay)
|
||||||
|
}, [isEditMode, numericGoodsId, ticketLoaded, ticketLoading, usableTickets.length])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
return () => {
|
||||||
|
if (ticketAutoRetryTimerRef.current) {
|
||||||
|
clearTimeout(ticketAutoRetryTimerRef.current)
|
||||||
|
ticketAutoRetryTimerRef.current = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [])
|
||||||
|
|
||||||
const maxQuantity = useMemo(() => {
|
const maxQuantity = useMemo(() => {
|
||||||
const stockMax = goods?.stock ?? 999
|
const stockMax = goods?.stock ?? 999
|
||||||
if (!isEditMode) return Math.max(0, Math.min(stockMax, availableTicketTotal))
|
if (!isEditMode) return Math.max(0, Math.min(stockMax, availableTicketTotal))
|
||||||
@@ -784,6 +819,11 @@ const OrderConfirm = () => {
|
|||||||
useDidShow(() => {
|
useDidShow(() => {
|
||||||
// 返回/切换到该页面时,刷新一下当前已选门店
|
// 返回/切换到该页面时,刷新一下当前已选门店
|
||||||
setSelectedStore(getSelectedStoreFromStorage())
|
setSelectedStore(getSelectedStoreFromStorage())
|
||||||
|
ticketAutoRetryCountRef.current = 0
|
||||||
|
if (ticketAutoRetryTimerRef.current) {
|
||||||
|
clearTimeout(ticketAutoRetryTimerRef.current)
|
||||||
|
ticketAutoRetryTimerRef.current = null
|
||||||
|
}
|
||||||
loadAllData({ silent: hasInitialLoadedRef.current })
|
loadAllData({ silent: hasInitialLoadedRef.current })
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -913,22 +953,24 @@ const OrderConfirm = () => {
|
|||||||
// If user has no usable tickets, proactively guide them to purchase (only once per page lifecycle).
|
// If user has no usable tickets, proactively guide them to purchase (only once per page lifecycle).
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!noUsableTickets) return
|
if (!noUsableTickets) return
|
||||||
|
// Editing an existing order: don't interrupt with "no tickets" prompt.
|
||||||
|
if (isEditMode) return
|
||||||
if (noTicketPromptedRef.current) return
|
if (noTicketPromptedRef.current) return
|
||||||
noTicketPromptedRef.current = true
|
noTicketPromptedRef.current = true
|
||||||
|
|
||||||
;(async () => {
|
// ;(async () => {
|
||||||
const r = await Taro.showModal({
|
// const r = await Taro.showModal({
|
||||||
title: '暂无可用水票',
|
// title: '暂无可用水票',
|
||||||
content: '您当前没有可用水票,购买后再来下单更方便。',
|
// content: '您当前没有可用水票,购买后再来下单更方便。',
|
||||||
confirmText: '去购买',
|
// confirmText: '去购买',
|
||||||
cancelText: '暂不'
|
// cancelText: '暂不'
|
||||||
})
|
// })
|
||||||
if (r.confirm) {
|
// if (r.confirm) {
|
||||||
await goBuyTickets()
|
// await goBuyTickets()
|
||||||
}
|
// }
|
||||||
})()
|
// })()
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [noUsableTickets])
|
}, [noUsableTickets, isEditMode])
|
||||||
|
|
||||||
// 重新加载数据
|
// 重新加载数据
|
||||||
const handleRetry = () => {
|
const handleRetry = () => {
|
||||||
@@ -1094,7 +1136,7 @@ const OrderConfirm = () => {
|
|||||||
await loadUserTickets()
|
await loadUserTickets()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (noUsableTickets) {
|
if (noUsableTickets && !isEditMode) {
|
||||||
const r = await Taro.showModal({
|
const r = await Taro.showModal({
|
||||||
title: '暂无可用水票',
|
title: '暂无可用水票',
|
||||||
content: '您还没有可用水票,是否前往购买?',
|
content: '您还没有可用水票,是否前往购买?',
|
||||||
@@ -1107,7 +1149,7 @@ const OrderConfirm = () => {
|
|||||||
setTicketPopupVisible(true)
|
setTicketPopupVisible(true)
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
{noUsableTickets && (
|
{(noUsableTickets && !isEditMode) && (
|
||||||
<Cell
|
<Cell
|
||||||
title={<Text className="text-gray-500">还没有购买水票</Text>}
|
title={<Text className="text-gray-500">还没有购买水票</Text>}
|
||||||
description="购买水票后即可在这里直接下单送水"
|
description="购买水票后即可在这里直接下单送水"
|
||||||
@@ -1183,8 +1225,11 @@ const OrderConfirm = () => {
|
|||||||
<View className="py-10 text-center">
|
<View className="py-10 text-center">
|
||||||
<Empty description="暂无可用水票" />
|
<Empty description="暂无可用水票" />
|
||||||
<View className="mt-4 flex justify-center">
|
<View className="mt-4 flex justify-center">
|
||||||
<Button type="primary" onClick={goBuyTickets}>
|
<Button
|
||||||
确定下单
|
type="primary"
|
||||||
|
onClick={isEditMode ? () => setTicketPopupVisible(false) : goBuyTickets}
|
||||||
|
>
|
||||||
|
{isEditMode ? '确定修改' : '确定下单'}
|
||||||
</Button>
|
</Button>
|
||||||
</View>
|
</View>
|
||||||
</View>
|
</View>
|
||||||
@@ -1266,9 +1311,9 @@ const OrderConfirm = () => {
|
|||||||
</View>
|
</View>
|
||||||
</div>
|
</div>
|
||||||
<div className={'buy-btn mx-4'}>
|
<div className={'buy-btn mx-4'}>
|
||||||
{noUsableTickets ? (
|
{noUsableTickets && !isEditMode ? (
|
||||||
<Button type="primary" size="large" onClick={goBuyTickets}>
|
<Button type="primary" size="large" onClick={goBuyTickets}>
|
||||||
确定下单
|
{isEditMode ? '确定修改' : '确定下单'}
|
||||||
</Button>
|
</Button>
|
||||||
) : (
|
) : (
|
||||||
<Button
|
<Button
|
||||||
@@ -1280,7 +1325,7 @@ const OrderConfirm = () => {
|
|||||||
!address?.id ||
|
!address?.id ||
|
||||||
!addressHasCoords ||
|
!addressHasCoords ||
|
||||||
(deliveryRangeCheckedAddressId === address?.id && inDeliveryRange === false) ||
|
(deliveryRangeCheckedAddressId === address?.id && inDeliveryRange === false) ||
|
||||||
availableTicketTotal <= 0 ||
|
(!isEditMode && availableTicketTotal <= 0) ||
|
||||||
!canStartOrder
|
!canStartOrder
|
||||||
}
|
}
|
||||||
onClick={onSubmit}
|
onClick={onSubmit}
|
||||||
@@ -1293,7 +1338,7 @@ const OrderConfirm = () => {
|
|||||||
? '地址缺少定位'
|
? '地址缺少定位'
|
||||||
: ((deliveryRangeCheckedAddressId === address?.id && inDeliveryRange === false)
|
: ((deliveryRangeCheckedAddressId === address?.id && inDeliveryRange === false)
|
||||||
? '不在配送范围'
|
? '不在配送范围'
|
||||||
: (submitLoading ? '提交中...' : '立即提交')
|
: (submitLoading ? '提交中...' : (isEditMode ? '确定修改' : '立即提交'))
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user