微信登录完成+阿里大鱼sms短信验证码发送

This commit is contained in:
clay 2021-08-08 23:10:16 +08:00
parent 1fcc7988f5
commit 58b7a74c6f
7 changed files with 217 additions and 45 deletions

View File

@ -0,0 +1,93 @@
package com.ebts.web.controller.sms;
import com.ebts.common.constant.Constants;
import com.ebts.common.core.entity.AjaxResult;
import com.ebts.common.core.redis.RedisCache;
import com.ebts.framework.config.SMSConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import java.util.regex.Pattern;
/**
* @author: Clay
* @date: 2021/8/8 22:51
* @descriptionSmsController
* @email: clay@hchyun.com
*/
@Api(value = "小程序获取短信", tags = "小程序获取短信")
@RestController
@RequestMapping("/applet/sms")
public class SmsController {
private final Logger logger = LoggerFactory.getLogger(SmsController.class);
@Autowired
private RedisCache redisCache;
@ApiOperation("获取设备信息")
@GetMapping("/{phone}")
public AjaxResult getSms(@PathVariable("phone") String phone) {
try {
if (null==phone||"".equals(phone)){
return AjaxResult.error("电话号码不能为空!");
}
String regard = "^1[0-9]{10}$";
if (!Pattern.matches(regard, phone)){
return AjaxResult.error("手机号输入有误,请重新输入");
}
String code = redisCache.getCacheObject(Constants.PHONE_CODE_KEY+phone);
if (code!=null){
return AjaxResult.info("重复获取验证码");
}
code = SMSConfig.code();
// todo 设置验证码时效性
// redisCache.setCacheObject(Constants.PHONE_CODE_KEY+phone,code,10, TimeUnit.MINUTES);
redisCache.setCacheObject(Constants.PHONE_CODE_KEY+phone,code);
boolean state = SMSConfig.smsCode(phone,code);
if (state){
return AjaxResult.success("验证码发送成功");
}else {
return AjaxResult.error("获取失败,请重新获取");
}
} catch (RuntimeException e) {
logger.error(e.getMessage());
return AjaxResult.error();
}
}
@ApiOperation("获取设备信息")
@PostMapping("/val")
public AjaxResult smsVal(@RequestBody Map<String,String> params){
try {
if (null == params.get("phone")|| "".equals(params.get("phone"))){
return AjaxResult.error("手机号码为空");
}
if (null == params.get("code")|| "".equals(params.get("code"))){
return AjaxResult.error("手机验证码为空");
}
String code = redisCache.getCacheObject(Constants.PHONE_CODE_KEY+params.get("phone"));
if (code==null){
return AjaxResult.info("验证码已过期,请重新获取");
}
if (code.equals(params.get("code"))){
redisCache.deleteObject(Constants.PHONE_CODE_KEY+params.get("phone"));
return AjaxResult.success("验证成功!");
}else {
return AjaxResult.error("验证码错误!");
}
}catch (RuntimeException e){
logger.error(e.getMessage());
return AjaxResult.error();
}
}
}

View File

@ -69,12 +69,6 @@ public class WeChatController {
case "01": case "01":
loginUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + WeChatConfig.getUserAppid() + "&secret=" + WeChatConfig.getUserSecret() + "&js_code=" + code + "&grant_type=" + WeChatConfig.getGrant_type(); loginUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + WeChatConfig.getUserAppid() + "&secret=" + WeChatConfig.getUserSecret() + "&js_code=" + code + "&grant_type=" + WeChatConfig.getGrant_type();
break; break;
case "02":
loginUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + WeChatConfig.getFunAppid() + "&secret=" + WeChatConfig.getFunSecret() + "&js_code=" + code + "&grant_type=" + WeChatConfig.getGrant_type();
break;
case "03":
loginUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + WeChatConfig.getEleAppid() + "&secret=" + WeChatConfig.getEleSecret() + "&js_code=" + code + "&grant_type=" + WeChatConfig.getGrant_type();
break;
default: default:
break; break;
} }

View File

