OrderReminderServiceImpl.java
3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.infoloop.tianting.service.impl;
import com.infoloop.tianting.model.dto.WxUserDto.SendSubscriptionMessageResponse;
import com.infoloop.tianting.service.OrderReminderService;
import com.infoloop.tianting.service.WxMiniProgramService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
import static com.infoloop.tianting.constant.ConfigConstants.MINI_PROGRAM_ORDER_REMINDER_TEMPLATE_ID;
/**
* 订餐提醒服务实现
* 将业务参数转换为微信订阅消息模板格式并发送
*/
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class OrderReminderServiceImpl implements OrderReminderService {
private final WxMiniProgramService wxMiniProgramService;
@Value(MINI_PROGRAM_ORDER_REMINDER_TEMPLATE_ID)
private String templateId;
@Override
public SendSubscriptionMessageResponse sendOrderReminder(String openId,
String page,
String reminderContent,
String orderTime,
String tips) {
log.info("Sending order reminder to openId: {}, page: {}", openId, page);
// 将业务参数转换为微信模板数据格式
// 根据文档,模板字段为:thing1 (提醒内容), time2 (订餐时间), thing3 (温馨提示)
Map<String, Object> data = new HashMap<>();
// thing1: 提醒内容
Map<String, String> thing1Value = new HashMap<>();
thing1Value.put("value", reminderContent);
data.put("thing1", thing1Value);
// time2: 订餐时间
Map<String, String> time2Value = new HashMap<>();
time2Value.put("value", orderTime);
data.put("time2", time2Value);
// thing3: 温馨提示
Map<String, String> thing3Value = new HashMap<>();
thing3Value.put("value", tips);
data.put("thing3", thing3Value);
try {
SendSubscriptionMessageResponse response = wxMiniProgramService.sendSubscriptionMessage(
openId, templateId, page, data);
if (response.getErrcode() != null && response.getErrcode() == 0) {
log.info("Order reminder sent successfully to openId: {}", openId);
} else {
log.warn("Order reminder sent with error. openId: {}, errcode: {}, errmsg: {}",
openId, response.getErrcode(), response.getErrmsg());
}
return response;
} catch (Exception e) {
log.error("Failed to send order reminder to openId: {}", openId, e);
throw e;
}
}
}