用户注册(阿里云手机验证码)

阿里云开通三网106短信

①、在阿里云市场搜索"短信",开通三网短信,并可以查看其中的请求示例(java PHP^)

并在个人中心的管理控制台中查到,获取其中的AppKey AppCode

②、接口开发

service-user模块中依赖spring-boot-stater-data-redis(配置文件中连接redis的信息)

cmmon-utils模块中依赖httpclient,httpcore,commons.lang,jetty-util

java 复制代码
@RestController
@RequestMapping("api/user/sms")
public class SmsController{
	
	@Autowired
	private SmsService smsService;

	@GetMapping(value="/sendCode/{phone}")
	public Result sendValidateCode(@PathVariable String phone){
		smsService.sendValidateCode(phone);
		return Reuslt.build(null,ResultCodeEnum.SUCCESS);
	}
}
java 复制代码
@Service
public class SmsServiceImpl implements SmsService{

	@Autowired
	private RedisTemplate<String,String> redisTemplate;
	
	@Override
	public void sendCode(String phone){
	
		//生成验证码(通过common.lang工具生成4位验证码)
		String code = RandomStringUtils.randomNumeric(4);

		//将验证码放到redis,并设置过期时间
		redisTemplate.opsForValue().set(phone,code,5,TimeUnit.MINUTES);

		//向手机号发送短信验证码
		sendMessage(phone,code);
	}

	private void sendMessage(String phone,String code){
		
		String host = "https://dfsns.market.alicloudapi.com";
		String path = "/data/send_sms";
		String method = "POST";
		String appcode = "开通阿里云短信服务时的code";
		Map<String,String> headers = new HashMap<>();

		headers.put("Authorization","APPCODE" + appcode);
		headers.put("Content-Type","application/x-www-form-urlencoded";charset-UTF-8);

		Map<String,String> querys = new HashMap<>();
		Map<String,String> bodys = new HashMap<>();
		bodys.put("content","code:"+code);
		bodys.put("template_id","CST-ptdie100");
		bodys.put("phone_number",phone);

		try{
			//通过HttpUtils工具类进行短信的发送
			HttpResponse response = HttpUtils.doPost(host,path,method,headers,querys,body);
			System.out.println(response.toString())
		}catch(Exception e){
			e.printStackTrace()
		}
	}
}

HttpUtils工具类及其依赖,从github中aliyun下载

java 复制代码
public class HttpUtils{
	
	public static HttpRespose doGet(String host,String path,String method,
									Map<String,String> headers,
									Map<String,String> querys) throws Exception{
		HttpClient httpClient = wrapClient(host);
		HttpGet request = new HttpGet(buildUrl(host,path,querys));
		for(Map.Entry<String,String> e:headers.entrySet){
			request.addHeader(e.getKey(),e.getValue());
		}
		return httpClient.execute(request);
	}

	public static HttpResponse doPost(String host,String path,String method,
										Map<String,String> headers,
										Map<String,String> querys,
										Map<String,String> bodys)throws Exception{
		HttpClient httpClient = wrapClient(host);
		HttpPost request = new HttpPost(buildUrl(host,path,querys));
		for(Map.Entry<String,String> e:headers.entrySet()){
			request.addHeader(e.getKey(),e.getValue());
		}
	}
}

====================================================================

①、依赖

xml 复制代码
<dependency>
    <groupId>com.github.qcloudsms</groupId>
    <artifactId>qcloudsms</artifactId>
    <version>1.0.6</version>
</dependency>

②、

java 复制代码
public interface SmsService {
    /**
     * 发送还款短信通知
     * @param mobile 还款人手机号
     * @param date 日期
     * @param amount 应还金额
     */
    void sendRepaymentNotify(String mobile, String date, BigDecimal amount);
}
java 复制代码
@Service
@Slf4j
public class QCloudSmsServiceImpl implements SmsService{
	
	@Value("${sms.qcloud.appId}")
	private int appId;

	@Value("${sms.qcloud.appKey}")
	private String appKey;

	@Value("{sms.qcloud.templateId}")
	private int templateId;

	@Value("{sms.qcloud.sign}")
	private String sign;

	@Override
	public void sendRepaymentNotify(String mobile, String date, BigDecimal amount){
		
		log.info("给手机号{},发送还款提醒:{},金额:{}",mobile,date,amount);
		
		SmsSingleSender ssender = new SmsSingleSender(appId, appKey);//qcloudsms-1.0.6
		try{
			ssender.sendWithParam("86", mobile,
                    templateId, new String[]{date, amount.toString()}, sign, "", "");
		}catch(Exception ex){
			log.error("发送失败:{}",ex .getMessage());
		}
	}
}

③、业务服务调用

java 复制代码
@Override
public void sendRepaymentNotify(String date){

	//1.查询到期的还款计划
        List<RepaymentPlan> repaymentPlanList = selectDueRepayment(date);

        //2.遍历还款计划
        repaymentPlanList.forEach(repaymentPlan -> {
            //3.得到还款人的信息
            RestResponse<BorrowerDTO> consumerReponse = consumerApiAgent.getBorrowerMobile(repaymentPlan.getConsumerId());

            //4.得到还款人的手机号
            String mobile = consumerReponse.getResult().getMobile();

            //5.发送还款短信
            smsService.sendRepaymentNotify(mobile,date,repaymentPlan.getAmount());
        });
}
相关推荐
翼龙云_cloud7 小时前
阿里云渠道商:如何手动一键扩缩容ECS实例?
运维·服务器·阿里云·云计算
AKAMAI9 小时前
基准测试:Akamai云上的NVIDIA RTX Pro 6000 Blackwell
人工智能·云计算·测试
齐 飞11 小时前
使用阿里云的MaxCompute查询sql时报错:DruidPooledPreparedStatement: getMaxFieldSize error
sql·阿里云·odps
China_Yanhy12 小时前
AWS EKS三种类别,如何选择
云计算·aws
xybDIY13 小时前
亚马逊云 Organizations 组织 Link 账号关联与解绑自动化解决方案
运维·自动化·云计算·aws
倪某某13 小时前
阿里云无影GPU部署WAN2.2模型
阿里云·云计算
阿里云通信13 小时前
WhatsApp 账号被封怎么办?日常“养号”、防封、解封实践
阿里云·whatsapp·whatsapp 封号
倪某某13 小时前
阿里云ECS GPU部署WAN2.2
人工智能·阿里云·云计算
风吹落叶花飘荡15 小时前
将mysql数据库的内容备份至阿里云 oss归档存储
数据库·mysql·阿里云
小白考证进阶中16 小时前
阿里云ACA认证常见问题答疑
阿里云·大模型·云计算·阿里云aca证书·阿里云aca·aca认证·入门证书