From 551f6aa6730fcb61fe54e808814442b95542aa23 Mon Sep 17 00:00:00 2001 From: sin <2943460818@qq.com> Date: Sat, 18 May 2019 10:34:25 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E7=9F=AD=E4=BF=A1=E6=A8=A1=E6=9D=BF=20?= =?UTF-8?q?=E3=80=81=20=E5=92=8C=E7=AD=BE=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iocoder/mall/admin/api/SmsPlatform.java | 26 +++-- .../cn/iocoder/mall/admin/api/SmsService.java | 15 ++- .../api/constant/AdminErrorCodeEnum.java | 9 +- .../mall/admin/dataobject/SmsSignDO.java | 2 + .../mall/admin/dataobject/SmsTemplateDO.java | 2 + .../mall/admin/service/SmsServiceImpl.java | 42 +++++++- .../admin/service/SmsYunPianPlatform.java | 102 ++++++++++++++---- .../admin/service/SmsServiceImplTest.java | 46 +++++++- 8 files changed, 195 insertions(+), 49 deletions(-) diff --git a/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/SmsPlatform.java b/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/SmsPlatform.java index 597db3122..c2f8f1178 100644 --- a/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/SmsPlatform.java +++ b/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/SmsPlatform.java @@ -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); } diff --git a/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/SmsService.java b/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/SmsService.java index a3d36139b..ee5079358 100644 --- a/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/SmsService.java +++ b/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/SmsService.java @@ -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); } diff --git a/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/constant/AdminErrorCodeEnum.java b/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/constant/AdminErrorCodeEnum.java index 1626a6668..0658ec125 100644 --- a/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/constant/AdminErrorCodeEnum.java +++ b/system/system-service-api/src/main/java/cn/iocoder/mall/admin/api/constant/AdminErrorCodeEnum.java @@ -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; diff --git a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/dataobject/SmsSignDO.java b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/dataobject/SmsSignDO.java index f25afcd1f..c4438cb05 100644 --- a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/dataobject/SmsSignDO.java +++ b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/dataobject/SmsSignDO.java @@ -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 { /** diff --git a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/dataobject/SmsTemplateDO.java b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/dataobject/SmsTemplateDO.java index 00e7f18f1..b38a5dc6d 100644 --- a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/dataobject/SmsTemplateDO.java +++ b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/dataobject/SmsTemplateDO.java @@ -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 { /** diff --git a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/service/SmsServiceImpl.java b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/service/SmsServiceImpl.java index 836ee9eb9..296aa0777 100644 --- a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/service/SmsServiceImpl.java +++ b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/service/SmsServiceImpl.java @@ -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().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().eq("id", id)); @@ -158,15 +164,43 @@ public class SmsServiceImpl implements SmsService { AdminErrorCodeEnum.SMS_TEMPLATE_NOT_EXISTENT.getMessage()); } + SmsSignDO smsSignDO = smsSignMapper.selectOne( + new QueryWrapper().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().eq("id", id) ); } + + @Override + @Transactional + public void deleteTemplate(Integer id) { + SmsTemplateDO smsTemplateDO = smsTemplateMapper.selectOne( + new QueryWrapper().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() + .set("deleted", DeletedStatusEnum.DELETED_YES).eq("id", id)); + } } diff --git a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/service/SmsYunPianPlatform.java b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/service/SmsYunPianPlatform.java index 8d852a9d3..f23af2739 100644 --- a/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/service/SmsYunPianPlatform.java +++ b/system/system-service-impl/src/main/java/cn/iocoder/mall/admin/service/SmsYunPianPlatform.java @@ -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 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 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 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 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 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); } /** diff --git a/system/system-service-impl/src/test/java/cn/iocoder/mall/admin/service/SmsServiceImplTest.java b/system/system-service-impl/src/test/java/cn/iocoder/mall/admin/service/SmsServiceImplTest.java index 07c1e6cc6..64e77e8ef 100644 --- a/system/system-service-impl/src/test/java/cn/iocoder/mall/admin/service/SmsServiceImplTest.java +++ b/system/system-service-impl/src/test/java/cn/iocoder/mall/admin/service/SmsServiceImplTest.java @@ -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); } }