OrderReminderServiceImpl.java
3.37 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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;
/**
* 订餐提醒服务实现
* 使用小程序订阅消息接口发送提醒
*
* 小程序模板字段(模板ID: LtCsW7ciF0edUwNwt3h5C_ZCIRzOeO7MnvozRl2CxlE):
* - time4: 开始时间
* - time5: 结束时间
* - thing1: 温馨提醒
*
* 注意:
* 1. 使用的是小程序的 access_token 和 template_id
* 2. openId 是用户在小程序下的 openId
*/
@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 startTime,
String endTime,
String tips) {
log.info("Sending order reminder via MiniProgram to openId: {}, page: {}, startTime: {}, endTime: {}, tips: {}",
openId, page, startTime, endTime, tips);
// 构建小程序订阅消息模板数据
Map<String, Object> data = new HashMap<>();
// time4: 开始时间
Map<String, String> time4Value = new HashMap<>();
time4Value.put("value", startTime);
data.put("time4", time4Value);
// time5: 结束时间
Map<String, String> time5Value = new HashMap<>();
time5Value.put("value", endTime);
data.put("time5", time5Value);
// thing1: 温馨提醒
Map<String, String> thing1Value = new HashMap<>();
thing1Value.put("value", tips);
data.put("thing1", thing1Value);
try {
// 使用小程序订阅消息接口
SendSubscriptionMessageResponse response = wxMiniProgramService.sendSubscriptionMessage(
openId,
templateId,
page,
data
);
if (response.getErrcode() != null && response.getErrcode() == 0) {
log.info("Order reminder sent successfully via MiniProgram 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 via MiniProgram to openId: {}", openId, e);
throw e;
}
}
}