短信验证

This commit is contained in:
huangdeliang 2021-06-09 19:12:48 +08:00
parent 02e5110d2c
commit e99ebfadb7
9 changed files with 346 additions and 193 deletions

View File

@ -3,12 +3,10 @@ package com.stdiet.web.controller.custom;
import com.stdiet.common.core.controller.BaseController;
import com.stdiet.common.core.domain.AjaxResult;
import com.stdiet.common.utils.DateUtils;
import com.stdiet.common.utils.StringUtils;
import com.stdiet.custom.domain.SysOrderPause;
import com.stdiet.custom.domain.SysWxAdLog;
import com.stdiet.custom.service.ISysOrderPauseService;
import com.stdiet.custom.service.ISysRecipesService;
import com.stdiet.custom.service.ISysWapServices;
import com.stdiet.custom.service.ISysWxSaleAccountService;
import com.stdiet.custom.service.*;
import com.stdiet.custom.utils.HttpRequestUtils;
import com.stdiet.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
@ -37,6 +35,9 @@ public class SysWapController extends BaseController {
@Autowired
ISysWxSaleAccountService iSysWxSaleAccountService;
@Autowired
ISysSmsConfirmServie iSysSmsConfirmServie;
/**
* 客户食谱详情
*
@ -139,4 +140,39 @@ public class SysWapController extends BaseController {
return toAjax(iSysWxSaleAccountService.logWxAd(sysWxAdLog));
}
@GetMapping(value = "/getCode")
public AjaxResult getCode(@RequestParam String phone) {
if (!StringUtils.isMobileNO(phone)) {
return AjaxResult.error(50001, "非法手机号");
}
int code = iSysSmsConfirmServie.sendSmsCode(phone);
if (code == 1) {
return AjaxResult.error(50001, "非法手机号");
} else if (code == 0) {
return AjaxResult.success();
} else {
return AjaxResult.error();
}
}
@GetMapping(value = "/checkCode")
public AjaxResult checkCode(@RequestParam String phone, @RequestParam String code) {
if (StringUtils.isEmpty(code)) {
return AjaxResult.error(50002, "验证码不能为空");
}
int checkCode = iSysSmsConfirmServie.checkSmsCode(phone, code);
if (checkCode == 0) {
return AjaxResult.success();
} else if (checkCode == 1) {
return AjaxResult.error(50003, "验证码失效");
} else if (checkCode == 2) {
return AjaxResult.error(50004, "验证码错误");
} else {
return AjaxResult.error();
}
}
}

View File

@ -1,24 +1,25 @@
package com.stdiet.common.utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.stdiet.common.core.text.StrFormatter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 字符串工具类
*
* @author ruoyi
*/
public class StringUtils extends org.apache.commons.lang3.StringUtils
{
/** 空字符串 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {
/**
* 空字符串
*/
private static final String NULLSTR = "";
/** 下划线 */
/**
* 下划线
*/
private static final char SEPARATOR = '_';
/**
@ -27,8 +28,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param value defaultValue 要判断的value
* @return value 返回值
*/
public static <T> T nvl(T value, T defaultValue)
{
public static <T> T nvl(T value, T defaultValue) {
return value != null ? value : defaultValue;
}
@ -38,8 +38,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param coll 要判断的Collection
* @return true为空 false非空
*/
public static boolean isEmpty(Collection<?> coll)
{
public static boolean isEmpty(Collection<?> coll) {
return isNull(coll) || coll.isEmpty();
}
@ -49,8 +48,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param coll 要判断的Collection
* @return true非空 false
*/
public static boolean isNotEmpty(Collection<?> coll)
{
public static boolean isNotEmpty(Collection<?> coll) {
return !isEmpty(coll);
}
@ -60,8 +58,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param objects 要判断的对象数组
* * @return true为空 false非空
*/
public static boolean isEmpty(Object[] objects)
{
public static boolean isEmpty(Object[] objects) {
return isNull(objects) || (objects.length == 0);
}
@ -71,8 +68,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param objects 要判断的对象数组
* @return true非空 false
*/
public static boolean isNotEmpty(Object[] objects)
{
public static boolean isNotEmpty(Object[] objects) {
return !isEmpty(objects);
}
@ -82,8 +78,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param map 要判断的Map
* @return true为空 false非空
*/
public static boolean isEmpty(Map<?, ?> map)
{
public static boolean isEmpty(Map<?, ?> map) {
return isNull(map) || map.isEmpty();
}
@ -93,8 +88,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param map 要判断的Map
* @return true非空 false
*/
public static boolean isNotEmpty(Map<?, ?> map)
{
public static boolean isNotEmpty(Map<?, ?> map) {
return !isEmpty(map);
}
@ -104,8 +98,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param str String
* @return true为空 false非空
*/
public static boolean isEmpty(String str)
{
public static boolean isEmpty(String str) {
return isNull(str) || NULLSTR.equals(str.trim());
}
@ -115,8 +108,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param str String
* @return true非空串 false空串
*/
public static boolean isNotEmpty(String str)
{
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
@ -126,8 +118,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param object Object
* @return true为空 false非空
*/
public static boolean isNull(Object object)
{
public static boolean isNull(Object object) {
return object == null;
}
@ -137,8 +128,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param object Object
* @return true非空 false
*/
public static boolean isNotNull(Object object)
{
public static boolean isNotNull(Object object) {
return !isNull(object);
}
@ -148,16 +138,14 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param object 对象
* @return true是数组 false不是数组
*/
public static boolean isArray(Object object)
{
public static boolean isArray(Object object) {
return isNotNull(object) && object.getClass().isArray();
}
/**
* 去空格
*/
public static String trim(String str)
{
public static String trim(String str) {
return (str == null ? "" : str.trim());
}
@ -168,24 +156,19 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param start 开始
* @return 结果
*/
public static String substring(final String str, int start)
{
if (str == null)
{
public static String substring(final String str, int start) {
if (str == null) {
return NULLSTR;
}
if (start < 0)
{
if (start < 0) {
start = str.length() + start;
}
if (start < 0)
{
if (start < 0) {
start = 0;
}
if (start > str.length())
{
if (start > str.length()) {
return NULLSTR;
}
@ -200,38 +183,30 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param end 结束
* @return 结果
*/
public static String substring(final String str, int start, int end)
{
if (str == null)
{
public static String substring(final String str, int start, int end) {
if (str == null) {
return NULLSTR;
}
if (end < 0)
{
if (end < 0) {
end = str.length() + end;
}
if (start < 0)
{
if (start < 0) {
start = str.length() + start;
}
if (end > str.length())
{
if (end > str.length()) {
end = str.length();
}
if (start > end)
{
if (start > end) {
return NULLSTR;
}
if (start < 0)
{
if (start < 0) {
start = 0;
}
if (end < 0)
{
if (end < 0) {
end = 0;
}
@ -251,10 +226,8 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param params 参数值
* @return 格式化后的文本
*/
public static String format(String template, Object... params)
{
if (isEmpty(params) || isEmpty(template))
{
public static String format(String template, Object... params) {
if (isEmpty(params) || isEmpty(template)) {
return template;
}
return StrFormatter.format(template, params);
@ -267,8 +240,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param sep 分隔符
* @return set集合
*/
public static final Set<String> str2Set(String str, String sep)
{
public static final Set<String> str2Set(String str, String sep) {
return new HashSet<String>(str2List(str, sep, true, false));
}
@ -281,28 +253,22 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param trim 去掉首尾空白
* @return list集合
*/
public static final List<String> str2List(String str, String sep, boolean filterBlank, boolean trim)
{
public static final List<String> str2List(String str, String sep, boolean filterBlank, boolean trim) {
List<String> list = new ArrayList<String>();
if (StringUtils.isEmpty(str))
{
if (StringUtils.isEmpty(str)) {
return list;
}
// 过滤空白字符串
if (filterBlank && StringUtils.isBlank(str))
{
if (filterBlank && StringUtils.isBlank(str)) {
return list;
}
String[] split = str.split(sep);
for (String string : split)
{
if (filterBlank && StringUtils.isBlank(string))
{
for (String string : split) {
if (filterBlank && StringUtils.isBlank(string)) {
continue;
}
if (trim)
{
if (trim) {
string = string.trim();
}
list.add(string);
@ -314,10 +280,8 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
/**
* 下划线转驼峰命名
*/
public static String toUnderScoreCase(String str)
{
if (str == null)
{
public static String toUnderScoreCase(String str) {
if (str == null) {
return null;
}
StringBuilder sb = new StringBuilder();
@ -327,31 +291,23 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
boolean curreCharIsUpperCase = true;
// 下一字符是否大写
boolean nexteCharIsUpperCase = true;
for (int i = 0; i < str.length(); i++)
{
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (i > 0)
{
if (i > 0) {
preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1));
}
else
{
} else {
preCharIsUpperCase = false;
}
curreCharIsUpperCase = Character.isUpperCase(c);
if (i < (str.length() - 1))
{
if (i < (str.length() - 1)) {
nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));
}
if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase)
{
if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase) {
sb.append(SEPARATOR);
}
else if ((i != 0 && !preCharIsUpperCase) && curreCharIsUpperCase)
{
} else if ((i != 0 && !preCharIsUpperCase) && curreCharIsUpperCase) {
sb.append(SEPARATOR);
}
sb.append(Character.toLowerCase(c));
@ -367,14 +323,10 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param strs 字符串组
* @return 包含返回true
*/
public static boolean inStringIgnoreCase(String str, String... strs)
{
if (str != null && strs != null)
{
for (String s : strs)
{
if (str.equalsIgnoreCase(trim(s)))
{
public static boolean inStringIgnoreCase(String str, String... strs) {
if (str != null && strs != null) {
for (String s : strs) {
if (str.equalsIgnoreCase(trim(s))) {
return true;
}
}
@ -388,27 +340,21 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
* @param name 转换前的下划线大写方式命名的字符串
* @return 转换后的驼峰式命名的字符串
*/
public static String convertToCamelCase(String name)
{
public static String convertToCamelCase(String name) {
StringBuilder result = new StringBuilder();
// 快速检查
if (name == null || name.isEmpty())
{
if (name == null || name.isEmpty()) {
// 没必要转换
return "";
}
else if (!name.contains("_"))
{
} else if (!name.contains("_")) {
// 不含下划线仅将首字母大写
return name.substring(0, 1).toUpperCase() + name.substring(1);
}
// 用下划线将原始字符串分割
String[] camels = name.split("_");
for (String camel : camels)
{
for (String camel : camels) {
// 跳过原始字符串中开头结尾的下换线或双重下划线
if (camel.isEmpty())
{
if (camel.isEmpty()) {
continue;
}
// 首字母大写
@ -421,30 +367,22 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
/**
* 驼峰式命名法 例如user_name->userName
*/
public static String toCamelCase(String s)
{
if (s == null)
{
public static String toCamelCase(String s) {
if (s == null) {
return null;
}
s = s.toLowerCase();
StringBuilder sb = new StringBuilder(s.length());
boolean upperCase = false;
for (int i = 0; i < s.length(); i++)
{
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == SEPARATOR)
{
if (c == SEPARATOR) {
upperCase = true;
}
else if (upperCase)
{
} else if (upperCase) {
sb.append(Character.toUpperCase(c));
upperCase = false;
}
else
{
} else {
sb.append(c);
}
}
@ -452,15 +390,21 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
}
@SuppressWarnings("unchecked")
public static <T> T cast(Object obj)
{
public static <T> T cast(Object obj) {
return (T) obj;
}
/**
* 手机号隐式处理
* */
*/
public static String hiddenPhoneNumber(String phoneNumber) {
return phoneNumber.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
}
public static boolean isMobileNO(String phone) {
String rule = "((\\+86|0086)?\\s*)((134[0-8]\\d{7})|(((13([0-3]|[5-9]))|(14[5-9])|15([0-3]|[5-9])|(16(2|[5-7]))|17([0-3]|[5-8])|18[0-9]|19(1|[8-9]))\\d{8})|(14(0|1|4)0\\d{7})|(1740([0-5]|[6-9]|[10-12])\\d{7}))";
Pattern p = Pattern.compile(rule);
Matcher m = p.matcher(phone);
return m.matches();
}
}

View File

@ -0,0 +1,13 @@
package com.stdiet.custom.service;
public interface ISysRedisService {
void set(String key, String value);
String get(String key);
Boolean expire(String key, long expire);
void remove(String key);
Long increment(String key, long delta);
}

View File

@ -0,0 +1,9 @@
package com.stdiet.custom.service;
public interface ISysSmsConfirmServie {
public Integer sendSmsCode(String phone);
public Integer checkSmsCode(String phone, String code);
}

View File

@ -0,0 +1,40 @@
package com.stdiet.custom.service.impl;
import com.stdiet.custom.service.ISysRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class SysRedisServiceImp implements ISysRedisService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Override
public void set(String key, String value) {
stringRedisTemplate.opsForValue().set(key, value);
}
@Override
public String get(String key) {
return stringRedisTemplate.opsForValue().get(key);
}
@Override
public Boolean expire(String key, long expire) {
return stringRedisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
@Override
public void remove(String key) {
stringRedisTemplate.delete(key);
}
@Override
public Long increment(String key, long delta) {
return stringRedisTemplate.opsForValue().increment(key, delta);
}
}

View File

@ -0,0 +1,87 @@
package com.stdiet.custom.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.stdiet.common.utils.DateUtils;
import com.stdiet.common.utils.StringUtils;
import com.stdiet.custom.domain.wechat.WxSubscribePostLog;
import com.stdiet.custom.service.ISysRedisService;
import com.stdiet.custom.service.ISysSmsConfirmServie;
import com.stdiet.custom.service.IWxSubscribePostLogService;
import com.stdiet.custom.utils.SmsUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SysSmsConfirmServiceImpl implements ISysSmsConfirmServie {
private static long CODE_EXPIRE_SECONDS = 600; //设置验证码过期时间为600秒
@Autowired
ISysRedisService redisService;
@Autowired
IWxSubscribePostLogService wxSubscribePostLogService;
@Override
public Integer sendSmsCode(String phone) {
int reCode = -1;
try {
String code = RandomStringUtils.randomNumeric(6);
JSONObject paramObj = new JSONObject();
paramObj.put("code", code);
SendSmsResponse response = SmsUtils.sendSms(phone, paramObj.toJSONString(), SmsUtils.SMS_217025172);
if (response.getCode().equals("OK")) {
// 发送成功
redisService.remove(phone);
redisService.set(phone, code);
redisService.expire(phone, CODE_EXPIRE_SECONDS);
reCode = 0;
} else if (response.getCode().equals("isv.MOBILE_NUMBER_ILLEGAL")) {
// 非法手机号
reCode = 1;
}
WxSubscribePostLog postLog = new WxSubscribePostLog();
postLog.setPhone(phone);
JSONObject resultObj = new JSONObject();
resultObj.put("requestId", response.getRequestId());
resultObj.put("bizId", response.getBizId());
resultObj.put("code", response.getCode());
resultObj.put("message", response.getMessage());
postLog.setResult(resultObj);
paramObj.put("phone", phone);
paramObj.put("tmpCode", SmsUtils.SMS_217025172);
paramObj.put("signName", SmsUtils.SMS_SIGN_NAME);
postLog.setData(paramObj);
postLog.setSendTime(DateUtils.getNowDate());
postLog.setType(2);
wxSubscribePostLogService.insertWxSubscribePostLog(postLog);
} catch (ClientException e) {
e.printStackTrace();
}
return reCode;
}
@Override
public Integer checkSmsCode(String phone, String code) {
String cachedCode = redisService.get(phone);
int resCode = -1;
if (StringUtils.isEmpty(cachedCode)) {
// 验证码失效
resCode = 1;
redisService.remove(phone);
} else if (!code.equals(cachedCode)) {
// 验证码错误
resCode = 2;
} else if (code.equals(cachedCode)) {
// 校验成功
resCode = 0;
redisService.remove(phone);
}
return resCode;
}
}

View File

@ -29,9 +29,6 @@ import java.util.concurrent.TimeUnit;
public class WeChartAppletServiceImp implements IWechatAppletService {
static final String WX_TEM_ID = "Ow0j0Jt4OJhjy6GruBstOMLTGjAVagM4hTZRLAaxqJo";
static final String SMS_TEM_ID = "SMS_216839183";
static final String SMS_SIGN_NAME = "胜唐体控";
@Autowired
private RedisCache redisCache;
@ -128,7 +125,9 @@ public class WeChartAppletServiceImp implements IWechatAppletService {
public Integer postSms(Long cusId, Long planId, String plan) {
try {
SysCustomer customer = sysCustomerService.selectSysCustomerById(cusId);
SendSmsResponse response = SmsUtils.sendSms(customer.getPhone(), plan, SMS_TEM_ID, SMS_SIGN_NAME);
JSONObject paramObj = new JSONObject();
paramObj.put("plan", plan);
SendSmsResponse response = SmsUtils.sendSms(customer.getPhone(), paramObj.toJSONString(), SmsUtils.SMS_216839183, SmsUtils.SMS_SIGN_NAME);
WxSubscribePostLog postLog = new WxSubscribePostLog();
postLog.setPhone(customer.getPhone());
@ -142,8 +141,8 @@ public class WeChartAppletServiceImp implements IWechatAppletService {
JSONObject dataParam = new JSONObject();
dataParam.put("phone", customer.getPhone());
dataParam.put("plan", plan);
dataParam.put("tmpCode", SMS_TEM_ID);
dataParam.put("signName", SMS_SIGN_NAME);
dataParam.put("tmpCode", SmsUtils.SMS_216839183);
dataParam.put("signName", SmsUtils.SMS_SIGN_NAME);
postLog.setData(dataParam);
postLog.setSendTime(DateUtils.getNowDate());
postLog.setType(1);

View File

@ -1,6 +1,5 @@
package com.stdiet.custom.utils;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest;
@ -16,12 +15,42 @@ import java.text.SimpleDateFormat;
import java.util.Date;
public class SmsUtils {
/**
* 身份验证码
*/
public static final String SMS_217025173 = "SMS_217025173";
/**
* 登录确认验证码
*/
public static final String SMS_217025172 = "SMS_217025172";
/**
* 用户注册验证码
*/
public static final String SMS_217025170 = "SMS_217025170";
/**
* 修改密码验证码
*/
public static final String SMS_217025169 = "SMS_217025169";
/**
* 新食谱通知
*/
public static final String SMS_216839183 = "SMS_216839183";
/**
* 签名
*/
public static final String SMS_SIGN_NAME = "胜唐体控";
//产品名称:云通信短信API产品,开发者无需替换
static final String product = "Dysmsapi";
//产品域名,开发者无需替换
static final String domain = "dysmsapi.aliyuncs.com";
public static SendSmsResponse sendSms(String phone, String plan, String tmpCode, String signName) throws ClientException {
public static SendSmsResponse sendSms(String phone, String paramStr, String tmpCode) throws ClientException {
return sendSms(phone, paramStr, tmpCode, SMS_SIGN_NAME);
}
public static SendSmsResponse sendSms(String phone, String paramStr, String tmpCode, String signName) throws ClientException {
//可自助调整超时时间
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
@ -41,9 +70,7 @@ public class SmsUtils {
//必填:短信模板-可在短信控制台中找到
request.setTemplateCode(tmpCode);
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}",此处的值为
JSONObject paramObj = new JSONObject();
paramObj.put("plan", plan);
request.setTemplateParam(paramObj.toJSONString());
request.setTemplateParam(paramStr);
//选填-上行短信扩展码(无特殊需求用户请忽略此字段)
//request.setSmsUpExtendCode("90997");

View File

@ -10,9 +10,11 @@
}`"
@click="handleOnCustomerClick(customer)"
>
<span class="customer_avatar">
<el-avatar size="medium" :src="customer.avatar">
{{ customer.name && customer.name.substr(-1) }}
</el-avatar>
</span>
<span class="customer_name">
{{ customer.name }}
</span>
@ -38,11 +40,10 @@
<div class="topic_content" :style="{ width: `${itemWidth}px` }">
{{ topic.content }}
</div>
<div class="topic_user_name">by {{ topic.name }}</div>
<div class="topic_time">{{ formatDate(topic.createTime) }}</div>
</div>
<div class="topic_info">
<el-tag size="small">{{ topicTypeDict[topic.topicType] }}</el-tag>
<div class="topic_time">{{ formatDate(topic.createTime) }}</div>
</div>
</div>
</div>
@ -306,6 +307,9 @@ export default {
.customer_name {
margin-left: 8px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
}
@ -362,22 +366,16 @@ export default {
line-height: 1.5;
}
.topic_user_name {
color: #8c8c8c;
.topic_time {
font-size: 14px;
margin-top: 8px;
margin-top: 6px;
color: #8c8c8c;
}
}
.topic_info {
flex: 0 0 80px;
text-align: center;
.topic_time {
font-size: 14px;
margin-top: 8px;
color: #8c8c8c;
}
}
}