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.util.ObjectUtil; |
|
28 |
import cn.hutool.log.Log; |
|
29 |
import com.alibaba.fastjson.JSON; |
|
30 |
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
31 |
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
32 |
import org.springframework.stereotype.Service; |
|
33 |
import org.springframework.transaction.annotation.Transactional; |
|
34 |
import vip.xiaonuo.core.context.constant.ConstantContextHolder; |
|
35 |
import vip.xiaonuo.core.exception.ServiceException; |
|
36 |
import vip.xiaonuo.core.factory.PageFactory; |
|
37 |
import vip.xiaonuo.core.pojo.page.PageResult; |
|
38 |
import vip.xiaonuo.core.pojo.sms.AliyunSmsConfigs; |
|
39 |
import vip.xiaonuo.sys.modular.sms.entity.SysSms; |
|
40 |
import vip.xiaonuo.sys.modular.sms.enums.SmsSendExceptionEnum; |
|
41 |
import vip.xiaonuo.sys.modular.sms.enums.SmsSendStatusEnum; |
|
42 |
import vip.xiaonuo.sys.modular.sms.enums.SmsVerifyEnum; |
|
43 |
import vip.xiaonuo.sys.modular.sms.mapper.SysSmsMapper; |
|
44 |
import vip.xiaonuo.sys.modular.sms.param.SysSmsInfoParam; |
|
45 |
import vip.xiaonuo.sys.modular.sms.param.SysSmsSendParam; |
|
46 |
import vip.xiaonuo.sys.modular.sms.param.SysSmsVerifyParam; |
|
47 |
import vip.xiaonuo.sys.modular.sms.service.SysSmsInfoService; |
|
48 |
|
|
49 |
import java.util.Date; |
|
50 |
import java.util.List; |
|
51 |
|
|
52 |
/** |
|
53 |
* 系统短信接口实现类 |
|
54 |
* |
|
55 |
* @author yubaoshan |
|
56 |
* @date 2018/7/5 13:44 |
|
57 |
*/ |
|
58 |
|
|
59 |
@Service |
|
60 |
public class SysSmsInfoServiceImpl extends ServiceImpl<SysSmsMapper, SysSms> implements SysSmsInfoService { |
|
61 |
|
|
62 |
private static final Log log = Log.get(); |
|
63 |
|
|
64 |
@Override |
|
65 |
public Long saveSmsInfo(SysSmsSendParam sysSmsSendParam, String validateCode) { |
|
66 |
|
|
67 |
if (ObjectUtil.isEmpty(sysSmsSendParam.getPhoneNumbers())) { |
|
68 |
throw new ServiceException(SmsSendExceptionEnum.PHONE_NUMBER_EMPTY); |
|
69 |
} |
|
70 |
if (ObjectUtil.isEmpty(validateCode)) { |
|
71 |
throw new ServiceException(SmsSendExceptionEnum.VALIDATE_CODE_EMPTY); |
|
72 |
} |
|
73 |
if (ObjectUtil.isEmpty(sysSmsSendParam.getTemplateCode())) { |
|
74 |
throw new ServiceException(SmsSendExceptionEnum.TEMPLATE_CODE_EMPTY); |
|
75 |
} |
|
76 |
|
|
77 |
//当前时间 |
|
78 |
Date nowDate = new Date(); |
|
79 |
|
|
80 |
//短信失效时间 |
|
81 |
AliyunSmsConfigs aliyunSmsProperties = ConstantContextHolder.getAliyunSmsConfigs(); |
|
82 |
long invalidateTime = nowDate.getTime() + aliyunSmsProperties.getInvalidateMinutes() * 60 * 1000; |
|
83 |
Date invalidate = new Date(invalidateTime); |
|
84 |
|
|
85 |
SysSms sysSms = new SysSms(); |
|
86 |
sysSms.setCreateTime(nowDate); |
|
87 |
sysSms.setInvalidTime(invalidate); |
|
88 |
sysSms.setPhoneNumbers(sysSmsSendParam.getPhoneNumbers()); |
|
89 |
sysSms.setStatus(SmsSendStatusEnum.WAITING.getCode()); |
|
90 |
sysSms.setSource(sysSmsSendParam.getSmsSendSourceEnum().getCode()); |
|
91 |
sysSms.setTemplateCode(sysSmsSendParam.getTemplateCode()); |
|
92 |
sysSms.setValidateCode(validateCode); |
|
93 |
|
|
94 |
this.save(sysSms); |
|
95 |
|
|
96 |
log.info(">>> 发送短信,存储短信到数据库,数据为:" + JSON.toJSONString(sysSms)); |
|
97 |
|
|
98 |
return sysSms.getId(); |
|
99 |
} |
|
100 |
|
|
101 |
@Override |
|
102 |
public void updateSmsInfo(Long smsId, SmsSendStatusEnum smsSendStatusEnum) { |
|
103 |
SysSms sysSms = this.getById(smsId); |
|
104 |
sysSms.setStatus(smsSendStatusEnum.getCode()); |
|
105 |
this.updateById(sysSms); |
|
106 |
} |
|
107 |
|
|
108 |
@Transactional(rollbackFor = Exception.class) |
|
109 |
@Override |
|
110 |
public SmsVerifyEnum validateSmsInfo(SysSmsVerifyParam sysSmsVerifyParam) { |
|
111 |
|
|
112 |
if (ObjectUtil.isEmpty(sysSmsVerifyParam.getPhoneNumbers())) { |
|
113 |
throw new ServiceException(SmsSendExceptionEnum.PHONE_NUMBER_EMPTY); |
|
114 |
} |
|
115 |
if (ObjectUtil.isEmpty(sysSmsVerifyParam)) { |
|
116 |
throw new ServiceException(SmsSendExceptionEnum.VALIDATE_CODE_EMPTY); |
|
117 |
} |
|
118 |
if (ObjectUtil.isEmpty(sysSmsVerifyParam.getTemplateCode())) { |
|
119 |
throw new ServiceException(SmsSendExceptionEnum.TEMPLATE_CODE_EMPTY); |
|
120 |
} |
|
121 |
|
|
122 |
//查询有没有这条记录 |
|
123 |
LambdaQueryWrapper<SysSms> smsQueryWrapper = new LambdaQueryWrapper<>(); |
|
124 |
|
|
125 |
smsQueryWrapper.eq(SysSms::getPhoneNumbers, sysSmsVerifyParam.getPhoneNumbers()) |
|
126 |
.and(f -> f.eq(SysSms::getSource, sysSmsVerifyParam.getSmsSendSourceEnum().getCode())) |
|
127 |
.and(f -> f.eq(SysSms::getTemplateCode, sysSmsVerifyParam.getTemplateCode())); |
|
128 |
|
|
129 |
smsQueryWrapper.orderByDesc(SysSms::getCreateTime); |
|
130 |
|
|
131 |
List<SysSms> sysSmsList = this.list(smsQueryWrapper); |
|
132 |
|
|
133 |
log.info(">>> 验证短信Provider接口,查询到sms记录:" + JSON.toJSONString(sysSmsList)); |
|
134 |
|
|
135 |
//如果找不到记录,提示验证失败 |
|
136 |
if (ObjectUtil.isEmpty(sysSmsList)) { |
|
137 |
log.info(">>> 验证短信Provider接口,找不到验证码记录,响应验证失败!"); |
|
138 |
return SmsVerifyEnum.ERROR; |
|
139 |
} else { |
|
140 |
|
|
141 |
//获取最近发送的第一条 |
|
142 |
SysSms sysSms = sysSmsList.get(0); |
|
143 |
|
|
144 |
//先判断状态是不是失效的状态 |
|
145 |
if (SmsSendStatusEnum.INVALID.getCode().equals(sysSms.getStatus())) { |
|
146 |
log.info(">>> 验证短信Provider接口,短信状态是失效,响应验证失败!"); |
|
147 |
return SmsVerifyEnum.ERROR; |
|
148 |
} |
|
149 |
|
|
150 |
//如果验证码和传过来的不一致 |
|
151 |
if (!sysSmsVerifyParam.getCode().equals(sysSms.getValidateCode())) { |
|
152 |
log.info(">>> 验证短信Provider接口,验证手机号和验证码不一致,响应验证失败!"); |
|
153 |
return SmsVerifyEnum.ERROR; |
|
154 |
} |
|
155 |
|
|
156 |
//判断是否超时 |
|
157 |
Date invalidTime = sysSms.getInvalidTime(); |
|
158 |
if (ObjectUtil.isEmpty(invalidTime) || new Date().after(invalidTime)) { |
|
159 |
log.info(">>> 验证短信Provider接口,验证码超时,响应验证失败!"); |
|
160 |
return SmsVerifyEnum.EXPIRED; |
|
161 |
} |
|
162 |
|
|
163 |
//验证成功把短信设置成失效 |
|
164 |
sysSms.setStatus(SmsSendStatusEnum.INVALID.getCode()); |
|
165 |
this.updateById(sysSms); |
|
166 |
|
|
167 |
log.info(">>> 验证短信Provider接口,验证码验证成功!"); |
|
168 |
return SmsVerifyEnum.SUCCESS; |
|
169 |
} |
|
170 |
} |
|
171 |
|
|
172 |
@Override |
|
173 |
public PageResult<SysSms> page(SysSmsInfoParam sysSmsInfoParam) { |
|
174 |
LambdaQueryWrapper<SysSms> queryWrapper = new LambdaQueryWrapper<>(); |
|
175 |
if (ObjectUtil.isNotNull(sysSmsInfoParam)) { |
|
176 |
//根据手机号模糊查询 |
|
177 |
if (ObjectUtil.isNotEmpty(sysSmsInfoParam.getPhoneNumbers())) { |
|
178 |
queryWrapper.like(SysSms::getPhoneNumbers, sysSmsInfoParam.getPhoneNumbers()); |
|
179 |
} |
|
180 |
//根据发送状态查询(字典 0 未发送,1 发送成功,2 发送失败,3 失效) |
|
181 |
if (ObjectUtil.isNotEmpty(sysSmsInfoParam.getStatus())) { |
|
182 |
queryWrapper.eq(SysSms::getStatus, sysSmsInfoParam.getStatus()); |
|
183 |
} |
|
184 |
//根据来源查询(字典 1 app, 2 pc, 3 其他) |
|
185 |
if (ObjectUtil.isNotEmpty(sysSmsInfoParam.getSource())) { |
|
186 |
queryWrapper.eq(SysSms::getSource, sysSmsInfoParam.getSource()); |
|
187 |
} |
|
188 |
} |
|
189 |
return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper)); |
|
190 |
} |
|
191 |
|
|
192 |
|
|
193 |
} |