陈昶聿
昨天 a993043a20a6c9bccb0ab563e2d955b36a801408
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package com.ruoyi.web.controller.common;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.ruoyi.common.annotation.AddOrgId;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.dx.MessageSend;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.HttpUtil;
import com.ruoyi.common.utils.RSAPublicKeyExample;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.sms.SmsUtils;
import com.ruoyi.system.service.ISysConfigService;
import com.smartor.common.MtSubmitSmUtil;
import com.smartor.common.ShiyiSmsUtil;
import com.smartor.domain.*;
import com.smartor.mapper.UtilsMapper;
import com.smartor.service.IServiceOutPathService;
import com.smartor.service.IServiceSubtaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
@RestController
@Api(description = "知信接口")
@RequestMapping("/sms")
@Slf4j
public class SmsController {
    @Value("${xhsmsPath}")
    private String xhsmsPath;
 
    @Autowired
    private MtSubmitSmUtil mtSubmitSmUtil;
 
    @Autowired
    private ShiyiSmsUtil shiyiSmsUtil;
 
    @Value("${xhsmsAccount}")
    private String xhsmsAccount;
 
    @Value("${xhsmsPwd}")
    private String xhsmsPwd;
    // 虚拟接入码
    @Value("${xhsmsjrm}")
    private String xhsmsjrm;
 
    @Value("${localIP}")
    private String ip;
 
    @Value("${req_path}")
    private String req_path;
 
    @Value("${pub_key}")
    private String pub_key;
 
    @Autowired
    private IServiceOutPathService iServiceOutPathService;
    @Autowired
    private IServiceSubtaskService iServiceSubtaskService;
    @Autowired
    private ISysConfigService configService;
 
    @Value("${spring.profiles.active}")
    private String active;
 
    /**
     * @param
     * @return
     */
    @ApiOperation("短信发送")
    @PostMapping("/sendTest")
    public AjaxResult send(@RequestBody smsVO vo) {
        String sendMsg = SmsUtils.sendSms(xhsmsPath, xhsmsAccount, xhsmsPwd, vo.getPhone(), vo.getContent());
        return AjaxResult.success(sendMsg);
    }
 
    @ApiOperation("市一HIS短信XML测试")
    @PostMapping("/sendShiyiTest")
    public AjaxResult sendShiyiTest(@RequestBody ShiyiSmsRequest request) {
        try {
            ShiyiSmsResponse response = shiyiSmsUtil.sendSms(request);
            return AjaxResult.success(response);
        } catch (Exception e) {
            log.error("市一HIS短信XML测试失败", e);
            return AjaxResult.error("市一HIS短信XML测试失败: " + e.getMessage());
        }
    }
 
