阿里云短信工具类

master
litao 1 year ago
parent 203646760e
commit 899ee964b9
  1. 25
      src/main/java/org/springblade/modules/business/service/impl/DeviceServiceImpl.java
  2. 39
      src/main/java/org/springblade/modules/business/sms/AliYunSmsConfig.java
  3. 72
      src/main/java/org/springblade/modules/business/sms/AliYunSmsManager.java
  4. 2
      src/main/java/org/springblade/modules/system/controller/DeptController.java
  5. 5
      src/main/java/org/springblade/modules/system/mapper/TopMenuSettingMapper.xml
  6. 10
      src/main/resources/application-dev.yml

@ -195,6 +195,7 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceMapper, Device> imp
throw new ServiceException("实验室【" + device.getLimsName() + "】的房间【" + device.getRoomName() + "】不存在,请修改后再上传!");
}
device.setRoomId(rooms.get(0).getId());
device.setRunStatus(CommonConstant.DEVICE_RUN_STATUS_WORKING);
list.add(device);
}
});
@ -214,16 +215,20 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceMapper, Device> imp
.eq(Device::getFloorId, device.getFloorId())
.eq(Device::getRoomId, device.getRoomId());
List<Device> devices = this.list(wrapper);
// 查询关联的设备巡检数据
List<DeviceMaintenance> maintenances = maintenanceService.list(Wrappers.lambdaQuery(DeviceMaintenance.class).in(DeviceMaintenance::getDeviceId, devices.stream().map(Device::getId).toList()));
// 组装视图返回
devices.forEach(devic -> {
DeviceVO deviceVO = BeanUtil.copyProperties(devic, DeviceVO.class);
if (deviceVO != null) {
deviceVO.setMaintenances(maintenances.stream().filter(maintenance -> maintenance.getDeviceId().equals(devic.getId())).toList());
deviceVOList.add(deviceVO);
}
});
if (CollectionUtil.isNotEmpty(devices)) {
// 查询关联的设备巡检数据
List<Long> deviceIds = devices.stream().map(Device::getId).toList();
List<DeviceMaintenance> maintenances = maintenanceService.list(Wrappers.lambdaQuery(DeviceMaintenance.class).in(DeviceMaintenance::getDeviceId, deviceIds));
// 组装视图返回
devices.forEach(devic -> {
DeviceVO deviceVO = BeanUtil.copyProperties(devic, DeviceVO.class);
if (deviceVO != null) {
deviceVO.setMaintenances(maintenances.stream().filter(maintenance -> maintenance.getDeviceId().equals(devic.getId())).toList());
deviceVOList.add(deviceVO);
}
});
}
return deviceVOList;
}

@ -0,0 +1,39 @@
package org.springblade.modules.business.sms;
import com.aliyun.teaopenapi.models.*;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author xueyaoxuan
*/
@Data
@Component
@ConfigurationProperties(prefix = "sms.aliyun")
public class AliYunSmsConfig {
private String accessKeyId;
private String accessKeySecret;
private String signName;
private boolean isPushSms;
/**
* 使用AK&SK初始化账号Client
*
* @return Client
* @throws Exception
*/
public com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
Config config = new Config()
// AccessKey ID
.setAccessKeyId(this.getAccessKeyId())
// AccessKey Secret
.setAccessKeySecret(this.getAccessKeySecret());
// 访问的域名
config.endpoint = "dysmsapi.aliyuncs.com";
return new com.aliyun.dysmsapi20170525.Client(config);
}
}

@ -0,0 +1,72 @@
package org.springblade.modules.business.sms;
import cn.hutool.core.util.StrUtil;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Component
public class AliYunSmsManager {
@Autowired
private AliYunSmsConfig aliYunSmsConfig;
public String sendSms(List<String> mobiles, String message, String code) {
SendSmsResponse sendSmsResponse = null;
try{
//调用阿里云api手机号上限1000
if (mobiles.size()>1000){
throw new ServiceException("发送短信手机号上限!");
}
//检验手机号格式
mobiles.forEach(mobile->{
if (StrUtil.isAllEmpty(mobile)){
throw new ServiceException("手机号格式错误!");
}
});
sendSmsResponse = sendALiYunSms(mobiles.stream().collect(Collectors.joining(",")),
message, code);
//
if (200 == sendSmsResponse.getStatusCode()) {
// 发送短信成功,返回短信码进行redis存储
return message;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 发送阿里云短信
*
* @param mobiles 手机号列表
* @param message json格式的模板参数
* @param code 阿里云短信模板code
* @return
* @throws Exception
*/
private SendSmsResponse sendALiYunSms(String mobiles, String message, String code) throws Exception {
//初始化Client对象
Client client = aliYunSmsConfig.createClient();
//构建请求参数
SendSmsRequest sendSmsRequest = new SendSmsRequest()
.setPhoneNumbers(mobiles)
.setSignName(aliYunSmsConfig.getSignName())
.setTemplateCode(code)
.setTemplateParam(message);
//发送短信
return client.sendSms(sendSmsRequest);
}
}

@ -71,7 +71,7 @@ import static org.springblade.core.cache.constant.CacheConstant.SYS_CACHE;
@NonDS
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SYSTEM_NAME + "")
@RequestMapping(AppConstant.APPLICATION_SYSTEM_NAME + "/dept")
@Tag(name = "部门", description = "部门")
public class DeptController extends BladeController {

@ -1,5 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.springblade.modules.system.mapper.TopMenuSettingMapper">
</mapper>

@ -11,8 +11,8 @@ spring:
enabled: false
datasource:
# MySql
# url: jdbc:mysql://192.168.1.12:3306/lab-ops?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
url: jdbc:mysql://127.0.0.1:3306/lab-ops?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
url: jdbc:mysql://192.168.1.12:3306/lab-ops?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
# url: jdbc:mysql://127.0.0.1:3306/lab-ops?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
username: root
password: 123456
@ -29,6 +29,12 @@ sms:
secret-key: PWFmEByyD5YrjMlAyioZZqLtMGxZLu
# 地域,主要由阿里云配置
region-id: cn-hangzhou
aliyun:
accessKeyId: 111
accessKeySecret: 111
signName: 111
isPushSms: true
templateCode: 111
#blade配置
#blade:

Loading…
Cancel
Save