- 短信模板 、 和签名
This commit is contained in:
parent
be94f29791
commit
551f6aa673
@ -1,10 +1,7 @@
|
||||
package cn.iocoder.mall.admin.api;
|
||||
|
||||
import cn.iocoder.mall.admin.api.bo.sms.SmsSignBO;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 短信平台
|
||||
@ -56,25 +53,32 @@ public interface SmsPlatform {
|
||||
/**
|
||||
* 模板 - 创建
|
||||
*
|
||||
* @param sign 选用的哪个签名
|
||||
* @param template 模板内容
|
||||
* @param tplType 1 为验证码类型,其他为 null
|
||||
*/
|
||||
Result createTemplate(String sign, String template, Integer tplType);
|
||||
Result createTemplate(String template, Integer tplType);
|
||||
|
||||
/**
|
||||
* 获取模板信息
|
||||
* 模板 - 获取
|
||||
*
|
||||
* @param tipId
|
||||
* @param tplId
|
||||
*/
|
||||
Result getTemplate(String tipId);
|
||||
Result getTemplate(String tplId);
|
||||
|
||||
/**
|
||||
* 更新模板内容
|
||||
* 模板 - 更新
|
||||
*
|
||||
* @param tipId 选用的哪个签名
|
||||
* @param tplId 选用的哪个签名
|
||||
* @param template 模板内容
|
||||
* @param tplType 1 为验证码类型,其他为 null
|
||||
*/
|
||||
Result updateTemplate(String tipId, String template, Integer tplType);
|
||||
Result updateTemplate(String tplId, String template, Integer tplType);
|
||||
|
||||
/**
|
||||
* 模板 - 删除
|
||||
*
|
||||
* @param tplId
|
||||
* @return
|
||||
*/
|
||||
Result deleteTemplate(String tplId);
|
||||
}
|
||||
|
@ -43,18 +43,25 @@ public interface SmsService {
|
||||
void createTemplate(Integer smsSignId, String template, Integer tplType);
|
||||
|
||||
/**
|
||||
* 获取模板信息
|
||||
* 模板 - 获取
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
SmsTemplateBO getTemplate(String id);
|
||||
SmsTemplateBO getTemplate(Integer id);
|
||||
|
||||
/**
|
||||
* 更新模板内容
|
||||
* 模板 - 更新
|
||||
*
|
||||
* @param id 模板id
|
||||
* @param template 模板内容
|
||||
* @param tplType 1 为验证码类型,其他为 null
|
||||
*/
|
||||
void updateTemplate(String id, String template, Integer tplType);
|
||||
void updateTemplate(Integer id, String template, Integer tplType);
|
||||
|
||||
/**
|
||||
* 模板 - 删除
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
void deleteTemplate(Integer id);
|
||||
}
|
||||
|
@ -48,14 +48,11 @@ public enum AdminErrorCodeEnum {
|
||||
DATA_DICT_NOT_EXISTS(1002005001, "该数据字典不存在"),
|
||||
|
||||
// ========== 短信模板 1002006000 ==========
|
||||
SMS_SIGN_ADD_FAIL(1002006000, "短信签名添加失败"),
|
||||
SMS_PLATFORM_FAIL(1002006000, "短信模板添加失败"),
|
||||
SMS_SIGN_NOT_EXISTENT(1002006001, "短信签名不存在"),
|
||||
SMS_SIGN_IS_EXISTENT(1002006002, "短信签名已存在"),
|
||||
SMS_SIGN_UPDATE_FAIL(1002006003, "短信更新失败"),
|
||||
|
||||
SMS_TEMPLATE_ADD_FAIL(1002006020, "短信签名不存在"),
|
||||
SMS_TEMPLATE_NOT_EXISTENT(1002006021, "短信签名不存在"),
|
||||
SMS_TEMPLATE_IS_EXISTENT(1002006022, "短信签名不存在"),
|
||||
SMS_TEMPLATE_NOT_EXISTENT(1002006020, "短信签名不存在"),
|
||||
SMS_TEMPLATE_IS_EXISTENT(1002006021, "短信签名不存在"),
|
||||
;
|
||||
|
||||
private final int code;
|
||||
|
@ -2,6 +2,7 @@ package cn.iocoder.mall.admin.dataobject;
|
||||
|
||||
import cn.iocoder.common.framework.dataobject.DeletableDO;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@ -15,6 +16,7 @@ import lombok.experimental.Accessors;
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
@TableName("sms_sign")
|
||||
public class SmsSignDO extends DeletableDO {
|
||||
|
||||
/**
|
||||
|
@ -1,6 +1,7 @@
|
||||
package cn.iocoder.mall.admin.dataobject;
|
||||
|
||||
import cn.iocoder.common.framework.dataobject.DeletableDO;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@ -12,6 +13,7 @@ import lombok.experimental.Accessors;
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
@TableName("sms_template")
|
||||
public class SmsTemplateDO extends DeletableDO {
|
||||
|
||||
/**
|
||||
|
@ -14,6 +14,7 @@ import cn.iocoder.mall.admin.dao.SmsTemplateMapper;
|
||||
import cn.iocoder.mall.admin.dataobject.SmsSignDO;
|
||||
import cn.iocoder.mall.admin.dataobject.SmsTemplateDO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -31,6 +32,8 @@ import java.util.Date;
|
||||
@org.apache.dubbo.config.annotation.Service(validation = "true", version = "${dubbo.provider.SmsService.version}")
|
||||
public class SmsServiceImpl implements SmsService {
|
||||
|
||||
private static final String SMS_TEMPLATE = "【%s】%s";
|
||||
|
||||
@Autowired
|
||||
private SmsSignMapper smsSignMapper;
|
||||
@Autowired
|
||||
@ -64,6 +67,7 @@ public class SmsServiceImpl implements SmsService {
|
||||
.setApplyStatus(result.getApplyStatus())
|
||||
.setDeleted(DeletedStatusEnum.DELETED_NO.getValue())
|
||||
.setUpdateTime(new Date())
|
||||
.setCreateTime(new Date())
|
||||
);
|
||||
}
|
||||
|
||||
@ -107,6 +111,7 @@ public class SmsServiceImpl implements SmsService {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void createTemplate(Integer smsSignId, String template, Integer tplType) {
|
||||
|
||||
SmsSignDO smsSignDO = smsSignMapper.selectOne(
|
||||
@ -119,7 +124,7 @@ public class SmsServiceImpl implements SmsService {
|
||||
|
||||
// 调用平台
|
||||
SmsPlatform.Result result = smsPlatform
|
||||
.createTemplate(smsSignDO.getSign(), template, tplType);
|
||||
.createTemplate(String.format(SMS_TEMPLATE, smsSignDO.getSign(), template), tplType);
|
||||
|
||||
// 保存数据库
|
||||
smsTemplateMapper.insert(
|
||||
@ -136,7 +141,7 @@ public class SmsServiceImpl implements SmsService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public SmsTemplateBO getTemplate(String id) {
|
||||
public SmsTemplateBO getTemplate(Integer id) {
|
||||
SmsTemplateDO smsTemplateDO = smsTemplateMapper.selectOne(
|
||||
new QueryWrapper<SmsTemplateDO>().eq("id", id));
|
||||
|
||||
@ -149,7 +154,8 @@ public class SmsServiceImpl implements SmsService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateTemplate(String id, String template, Integer tplType) {
|
||||
@Transactional
|
||||
public void updateTemplate(Integer id, String template, Integer tplType) {
|
||||
SmsTemplateDO smsTemplateDO = smsTemplateMapper.selectOne(
|
||||
new QueryWrapper<SmsTemplateDO>().eq("id", id));
|
||||
|
||||
@ -158,15 +164,43 @@ public class SmsServiceImpl implements SmsService {
|
||||
AdminErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getMessage());
|
||||
}
|
||||
|
||||
SmsSignDO smsSignDO = smsSignMapper.selectOne(
|
||||
new QueryWrapper<SmsSignDO>().eq("id", smsTemplateDO.getSmsSignId()));
|
||||
|
||||
|
||||
SmsPlatform.Result result = smsPlatform.updateTemplate(
|
||||
smsTemplateDO.getPlatformId(), template, tplType);
|
||||
smsTemplateDO.getPlatformId(),
|
||||
String.format(SMS_TEMPLATE, smsSignDO.getSign(), template), tplType);
|
||||
|
||||
smsTemplateMapper.update(
|
||||
(SmsTemplateDO) new SmsTemplateDO()
|
||||
.setTemplate(template)
|
||||
.setApplyStatus(result.getApplyStatus())
|
||||
.setApplyMessage(result.getApplyMessage())
|
||||
.setUpdateTime(new Date()),
|
||||
new QueryWrapper<SmsTemplateDO>().eq("id", id)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void deleteTemplate(Integer id) {
|
||||
SmsTemplateDO smsTemplateDO = smsTemplateMapper.selectOne(
|
||||
new QueryWrapper<SmsTemplateDO>().eq("id", id));
|
||||
|
||||
if (smsTemplateDO == null
|
||||
|| smsTemplateDO.getDeleted().equals(DeletedStatusEnum.DELETED_YES.getValue())) {
|
||||
throw new ServiceException(AdminErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getCode(),
|
||||
AdminErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getMessage());
|
||||
}
|
||||
|
||||
// 删除 平台的模板
|
||||
smsPlatform.deleteTemplate(smsTemplateDO.getPlatformId());
|
||||
|
||||
// 删除 数据库模板
|
||||
SmsTemplateDO updateTemplate =new SmsTemplateDO();
|
||||
updateTemplate.setDeleted(DeletedStatusEnum.DELETED_YES.getValue());
|
||||
smsTemplateMapper.delete(new UpdateWrapper<SmsTemplateDO>()
|
||||
.set("deleted", DeletedStatusEnum.DELETED_YES).eq("id", id));
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
package cn.iocoder.mall.admin.service;
|
||||
|
||||
import cn.iocoder.common.framework.exception.ServiceException;
|
||||
import cn.iocoder.mall.admin.api.SmsPlatform;
|
||||
import cn.iocoder.mall.admin.api.constant.AdminErrorCodeEnum;
|
||||
import cn.iocoder.mall.admin.api.constant.SmsApplyStatusEnum;
|
||||
import cn.iocoder.mall.admin.api.exception.SmsFailException;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
@ -87,6 +87,10 @@ public class SmsYunPianPlatform implements SmsPlatform {
|
||||
* 模板 - 更新
|
||||
*/
|
||||
private static final String URL_TEMPLATE_UPDATE = "https://sms.yunpian.com/v2/tpl/update.json";
|
||||
/**
|
||||
* 模板 - 删除
|
||||
*/
|
||||
private static final String URL_TEMPLATE_DELETE = "https://sms.yunpian.com/v2/tpl/del.json";
|
||||
|
||||
//编码格式。发送编码格式统一用UTF-8
|
||||
private static String ENCODING = "UTF-8";
|
||||
@ -96,6 +100,13 @@ public class SmsYunPianPlatform implements SmsPlatform {
|
||||
|
||||
@Override
|
||||
public Result createSign(String sign) {
|
||||
try {
|
||||
// 存在直接 return 相当于,创建了
|
||||
return getSign(sign);
|
||||
} catch (ServiceException e) {
|
||||
// skip 不存在会进这里,不错任何操作
|
||||
}
|
||||
|
||||
// 调用 短信平台
|
||||
Map<String, String> params = new LinkedHashMap<>();
|
||||
params.put("apikey", apiKey);
|
||||
@ -103,9 +114,10 @@ public class SmsYunPianPlatform implements SmsPlatform {
|
||||
params.put("notify", "true");
|
||||
String result = post(URL_SIGN_ADD, params);
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
if (!(jsonObject.getInteger("code") == SUCCESS_CODE)) {
|
||||
throw new SmsFailException(AdminErrorCodeEnum.SMS_SIGN_ADD_FAIL.getCode(),
|
||||
AdminErrorCodeEnum.SMS_SIGN_ADD_FAIL.getMessage());
|
||||
if (jsonObject.containsKey("code")
|
||||
&& !(jsonObject.getInteger("code") == SUCCESS_CODE)) {
|
||||
throw new ServiceException(AdminErrorCodeEnum.SMS_PLATFORM_FAIL.getCode(),
|
||||
jsonObject.getString("detail"));
|
||||
}
|
||||
|
||||
JSONObject signJSONObject = (JSONObject) jsonObject.get("sign");
|
||||
@ -123,14 +135,15 @@ public class SmsYunPianPlatform implements SmsPlatform {
|
||||
String result = post(URL_SIGN_GET, params);
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
|
||||
if (!(jsonObject.getInteger("code") == SUCCESS_CODE)) {
|
||||
throw new SmsFailException(AdminErrorCodeEnum.SMS_SIGN_ADD_FAIL.getCode(),
|
||||
AdminErrorCodeEnum.SMS_SIGN_ADD_FAIL.getMessage());
|
||||
if (jsonObject.containsKey("code")
|
||||
&& !(jsonObject.getInteger("code") == SUCCESS_CODE)) {
|
||||
throw new ServiceException(AdminErrorCodeEnum.SMS_PLATFORM_FAIL.getCode(),
|
||||
jsonObject.getString("detail"));
|
||||
}
|
||||
|
||||
JSONArray jsonArray = jsonObject.getJSONArray("sign");
|
||||
if (jsonArray.size() <= 0) {
|
||||
throw new SmsFailException(AdminErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getCode(),
|
||||
throw new ServiceException(AdminErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getCode(),
|
||||
AdminErrorCodeEnum.SMS_SIGN_NOT_EXISTENT.getMessage());
|
||||
}
|
||||
|
||||
@ -150,9 +163,10 @@ public class SmsYunPianPlatform implements SmsPlatform {
|
||||
String result = post(URL_SIGN_UPDATE, params);
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
|
||||
if (!(jsonObject.getInteger("code") == SUCCESS_CODE)) {
|
||||
throw new SmsFailException(AdminErrorCodeEnum.SMS_SIGN_UPDATE_FAIL.getCode(),
|
||||
AdminErrorCodeEnum.SMS_SIGN_UPDATE_FAIL.getMessage());
|
||||
if (jsonObject.containsKey("code")
|
||||
&& !(jsonObject.getInteger("code") == SUCCESS_CODE)) {
|
||||
throw new ServiceException(AdminErrorCodeEnum.SMS_PLATFORM_FAIL.getCode(),
|
||||
jsonObject.getString("detail"));
|
||||
}
|
||||
|
||||
JSONObject signJSONObject = (JSONObject) jsonObject.get("sign");
|
||||
@ -161,15 +175,22 @@ public class SmsYunPianPlatform implements SmsPlatform {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result createTemplate(String sign, String template, Integer tplType) {
|
||||
public Result createTemplate(String template, Integer tplType) {
|
||||
Map<String, String> params = new LinkedHashMap<>();
|
||||
params.put("apikey", apiKey);
|
||||
params.put("tpl_content", sign + template);
|
||||
params.put("tpl_content", template);
|
||||
if (tplType != null) {
|
||||
params.put("tplType", String.valueOf(tplType));
|
||||
}
|
||||
String result = post(URL_TEMPLATE_ADD, params);
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
|
||||
if (jsonObject.containsKey("code")
|
||||
&& !(jsonObject.getInteger("code") == SUCCESS_CODE)) {
|
||||
throw new ServiceException(AdminErrorCodeEnum.SMS_PLATFORM_FAIL.getCode(),
|
||||
jsonObject.getString("detail"));
|
||||
}
|
||||
|
||||
String tipId = jsonObject.getString("tpl_id");
|
||||
String checkStatus = jsonObject.getString("check_status");
|
||||
String reason = jsonObject.getString("reason");
|
||||
@ -178,32 +199,71 @@ public class SmsYunPianPlatform implements SmsPlatform {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result getTemplate(String tipId) {
|
||||
public Result getTemplate(String tplId) {
|
||||
Map<String, String> params = new LinkedHashMap<>();
|
||||
params.put("apikey", apiKey);
|
||||
params.put("tipId", tipId);
|
||||
params.put("tpl_id", tplId);
|
||||
String result = post(URL_TEMPLATE_GET, params);
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
|
||||
if (jsonObject.containsKey("code")
|
||||
&& !(jsonObject.getInteger("code") == SUCCESS_CODE)) {
|
||||
throw new ServiceException(AdminErrorCodeEnum.SMS_PLATFORM_FAIL.getCode(),
|
||||
jsonObject.getString("detail"));
|
||||
}
|
||||
|
||||
String checkStatus = jsonObject.getString("check_status");
|
||||
Integer applyStatus = smsStatusMapping(checkStatus);
|
||||
String reason = jsonObject.getString("reason");
|
||||
return new Result().setId(tipId).setApplyStatus(applyStatus).setApplyMessage(reason);
|
||||
return new Result().setId(tplId).setApplyStatus(applyStatus).setApplyMessage(reason);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result updateTemplate(String tipId, String template, Integer tplType) {
|
||||
public Result updateTemplate(String tplId, String template, Integer tplType) {
|
||||
Map<String, String> params = new LinkedHashMap<>();
|
||||
params.put("apikey", apiKey);
|
||||
params.put("tipId", tipId);
|
||||
params.put("template", template);
|
||||
params.put("tpl_id", tplId);
|
||||
params.put("tpl_content", template);
|
||||
String result = post(URL_TEMPLATE_UPDATE, params);
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
|
||||
String checkStatus = jsonObject.getString("check_status");
|
||||
if (jsonObject.containsKey("code")
|
||||
&& !(jsonObject.getInteger("code") == SUCCESS_CODE)) {
|
||||
throw new ServiceException(AdminErrorCodeEnum.SMS_PLATFORM_FAIL.getCode(),
|
||||
jsonObject.getString("detail"));
|
||||
}
|
||||
|
||||
JSONObject templateJSONObject = (JSONObject) jsonObject.get("template");
|
||||
String checkStatus = templateJSONObject.getString("check_status");
|
||||
Integer applyStatus = smsStatusMapping(checkStatus);
|
||||
String reason = jsonObject.getString("reason");
|
||||
return new Result().setId(tipId).setApplyStatus(applyStatus).setApplyMessage(reason);
|
||||
return new Result().setId(tplId).setApplyStatus(applyStatus).setApplyMessage(reason);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result deleteTemplate(String tplId) {
|
||||
|
||||
// 如果不存在/已删除,直接返回
|
||||
try {
|
||||
getTemplate(tplId);
|
||||
} catch (ServiceException e) {
|
||||
// skip
|
||||
return new Result().setId(tplId).setApplyStatus(null).setApplyMessage(null);
|
||||
}
|
||||
|
||||
Map<String, String> params = new LinkedHashMap<>();
|
||||
params.put("apikey", apiKey);
|
||||
params.put("tpl_id", tplId);
|
||||
String result = post(URL_TEMPLATE_DELETE, params);
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
|
||||
if (jsonObject.containsKey("code")
|
||||
&& !(jsonObject.getInteger("code") == SUCCESS_CODE)) {
|
||||
throw new ServiceException(AdminErrorCodeEnum.SMS_PLATFORM_FAIL.getCode(),
|
||||
jsonObject.getString("detail"));
|
||||
}
|
||||
|
||||
return new Result().setId(tplId).setApplyStatus(null).setApplyMessage(null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,7 +1,9 @@
|
||||
package cn.iocoder.mall.admin.service;
|
||||
|
||||
import cn.iocoder.mall.admin.SystemApplicationTest;
|
||||
import cn.iocoder.mall.admin.service.SmsServiceImpl;
|
||||
import cn.iocoder.mall.admin.api.bo.sms.SmsSignBO;
|
||||
import cn.iocoder.mall.admin.api.bo.sms.SmsTemplateBO;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -23,7 +25,45 @@ public class SmsServiceImplTest {
|
||||
|
||||
@Test
|
||||
public void createSignTest() {
|
||||
// smsService.createSign("测试签名1");
|
||||
smsService.getSign("测试签名1");
|
||||
smsService.createSign("测试签名1");
|
||||
// smsService.createSign("悦跑会");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSignTest() {
|
||||
SmsSignBO smsSignBO = smsService.getSign("悦跑会");
|
||||
Assert.assertNotNull(smsSignBO);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateSignTest() {
|
||||
smsService.updateSign("测试签名2", "测试签名3");
|
||||
SmsSignBO newSmsSignBO = smsService.getSign("测试签名3");
|
||||
Assert.assertNotNull(newSmsSignBO);
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// template
|
||||
|
||||
@Test
|
||||
public void createTemplateTest() {
|
||||
smsService.createTemplate(1, "打死也不要告诉别人哦002 #code# ", 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTemplateTest() {
|
||||
SmsTemplateBO smsTemplateBO = smsService.getTemplate(3);
|
||||
Assert.assertNotNull(smsTemplateBO);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateTemplateTest() {
|
||||
smsService.updateTemplate(3, "打死也不要告诉别人哦444 #code# ", 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteTemplateTest() {
|
||||
smsService.deleteTemplate(3);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user