陈昶聿
7 天以前 fc1f48f6cb2736bf03b7750563e5d9830d6fd4ba
ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/SmsController.java
@@ -1,44 +1,64 @@
package com.ruoyi.web.controller.common;
import com.ruoyi.common.config.RuoYiConfig;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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.enums.BusinessType;
import com.ruoyi.common.utils.RSAPublicKeyExample;
import com.ruoyi.common.utils.ShortCode;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.sms.smsUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.smartor.domain.HtmlContentVO;
import com.smartor.domain.ServiceOutPath;
import com.smartor.domain.ServiceTask;
import com.smartor.domain.smsVO;
import com.smartor.mapper.ServiceTaskMapper;
import com.smartor.service.IServiceOutPathService;
import com.ruoyi.common.utils.sms.SmsUtils;
import com.ruoyi.message.SendResult;
import com.ruoyi.message.sms.SmsSendRequest;
import com.ruoyi.message.sms.SmsSenderFactory;
import com.ruoyi.message.sms.provider.shiyi.ShiyiSmsRequest;
import com.ruoyi.message.sms.provider.shiyi.ShiyiSmsResponse;
import com.ruoyi.message.sms.provider.shiyi.ShiyiSmsUtil;
import com.ruoyi.message.wechat.WechatSendRequest;
import com.ruoyi.message.wechat.WechatSenderFactory;
import com.ruoyi.system.service.ISysConfigService;
import com.smartor.domain.*;
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.http.MediaType;
import org.springframework.web.bind.annotation.*;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import com.ruoyi.task.domain.ServiceSubtask;
import com.ruoyi.task.domain.ServiceOutPath;
import com.ruoyi.task.service.IServiceOutPathService;
import com.ruoyi.task.service.IServiceSubtaskService;
/**
 * 知信接口(对外短信 / 微信公众号发送入口)。
 * <p>
 * 说明:短信与公众号的站点差异(报文格式、签名、token、返回判定)已全部收口到
 * {@code ruoyi-message} 模块,本类只负责参数组装与按 {@code active} 路由。
 */
@RestController
@Api(description = "知信接口")
@RequestMapping("/sms")
@Slf4j
public class SmsController {
    @Value("${xhsmsPath}")
    private String xhsmsPath;
    @Value("${xhsmsAccount}")
    private String xhsmsAccount;
    @Autowired
    private SmsSenderFactory smsSenderFactory;
    @Value("${xhsmsPwd}")
    private String xhsmsPwd;
    @Autowired
    private WechatSenderFactory wechatSenderFactory;
    @Autowired
    private ShiyiSmsUtil shiyiSmsUtil;
    @Value("${localIP}")
    private String ip;
@@ -51,20 +71,167 @@
    @Autowired
    private IServiceOutPathService iServiceOutPathService;
    @Autowired
    private ServiceTaskMapper serviceTaskMapper;
    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) {
        //与原实现一致:测试接口固定走市一(smsUtils.sendSms,吴山签名)
        SmsSendRequest request = SmsSendRequest.of(vo.getPhone(), vo.getContent());
        request.setCampus("2");
        SendResult sendResult = smsSenderFactory.send("hzszlyy", request);
        return AjaxResult.success(sendResult.getRawResponse());
    }
    @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());
        }
    }
    /**
     * 短信发送
     */
    @ApiOperation("短信发送")
    @PostMapping("/send")
    public AjaxResult send(@RequestBody smsVO vo) {
        String sendMsg = smsUtils.sendSms(xhsmsPath, xhsmsAccount, xhsmsPwd, vo.getPhone(), vo.getContent());
    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 = ShortCode.encode(radix.longValue());
            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 ("ls".equals(active)) {
                //与原实现一致:丽水13家医院的短信功能标记为未开通
                return AjaxResult.error("该短信功能还未开通");
            }
            SmsSendRequest request = SmsSendRequest.of(vo.getPhone(), content, vo.getOrgid());
            //市一区分湖滨/吴山签名,此接口无院区信息,与原实现一致(非湖滨即吴山)
            SendResult sendResult = smsSenderFactory.send(active, request);
            sendMsg = sendResult.asLegacyFlag();
            log.info(sendMsg);
        } catch (Exception ex) {
            log.error(ex.getMessage());
        }
        return AjaxResult.success(sendMsg);
    }
    /**
     * 微信发送
     */
    @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 = ShortCode.encode(radix.longValue());
            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;
            //与原实现一致:该接口固定走市一公众号
            WechatSendRequest request = new WechatSendRequest();
            request.setIdCard(vo.getIdCard());
            request.setPhone(vo.getPhone());
            request.setUrl(url);
            request.setOrgid(vo.getOrgid());
            SendResult sendResult = wechatSenderFactory.send("hzszlyy", request);
            sendMsg = sendResult.getRawResponse();
        }
        return AjaxResult.success(sendMsg);
    }
    /**
     * 生成外链
     */
    @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 = ShortCode.encode(radix.longValue());
            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);
    }
    /**
@@ -72,36 +239,70 @@
     * @return
     */
    @ApiOperation("短信发送")
    @PostMapping("/sendAsk")
    public AjaxResult sendAsk(@RequestBody smsVO vo) throws UnsupportedEncodingException {
        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);
        ServiceOutPath serviceOutPath = new ServiceOutPath();
        serviceOutPath.setParam1(taskId);
        serviceOutPath.setParam2(patid);
        serviceOutPath.setParam3(vo.getTaskName());
        serviceOutPath.setParam6(subId);
        serviceOutPath.setCreateTime(new Date());
        iServiceOutPathService.insertServiceOutPath(serviceOutPath);
        String format = String.format("%03X", serviceOutPath.getId());
        serviceOutPath.setRadix(format);
        serviceOutPath.setUpdateTime(new Date());
        iServiceOutPathService.updateServiceOutPath(serviceOutPath);
        String url = ip + ":" + req_path + "/wt?p=" + format;
        ServiceTask serviceTask = serviceTaskMapper.selectServiceTaskByTaskid(Long.valueOf(taskId));
        //如果type是语音随访的话(说明补偿发送方式中有电话随访的方式,这里的外链就地址只能用/sf)
        if (serviceTask.getType().equals("1")) {
            url = ip + ":" + req_path + "/sf?p=" + format;
        }
        //String url = = ip + ":" + req_path + "/outsideChainwt?param1=" + taskId + "&param2=" + patid + "&param3=" + URLEncoder.encode(vo.getTaskName(), StandardCharsets.UTF_8.toString()) + "&param5=false";
        String content = "您好,邀请您填写出院调查表,请点击" + url + "填写。感谢您配合!";
        String sendMsg = smsUtils.sendSms(xhsmsPath, xhsmsAccount, xhsmsPwd, vo.getPhone(), content);
        return AjaxResult.success(sendMsg);
    @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());
        }
    }
}