zhuguifei
2025-04-28 442928123f63ee497d766f9a7a14f0a6ee067e25
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package org.jeecg.modules.lims.service.impl;
 
import cn.binarywang.wx.miniapp.api.WxMaMsgService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaUserService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.lims.api.JeecgSystemClient;
import org.jeecg.modules.lims.entity.LimsAppointmentAudit;
import org.jeecg.modules.lims.entity.SysUser;
import org.jeecg.modules.lims.entity.WxAccessToken;
import org.jeecg.modules.lims.service.IWxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
 
@Service
public class WxServiceImpl implements IWxService {
    @Autowired
    private WxMaService wxMaService;
    @Autowired
    private JeecgSystemClient systemClient;
 
    @Autowired
    private LimsAppointmentAuditServiceImpl auditService;
 
    @Override
    public WxAccessToken getWxAccessToken() {
//        RestTemplate restTemplate = new RestTemplate();
//        String uri = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+wxMaConfig.getAppid()+"&secret="+wxMaConfig.getSecret();
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
//        HttpEntity<String> entity = new HttpEntity<String>(headers);
//        ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
//        System.err.println("1212121212");
//        System.err.println(uri);
//        System.out.println(response);
//        System.out.println(response.getBody());
 
        try {
            String accessToken = wxMaService.getAccessToken();
 
            WxMaMsgService msgService = wxMaService.getMsgService();
            WxMaSubscribeMessage msg = new WxMaSubscribeMessage();
            List<WxMaSubscribeMessage.MsgData> data = new ArrayList<>();
            WxMaSubscribeMessage.MsgData d1 = new WxMaSubscribeMessage.MsgData("thing1", "朱桂飞");
            WxMaSubscribeMessage.MsgData d2 = new WxMaSubscribeMessage.MsgData("thing2", "软件开发部");
            WxMaSubscribeMessage.MsgData d3 = new WxMaSubscribeMessage.MsgData("thing3", "高温测试仪");
            WxMaSubscribeMessage.MsgData d4 = new WxMaSubscribeMessage.MsgData("short_thing4", "4小时");
            data.add(d1);
            data.add(d2);
            data.add(d3);
            data.add(d4);
            msg.setMiniprogramState("developer");
            msg.setTemplateId("B_o0wim0Y0logboa6uuu3dYurdW9uHiVHOGAEAd1sOw");
            msg.setPage("pages/appo/appoAudit");
            msg.setToUser("ogmOE5B2uxDwMhuWkkOsq885p_hQ");
            msg.setData(data);
            msgService.sendSubscribeMsg(msg);
            System.err.println(accessToken);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
 
        return null;
    }
 
    @Override
    public void sendMessage(Map<String, String> params, LimsAppointmentAudit audit) {
        System.err.println(params.toString());
        //1.发送消息服务
        WxMaMsgService msgService = wxMaService.getMsgService();
        //2.组装消息
        WxMaSubscribeMessage msg = new WxMaSubscribeMessage();
        List<WxMaSubscribeMessage.MsgData> data = new ArrayList<>();
        WxMaSubscribeMessage.MsgData d1 = new WxMaSubscribeMessage.MsgData("thing1", params.get("realname"));
        WxMaSubscribeMessage.MsgData d2 = new WxMaSubscribeMessage.MsgData("thing2", params.get("depart"));
        WxMaSubscribeMessage.MsgData d3 = new WxMaSubscribeMessage.MsgData("thing3", params.get("equ"));
        WxMaSubscribeMessage.MsgData d4 = new WxMaSubscribeMessage.MsgData("short_thing4", params.get("time"));
        data.add(d1);
        data.add(d2);
        data.add(d3);
        data.add(d4);
        msg.setMiniprogramState("formal");
        msg.setTemplateId("B_o0wim0Y0logboa6uuu3dYurdW9uHiVHOGAEAd1sOw");
        msg.setPage("pages/appo/appoAudit");
        msg.setData(data);
        //3.查询需要推送的用户
        List<SysUser> result = systemClient.minilist().getResult();
        AtomicReference<String> errMessage = new AtomicReference<>();
        errMessage.set("");
        AtomicBoolean success = new AtomicBoolean(false);
        if (result.size()>0) {
            result.forEach(item -> {
                //4.发送消息
                try {
                    if (!StringUtils.isEmpty(item.getOpenid())) {
                        msg.setToUser(item.getOpenid());
                        msgService.sendSubscribeMsg(msg);
                        success.set(true);
                        errMessage.set("");
                    } else {
                        System.err.println("微信推送消息:审批管理员openid为空!");
                        if (!success.get()) {
                            errMessage.set("微信推送消息:审批管理员openid为空!");
                        }
                    }
                } catch (WxErrorException e) {
                    e.printStackTrace();
                    if (!success.get()) {
                        errMessage.set("微信推送消息:消息推送异常!");
                    }
                }
            });
        } else {
            errMessage.set("微信推送消息:没有审批用户,推送消息失败!");
        }
 
 
        audit.setRemark(StringUtils.isEmpty(errMessage.get()) ? "推送成功" : errMessage.get());
        audit.setPush(StringUtils.isEmpty(errMessage.get()) ? 1 : 0);
        auditService.updateById(audit);
    }
 
    @Override
    public void sendMessageToUser(Map<String, String> params, LimsAppointmentAudit audit) {
        //1.发送消息服务
        WxMaMsgService msgService = wxMaService.getMsgService();
        //2.组装消息
        WxMaSubscribeMessage msg = new WxMaSubscribeMessage();
        List<WxMaSubscribeMessage.MsgData> data = new ArrayList<>();
        WxMaSubscribeMessage.MsgData d1 = new WxMaSubscribeMessage.MsgData("thing7", params.get("equ"));
        WxMaSubscribeMessage.MsgData d2 = new WxMaSubscribeMessage.MsgData("thing2", params.get("realname"));
        WxMaSubscribeMessage.MsgData d3 = new WxMaSubscribeMessage.MsgData("time3", params.get("date"));
        WxMaSubscribeMessage.MsgData d4 = new WxMaSubscribeMessage.MsgData("thing10", params.get("result"));
        WxMaSubscribeMessage.MsgData d5 = new WxMaSubscribeMessage.MsgData("thing6", params.get("remark"));
        data.add(d1);
        data.add(d2);
        data.add(d3);
        data.add(d4);
        data.add(d5);
        msg.setMiniprogramState("formal");
        msg.setTemplateId("ZDDaS7ujKRQJuFbNbVCRCbEYlOGF9_lf_xV1GQzeOvg");
        msg.setPage("pages/appo/appoAudit");
        msg.setData(data);
        //3.查询需要推送的用户
        SysUser user = systemClient.queryByUserName(params.get("username")).getResult();
        AtomicReference<String> errMessage = new AtomicReference<>();
        errMessage.set("");
        AtomicBoolean success = new AtomicBoolean(false);
        if (user!=null) {
                //4.发送消息
                try {
                    if (!StringUtils.isEmpty(user.getOpenid())) {
                        msg.setToUser(user.getOpenid());
                        msgService.sendSubscribeMsg(msg);
                        success.set(true);
                        errMessage.set("");
                    } else {
                        System.err.println("微信推送消息:被通知用户openid为空!");
                        if (!success.get()) {
                            errMessage.set("微信推送消息:被通知用户openid为空!");
                        }
                    }
                } catch (WxErrorException e) {
                    e.printStackTrace();
                    if (!success.get()) {
                        errMessage.set("微信推送消息:消息推送异常!");
                    }
                }
 
        }
    }
 
    @Override
    public String getOpenId(String js_code) {
        WxMaUserService userService = wxMaService.getUserService();
        String openid = null;
        try {
            WxMaJscode2SessionResult sessionInfo = userService.getSessionInfo(js_code);
            openid = sessionInfo.getOpenid();
            System.err.println(openid);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
 
        return openid;
    }
 
 
}