mirror of
https://gitee.com/y_project/RuoYi-Cloud.git
synced 2026-01-26 19:51:56 +08:00
短信模块写成starter
发送短信需要环境
This commit is contained in:
@@ -76,6 +76,17 @@
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common-sms</artifactId>
|
||||
<version>3.6.4</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -33,12 +33,13 @@ public class CommonController extends BaseController
|
||||
|
||||
/**
|
||||
* H5发送验证码
|
||||
* @param phone
|
||||
* @return
|
||||
* @param phone 手机号码
|
||||
* @return AjaxResult
|
||||
*/
|
||||
@GetMapping("/sendSms")
|
||||
public AjaxResult getChannelBySign(@RequestParam("phone")String phone, HttpServletRequest request){
|
||||
return commonService.sendSms(phone);
|
||||
public AjaxResult sendSms(@RequestParam("phone")String phone, HttpServletRequest request){
|
||||
String header = request.getHeader("x-sms-source");
|
||||
return commonService.sendSms(phone,header);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -175,4 +175,10 @@ public class CustomerController extends BaseController
|
||||
{
|
||||
return success(merchantService.findAllMerchantList());
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/v1/saveCustomerInfo")
|
||||
public AjaxResult v1SaveCustomerInfo(@RequestBody Customer customer, HttpServletRequest request){
|
||||
return customerService.v1SaveCustomerInfo(customer,request);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,4 +148,10 @@ public class MerchantController extends BaseController
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/v1/getMatchMerchant")
|
||||
public AjaxResult V1GetMatchMerchant(HttpServletRequest request){
|
||||
return merchantService.V1GetMatchMerchant(request);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.ruoyi.system.process;
|
||||
|
||||
import com.ruoyi.common.core.web.domain.AjaxResult;
|
||||
|
||||
/**
|
||||
*根据流程类型 执行不同的处理器
|
||||
*/
|
||||
public interface ProcessHandler {
|
||||
|
||||
|
||||
AjaxResult invoke();
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.ruoyi.system.process.enums;
|
||||
|
||||
import com.ruoyi.common.core.exception.ServiceException;
|
||||
|
||||
public enum ProcessHandlerEnum {
|
||||
|
||||
H5_HANDLER("H5_HANDLER", "H5Handler"),
|
||||
HALF_HANDLER("HALF_HANDLER", "HalfHandler");
|
||||
final String code;
|
||||
|
||||
final String name;
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
ProcessHandlerEnum(String code, String name) {
|
||||
this.code = code;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public static String getNameByCode(String code) {
|
||||
ProcessHandlerEnum[] processHandlerEnums = values();
|
||||
for (ProcessHandlerEnum processHandlerEnum : processHandlerEnums) {
|
||||
if (processHandlerEnum.getCode().equals(code)) {
|
||||
return processHandlerEnum.getName();
|
||||
}
|
||||
}
|
||||
String msg = String.format("请检查对应的流程处理器是否实现,编码为:%s", code);
|
||||
throw new ServiceException(msg, 500);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.ruoyi.system.process.handler;
|
||||
|
||||
import com.ruoyi.common.core.web.domain.AjaxResult;
|
||||
import com.ruoyi.system.process.ProcessHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component(value = "H5Handler")
|
||||
public class H5Handler implements ProcessHandler {
|
||||
@Override
|
||||
public AjaxResult invoke() {
|
||||
return null;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.ruoyi.system.process.handler;
|
||||
|
||||
import com.ruoyi.common.core.web.domain.AjaxResult;
|
||||
import com.ruoyi.system.process.ProcessHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component(value = "HalfHandler")
|
||||
public class HalfHandler implements ProcessHandler {
|
||||
@Override
|
||||
public AjaxResult invoke() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package com.ruoyi.system.process;
|
||||
@@ -20,5 +20,5 @@ public interface ICommonService
|
||||
* @param phone
|
||||
* @return
|
||||
*/
|
||||
AjaxResult sendSms(String phone);
|
||||
AjaxResult sendSms(String phone,String smsSource);
|
||||
}
|
||||
|
||||
@@ -109,4 +109,6 @@ public interface ICustomerService extends IService<Customer>
|
||||
* @return
|
||||
*/
|
||||
AjaxResult saveCustomerInfo(Customer customer, HttpServletRequest request);
|
||||
|
||||
AjaxResult v1SaveCustomerInfo(Customer customer, HttpServletRequest request);
|
||||
}
|
||||
|
||||
@@ -93,4 +93,6 @@ public interface IMerchantService extends IService<Merchant>
|
||||
AjaxResult H5applyMerchant(Long merchantId, HttpServletRequest request);
|
||||
|
||||
AjaxResult getMatchMerchantNew();
|
||||
|
||||
AjaxResult V1GetMatchMerchant(HttpServletRequest request);
|
||||
}
|
||||
|
||||
@@ -3,43 +3,100 @@ package com.ruoyi.system.service.impl;
|
||||
import com.ruoyi.common.core.constant.RedisConstant;
|
||||
import com.ruoyi.common.core.web.domain.AjaxResult;
|
||||
import com.ruoyi.common.redis.service.RedisService;
|
||||
import com.ruoyi.common.sms.component.SmsComponent;
|
||||
import com.ruoyi.system.service.ICommonService;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 渠道配置Service业务层处理
|
||||
*
|
||||
*
|
||||
* @author ruoyi
|
||||
* @date 2024-09-15
|
||||
*/
|
||||
@Service
|
||||
public class CommonServiceImpl implements ICommonService
|
||||
{
|
||||
public class CommonServiceImpl implements ICommonService {
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
@Autowired
|
||||
private SmsComponent smsComponent;
|
||||
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
|
||||
@Override
|
||||
public AjaxResult sendSms(String phone) {
|
||||
if (StringUtils.isEmpty(phone)){
|
||||
public AjaxResult sendSms(String phone, String smsSource) {
|
||||
if (StringUtils.isEmpty(phone)) {
|
||||
return AjaxResult.error("手机号未空");
|
||||
}
|
||||
if (phone.length()!=11){
|
||||
if (phone.length() != 11) {
|
||||
return AjaxResult.error("手机号长度异常");
|
||||
}
|
||||
//发送验证码
|
||||
SecureRandom secureRandom = new SecureRandom();
|
||||
int code = secureRandom.nextInt(9000) + 1000;
|
||||
|
||||
//发送验证码工具类
|
||||
//正式环境发送验证码 pc4位 h5注册页6位
|
||||
String code;
|
||||
|
||||
// 判断是否为正式环境
|
||||
if (isProductionProfileActive()) {
|
||||
//正式环境操作
|
||||
code = smsSource.equalsIgnoreCase("h5") ? generateCode(6) : generateCode(4);
|
||||
// 发送短信
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put(phone, code);
|
||||
smsComponent.sendP2PMsg(params);
|
||||
} else {
|
||||
// 非正式环境直接设置固定验证码
|
||||
code = smsSource.equalsIgnoreCase("h5") ? "123456" : "1234";
|
||||
}
|
||||
//放入缓存 3分钟
|
||||
redisService.setCacheObject(RedisConstant.H5_LOGIN_CACHE+phone,1234,3*60l, TimeUnit.SECONDS);
|
||||
|
||||
redisService.setCacheObject(RedisConstant.H5_LOGIN_CACHE + phone, code, 3 * 60L, TimeUnit.SECONDS);
|
||||
return AjaxResult.success("发送成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前活跃环境是否是 正式环境
|
||||
* if (isProductionProfileActive()) {
|
||||
* // 进行与生产环境相关的逻辑处理
|
||||
* } else {
|
||||
* // 进行非生产环境的处理
|
||||
* }
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isProductionProfileActive() {
|
||||
// 获取当前应用的活跃环境
|
||||
String[] activeProfiles = environment.getActiveProfiles();
|
||||
|
||||
// 定义线上环境列表
|
||||
List<String> proList = new ArrayList<>(Arrays.asList("prod", "pro"));
|
||||
|
||||
// 检查活跃环境是否包含在 proList 中
|
||||
for (String profile : activeProfiles) {
|
||||
if (proList.contains(profile)) {
|
||||
return true; // 如果有活跃环境在 proList 中,返回 true
|
||||
}
|
||||
}
|
||||
return false; // 如果没有匹配的环境,返回 false
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义生成验证码的方法
|
||||
*
|
||||
* @param length 验证码长度
|
||||
* @return
|
||||
*/
|
||||
private String generateCode(int length) {
|
||||
SecureRandom secureRandom = new SecureRandom();
|
||||
int bound = (int) Math.pow(10, length);
|
||||
int min = bound / 10;
|
||||
return String.valueOf(secureRandom.nextInt(bound - min) + min);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -254,5 +254,29 @@ public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> i
|
||||
return AjaxResult.success("保存成功");
|
||||
}
|
||||
|
||||
@Override
|
||||
public AjaxResult v1SaveCustomerInfo(Customer customer, HttpServletRequest request) {
|
||||
String authorization = request.getHeader("Authorization");
|
||||
Long customerId = customerTokenService.getCustomerId(authorization, false);
|
||||
String sign = request.getHeader("sign");
|
||||
if (StringUtils.isEmpty(sign)) {
|
||||
return AjaxResult.error("渠道标识不存在");
|
||||
}
|
||||
Channel channel = redisService.getCacheObject(CacheConstants.CHANNEL_SIGN + sign);
|
||||
if (customerId == null) {
|
||||
return AjaxResult.error("用户不存在或未登录");
|
||||
}
|
||||
|
||||
if (StringUtils.isEmpty(customer.getActurlName())) {
|
||||
return AjaxResult.error("姓名不能为空");
|
||||
}
|
||||
customer.setId(customerId);
|
||||
customer.setChannelId(channel.getId());
|
||||
customer.setIsAuth(true);
|
||||
customer.setStatus(1);
|
||||
updateById(customer);
|
||||
return AjaxResult.success("保存成功");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -235,6 +235,28 @@ public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> i
|
||||
return AjaxResult.success(results);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AjaxResult V1GetMatchMerchant(HttpServletRequest request) {
|
||||
|
||||
String authorization = request.getHeader("Authorization");
|
||||
Long customerId = customerTokenService.getCustomerId(authorization, false);
|
||||
if (customerId==null){
|
||||
return AjaxResult.error("用户不存在或未登录");
|
||||
}
|
||||
Customer customer = customerMapper.selectById(customerId);
|
||||
List<Merchant> merchants = matchMerchant(customer);
|
||||
List<MerchantListDto> merchantListDtos = new ArrayList<>();
|
||||
for (Merchant merchant:merchants) {
|
||||
MerchantListDto merchantListDto = new MerchantListDto();
|
||||
merchantListDto.setMerchantName(merchant.getMerchantName());
|
||||
merchantListDto.setMerchantDescribe(merchant.getMerchantDescribe());
|
||||
merchantListDto.setMerchantUrl(merchant.getHitUrl());
|
||||
merchantListDto.setMerchantId(merchant.getId());
|
||||
merchantListDtos.add(merchantListDto);
|
||||
}
|
||||
return AjaxResult.success(merchantListDtos);
|
||||
}
|
||||
|
||||
|
||||
private List<Merchant> getMerchantLists() {
|
||||
LambdaQueryWrapper<Merchant> queryWrapper = new LambdaQueryWrapper<Merchant>().eq(Merchant::getStatus, true);
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.ruoyi.system;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.ruoyi.common.sms.component.SmsComponent;
|
||||
import com.ruoyi.common.sms.entity.response.SmsResponse;
|
||||
import org.assertj.core.util.Maps;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static cn.hutool.extra.spring.SpringUtil.getActiveProfile;
|
||||
|
||||
@SpringBootTest
|
||||
public class RuoYiSystemApplicationTest {
|
||||
|
||||
@Autowired
|
||||
SmsComponent smsComponent;
|
||||
|
||||
@Test
|
||||
public void testGroupMsg(){
|
||||
|
||||
String mobile = "15826189779,15826189779,17382317154";
|
||||
String content = "123456";
|
||||
|
||||
SmsResponse groupMsg = smsComponent.sendGroupMsg(mobile, content);
|
||||
System.out.println(JSONUtil.toJsonStr(groupMsg));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testP2pMsg(){
|
||||
|
||||
String content = "123456";
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("15826189779", content);
|
||||
SmsResponse sendP2PMsg = smsComponent.sendP2PMsg(params);
|
||||
System.out.println(JSONUtil.toJsonStr(sendP2PMsg));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getActivePro(){
|
||||
|
||||
String activeProfile = getActiveProfile();
|
||||
System.out.println(activeProfile);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user