From 0cb60a824715c151f0d3f1bb772958fba728bbe5 Mon Sep 17 00:00:00 2001 From: qzx0712 <1500418656@qq.com> Date: Tue, 7 May 2024 17:28:04 +0800 Subject: [PATCH] msm --- .../controller/VerifyController.java | 94 +++++++++---------- .../qzx/xdupartner/service/MsmService.java | 2 +- .../service/impl/MsmServiceImpl.java | 8 +- .../service/impl/PhoneServiceImpl.java | 12 ++- .../qzx/xdupartner/util/MsmConstantUtils.java | 43 ++++----- .../com/qzx/xdupartner/util/VerifyUtil.java | 5 + 6 files changed, 88 insertions(+), 76 deletions(-) diff --git a/src/main/java/com/qzx/xdupartner/controller/VerifyController.java b/src/main/java/com/qzx/xdupartner/controller/VerifyController.java index 55f1adc..1129213 100644 --- a/src/main/java/com/qzx/xdupartner/controller/VerifyController.java +++ b/src/main/java/com/qzx/xdupartner/controller/VerifyController.java @@ -43,49 +43,6 @@ public class VerifyController { @Resource PhoneService phoneService; - @Deprecated - @ApiOperation("") - @GetMapping("/sendCode") - public R sendCode(@RequestParam("stuId") String stuId) { - if (!(StrUtil.isNumeric(stuId) && stuId.length() == 11)) { - return new R<>(ResultCode.STU_ID_ERROR); - } - String verCode = VerifyUtil.getVerCode(); - boolean isSend = mailService.sendVerifyEmail(stuId, verCode); - if (isSend) { - //发送验证码成功 - stringRedisTemplate.opsForValue().set(RedisConstant.MAIL_CODE_PREFIX + stuId, verCode, 10, - TimeUnit.MINUTES); - return RUtil.success("发送成功"); - } - return new R<>(ResultCode.MAIL_SEND_ERROR); - } - - @Deprecated - @ApiOperation("") - @GetMapping("/testLogin") - public R testLogin(@RequestParam("stuId") String stuId) { - String sessionKey1 = "12345678"; - String sessionKey2 = "123456789"; - if (stuId.equals(sessionKey1)) { - User user = userService.lambdaQuery().eq(User::getId, 35).one(); - return getStringR(sessionKey1, user); - } else if (stuId.equals(sessionKey2)) { - User user = userService.lambdaQuery().eq(User::getId, 14).one(); - return getStringR(sessionKey2, user); - } - return new R<>(ResultCode.FAILED, ""); - } - - private R getStringR(String sessionKey, User user) { - if (ObjectUtil.isNull(user)) { - return null; - } - user.setSessionKey(sessionKey); - stringRedisTemplate.opsForValue().set(RedisConstant.LOGIN_PREFIX + sessionKey, JSONUtil.toJsonStr(user), - RedisConstant.LOGIN_VALID_TTL, TimeUnit.DAYS); - return RUtil.success(sessionKey); - } @ApiOperation("") @GetMapping("/email/send") @@ -126,9 +83,9 @@ public R verifyPhoneVerCode(@RequestBody PhoneAuthDto phoneAuthDto) { @ApiOperation("") @PostMapping("/manual") public R manualVerify(@Validated @RequestBody ManualVerifyDto manualVerifyDto) { -// if (userService.checkUserIsVerified(UserHolder.getUserId())) { -// return RUtil.error(ResultCode.HAS_VERIFIED_ERROR); -// } + if (userService.checkUserIsVerified(UserHolder.getUserId())) { + return RUtil.error(ResultCode.HAS_VERIFIED_ERROR); + } return mailService.sendToAuditor(manualVerifyDto) ? RUtil.success("提交成功,请耐心等待") : RUtil.error(ResultCode.MAIL_SEND_ERROR); } @@ -156,4 +113,47 @@ private ManualVerifyDto explainToken(String token) { return JSONUtil.toBean(jsonStr, ManualVerifyDto.class); } -} + @Deprecated + @ApiOperation("") + @GetMapping("/sendCode") + public R sendCode(@RequestParam("stuId") String stuId) { + if (!(StrUtil.isNumeric(stuId) && stuId.length() == 11)) { + return new R<>(ResultCode.STU_ID_ERROR); + } + String verCode = VerifyUtil.getVerCode(); + boolean isSend = mailService.sendVerifyEmail(stuId, verCode); + if (isSend) { + //发送验证码成功 + stringRedisTemplate.opsForValue().set(RedisConstant.MAIL_CODE_PREFIX + stuId, verCode, 10, + TimeUnit.MINUTES); + return RUtil.success("发送成功"); + } + return new R<>(ResultCode.MAIL_SEND_ERROR); + } + + @Deprecated + @ApiOperation("") + @GetMapping("/testLogin") + public R testLogin(@RequestParam("stuId") String stuId) { + String sessionKey1 = "12345678"; + String sessionKey2 = "123456789"; + if (stuId.equals(sessionKey1)) { + User user = userService.lambdaQuery().eq(User::getId, 35).one(); + return getStringR(sessionKey1, user); + } else if (stuId.equals(sessionKey2)) { + User user = userService.lambdaQuery().eq(User::getId, 14).one(); + return getStringR(sessionKey2, user); + } + return new R<>(ResultCode.FAILED, ""); + } + + private R getStringR(String sessionKey, User user) { + if (ObjectUtil.isNull(user)) { + return null; + } + user.setSessionKey(sessionKey); + stringRedisTemplate.opsForValue().set(RedisConstant.LOGIN_PREFIX + sessionKey, JSONUtil.toJsonStr(user), + RedisConstant.LOGIN_VALID_TTL, TimeUnit.DAYS); + return RUtil.success(sessionKey); + } +} \ No newline at end of file diff --git a/src/main/java/com/qzx/xdupartner/service/MsmService.java b/src/main/java/com/qzx/xdupartner/service/MsmService.java index 973d1e8..47c2008 100644 --- a/src/main/java/com/qzx/xdupartner/service/MsmService.java +++ b/src/main/java/com/qzx/xdupartner/service/MsmService.java @@ -1,5 +1,5 @@ package com.qzx.xdupartner.service; public interface MsmService { - boolean send(String phone); + boolean send(String phone, String verificationCode); } diff --git a/src/main/java/com/qzx/xdupartner/service/impl/MsmServiceImpl.java b/src/main/java/com/qzx/xdupartner/service/impl/MsmServiceImpl.java index ff1216c..bd81b5e 100644 --- a/src/main/java/com/qzx/xdupartner/service/impl/MsmServiceImpl.java +++ b/src/main/java/com/qzx/xdupartner/service/impl/MsmServiceImpl.java @@ -2,7 +2,6 @@ import com.qzx.xdupartner.service.MsmService; import com.qzx.xdupartner.util.MsmConstantUtils; -import com.qzx.xdupartner.util.VerifyUtil; import com.tencentcloudapi.common.Credential; import com.tencentcloudapi.common.profile.ClientProfile; import com.tencentcloudapi.common.profile.HttpProfile; @@ -17,7 +16,7 @@ @Slf4j public class MsmServiceImpl implements MsmService { @Override - public boolean send(String phone) { + public boolean send(String phone, String verificationCode) { try { //这里是实例化一个Credential,也就是认证对象,参数是密钥对;你要使用肯定要进行认证 Credential credential = new Credential(MsmConstantUtils.SECRET_ID, MsmConstantUtils.SECRET_KEY); @@ -26,7 +25,7 @@ public boolean send(String phone) { //最简单的就是实例化该对象即可,它的构造方法已经帮我们设置了一些默认的值 HttpProfile httpProfile = new HttpProfile(); //这个setEndpoint可以省略的 - httpProfile.setEndpoint(MsmConstantUtils.END_POINT); +// httpProfile.setEndpoint(MsmConstantUtils.END_POINT); //实例化一个客户端配置对象,这个配置可以进行签名(使用私钥进行加密的过程),对方可以利用公钥进行解密 ClientProfile clientProfile = new ClientProfile(); @@ -43,8 +42,7 @@ public boolean send(String phone) { request.setSign(MsmConstantUtils.SIGN_NAME); request.setTemplateID(MsmConstantUtils.TEMPLATE_ID); //生成随机验证码,我的模板内容的参数只有一个 - String verificationCode = VerifyUtil.getVerCode(); - String[] templateParamSet = {verificationCode}; + String[] templateParamSet = {verificationCode, "5"}; request.setTemplateParamSet(templateParamSet); //发送短信 diff --git a/src/main/java/com/qzx/xdupartner/service/impl/PhoneServiceImpl.java b/src/main/java/com/qzx/xdupartner/service/impl/PhoneServiceImpl.java index 6f3f295..36bb36b 100644 --- a/src/main/java/com/qzx/xdupartner/service/impl/PhoneServiceImpl.java +++ b/src/main/java/com/qzx/xdupartner/service/impl/PhoneServiceImpl.java @@ -9,22 +9,27 @@ import com.qzx.xdupartner.entity.vo.LoginVo; import com.qzx.xdupartner.entity.vo.ResultCode; import com.qzx.xdupartner.exception.APIException; +import com.qzx.xdupartner.service.MsmService; import com.qzx.xdupartner.service.PhoneService; import com.qzx.xdupartner.service.UserService; import com.qzx.xdupartner.util.UserUtil; import com.qzx.xdupartner.util.VerifyUtil; +import lombok.extern.slf4j.Slf4j; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.concurrent.TimeUnit; +@Slf4j @Service public class PhoneServiceImpl implements PhoneService { @Resource StringRedisTemplate stringRedisTemplate; @Resource UserService userService; + @Resource + MsmService msmService; @Override public boolean checkSent(String phone) { @@ -35,8 +40,11 @@ public boolean checkSent(String phone) { @Override public boolean sendVerCode(String phone) { String verCode = VerifyUtil.getVerCode(); - //todo 发送短信 - + boolean sent = msmService.send(phone, verCode); + log.info("send tencent phone msg: phone:[{}] vcode:[{}]", phone, verCode); + if (!sent) { + throw new APIException(ResultCode.MESSAGE_SEND_ERROR); + } stringRedisTemplate.opsForValue().set(RedisConstant.PHONE_LOGIN_PREFIX + phone, verCode, 5, TimeUnit.MINUTES); return true; } diff --git a/src/main/java/com/qzx/xdupartner/util/MsmConstantUtils.java b/src/main/java/com/qzx/xdupartner/util/MsmConstantUtils.java index d2fc947..7c15287 100644 --- a/src/main/java/com/qzx/xdupartner/util/MsmConstantUtils.java +++ b/src/main/java/com/qzx/xdupartner/util/MsmConstantUtils.java @@ -1,6 +1,7 @@ package com.qzx.xdupartner.util; import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @@ -8,39 +9,39 @@ @Component public class MsmConstantUtils implements InitializingBean { - // @Value("${tencent.msm.id}") -// private String secretID ; -// -// @Value("${tencent.msm.secret}") -// private String secretKey ; -// + @Value("${tencent.msm.id}") + private String secretID; + + @Value("${tencent.msm.secret}") + private String secretKey; + // @Value("${tencent.msm.endPoint}") // private String endPoint; -// -// @Value("${tencent.msm.appId}") -// private String appId; -// -// @Value("${tencent.msm.signName}") -// private String signName; -// -// @Value("${tencent.msm.templateId}") -// private String templateId; + + @Value("${tencent.msm.appId}") + private String appId; + + @Value("${tencent.msm.signName}") + private String signName; + + @Value("${tencent.msm.templateId}") + private String templateId; //六个相关的参数 public static String SECRET_ID; public static String SECRET_KEY; - public static String END_POINT; + // public static String END_POINT; public static String APP_ID; public static String SIGN_NAME; public static String TEMPLATE_ID; @Override public void afterPropertiesSet() throws Exception { -// SECRET_ID = secretID; -// SECRET_KEY = secretKey; + SECRET_ID = secretID; + SECRET_KEY = secretKey; // END_POINT = endPoint; -// APP_ID = appId; -// SIGN_NAME = signName; -// TEMPLATE_ID = templateId; + APP_ID = appId; + SIGN_NAME = signName; + TEMPLATE_ID = templateId; } } diff --git a/src/main/java/com/qzx/xdupartner/util/VerifyUtil.java b/src/main/java/com/qzx/xdupartner/util/VerifyUtil.java index de445b0..713b2f6 100644 --- a/src/main/java/com/qzx/xdupartner/util/VerifyUtil.java +++ b/src/main/java/com/qzx/xdupartner/util/VerifyUtil.java @@ -1,5 +1,6 @@ package com.qzx.xdupartner.util; +import cn.hutool.core.util.PhoneUtil; import cn.hutool.core.util.StrUtil; import java.security.SecureRandom; @@ -34,5 +35,9 @@ public static String getVerCode() { public static boolean regexVerCode(String string) { return StrUtil.isNumeric(string) && string.length() == verCodeLength; } + + public static void main(String[] args) { + System.out.println(PhoneUtil.isPhone("18622189436")); + } }