Browse Source

验证码

master
wangpengfei 2 years ago
parent
commit
5314cdbc74
  1. 74
      yxt_supervise/supervise-system/supervise-system-biz/src/main/java/com/yxt/supervise/system/sysuser/wx/WxSysUserRest.java
  2. 53
      yxt_supervise/supervise-system/supervise-system-biz/src/main/java/com/yxt/supervise/utils/MsgWs.java

74
yxt_supervise/supervise-system/supervise-system-biz/src/main/java/com/yxt/supervise/system/sysuser/wx/WxSysUserRest.java

@ -1,6 +1,8 @@
package com.yxt.supervise.system.sysuser.wx;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.thread.ThreadUtil;
import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.yxt.supervise.system.config.DictCommonType;
@ -15,8 +17,12 @@ import com.yxt.common.core.result.ResultBean;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
@ -40,6 +46,7 @@ import java.util.*;
@RequestMapping("v1/wxuser")
@Api(tags = "用户表-小程序端")
public class WxSysUserRest implements WxSysUserFeign {
private static final String WX_URL_LOGIN = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code ";
@Autowired
private SysUserService sysUserService;
@ -361,4 +368,71 @@ public class WxSysUserRest implements WxSysUserFeign {
return sb.toString();
}
/**
* 验证码长度
*/
static final int LENGTH_OF_CODE = 4;
/**
* 发送手机端登录验证码
*
* @param mobile
* @param type 1登录2修改密码3找回密码
* @return
*/
public ResultBean sendVerificationCodeForApp(String mobile, String type) {
Date date = new Date();
String redisKey = "";
if (type.equals("1")) {
redisKey = "loginCode";
} else if (type.equals("2")) {
redisKey = "updatePwdCode";
} else if (type.equals("3")) {
redisKey = "resetPwdCode";
}
String codeRedis = redisUtil.get(redisKey + mobile);
if (StringUtils.isNotEmpty(codeRedis)) {
//查看请求间隔,默认是一分钟,小于一分钟继续等待,超过一分钟发送短信
String sendTime = codeRedis.substring(4);
long diffSecond = (date.getTime() - Long.parseLong(sendTime)) / 1000;
if (diffSecond < 60) {
return ResultBean.fireFail().setMsg("请等待一分钟后再次重试!");
}
}
String verificationCode = "";
for (int i = 0; i < LENGTH_OF_CODE; i++) {
// 定义随机类
Random random = new Random();
// 返回[0,10)集合中的整数,注意不包括10
int result = random.nextInt(10);
// +1后,[0,10)集合变为[1,11)集合,满足要求
verificationCode = verificationCode + result;
}
String content = "";
if (type.equals("1")) {
content = "登录验证码:" + verificationCode + ",用于登录App,有效期5分钟,如非本人操作,请忽略。";
} else {
content = "修改密码验证码:" + verificationCode + ",用于修改登录密码,有效期5分钟,如非本人操作,请忽略。";
}
String res = MsgWs.SendWaitWorkMsg(mobile, content);
long i=1000;
if (res.equals("1")) {
redisUtil.set(redisKey + mobile, verificationCode + date.getTime(), i);
}
return ResultBean.fireSuccess().setMsg("发送短信验证码成功");
}
@PostMapping("/wxLogin/{appid}/{secret}/{jsCode}/{grantType}")
public ResultBean wxLogin(@PathVariable("appid") String appid,
@PathVariable("secret")String secret,
@PathVariable("jsCode")String jsCode,
@PathVariable("grantType")String grantType) throws Exception {
ResultBean rb=new ResultBean();
String url = WX_URL_LOGIN.replace("APPID", appid).replace("SECRET", secret).replace("JSCODE",jsCode).replace("authorization_code",grantType);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> forEntity = restTemplate.getForEntity(url,String.class);
JSONObject jsonObject = JSONObject.parseObject(forEntity.getBody());
System.out.println(jsonObject);
return rb.success().setData(jsonObject);
}
}

53
yxt_supervise/supervise-system/supervise-system-biz/src/main/java/com/yxt/supervise/utils/MsgWs.java

@ -0,0 +1,53 @@
package com.yxt.supervise.utils;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import javax.xml.namespace.QName;
/**
* @author dimengzhe
* @date 2020/9/11 8:59
* @description 发送短信调用接口
*/
public class MsgWs {
public static String SendWaitWorkMsg(String mobile, String msg) {
try {
String urlname = "http://sdk1.mb345.com/ws/LinkWS.asmx";
String soapActionURI = "http://tempuri.org/BatchSend";
Service s = new Service();
Call call = (Call) s.createCall();
call.setTimeout(new Integer(5000));
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapActionURI);
// wsdl中接口名称
call.setOperationName(new QName("http://tempuri.org/", "BatchSend"));
call.setTargetEndpointAddress(urlname);
call.addParameter(new QName("http://tempuri.org/", "CorpID"), XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName("http://tempuri.org/", "Pwd"), XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName("http://tempuri.org/", "Mobile"), XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName("http://tempuri.org/", "Content"), XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName("http://tempuri.org/", "Cell"), XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName("http://tempuri.org/", "SendTime"), XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
// String[] fn01 = {"YXT010045", "yuxintonghygl", mobile, msgtitle+msg+msgSign, "", ""};
// String[] fn01 = {"YXT011852", "yxt_ar230314", mobile, msg+ SmsFeign.msgSign, "", ""};
String[] fn01 = {"YXT011852", "yxt_ar230314", mobile, msg, "", ""};
String val = (String) call.invoke(fn01);
// String val = "-1";//用于先去掉短信功能,不发短信
return val;
} catch (Exception e) {
return e.getMessage();
}
}
}
Loading…
Cancel
Save