瀏覽代碼

增加发送语音

suntianwu 3 年之前
父節點
當前提交
3ea9f7218b

+ 11 - 0
src/main/java/com/care/bms/controller/TestController.java

@@ -48,4 +48,15 @@ public class TestController {
         }
         }
         return Result.success("生成成功",redisUtil.get(RedisKeyConstant.SWITCH_SMS));
         return Result.success("生成成功",redisUtil.get(RedisKeyConstant.SWITCH_SMS));
     }
     }
+
+    @GetMapping("/bms/switch/autoVoice")
+    @ApiOperation(value = "自动发送语音通知")
+    public Result<Object> autoVoiceSwitch(@RequestParam("flag") String flag){
+        if ("on".equals(flag)){
+            redisUtil.set(RedisKeyConstant.SWITCH_VOICE,"on",60*60*24);
+        }else{
+            redisUtil.del(RedisKeyConstant.SWITCH_VOICE);
+        }
+        return Result.success("生成成功",redisUtil.get(RedisKeyConstant.SWITCH_VOICE));
+    }
 }
 }

+ 1 - 0
src/main/java/com/care/common/cache/RedisKeyConstant.java

@@ -52,6 +52,7 @@ public class RedisKeyConstant {
     public static final String KEY_APPLY_LOCAK = "KEY_APPLY:LOCK";
     public static final String KEY_APPLY_LOCAK = "KEY_APPLY:LOCK";
 
 
     public static final String SWITCH_SMS = "SWITCH:SMS";
     public static final String SWITCH_SMS = "SWITCH:SMS";
+    public static final String SWITCH_VOICE = "SWITCH:VOICE";
 
 
     // 频安保用户微信登录信息 2h
     // 频安保用户微信登录信息 2h
     public static final String PINANBAO_WX_LOGIN_INFO = "PINANBAO:WXLOGIN";
     public static final String PINANBAO_WX_LOGIN_INFO = "PINANBAO:WXLOGIN";

+ 26 - 0
src/main/java/com/care/common/service/VoiceSendService.java

@@ -0,0 +1,26 @@
+package com.care.common.service;
+
+
+/**
+ * 编码类型表(SysDimType)表服务接口
+ *
+ * @author makejava
+ * @since 2021-05-21 00:08:30
+ */
+public interface VoiceSendService {
+
+    /**
+     * 事件发生给紧急联系人等发送语音提示
+     * @param phone
+     * @param params
+     * @return
+     */
+    boolean sendVoiceHappen(String phone, String[] params);
+    /**
+     * 事件取消给紧急联系人等发送语音提示
+     * @param phone
+     * @param params
+     * @return
+     */
+    boolean sendVoiceCancel(String phone, String[] params);
+}

+ 163 - 1
src/main/java/com/care/common/service/impl/CareEventOrderServiceImpl.java

@@ -54,6 +54,8 @@ public class CareEventOrderServiceImpl extends ServiceImpl<CareEventOrderMapper,
     @Resource
     @Resource
     private SmsSendService smsSendService;
     private SmsSendService smsSendService;
     @Resource
     @Resource
+    private VoiceSendService voiceSendService;
+    @Resource
     private CareHouseOlderRelService careHouseOlderRelService;
     private CareHouseOlderRelService careHouseOlderRelService;
 
 
     /**
     /**
@@ -145,9 +147,16 @@ public class CareEventOrderServiceImpl extends ServiceImpl<CareEventOrderMapper,
         List<CareEventOrderChamb> chambList =  addChamberlainToOrder(order);
         List<CareEventOrderChamb> chambList =  addChamberlainToOrder(order);
         List<CareEventOrderContactStatus> contacts = addContactToOrderContact(order);
         List<CareEventOrderContactStatus> contacts = addContactToOrderContact(order);
         sendSms(order,chambList,contacts);
         sendSms(order,chambList,contacts);
+        sendVoice(order,chambList,contacts);
         return true;
         return true;
     }
     }
 
 
+    /**
+     * 发送短信
+     * @param order
+     * @param chambList
+     * @param contacts
+     */
     private void sendSms(CareEventOrder order,List<CareEventOrderChamb> chambList,List<CareEventOrderContactStatus> contacts){
     private void sendSms(CareEventOrder order,List<CareEventOrderChamb> chambList,List<CareEventOrderContactStatus> contacts){
         try{
         try{
             Object switchSms = redisUtil.get(RedisKeyConstant.SWITCH_SMS);
             Object switchSms = redisUtil.get(RedisKeyConstant.SWITCH_SMS);
@@ -208,6 +217,81 @@ public class CareEventOrderServiceImpl extends ServiceImpl<CareEventOrderMapper,
             log.error("发送短信通知失败",e);
             log.error("发送短信通知失败",e);
         }
         }
     }
     }
+
+    /**
+     * 发送语音通知
+     * @param order
+     * @param chambList
+     * @param contacts
+     */
+    private void sendVoice(CareEventOrder order,List<CareEventOrderChamb> chambList,List<CareEventOrderContactStatus> contacts){
+        try{
+            Object switchVoice = redisUtil.get(RedisKeyConstant.SWITCH_VOICE);
+            if (switchVoice != null){
+                //给联系人发送语音通知
+                CareHouse house = this.careHouseService.getById(order.getHouseId());
+                if (CollUtil.isNotEmpty(contacts)){
+                    contacts.forEach(item ->{
+                        boolean voiceResult = false;
+                        String[] params = null;
+                        if (OrderTypeEnum.JIU_ZHI.getValue().equals(order.getOrderType())){
+                            params = new String[]{DateUtil.format(order.getCreateTime(),"yyyy年MM月dd日HH时mm分ss秒"),"久滞",house.getName()};
+                            voiceResult = voiceSendService.sendVoiceHappen(item.getContactPhone(),params);
+                        } else if(OrderTypeEnum.DIE_DAO.getValue().equals(order.getOrderType())){
+                            params = new String[]{DateUtil.format(order.getCreateTime(),"yyyy年MM月dd日HH时mm分ss秒"),"跌倒",house.getName()};
+                            voiceResult = voiceSendService.sendVoiceHappen(item.getContactPhone(),params);
+                        }
+
+                        if (voiceResult) {
+                            CareEventOrderHandleHis his2 = new CareEventOrderHandleHis();
+                            his2.setOrgId(item.getOrgId());
+                            his2.setStationId(item.getStationId());
+                            his2.setOrderId(item.getOrderId());
+                            his2.setLogType(LogTypeEnum.SMS.getValue());
+                            his2.setLogObjectId(item.getContactId());
+                            his2.setLogObjectName(item.getContactName());
+                            his2.setLogResult("语音通知");
+                            his2.setOpUserRole(UserRoleEnum.SEAT.getValue());
+                            his2.setCreateTime(DateUtil.date());
+                            his2.setRelationTypeDesc(item.getRelationTypeDesc());
+                            this.careEventOrderHandleHisService.save(his2);
+                        }
+                    });
+                }
+                //给管家发语音通知
+                if (CollUtil.isNotEmpty(chambList)){
+                    chambList.forEach(item ->{
+                        boolean voiceResult = false;
+                        String[] params = null;
+                        if (OrderTypeEnum.JIU_ZHI.getValue().equals(order.getOrderType())){
+                            params = new String[]{DateUtil.format(order.getCreateTime(),"yyyy年MM月dd日HH时mm分ss秒"),"久滞",house.getName()};
+                            voiceResult = voiceSendService.sendVoiceHappen(item.getPhone(),params);
+                        }else if(OrderTypeEnum.DIE_DAO.getValue().equals(order.getOrderType())){
+                            params = new String[]{DateUtil.format(order.getCreateTime(),"yyyy年MM月dd日HH时mm分ss秒"),"跌倒",house.getName()};
+                            voiceResult = voiceSendService.sendVoiceHappen(item.getPhone(),params);
+                        }
+                        if (voiceResult){
+                            CareEventOrderHandleHis his2 = new CareEventOrderHandleHis();
+                            his2.setOrgId(item.getOrgId());
+                            his2.setStationId(item.getStationId());
+                            his2.setOrderId(item.getOrderId());
+                            his2.setLogType(LogTypeEnum.SMS.getValue());
+                            his2.setLogObjectId(item.getChambId());
+                            his2.setLogObjectName(item.getChambName());
+                            his2.setLogResult("语音通知");
+                            his2.setOpUserRole(UserRoleEnum.SEAT.getValue());
+                            his2.setCreateTime(DateUtil.date());
+                            his2.setRelationTypeDesc("管家");
+                            this.careEventOrderHandleHisService.save(his2);
+                        }
+                    });
+                }
+            }
+        }catch (Exception e){
+            log.error("发送语音通知失败",e);
+        }
+    }
+
     /**
     /**
      * 将老人加入到 工单中
      * 将老人加入到 工单中
      * @param order
      * @param order
@@ -322,9 +406,9 @@ public class CareEventOrderServiceImpl extends ServiceImpl<CareEventOrderMapper,
         orderDb.setId(order.getId());
         orderDb.setId(order.getId());
         this.saveHisOrder(order, orderDb,true);
         this.saveHisOrder(order, orderDb,true);
         cancelEventSMSNotic(order);
         cancelEventSMSNotic(order);
+        cancelEventVoiceNotic(order);
         return true;
         return true;
     }
     }
-
     /**
     /**
      * 取消事件,短信通知
      * 取消事件,短信通知
      * @param order
      * @param order
@@ -396,4 +480,82 @@ public class CareEventOrderServiceImpl extends ServiceImpl<CareEventOrderMapper,
             log.error("发送短信通知失败",e);
             log.error("发送短信通知失败",e);
         }
         }
     }
     }
+
+    /**
+     * 取消事件,语音通知
+     * @param order
+     */
+    private void cancelEventVoiceNotic(CareEventOrder order){
+        try{
+            Object switchVoice = redisUtil.get(RedisKeyConstant.SWITCH_VOICE);
+            if (switchVoice != null){
+                List<CareEventOrderChamb> chambList = new ArrayList<>();
+                CareEventOrderChamb chamb =  this.careEventOrderChambService.getChambOrderByOrderId(order.getId());
+                chambList.add(chamb);
+                QueryWrapper<CareEventOrderContactStatus> queryWrapper = new QueryWrapper<>();
+                queryWrapper.lambda().eq(CareEventOrderContactStatus::getOrderId,order.getId());
+                List<CareEventOrderContactStatus> contacts = this.careEventOrderContactStatusService.list(queryWrapper);
+
+                //给联系人发送语音
+                CareHouse house = this.careHouseService.getById(order.getHouseId());
+                if (CollUtil.isNotEmpty(contacts)){
+                    contacts.forEach(item ->{
+                        boolean voiceResult = false;
+                        String[] params = null;
+                        if (OrderTypeEnum.JIU_ZHI.getValue().equals(order.getOrderType())){
+                            params = new String[]{DateUtil.format(order.getCreateTime(),"yyyy年MM月dd日HH时mm分ss秒"),"久滞",house.getName()};
+                            voiceResult = voiceSendService.sendVoiceCancel(item.getContactPhone(),params);
+                        }else if(OrderTypeEnum.DIE_DAO.getValue().equals(order.getOrderType())){
+                            params = new String[]{DateUtil.format(order.getCreateTime(),"yyyy年MM月dd日HH时mm分ss秒"),"跌倒",house.getName()};
+                            voiceResult = voiceSendService.sendVoiceCancel(item.getContactPhone(),params);
+                        }
+                        if (voiceResult){
+                            CareEventOrderHandleHis his2 = new CareEventOrderHandleHis();
+                            his2.setOrgId(item.getOrgId());
+                            his2.setStationId(item.getStationId());
+                            his2.setOrderId(item.getOrderId());
+                            his2.setLogType(LogTypeEnum.SMS.getValue());
+                            his2.setLogObjectId(item.getContactId());
+                            his2.setLogObjectName(item.getContactName());
+                            his2.setLogResult("事件取消语音通知");
+                            his2.setOpUserRole(UserRoleEnum.SEAT.getValue());
+                            his2.setCreateTime(DateUtil.date());
+                            his2.setRelationTypeDesc(item.getRelationTypeDesc());
+                            this.careEventOrderHandleHisService.save(his2);
+                        }
+                    });
+                }
+                //给管家发短信
+                if (CollUtil.isNotEmpty(chambList)){
+                    chambList.forEach(item ->{
+                        boolean voiceResult = false;
+                        String[] params = null;
+                        if (OrderTypeEnum.JIU_ZHI.getValue().equals(order.getOrderType())){
+                            params = new String[]{DateUtil.format(order.getCreateTime(),"yyyy年MM月dd日HH时mm分ss秒"),"久滞",house.getName()};
+                            voiceResult = voiceSendService.sendVoiceCancel(item.getPhone(),params);
+                        }else if(OrderTypeEnum.DIE_DAO.getValue().equals(order.getOrderType())){
+                            params = new String[]{DateUtil.format(order.getCreateTime(),"yyyy年MM月dd日HH时mm分ss秒"),"跌倒",house.getName()};
+                            voiceResult = voiceSendService.sendVoiceCancel(item.getPhone(),params);
+                        }
+                        if (voiceResult){
+                            CareEventOrderHandleHis his2 = new CareEventOrderHandleHis();
+                            his2.setOrgId(item.getOrgId());
+                            his2.setStationId(item.getStationId());
+                            his2.setOrderId(item.getOrderId());
+                            his2.setLogType(LogTypeEnum.SMS.getValue());
+                            his2.setLogObjectId(item.getChambId());
+                            his2.setLogObjectName(item.getChambName());
+                            his2.setLogResult("事件取消语音通知");
+                            his2.setOpUserRole(UserRoleEnum.SEAT.getValue());
+                            his2.setCreateTime(DateUtil.date());
+                            his2.setRelationTypeDesc("管家");
+                            this.careEventOrderHandleHisService.save(his2);
+                        }
+                    });
+                }
+            }
+        }catch (Exception e){
+            log.error("发送语音通知失败",e);
+        }
+    }
 }
 }