@ -65,6 +65,10 @@ public class Constants {
* 防重提交 redis key * 防重提交 redis key
*/ */
public static final String REPEAT_SUBMIT_KEY = "repeat_submit:"; public static final String REPEAT_SUBMIT_KEY = "repeat_submit:";
/**
* 手机验证码
*/
public static final String PHONE_CODE_KEY = "phone_codes:";
/** /**
* 验证码有效期分钟 * 验证码有效期分钟

View File

@ -46,6 +46,12 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<!--阿里大鱼手机验证码-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
<version>2.0.4</version>
</dependency>
<!-- 获取系统信息 --> <!-- 获取系统信息 -->
<dependency> <dependency>

View File

@ -0,0 +1,112 @@
package com.ebts.framework.config;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.models.*;
import java.util.Random;
/**
* @author: Clay
* @date: 2021/8/8 22:54
* @descriptionSMSConfig
* @email: clay@hchyun.com
*/
public class SMSConfig {
private static String accessKeyId;
private static String accessKeySecret;
private static String signName;
public void setAccessKeyId(String accessKeyId) {
SMSConfig.accessKeyId = accessKeyId;
}
public void setAccessKeySecret(String accessKeySecret) {
SMSConfig.accessKeySecret = accessKeySecret;
}
public void setSignName(String signName) {
SMSConfig.signName = signName;
}
public static Object string(){
System.out.println(accessKeyId+accessKeySecret+signName);
return accessKeyId+accessKeySecret+signName;
}
/**
* 使用AK&SK初始化账号Client
*
* @return Client
* @throws Exception
*/
private static Client createClient() throws Exception {
Config config = new Config()
// 您的AccessKey ID
.setAccessKeyId(accessKeyId)
// 您的AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// 访问的域名
config.endpoint = "dysmsapi.aliyuncs.com";
return new Client(config);
}
public static String code() {
Random rand = new Random();
return String.valueOf(rand.nextInt(1000000) + 1);
}
/**
* 验证码
* @param phone
* @param code
* @return
*/
public static boolean smsCode(String phone, String code) {
Client client = null;
try {
client = SMSConfig.createClient();
SendSmsRequest sendSmsRequest = new SendSmsRequest()
.setPhoneNumbers(phone)
.setSignName(signName)
.setTemplateCode("SMS_176925470")
.setTemplateParam("{\"code\":\"" + code + "\"}");
// 复制代码运行请自行打印 API 的返回值
client.sendSms(sendSmsRequest);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 报警
* @param phone
* @param code
* @return
*/
public static boolean alarm(String phone, String code) {
Client client = null;
try {
client = SMSConfig.createClient();
SendSmsRequest sendSmsRequest = new SendSmsRequest()
.setPhoneNumbers(phone)
.setSignName(signName)
.setTemplateCode("SMS_176925470")
.setTemplateParam("{\"code\":\"" + code + "\"}");
// 复制代码运行请自行打印 API 的返回值
client.sendSms(sendSmsRequest);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}

View File

@ -14,12 +14,8 @@ import org.springframework.stereotype.Component;
public class WeChatConfig { public class WeChatConfig {
private static String userAppid; private static String userAppid;
private static String funAppid;
private static String eleAppid;
private static String userSecret; private static String userSecret;
private static String funSecret;
private static String eleSecret;
private static String grant_type; private static String grant_type;
@ -31,22 +27,6 @@ public class WeChatConfig {
WeChatConfig.userAppid = userAppid; WeChatConfig.userAppid = userAppid;
} }
public static String getFunAppid() {
return funAppid;
}
public void setFunAppid(String funAppid) {
WeChatConfig.funAppid = funAppid;
}
public static String getEleAppid() {
return eleAppid;
}
public void setEleAppid(String eleAppid) {
WeChatConfig.eleAppid = eleAppid;
}
public static String getUserSecret() { public static String getUserSecret() {
return userSecret; return userSecret;
} }
@ -55,22 +35,6 @@ public class WeChatConfig {
WeChatConfig.userSecret = userSecret; WeChatConfig.userSecret = userSecret;
} }
public static String getFunSecret() {
return funSecret;
}
public void setFunSecret(String funSecret) {
WeChatConfig.funSecret = funSecret;
}
public static String getEleSecret() {
return eleSecret;
}
public void setEleSecret(String eleSecret) {
WeChatConfig.eleSecret = eleSecret;
}
public static String getGrant_type() { public static String getGrant_type() {
return grant_type; return grant_type;
} }

View File

@ -26,7 +26,6 @@
<result property="userType" column="user_type" /> <result property="userType" column="user_type" />
<result property="openId" column="open_id" /> <result property="openId" column="open_id" />
<result property="unionId" column="union_id" /> <result property="unionId" column="union_id" />
<result property="city" column="city" />
<association property="dept" column="dept_id" javaType="Dept" resultMap="deptResult" /> <association property="dept" column="dept_id" javaType="Dept" resultMap="deptResult" />
<collection property="roles" javaType="java.util.List" resultMap="RoleResult" /> <collection property="roles" javaType="java.util.List" resultMap="RoleResult" />
</resultMap> </resultMap>
@ -50,7 +49,7 @@
</resultMap> </resultMap>
<sql id="selectUserVo"> <sql id="selectUserVo">
select u.user_id, u.dept_id, u.user_name, u.nick_name, u.email, u.avatar, u.phonenumber, u.password, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark, u.union_id,u.open_id,u.city,u.user_type, select u.user_id, u.dept_id, u.user_name, u.nick_name, u.email, u.avatar, u.phonenumber, u.password, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark, u.union_id,u.open_id,u.user_type,
d.dept_id, d.parent_id, d.dept_name, d.order_num, d.leader, d.status as dept_status, d.dept_id, d.parent_id, d.dept_name, d.order_num, d.leader, d.status as dept_status,
r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.status as role_status r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.status as role_status
from sys_user u from sys_user u