添加发送短信
This commit is contained in:
parent
6a8ea3416f
commit
21fb470052
|
|
@ -52,7 +52,7 @@ public class TbProskuConController {
|
|||
} catch (BadRequestException be) {
|
||||
throw new Exception(be.getMessage());
|
||||
}catch (Exception e){
|
||||
throw new Exception("系统异常");
|
||||
throw new Exception("相同商品耗材信息不允许添加");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,18 +16,30 @@
|
|||
package cn.ysk.cashier.controller;
|
||||
|
||||
import cn.ysk.cashier.annotation.Log;
|
||||
import cn.ysk.cashier.exception.BadRequestException;
|
||||
import cn.ysk.cashier.pojo.TbUserInfo;
|
||||
import cn.ysk.cashier.service.TbUserInfoService;
|
||||
import cn.ysk.cashier.dto.TbUserInfoQueryCriteria;
|
||||
import cn.ysk.cashier.utils.JSONUtil;
|
||||
import cn.ysk.cashier.utils.RedisUtils;
|
||||
import cn.ysk.cashier.utils.StringUtil;
|
||||
import cn.ysk.cashier.utils.ValidateCodeUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import io.swagger.annotations.*;
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
|
|
@ -75,4 +87,45 @@ public class TbUserInfoController {
|
|||
tbUserInfoService.deleteAll(ids);
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
ValidateCodeUtil validateCodeUtil;
|
||||
|
||||
@Autowired
|
||||
RedisUtils redisUtils;
|
||||
|
||||
@GetMapping("sendMsg")
|
||||
public Object sendMsg(){
|
||||
Object o= SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
||||
if(Objects.isNull(o)){
|
||||
throw new BadRequestException("用户登录信息失效");
|
||||
}
|
||||
JSONObject object=JSON.parseObject(JSON.toJSONString(o));
|
||||
if(Objects.isNull(object)){
|
||||
throw new BadRequestException("用户登录信息失效");
|
||||
}
|
||||
String regex="^\\d{11}$";
|
||||
|
||||
if(!object.containsKey("username")||Objects.isNull(object.getString("username"))||
|
||||
!object.getString("username").matches(regex)
|
||||
){
|
||||
throw new BadRequestException("用户登录信息失效");
|
||||
}
|
||||
String phone=object.getString("username");
|
||||
|
||||
String tempcode="SMS_244665149";
|
||||
|
||||
String random = StringUtil.random(6);
|
||||
try {
|
||||
validateCodeUtil.requestValidateCodeAli(phone, random,tempcode);
|
||||
redisUtils.set(phone.concat("#").concat(tempcode),random,300L);
|
||||
return "{\n" +
|
||||
" \"code\": 0,\n" +
|
||||
" \"msg\": \"成功\"\n" +
|
||||
"}";
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -80,4 +80,7 @@ public interface TbUserInfoService {
|
|||
* @throws IOException /
|
||||
*/
|
||||
void download(List<TbUserInfoDto> all, HttpServletResponse response) throws IOException;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,86 @@
|
|||
package cn.ysk.cashier.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* @author lyf
|
||||
*/
|
||||
public class StringUtil {
|
||||
/**
|
||||
* 生成指定长度的随机数
|
||||
* @param length
|
||||
* @return
|
||||
*/
|
||||
public static String genRandomNum(int length) {
|
||||
int maxNum = 36;
|
||||
int i;
|
||||
int count = 0;
|
||||
char[] str = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
|
||||
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
|
||||
'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
|
||||
StringBuffer pwd = new StringBuffer("");
|
||||
Random r = new Random();
|
||||
while (count < length) {
|
||||
i = Math.abs(r.nextInt(maxNum));
|
||||
pwd.append(str[i]);
|
||||
count++;
|
||||
}
|
||||
return pwd.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成订单号
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static synchronized String getBillno() {
|
||||
StringBuilder billno = new StringBuilder();
|
||||
|
||||
// 日期(格式:20080524)
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
|
||||
billno.append(format.format(new Date()));
|
||||
return billno.toString();
|
||||
}
|
||||
public static JSONArray stringChangeList(String listString){
|
||||
// 使用Fastjson将JSON字符串转换为JSONArray对象
|
||||
return JSON.parseArray(listString);
|
||||
|
||||
}
|
||||
|
||||
public static String random(int length) {
|
||||
Random random = new Random();
|
||||
String result = "";
|
||||
for (int i = 0; i < length; i++) {
|
||||
result += random.nextInt(10);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static final String CHINESE_CHARS = "你我他她它们这里那里多少是否好坏快慢上下左右前后高低大小长短方圆胖瘦黑白红绿蓝黄紫粉红桔红橙黄棕灰褐";
|
||||
|
||||
// 生成随机中文昵称
|
||||
public static String generateRandomNickname(int length) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < length; i++) {
|
||||
int index = random.nextInt(CHINESE_CHARS.length());
|
||||
char randomChar = CHINESE_CHARS.charAt(index);
|
||||
sb.append(randomChar);
|
||||
}
|
||||
return desensitizeNickname(sb.toString());
|
||||
}
|
||||
|
||||
// 对昵称进行脱敏处理
|
||||
public static String desensitizeNickname(String nickname) {
|
||||
if (nickname == null || nickname.length() != 5) {
|
||||
return nickname;
|
||||
}
|
||||
return nickname.charAt(0) + "***" + nickname.charAt(4);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
package cn.ysk.cashier.utils;
|
||||
|
||||
import com.aliyun.dysmsapi20170525.Client;
|
||||
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* 获取验证码相关工具
|
||||
* @author 12847
|
||||
*/
|
||||
@Component
|
||||
public class ValidateCodeUtil {
|
||||
/**
|
||||
* 阿里云key
|
||||
*/
|
||||
@Value("${aliyun.keyid}")
|
||||
private String ACCESSKEYID;
|
||||
/**
|
||||
* 阿里云secret
|
||||
*/
|
||||
@Value("${aliyun.keysecret}")
|
||||
private String ACCESSKEYSECRET;
|
||||
/**
|
||||
* 十小时
|
||||
*/
|
||||
protected static final long MILLIS_MINUTE = 10 *60 * 60 *1000;
|
||||
|
||||
@Autowired
|
||||
private HttpServletRequest request;
|
||||
|
||||
|
||||
/**
|
||||
* 获取验证码(阿里云)
|
||||
*/
|
||||
public SendSmsResponse requestValidateCodeAli(String phone, String checkCode,String templateCode) throws Exception {
|
||||
Client client = null;
|
||||
try {
|
||||
client = createClient();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 1.发送短信
|
||||
com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
|
||||
.setSignName("银收客")
|
||||
.setTemplateCode(templateCode)
|
||||
.setTemplateParam("{\"code\":" +"'"+checkCode +"'"+"}")
|
||||
.setPhoneNumbers(phone);
|
||||
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||
try {
|
||||
assert client != null;
|
||||
return client.sendSmsWithOptions(sendSmsRequest, runtime);
|
||||
} catch (Exception e) {
|
||||
throw new Exception(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* 发送短信(阿里云)
|
||||
*
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public Client createClient() throws Exception {
|
||||
Config config = new Config();
|
||||
config.accessKeyId = ACCESSKEYID;
|
||||
config.accessKeySecret = ACCESSKEYSECRET;
|
||||
return new com.aliyun.dysmsapi20170525.Client(config);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -72,3 +72,9 @@ mybatis-plus:
|
|||
global-config:
|
||||
db-config:
|
||||
id-type: auto
|
||||
aliyun:
|
||||
keyid: LTAI5tPdEfYSZcqHbjCrtPRD
|
||||
keysecret: DZjyHBq3nTujF0NMLxnZgsecU8ZCvy
|
||||
oss:
|
||||
bucketname: cashier-oss
|
||||
endpoint: oss-cn-beijing.aliyuncs.com
|
||||
|
|
|
|||
Loading…
Reference in New Issue