+ 79 - 0
src/main/java/com/care/common/service/impl/VoiceSendServiceImpl.java

@@ -0,0 +1,79 @@
+package com.care.common.service.impl;
+
+import cn.hutool.core.util.StrUtil;
+import com.care.common.enums.SmsTemplateTypeEnum;
+import com.care.common.service.VoiceSendService;
+import com.care.common.util.VoiceUtil;
+import com.tencentcloudapi.vms.v20200902.models.SendStatus;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+/**
+ * @Author: lilt
+ * @Date: 2021/6/5
+ * @Desc:
+ */
+@Service
+public class VoiceSendServiceImpl implements VoiceSendService {
+
+    @Value("${voice.notice.endpoint:#{null}}")
+    private String noticeEndpoint;
+    @Value("${voice.notice.region:#{null}}")
+    private String noticeRegion;
+    @Value("${voice.notice.secretId:#{null}}")
+    private String noticeSecretId;
+    @Value("${voice.notice.secretKey:#{null}}")
+    private String noticeecretKey;
+    @Value("${voice.notice.voiceSdkAppId:#{null}}")
+    private String noticeVoiceSdkAppId;
+
+    @Value("${voice.notice.templateidHappen:#{null}}")
+    private String templateidHappen;
+    @Value("${voice.notice.templateidCancel:#{null}}")
+    private String templateidCancel;
+
+    @Override
+    public boolean sendVoiceHappen(String phone, String[] params) {
+        String[] _p = params;
+        String _noticeTemplateId =  templateidHappen;
+        if (StrUtil.isNotEmpty(_noticeTemplateId)){
+            SendStatus sendStatus =  VoiceUtil.sendTtsVoice(noticeEndpoint,
+                    noticeRegion,
+                    noticeSecretId,
+                    noticeecretKey,
+                    noticeVoiceSdkAppId,
+                    _noticeTemplateId,
+                    phone,_p);
+            System.out.println("sendStatus.getCallId() == " +  sendStatus.getCallId());
+            if (sendStatus != null && sendStatus.getCallId() != null){
+                return true;
+            }else{
+                return  false;
+            }
+        }else {
+            return  false;
+        }
+    }
+    @Override
+    public boolean sendVoiceCancel(String phone, String[] params) {
+        String[] _p = params;
+        String _noticeTemplateId =  templateidCancel;
+        if (StrUtil.isNotEmpty(_noticeTemplateId)){
+            SendStatus sendStatus =  VoiceUtil.sendTtsVoice(noticeEndpoint,
+                    noticeRegion,
+                    noticeSecretId,
+                    noticeecretKey,
+                    noticeVoiceSdkAppId,
+                    _noticeTemplateId,
+                    phone,_p);
+            System.out.println("sendStatus.getCallId() == " +  sendStatus.getCallId());
+            if (sendStatus != null && sendStatus.getCallId() != null){
+                return true;
+            }else{
+                return  false;
+            }
+        }else {
+            return  false;
+        }
+    }
+}

