|
|
@@ -6,6 +6,7 @@ import { zodResolver } from '@hookform/resolvers/zod';
|
|
|
import { format } from 'date-fns';
|
|
|
import { toast } from 'sonner';
|
|
|
import { Search, Edit, Eye, Package, Truck } from 'lucide-react';
|
|
|
+
|
|
|
|
|
|
// 使用共享UI组件包的具体路径导入
|
|
|
import { Button } from '@d8d/shared-ui-components/components/ui/button';
|
|
|
@@ -66,6 +67,7 @@ import { adminOrderClient, orderClientManager } from '../api';
|
|
|
import type { InferResponseType } from 'hono/client';
|
|
|
import { UpdateOrderDto } from '@d8d/orders-module-mt/schemas';
|
|
|
|
|
|
+
|
|
|
// 类型定义
|
|
|
type OrderResponse = InferResponseType<typeof adminOrderClient.index.$get, 200>['data'][0] & {
|
|
|
user?: {
|
|
|
@@ -73,7 +75,9 @@ type OrderResponse = InferResponseType<typeof adminOrderClient.index.$get, 200>[
|
|
|
username: string;
|
|
|
phone: string | null;
|
|
|
openid?: string | null; // 添加openid字段
|
|
|
+ hasSubscribedDeliveryNotice?: boolean; // 添加订阅状态字段
|
|
|
} | null;
|
|
|
+ tenantId?: number; // 添加tenantId字段
|
|
|
};
|
|
|
type UpdateRequest = any;
|
|
|
// 发货请求类型 - 使用UpdateOrderDto的类型
|
|
|
@@ -93,6 +97,7 @@ interface WechatServiceMessageConfig {
|
|
|
page?: string;
|
|
|
data: Record<string, { value: string }>;
|
|
|
miniprogramState?: 'developer' | 'trial' | 'formal';
|
|
|
+ tenantId?: number; // 添加tenantId参数
|
|
|
}
|
|
|
|
|
|
// 微信服务消息通知结果类型
|
|
|
@@ -119,7 +124,8 @@ const sendWechatServiceMessage = async (config: WechatServiceMessageConfig): Pro
|
|
|
templateId: config.templateId,
|
|
|
data: config.data,
|
|
|
page: config.page || 'pages/index/index',
|
|
|
- miniprogramState: config.miniprogramState || 'formal'
|
|
|
+ miniprogramState: config.miniprogramState || 'formal',
|
|
|
+ tenantId: config.tenantId
|
|
|
}),
|
|
|
});
|
|
|
|
|
|
@@ -138,6 +144,8 @@ const sendWechatServiceMessage = async (config: WechatServiceMessageConfig): Pro
|
|
|
}
|
|
|
|
|
|
const result = await response.json();
|
|
|
+
|
|
|
+
|
|
|
console.debug('微信服务消息发送成功:', result);
|
|
|
|
|
|
return {
|
|
|
@@ -156,6 +164,32 @@ const sendWechatServiceMessage = async (config: WechatServiceMessageConfig): Pro
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+// 格式化微信日期时间
|
|
|
+const formatWechatDate = (isoDateString: string | null | undefined): string => {
|
|
|
+ try {
|
|
|
+ // 如果日期字符串为空,使用当前时间
|
|
|
+ const date = isoDateString ? new Date(isoDateString) : new Date();
|
|
|
+ // 微信date类型格式:YYYY年MM月DD日 HH:mm
|
|
|
+ const year = date.getFullYear();
|
|
|
+ const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
|
+ const day = String(date.getDate()).padStart(2, '0');
|
|
|
+ const hours = String(date.getHours()).padStart(2, '0');
|
|
|
+ const minutes = String(date.getMinutes()).padStart(2, '0');
|
|
|
+
|
|
|
+ return `${year}年${month}月${day}日 ${hours}:${minutes}`;
|
|
|
+ } catch (error) {
|
|
|
+ console.error('格式化微信日期失败:', error, isoDateString);
|
|
|
+ // 返回当前时间的格式化版本作为备用
|
|
|
+ const now = new Date();
|
|
|
+ const year = now.getFullYear();
|
|
|
+ const month = String(now.getMonth() + 1).padStart(2, '0');
|
|
|
+ const day = String(now.getDate()).padStart(2, '0');
|
|
|
+ const hours = String(now.getHours()).padStart(2, '0');
|
|
|
+ const minutes = String(now.getMinutes()).padStart(2, '0');
|
|
|
+ return `${year}年${month}月${day}日 ${hours}:${minutes}`;
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
// 发货成功微信通知函数 - 使用新的配置化设计
|
|
|
const sendDeliverySuccessNotification = async (order: OrderResponse, deliveryData: DeliveryRequest): Promise<WechatServiceMessageResult> => {
|
|
|
// 检查是否有用户信息和openid
|
|
|
@@ -173,6 +207,26 @@ const sendDeliverySuccessNotification = async (order: OrderResponse, deliveryDat
|
|
|
return { success: false, message: '用户没有绑定微信小程序,无法发送微信通知' };
|
|
|
}
|
|
|
|
|
|
+ // 检查用户是否已订阅发货通知
|
|
|
+ if (order.user.hasSubscribedDeliveryNotice !== true) {
|
|
|
+ console.debug('用户未订阅发货通知,跳过发送微信通知', {
|
|
|
+ userId: order.user.id,
|
|
|
+ username: order.user.username,
|
|
|
+ hasSubscribedDeliveryNotice: order.user.hasSubscribedDeliveryNotice
|
|
|
+ });
|
|
|
+ return {
|
|
|
+ success: false,
|
|
|
+ message: '用户未订阅发货通知,跳过发送微信通知',
|
|
|
+ data: { skipped: true, reason: 'user_not_subscribed' }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ console.debug('用户已订阅发货通知,准备发送微信通知', {
|
|
|
+ userId: order.user.id,
|
|
|
+ username: order.user.username,
|
|
|
+ openid: order.user.openid?.substring(0, 10) + '...' // 部分隐藏openid
|
|
|
+ });
|
|
|
+
|
|
|
// 构建微信服务消息配置 - 参考useShareAppMessage的配置对象模式
|
|
|
const config: WechatServiceMessageConfig = {
|
|
|
openid: order.user.openid,
|
|
|
@@ -181,22 +235,23 @@ const sendDeliverySuccessNotification = async (order: OrderResponse, deliveryDat
|
|
|
data: {
|
|
|
// 根据实际微信模板字段配置
|
|
|
character_string7: {
|
|
|
- value: `订单号:${order.orderNo}`
|
|
|
+ value: `${order.orderNo}`
|
|
|
},
|
|
|
date6: {
|
|
|
- value: `${deliveryData.deliveryTime}`
|
|
|
+ value: formatWechatDate(deliveryData.deliveryTime)
|
|
|
},
|
|
|
amount9: {
|
|
|
- value: `${order.payAmount}`
|
|
|
+ value: `¥${order.payAmount.toFixed(2)}`
|
|
|
},
|
|
|
phrase12: {
|
|
|
- value: `${deliveryData.deliveryType}`
|
|
|
+ value: deliveryTypeMap[deliveryData.deliveryType as keyof typeof deliveryTypeMap]?.label || '未知'
|
|
|
},
|
|
|
thing4: {
|
|
|
- value: `${deliveryData.deliveryRemark}` || '请收到货/提货后及时确认收货,2天后将自动确认收货,如有异常请及时进行交易投诉。'
|
|
|
+ value: (deliveryData.deliveryRemark || '请收到货/提货后及时确认收货,2天后将自动确认收货,如有异常请及时进行交易投诉。').substring(0, 20)
|
|
|
}
|
|
|
},
|
|
|
- miniprogramState: 'formal'
|
|
|
+ miniprogramState: 'formal',
|
|
|
+ tenantId: order.tenantId // 从订单数据中获取tenantId
|
|
|
};
|
|
|
|
|
|
// 调用微信服务消息函数
|
|
|
@@ -248,6 +303,7 @@ export const OrderManagement = () => {
|
|
|
const [deliveryModalOpen, setDeliveryModalOpen] = useState(false);
|
|
|
const [deliveringOrder, setDeliveringOrder] = useState<OrderResponse | null>(null);
|
|
|
|
|
|
+
|
|
|
// 表单实例
|
|
|
const updateForm = useForm<UpdateRequest>({
|
|
|
resolver: zodResolver(UpdateOrderDto),
|
|
|
@@ -264,6 +320,7 @@ export const OrderManagement = () => {
|
|
|
},
|
|
|
});
|
|
|
|
|
|
+
|
|
|
// 数据查询 - 60秒自动刷新
|
|
|
const { data, isLoading, refetch } = useQuery({
|
|
|
queryKey: ['orders', searchParams],
|
|
|
@@ -427,9 +484,19 @@ export const OrderManagement = () => {
|
|
|
// 发货成功后发送微信服务消息通知 - 使用新的配置化设计
|
|
|
try {
|
|
|
const notificationResult = await sendDeliverySuccessNotification(deliveringOrder, data);
|
|
|
+
|
|
|
+ // 根据通知结果记录不同的日志
|
|
|
if (notificationResult.success) {
|
|
|
console.debug('微信发货通知发送成功:', notificationResult);
|
|
|
+ } else if (notificationResult.data?.skipped) {
|
|
|
+ // 用户未订阅,这是正常情况,记录为debug级别
|
|
|
+ console.debug('用户未订阅发货通知,跳过发送:', {
|
|
|
+ userId: deliveringOrder.user?.id,
|
|
|
+ username: deliveringOrder.user?.username,
|
|
|
+ reason: notificationResult.data.reason
|
|
|
+ });
|
|
|
} else {
|
|
|
+ // 其他原因导致的失败,记录为warn级别
|
|
|
console.warn('微信发货通知发送失败,但发货成功:', notificationResult);
|
|
|
// 可以在这里添加额外的处理,比如记录到日志系统
|
|
|
}
|