Skip to content

Commit

Permalink
msm
Browse files Browse the repository at this point in the history
  • Loading branch information
Qi-Zhen-xin committed May 7, 2024
1 parent 3d670d6 commit 0cb60a8
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 76 deletions.
94 changes: 47 additions & 47 deletions src/main/java/com/qzx/xdupartner/controller/VerifyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,49 +43,6 @@ public class VerifyController {
@Resource
PhoneService phoneService;

@Deprecated
@ApiOperation("")
@GetMapping("/sendCode")
public R<String> 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<String> 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<String> 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")
Expand Down Expand Up @@ -126,9 +83,9 @@ public R<LoginVo> verifyPhoneVerCode(@RequestBody PhoneAuthDto phoneAuthDto) {
@ApiOperation("")
@PostMapping("/manual")
public R<String> 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);
}
Expand Down Expand Up @@ -156,4 +113,47 @@ private ManualVerifyDto explainToken(String token) {
return JSONUtil.toBean(jsonStr, ManualVerifyDto.class);
}

}
@Deprecated
@ApiOperation("")
@GetMapping("/sendCode")
public R<String> 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<String> 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<String> 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);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/qzx/xdupartner/service/MsmService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.qzx.xdupartner.service;

public interface MsmService {
boolean send(String phone);
boolean send(String phone, String verificationCode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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);

//发送短信
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down
43 changes: 22 additions & 21 deletions src/main/java/com/qzx/xdupartner/util/MsmConstantUtils.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
package com.qzx.xdupartner.util;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


//实现了InitializingBean接口,当spring进行初始化bean时,会执行afterPropertiesSet方法
@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;
}
}

5 changes: 5 additions & 0 deletions src/main/java/com/qzx/xdupartner/util/VerifyUtil.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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"));
}
}

0 comments on commit 0cb60a8

Please sign in to comment.