    /**
     * @param
     * @return
     */
    @ApiOperation("短信发送")
    @PostMapping("/send")
    public AjaxResult sendAsk(@RequestBody smsVO vo) throws UnsupportedEncodingException {
        String content = "";
        if (StringUtils.isNotEmpty(vo.getSubId())) {
            ServiceSubtask serviceSubtask = iServiceSubtaskService.selectServiceSubtaskById(Long.valueOf(vo.getSubId()));
            if (serviceSubtask.getSendstate() == 4L) content = "该任务不执行";
            return AjaxResult.error(content);
        }
 
        if (StringUtils.isNotEmpty(vo.getTaskId()) && StringUtils.isNotEmpty(vo.getPatId()) && StringUtils.isNotEmpty(vo.getSubId())) {
            RSAPublicKeyExample rsaPublicKeyExample = new RSAPublicKeyExample();
            String taskId = rsaPublicKeyExample.encryptedData(vo.getTaskId().toString(), pub_key);
            String patid = rsaPublicKeyExample.encryptedData(vo.getPatId().toString(), pub_key);
            String subId = rsaPublicKeyExample.encryptedData(vo.getSubId().toString(), pub_key);
            Integer radix = iServiceOutPathService.selectAutoId();
            String format = String.format("%03X", radix);
            ServiceOutPath serviceOutPath = new ServiceOutPath();
            serviceOutPath.setParam1(taskId);
            serviceOutPath.setParam2(patid);
            serviceOutPath.setParam3(vo.getTaskName());
            serviceOutPath.setParam6(subId);
            serviceOutPath.setRadix(format);
            serviceOutPath.setCreateTime(new Date());
            serviceOutPath.setOrgid(vo.getOrgid());
            String url = StringUtils.isNotEmpty(req_path) ? ip + ":" + req_path + "/wt?p=" + format : ip + "/wt?p=" + format;
            content = "您好,邀请您填写出院调查表,请点击" + url + "填写。感谢您配合!";
            serviceOutPath.setUrl(url);
            iServiceOutPathService.insertServiceOutPath(serviceOutPath);
        }
 
        if (ObjectUtils.isNotEmpty(vo.getContent())) content = vo.getContent();
 
        String sendMsg = "false";
        try {
            if (active.equals("hzszlyy")) {
                sendMsg = SmsUtils.sendSms(xhsmsPath, xhsmsAccount, xhsmsPwd, vo.getPhone(), content);
            } else if (active.equals("nhfy")) {
                Map<String, Object> smsResult = mtSubmitSmUtil.submitSm(vo.getPhone(), content);
                if (smsResult != null && smsResult.get("result") != null && smsResult.get("result").toString().equals("0")) {
                    sendMsg = "true";
                }
            } else if (active.equals("sltd")) {
                String data = MessageSend.sendMsg(content, "6", vo.getPhone(), LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
                if (StringUtils.isNotEmpty(data)) {
                    ObjectMapper mapper = new ObjectMapper();
                    JsonNode root = mapper.readTree(data);
                    String status = root.get("result").get(0).get("status").asText();
                    if (status.equals("00000")) {
                        sendMsg = "true";
                    }
                }
            } else if (active.equals("xh")) {
//                Map<String, String> req = new HashMap<>();
//                req.put("phone", vo.getPhone());
//                req.put("content", vo.getContent());
//                Boolean result = HttpUtil.postJsonRequest(xhsmsPath, new Gson().toJson(req));
                Map<String, String> map2 = new HashMap<String, String>();
                map2.put("action", "send");
                map2.put("account", xhsmsAccount);
                map2.put("password", xhsmsPwd);
                map2.put("mobile", vo.getPhone());
                map2.put("content", vo.getContent());
                map2.put("extno", xhsmsjrm);
                map2.put("rt", "json");
                StringBuilder encodedParams = new StringBuilder();
 
                for (Map.Entry<String, String> entry : map2.entrySet()) {
                    if (encodedParams.length() > 0) {
                        encodedParams.append("&");
                    }
                    // 对键和值进行URL编码
                    String encodedKey = URLEncoder.encode(entry.getKey(), "UTF-8");
                    String encodedValue = URLEncoder.encode(entry.getValue(), "UTF-8");
                    encodedParams.append(encodedKey).append("=").append(encodedValue);
                }
                Boolean result = HttpUtils.sendPost(xhsmsPath, encodedParams.toString());
                if (result) {
                    sendMsg = "true";
                }
            } else if (active.equals("ls")) {
                //丽水13家医院,有点不方便,有空再接
                return AjaxResult.error("该短信功能还未开通");
            }
            log.info(sendMsg);
        } catch (Exception ex) {
            log.error(ex.getMessage());
        }
        return AjaxResult.success(sendMsg);
    }
 
    /**
     * @param
     * @return
     */
    @ApiOperation("微信发送")
    @PostMapping("/sendChat")
    public AjaxResult sendChat(@RequestBody smsVO vo) {
        String sendMsg = "";
        if (StringUtils.isNotEmpty(vo.getSubId())) {
            ServiceSubtask serviceSubtask = iServiceSubtaskService.selectServiceSubtaskById(Long.valueOf(vo.getSubId()));
            if (serviceSubtask.getSendstate() == 4L) sendMsg = "该任务不执行";
            return AjaxResult.error(sendMsg);
        }
        if (StringUtils.isNotEmpty(vo.getTaskId()) && StringUtils.isNotEmpty(vo.getPatId()) && StringUtils.isNotEmpty(vo.getSubId())) {
            RSAPublicKeyExample rsaPublicKeyExample = new RSAPublicKeyExample();
            String taskId = rsaPublicKeyExample.encryptedData(vo.getTaskId().toString(), pub_key);
            String patid = rsaPublicKeyExample.encryptedData(vo.getPatId().toString(), pub_key);
            String subId = rsaPublicKeyExample.encryptedData(vo.getSubId().toString(), pub_key);
            Integer radix = iServiceOutPathService.selectAutoId();
            String format = String.format("%03X", radix);
            ServiceOutPath serviceOutPath = new ServiceOutPath();
            serviceOutPath.setParam1(taskId);
            serviceOutPath.setParam2(patid);
            serviceOutPath.setParam3(vo.getTaskName());
            serviceOutPath.setParam6(subId);
            serviceOutPath.setRadix(format);
            serviceOutPath.setCreateTime(new Date());
            serviceOutPath.setOrgid(vo.getOrgid());
            iServiceOutPathService.insertServiceOutPath(serviceOutPath);
            String url = StringUtils.isNotEmpty(req_path) ? ip + ":" + req_path + "/wt?p=" + format : ip + "/wt?p=" + format;
            sendMsg = SmsUtils.sendChat(url, vo.getPhone(), vo.getIdCard());
        }
        return AjaxResult.success(sendMsg);
    }
 
    /**
     * @param
     * @return
     */
    @ApiOperation("生成外链")
    @PostMapping("/getUrl")
    public AjaxResult getUrl(@RequestBody smsVO vo) throws UnsupportedEncodingException {
        String content = "";
        if (StringUtils.isNotEmpty(vo.getTaskId()) && StringUtils.isNotEmpty(vo.getPatId()) && StringUtils.isNotEmpty(vo.getSubId())) {
            RSAPublicKeyExample rsaPublicKeyExample = new RSAPublicKeyExample();
            String taskId = rsaPublicKeyExample.encryptedData(vo.getTaskId().toString(), pub_key);
            String patid = rsaPublicKeyExample.encryptedData(vo.getPatId().toString(), pub_key);
            String subId = rsaPublicKeyExample.encryptedData(vo.getSubId().toString(), pub_key);
            Integer radix = iServiceOutPathService.selectAutoId();
            String format = String.format("%03X", radix);
            ServiceOutPath serviceOutPath = new ServiceOutPath();
            serviceOutPath.setParam1(taskId);
            serviceOutPath.setParam2(patid);
            serviceOutPath.setParam3(vo.getTaskName());
            serviceOutPath.setParam6(subId);
            serviceOutPath.setRadix(format);
            serviceOutPath.setCreateTime(new Date());
            serviceOutPath.setOrgid(vo.getOrgid());
            String url = StringUtils.isNotEmpty(req_path) ? ip + ":" + req_path + "/wt?p=" + format : ip + "/wt?p=" + format;
//            content = "您好,邀请您填写出院调查表,请点击" + url + "填写。感谢您配合!";
            content = url;
            serviceOutPath.setUrl(url);
            iServiceOutPathService.insertServiceOutPath(serviceOutPath);
        }
        if (StringUtils.isNotEmpty(vo.getSubId())) {
            ServiceSubtask serviceSubtask = iServiceSubtaskService.selectServiceSubtaskById(Long.valueOf(vo.getSubId()));
            if (serviceSubtask.getSendstate() == 4L) content = "该任务不执行";
        }
 
        return AjaxResult.success(content);
    }
 
    /**
     * @param
     * @return
     */
    @ApiOperation("短信发送")
    @AddOrgId
    @PostMapping("/sendYunMasSms")
    public AjaxResult sendYunMasSms(@RequestBody smsVO vo) throws Exception {
        String yunMasConfigJson = configService.selectConfigByKey("ls.msg.yunmas", vo.getOrgid());
        SmsUtils.YunMasConfig yunMasConfig = SmsUtils.YunMasConfig.of(yunMasConfigJson);
        SmsUtils.SmsResult smsResult = SmsUtils.sendYunMasSms(yunMasConfig, vo.getPhone(), vo.getContent());
        return AjaxResult.success(smsResult);
    }
 
    /**
     * 丽水妇幼 微信消息发送
     * <p>
     * 外部系统传入患者信息和通知内容,由本系统统一获取 token、解析模板并调用丽水微信 sendMessage 接口。
     *
     * @param request 微信消息请求
     * @return 发送结果
     */
    @Anonymous
    @ApiOperation(value = "丽水妇幼 微信消息发送", notes = "供丽水市妇幼保健院外部调用")
    @Log(title = "丽水妇幼微信消息发送", businessType = BusinessType.OTHER)
    @PostMapping("/sendChatFuyou")
    public AjaxResult sendChatFuyou(@RequestBody LishuiFuyouWxChatRequest request) {
        if (request == null) {
            return AjaxResult.error("请求参数不能为空");
        }
        try {
            SmsUtils.LsWxConfig config = SmsUtils.LsWxConfig.defaultLishui();
 
            SmsUtils.LsWxMessage message = new SmsUtils.LsWxMessage();
            message.setId(request.getTemplateId());
            message.setPatientId(request.getPatientId());
            message.setPatientName(request.getPatientName());
            message.setIdCard(request.getIdCard());
            message.setMobile(request.getMobile());
            message.setMedCardNo(request.getMedCardNo());
            message.setTitleName(request.getTitleName());
            message.setContent(request.getContent());
            message.setUrl(request.getUrl());
 
            log.info("【丽水妇幼微信】收到外部调用,手机号:{},模板ID:{}",
                    request.getMobile(), request.getTemplateId());
            String resp = SmsUtils.sendChatLs(config, message);
            log.info("【丽水妇幼微信】外部调用结果:{}", resp);
 
            JSONObject result;
            try {
                result = JSON.parseObject(resp);
            } catch (Exception e) {
                log.error("【丽水妇幼微信】第三方响应不是合法JSON:{}", resp, e);
                return AjaxResult.error("丽水妇幼微信发送失败:第三方响应异常", resp);
            }
            if (result == null) {
                return AjaxResult.error("丽水妇幼微信发送失败:第三方响应为空", resp);
            }
            if (result.getIntValue("code") != 0) {
                String msg = result.getString("msg");
                return AjaxResult.error("丽水妇幼微信发送失败:"
                        + (StringUtils.isEmpty(msg) ? "第三方接口返回失败" : msg), result);
            }
            return AjaxResult.success("发送成功", result);
        } catch (Exception e) {
            log.error("【丽水妇幼微信】外部调用异常,手机号:{}", request.getMobile(), e);
            return AjaxResult.error("丽水妇幼微信发送异常:" + e.getMessage());
        }
    }
 
}