Files
hms/apps/miniprogram/src/pages/pkg-mall/exchange/index.tsx
iven 7b5138a630
Some checks failed
CI / rust-check (push) Has been cancelled
CI / rust-test (push) Has been cancelled
CI / frontend-build (push) Has been cancelled
CI / security-audit (push) Has been cancelled
feat(miniprogram): 关怀模式全覆盖 — 58/58 页面 100% 接入
- 医生端 18 个页面全部接入(首页/待办/告警/咨询/透析/随访/
  患者/处方/报告及其详情和创建页)
- 商城子包 3 页面(商品详情/积分兑换/订单)
- 患者端剩余页面(AI报告/文章/活动/设备同步/登录/随访详情/
  报告详情/知情同意/诊断/透析处方/透析记录/家庭成员添加)
- 页面覆盖率:22/59 (37%) → 58/58 (100%)
- useElderClass hook 统一接入模式,零样板代码

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-09 22:34:44 +08:00

218 lines
7.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import React, { useState, useCallback } from 'react';
import { View, Text } from '@tarojs/components';
import Taro, { useDidShow } from '@tarojs/taro';
import {
listProducts,
exchangeProduct,
} from '../../../services/points';
import type { PointsProduct } from '../../../services/points';
import { usePointsStore } from '../../../stores/points';
import Loading from '../../../components/Loading';
import { useElderClass } from '../../../hooks/useElderClass';
import './index.scss';
const TYPE_INITIAL: Record<string, string> = {
physical: '物',
service: '券',
privilege: '权',
};
const TYPE_LABEL: Record<string, string> = {
physical: '实物商品',
service: '服务券',
privilege: '权益卡',
};
const TYPE_COLOR: Record<string, string> = {
physical: '#5B7A5E',
service: '#C4623A',
privilege: '#8B3E1F',
};
export default function ExchangeConfirm() {
const modeClass = useElderClass();
const [product, setProduct] = useState<PointsProduct | null>(null);
const { account, refresh: refreshPoints } = usePointsStore();
const [loading, setLoading] = useState(true);
const [submitting, setSubmitting] = useState(false);
useDidShow(() => {
Taro.setNavigationBarTitle({ title: '确认兑换' });
loadData();
});
const loadData = useCallback(async () => {
const instance = Taro.getCurrentInstance();
const productId = instance.router?.params?.product_id;
if (!productId) {
Taro.showToast({ title: '参数错误', icon: 'none' });
setTimeout(() => Taro.navigateBack(), 1500);
return;
}
setLoading(true);
try {
const [productRes] = await Promise.all([
listProducts({ page: 1, page_size: 100 }),
refreshPoints(),
]);
const found = productRes.data.find((p) => p.id === productId);
if (!found) {
Taro.showToast({ title: '商品不存在', icon: 'none' });
setTimeout(() => Taro.navigateBack(), 1500);
return;
}
setProduct(found);
} catch {
Taro.showToast({ title: '加载失败', icon: 'none' });
setTimeout(() => Taro.navigateBack(), 1500);
} finally {
setLoading(false);
}
}, [refreshPoints]);
const balance = account?.balance ?? 0;
const cost = product?.points_cost ?? 0;
const insufficient = balance < cost;
const handleConfirm = useCallback(async () => {
if (!product || submitting) return;
if (insufficient) {
Taro.showToast({ title: '积分不足', icon: 'none' });
return;
}
const modalRes = await Taro.showModal({
title: '确认兑换',
content: `确定花费 ${cost} 积分兑换「${product.name}」吗?`,
});
if (!modalRes.confirm) return;
setSubmitting(true);
try {
const order = await exchangeProduct(product.id);
Taro.showToast({ title: '兑换成功', icon: 'success', duration: 2000 });
setTimeout(() => {
Taro.showModal({
title: '兑换成功',
content: `核销码: ${order.qr_code}\n请凭此码到前台核销`,
showCancel: false,
confirmText: '查看订单',
success: () => {
Taro.navigateTo({
url: `/pages/pkg-mall/orders/index`,
});
},
});
}, 2000);
} catch (err) {
const msg = err instanceof Error ? err.message : '兑换失败';
if (msg.includes('余额不足') || msg.includes('insufficient')) {
Taro.showToast({ title: '积分不足', icon: 'none' });
} else {
Taro.showToast({ title: msg, icon: 'none' });
}
} finally {
setSubmitting(false);
}
}, [product, submitting, insufficient, cost]);
if (loading) {
return (
<View className={`exchange-page ${modeClass}`}>
<Loading />
</View>
);
}
const productType = product?.product_type || 'physical';
const initial = TYPE_INITIAL[productType] || '礼';
const typeLabel = TYPE_LABEL[productType] || '商品';
const typeColor = TYPE_COLOR[productType] || '#C4623A';
return (
<View className={`exchange-page ${modeClass}`}>
{/* 商品预览卡片 */}
<View className='product-card'>
<View
className='product-icon-wrap'
style={{ backgroundColor: typeColor }}
>
<Text className='product-icon-char'>{initial}</Text>
</View>
<View className='product-meta'>
<Text className='product-name'>{product?.name || ''}</Text>
<Text className='product-type-tag'>{typeLabel}</Text>
</View>
</View>
{/* 兑换明细 */}
<View className='detail-section'>
<Text className='detail-section-title'></Text>
<View className='detail-card'>
<View className='detail-row'>
<Text className='detail-label'></Text>
<Text className='detail-value detail-cost'>{cost.toLocaleString()}</Text>
</View>
<View className='detail-row'>
<Text className='detail-label'></Text>
<Text
className={`detail-value ${insufficient ? 'detail-insufficient' : 'detail-sufficient'}`}
>
{balance.toLocaleString()}
</Text>
</View>
{insufficient && (
<View className='detail-row'>
<Text className='detail-label'></Text>
<Text className='detail-value detail-insufficient'>
-{(cost - balance).toLocaleString()}
</Text>
</View>
)}
<View className='detail-row last'>
<Text className='detail-label'></Text>
<Text className='detail-value'>
{product && product.stock > 0 ? `剩余 ${product.stock}` : '已兑完'}
</Text>
</View>
</View>
</View>
{/* 温馨提示 */}
<View className='notice-section'>
<Text className='notice-title'></Text>
<Text className='notice-text'>
</Text>
<Text className='notice-text'>退</Text>
</View>
{/* 底部操作 */}
<View className='exchange-footer'>
<View className='footer-cost'>
<Text className='footer-cost-label'></Text>
<Text className='footer-cost-num'>{cost.toLocaleString()}</Text>
<Text className='footer-cost-unit'></Text>
</View>
<View
className={`confirm-btn ${insufficient || (product?.stock ?? 0) <= 0 || submitting ? 'disabled' : ''}`}
onClick={insufficient || (product?.stock ?? 0) <= 0 || submitting ? undefined : handleConfirm}
>
<Text className='confirm-btn-text'>
{submitting
? '兑换中...'
: insufficient
? '积分不足'
: (product?.stock ?? 0) <= 0
? '已兑完'
: '确认兑换'}
</Text>
</View>
</View>
</View>
);
}