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());
|
}
|
}
|
|
}
|