nidapeng
2024-04-23 5072f33b5c54142f00991d0dafc9ea75af696b81
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.doumee.core.wx;
 
import com.alibaba.fastjson.JSONObject;
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.utils.DateUtil;
import com.doumee.core.utils.HttpsUtil;
import com.doumee.dao.business.model.Bookings;
import com.doumee.dao.business.model.Rooms;
import com.doumee.dao.system.model.SystemUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
 
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Rk
 * @create 2022/12/7 17:27
 */
@Service
@Slf4j
public class SendWxMessage {
 
    private static String programUrl = "packagesMine/meetingDetails/meetingDetails?id=";
 
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
 
    /**
     * 会议开始通知
     * @param systemUser 用户
     * @param bookings 会议信息
     * @param rooms 会议室信息
     */
    public  void  bookingsStart(SystemUser systemUser, Bookings bookings, Rooms rooms){
        RestTemplate restTemplate = new RestTemplate();
        String accessToken = systemDictDataBiz.queryCodeById(106);
        log.info("微信小程序 会议开始订阅消息发送日志 -> accessToken:{}",accessToken);
        //这里简单起见我们每次都获取最新的access_token(时间开发中,应该在access_token快过期时再重新获取)
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+accessToken;
        //拼接推送的模版
        WxMsgVO wxMsgVo = new WxMsgVO();
        //用户的openid(要发送给那个用户)
        wxMsgVo.setTouser(systemUser.getOpenid());
        //订阅消息模板id
        wxMsgVo.setTemplate_id("di_lWUtlqvWAo7aWKZCH8Kzs1Cv2z3H6fOkKI4RwJzg");
        Map<String, TemplateData> m = new HashMap<>(4);
        //会议主题
        m.put("thing1", new TemplateData(bookings.getName()));
        //会议地点
        m.put("thing2", new TemplateData(rooms.getName()));
        //时间
        m.put("thing3", new TemplateData(DateUtil.getDate(bookings.getStartTime(),"yyyy-MM-dd")));
        //主持人
        m.put("name4", new TemplateData(systemUser.getRealname()));
        wxMsgVo.setPage(programUrl +bookings.getId());
        wxMsgVo.setData(m);
        ResponseEntity<String> responseEntity =
                restTemplate.postForEntity(url, wxMsgVo, String.class);
        log.info("微信小程序-> 取消通知订阅消息发送日志:{}",JSONObject.toJSONString(responseEntity));
    }
 
    /**
     * 会议取消通知
     * @param systemUser 用户
     * @param bookings 会议信息
     * @param rooms 会议室信息
     */
    public  void  bookingsCancel(SystemUser systemUser, Bookings bookings, Rooms rooms){
        RestTemplate restTemplate = new RestTemplate();
        String accessToken = systemDictDataBiz.queryCodeById(106);
        log.info("微信小程序 会议开始订阅消息发送日志 -> accessToken:{}",accessToken);
        //这里简单起见我们每次都获取最新的access_token(时间开发中,应该在access_token快过期时再重新获取)
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+accessToken;
        //拼接推送的模版
        WxMsgVO wxMsgVo = new WxMsgVO();
        //用户的openid(要发送给那个用户)
        wxMsgVo.setTouser(systemUser.getOpenid());
        //订阅消息模板id
        wxMsgVo.setTemplate_id("6A79DG8Fd9CjUDHhxJVv8dWcqww0v9_wwQLr7_ZuNqU");
        Map<String, TemplateData> m = new HashMap<>(4);
        //会议名称
        m.put("thing4", new TemplateData(bookings.getName()));
        //会议时间
        m.put("time2", new TemplateData(DateUtil.getDate(bookings.getStartTime(),"yyyy-MM-dd")));
//        m.put("time4", new TemplateData(
//                DateUtil.getDate(bookings.getStartTime(),"yyyy-MM-dd")+
//                        " " + DateUtil.getDate(bookings.getStartTime(),"HH:mm")+
//                        " 至 " + DateUtil.getDate(bookings.getEndTime(),"HH:mm") ) );
        //会议室
        m.put("thing1", new TemplateData(rooms.getName()));
        //备注
        m.put("thing3", new TemplateData(bookings.getRemark()));
        wxMsgVo.setPage(programUrl +bookings.getId());
        wxMsgVo.setData(m);
        ResponseEntity<String> responseEntity =
                restTemplate.postForEntity(url, wxMsgVo, String.class);
        log.info("微信小程序->取消通知订阅消息发送日志:{}",JSONObject.toJSONString(responseEntity));
    }
 
 
    /**
     * 会议预约成功通知
     * @param systemUser 用户
     * @param bookings 会议信息
     * @param rooms 会议室信息
     */
    public void  bookingsReservation(SystemUser systemUser, Bookings bookings, Rooms rooms){
        RestTemplate restTemplate = new RestTemplate();
        String accessToken = systemDictDataBiz.queryCodeById(106);
        log.info("微信小程序 会议开始订阅消息发送日志 -> accessToken:{}",accessToken);
 
        //这里简单起见我们每次都获取最新的access_token(时间开发中,应该在access_token快过期时再重新获取)
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+accessToken;
        //拼接推送的模版
        WxMsgVO wxMsgVo = new WxMsgVO();
        //用户的openid(要发送给那个用户)
        wxMsgVo.setTouser(systemUser.getOpenid());
        //订阅消息模板id
        wxMsgVo.setTemplate_id("usZxJqJ2AYPg24ViPMuY2lI-YRjdSD2JvFY3gY5kqh4");
        Map<String, TemplateData> m = new HashMap<>(4);
        //会议主题
        m.put("thing3", new TemplateData(bookings.getName()));
        //会议时间
        m.put("time1", new TemplateData(
                DateUtil.getDate(bookings.getStartTime(),"yyyy-MM-dd")
//                        +
//                        " " + DateUtil.getDate(bookings.getStartTime(),"HH:mm")+
//                        " 至 " + DateUtil.getDate(bookings.getEndTime(),"HH:mm")
        ) );
        //会议地点
        m.put("thing2", new TemplateData(rooms.getName()));
        //发起人
        m.put("thing4", new TemplateData(systemUser.getRealname()));
        wxMsgVo.setPage(programUrl +bookings.getId());
        wxMsgVo.setData(m);
        ResponseEntity<String> responseEntity =
                restTemplate.postForEntity(url, wxMsgVo, String.class);
        log.info("微信小程序-> 会议预约成功订阅消息发送日志:{}",JSONObject.toJSONString(responseEntity));
    }
 
 
 
//    /**
//     * 获取token
//     */
//    private static String getAccessToken() {
//        //发送请求获取token
//        JSONObject token = null;
//        try {
//            token = JSONObject.parseObject(HttpsUtil.get(APP_ACCESS_TOKEN_URL,false));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        JSONObject jsonObject = token;
//        String accessToken = (String) jsonObject.get("access_token");
//        Integer expiresIn = (Integer) jsonObject.get("expires_in");
//        //创建token对象,并存储
//        return new AccessToken(accessToken,String.valueOf(expiresIn)).getAccessToken();
//    }
 
}