commit | author | age
|
9bcb19
|
1 |
/* |
I |
2 |
Copyright [2020] [https://www.xiaonuo.vip] |
|
3 |
|
|
4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
you may not use this file except in compliance with the License. |
|
6 |
You may obtain a copy of the License at |
|
7 |
|
|
8 |
http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
|
|
10 |
Unless required by applicable law or agreed to in writing, software |
|
11 |
distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
See the License for the specific language governing permissions and |
|
14 |
limitations under the License. |
|
15 |
|
|
16 |
Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: |
|
17 |
|
|
18 |
1.请不要删除和修改根目录下的LICENSE文件。 |
|
19 |
2.请不要删除和修改Snowy源码头部的版权声明。 |
|
20 |
3.请保留源码和相关描述文件的项目出处,作者声明等。 |
|
21 |
4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
22 |
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
23 |
6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip |
|
24 |
*/ |
|
25 |
package vip.xiaonuo.sys.modular.sms.service.impl; |
|
26 |
|
|
27 |
import cn.hutool.core.collection.CollectionUtil; |
|
28 |
import cn.hutool.core.util.RandomUtil; |
|
29 |
import cn.hutool.log.Log; |
|
30 |
import com.alibaba.fastjson.JSON; |
|
31 |
import org.springframework.stereotype.Component; |
|
32 |
import org.springframework.transaction.annotation.Transactional; |
|
33 |
import vip.xiaonuo.core.consts.CommonConstant; |
|
34 |
import vip.xiaonuo.core.sms.SmsSender; |
|
35 |
import vip.xiaonuo.sys.modular.sms.enums.SmsSendStatusEnum; |
|
36 |
import vip.xiaonuo.sys.modular.sms.enums.SmsTypeEnum; |
|
37 |
import vip.xiaonuo.sys.modular.sms.enums.SmsVerifyEnum; |
|
38 |
import vip.xiaonuo.sys.modular.sms.param.SysSmsSendParam; |
|
39 |
import vip.xiaonuo.sys.modular.sms.param.SysSmsVerifyParam; |
|
40 |
import vip.xiaonuo.sys.modular.sms.service.SmsSenderService; |
|
41 |
import vip.xiaonuo.sys.modular.sms.service.SysSmsInfoService; |
|
42 |
|
|
43 |
import javax.annotation.Resource; |
|
44 |
import java.util.Map; |
|
45 |
|
|
46 |
|
|
47 |
/** |
|
48 |
* 短信发送接口实现类 |
|
49 |
* |
|
50 |
* @author yubaoshan |
|
51 |
* @date 2018/7/5 21:05 |
|
52 |
*/ |
|
53 |
@Component |
|
54 |
public class SmsSenderServiceImpl implements SmsSenderService { |
|
55 |
|
|
56 |
private static final Log log = Log.get(); |
|
57 |
|
|
58 |
@Resource |
|
59 |
private SmsSender smsSender; |
|
60 |
|
|
61 |
@Resource |
|
62 |
private SysSmsInfoService sysSmsInfoService; |
|
63 |
|
|
64 |
@Transactional(rollbackFor = Exception.class) |
|
65 |
@Override |
|
66 |
public boolean sendShortMessage(SysSmsSendParam sysSmsSendParam) { |
|
67 |
|
|
68 |
Map<String, Object> params = sysSmsSendParam.getParams(); |
|
69 |
log.info(">>> 发送短信Provider接口,参数为:" + JSON.toJSONString(sysSmsSendParam)); |
|
70 |
|
|
71 |
//如果是纯消息发送,直接发送 |
|
72 |
if (SmsTypeEnum.MESSAGE.equals(sysSmsSendParam.getSmsTypeEnum())) { |
|
73 |
smsSender.sendSms(sysSmsSendParam.getPhoneNumbers(), sysSmsSendParam.getTemplateCode(), params); |
|
74 |
log.info(">>> 发送短信Provider接口--message,params的map具体为:" + JSON.toJSONString(params)); |
|
75 |
} else { |
|
76 |
|
|
77 |
//如果是验证类消息发送,需要存储到数据库验证码信息 |
|
78 |
String validateCode; |
|
79 |
|
|
80 |
//如果传的参数中没有code,就初始化一个code放到参数map里 |
|
81 |
if (params != null && params.get(CommonConstant.CODE) != null) { |
|
82 |
validateCode = params.get(CommonConstant.CODE).toString(); |
|
83 |
} else { |
|
84 |
validateCode = RandomUtil.randomNumbers(6); |
|
85 |
if (params == null) { |
|
86 |
params = CollectionUtil.newHashMap(); |
|
87 |
} |
|
88 |
params.put(CommonConstant.CODE, validateCode); |
|
89 |
} |
|
90 |
|
|
91 |
log.info(">>> 发送短信Provider接口,params的map具体为:" + JSON.toJSONString(params)); |
|
92 |
log.info(">>> 发送短信Provider接口,验证码为:" + validateCode); |
|
93 |
|
|
94 |
//存储短信到数据库 |
|
95 |
Long smsId = sysSmsInfoService.saveSmsInfo(sysSmsSendParam, validateCode); |
|
96 |
|
|
97 |
log.info(">>> 开始发送短信:发送的电话号码= " + sysSmsSendParam.getPhoneNumbers() + ",发送的模板号=" + sysSmsSendParam.getTemplateCode() + ",发送的参数是:" + JSON.toJSONString(params)); |
|
98 |
|
|
99 |
//发送短信 |
|
100 |
smsSender.sendSms(sysSmsSendParam.getPhoneNumbers(), sysSmsSendParam.getTemplateCode(), params); |
|
101 |
|
|
102 |
//更新短信发送状态 |
|
103 |
sysSmsInfoService.updateSmsInfo(smsId, SmsSendStatusEnum.SUCCESS); |
|
104 |
} |
|
105 |
|
|
106 |
return true; |
|
107 |
} |
|
108 |
|
|
109 |
@Override |
|
110 |
public SmsVerifyEnum verifyShortMessage(SysSmsVerifyParam sysSmsVerifyParam) { |
|
111 |
log.info(">>> 验证短信Provider接口,参数为:" + JSON.toJSONString(sysSmsVerifyParam)); |
|
112 |
SmsVerifyEnum smsVerifyEnum = sysSmsInfoService.validateSmsInfo(sysSmsVerifyParam); |
|
113 |
log.info(">>> 验证短信Provider接口,响应结果为:" + JSON.toJSONString(smsVerifyEnum)); |
|
114 |
return smsVerifyEnum; |
|
115 |
} |
|
116 |
|
|
117 |
@Override |
|
118 |
public SmsSendStatusEnum getMessageSendStatus(Integer smsId) { |
|
119 |
return null; |
|
120 |
} |
|
121 |
|
|
122 |
} |