+ 118 - 0
src/main/java/com/care/common/util/VoiceUtil.java

@@ -0,0 +1,118 @@
+package com.care.common.util;
+import com.tencentcloudapi.common.Credential;
+import com.tencentcloudapi.common.exception.TencentCloudSDKException;
+
+// 导入可选配置类
+import com.tencentcloudapi.common.profile.ClientProfile;
+import com.tencentcloudapi.common.profile.HttpProfile;
+
+// 导入对应VMS模块的client
+import com.tencentcloudapi.vms.v20200902.VmsClient;
+
+// 导入要请求接口对应的request response类
+import com.tencentcloudapi.vms.v20200902.models.SendStatus;
+import com.tencentcloudapi.vms.v20200902.models.SendTtsVoiceRequest;
+import com.tencentcloudapi.vms.v20200902.models.SendTtsVoiceResponse;
+
+/**
+ * Tencent Cloud Vms SendTtsVoice
+ * https://cloud.tencent.com/document/product/1128/51558
+ *
+ */
+
+public class VoiceUtil {
+    public static SendStatus sendTtsVoice(String endpoint,
+                                          String region,
+                                          String secretId,
+                                          String secretKey,
+                                          String voiceSdkAppId,
+                                          String templateId,
+                                          String phoneNumber,
+                                          String[] templateParam) {
+        try {
+            /* 必要步骤:
+             * 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey。
+             * 这里采用的是从环境变量读取的方式,需要在环境变量中先设置这两个值。
+             * 您也可以直接在代码中写死密钥对,但是小心不要将代码复制、上传或者分享给他人,
+             * 以免泄露密钥对危及您的财产安全。
+             * CAM密匙查询: https://console.cloud.tencent.com/cam/capi*/
+            Credential cred = new Credential(secretId, secretKey);
+
+            // 实例化一个http选项,可选,没有特殊需求可以跳过
+            HttpProfile httpProfile = new HttpProfile();
+            // 设置代理
+            // httpProfile.setProxyHost("host");
+            // httpProfile.setProxyPort(port);
+            /* SDK默认使用POST方法。
+             * 如果您一定要使用GET方法,可以在这里设置。GET方法无法处理一些较大的请求 */
+            httpProfile.setReqMethod("POST");
+            /* SDK有默认的超时时间,非必要请不要进行调整
+             * 如有需要请在代码中查阅以获取最新的默认值 */
+            httpProfile.setConnTimeout(60);
+            /* SDK会自动指定域名。通常是不需要特地指定域名的,但是如果您访问的是金融区的服务
+             * 则必须手动指定域名,例如vms的上海金融区域名: vms.ap-shanghai-fsi.tencentcloudapi.com */
+            httpProfile.setEndpoint(endpoint);
+
+            /* 非必要步骤:
+             * 实例化一个客户端配置对象,可以指定超时时间等配置 */
+            ClientProfile clientProfile = new ClientProfile();
+            /* SDK默认用TC3-HMAC-SHA256进行签名
+             * 非必要请不要修改这个字段 */
+            clientProfile.setSignMethod("TC3-HMAC-SHA256");
+            clientProfile.setHttpProfile(httpProfile);
+            /* 实例化要请求产品(以vms为例)的client对象
+             * 第二个参数是地域信息,可以直接填写字符串ap-guangzhou,或者引用预设的常量 */
+            VmsClient client = new VmsClient(cred, region, clientProfile);
+            /* 实例化一个请求对象,根据调用的接口和实际情况,可以进一步设置请求参数
+             * 您可以直接查询SDK源码确定接口有哪些属性可以设置
+             * 属性可能是基本类型,也可能引用了另一个数据结构
+             * 推荐使用IDE进行开发,可以方便的跳转查阅各个接口和数据结构的文档说明 */
+            SendTtsVoiceRequest req = new SendTtsVoiceRequest();
+
+            /* 填充请求参数,这里request对象的成员变量即对应接口的入参
+             * 您可以通过官网接口文档或跳转到request对象的定义处查看请求参数的定义
+             * 基本类型的设置:
+             * 帮助链接:
+             * 语音消息控制台:https://console.cloud.tencent.com/vms
+             * vms helper:https://cloud.tencent.com/document/product/1128/37720 */
+
+            // 模板 ID,必须填写在控制台审核通过的模板 ID,可登录 [语音消息控制台] 查看模板 ID
+            req.setTemplateId(templateId);
+
+            // 模板参数,若模板没有参数,请提供为空数组
+            req.setTemplateParamSet(templateParam);
+
+            /* 被叫手机号码,采用 e.164 标准,格式为+[国家或地区码][用户号码]
+             * 例如:+8613711112222,其中前面有一个+号,86为国家码,13711112222为手机号 */
+            req.setCalledNumber(phoneNumber);
+
+            // 在 [语音控制台] 添加应用后生成的实际SdkAppid,示例如1400006666
+            req.setVoiceSdkAppid(voiceSdkAppId);
+
+            // 播放次数,可选,最多3次,默认2次
+            Long playTimes = 2L;
+            req.setPlayTimes(playTimes);
+
+            // 用户的 session 内容,腾讯 server 回包中会原样返回
+            String sessionContext = "xxxx";
+            req.setSessionContext(sessionContext);
+
+            /* 通过 client 对象调用 SendTtsVoice 方法发起请求。注意请求方法名与请求对象是对应的
+             * 返回的 res 是一个 SendTtsVoiceResponse 类的实例,与请求对象对应 */
+            SendTtsVoiceResponse res = client.SendTtsVoice(req);
+
+            // 输出json格式的字符串回包
+            System.out.println(SendTtsVoiceResponse.toJsonString(res));
+
+            // 也可以取出单个值,您可以通过官网接口文档或跳转到response对象的定义处查看返回字段的定义
+            System.out.println(res.getRequestId());
+            return res.getSendStatus();
+        } catch (TencentCloudSDKException e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+    public static void main(String[] args) {
+
+    }
+}

+ 8 - 1
src/main/resources/application.properties

@@ -82,5 +82,12 @@ sms.notic.templateid.cancel.jiuzhi=1156954
 sms.notic.templateid.cancel.diedao=1156955
 sms.notic.templateid.cancel.diedao=1156955
 sms.notice.templateid.order.complete=1156955
 sms.notice.templateid.order.complete=1156955
 
 
-
+#语音信息
+voice.notice.endpoint=vms.tencentcloudapi.com
+voice.notice.region=ap-beijing
+voice.notice.secretId=AKIDkKfkbCX0HJ4YkgMlunPkpdBSVHo43mEQ
+voice.notice.secretKey=wb08zZrRkmY3IGyPZS4PUIp66pHUQ6Vd
+voice.notice.voiceSdkAppId=1400591555
+voice.notice.templateidHappen=1182623
+voice.notice.templateidCancel=1182623