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

阿里云开通三网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());
        });
}
相关推荐
AKAMAI15 小时前
每个应用程序现在都生活在人工智能生态系统中
人工智能·云计算
财迅通Ai16 小时前
科源制药携手腾讯云推进AI办公迭代升级
人工智能·云计算·腾讯云·科源制药
智造工厂18 小时前
领盾LEDEN-L1数模对讲+热成像+气体检测工业智能防爆巡检终端,全方位赋能工业巡检现场!
智能手机
wujian831118 小时前
【 文心手机版生成的表格怎么复制下来 】?这个问题,[ AI 导出鸭 ] 给出了完全不同的答案
人工智能·ai·chatgpt·智能手机·ai导出鸭
tg_xianheyun20 小时前
腾讯云国际账号注册代充值服务商怎么选?
大数据·运维·服务器·云计算·github·腾讯云·cdn加速
liuqs33220 小时前
AWS、Azure接连宕机后,“云存储”的这个软肋被重新摆上台面
云计算·azure·aws
不一样的少年_20 小时前
Docker 入门第一课:从架构、云服务器到安装完成
linux·docker·云计算
聚搜云——JuSouClouD21 小时前
上海阿里云代理商(聚搜云)分享:ECS 服务器 CPU 跑满 100%,如何找到占用高的进程
服务器·阿里云·云计算
星野川崎2061 天前
电商多店运维:云机24小时挂机频繁掉线、账号无故风控深度原因分析及解决方案
大数据·运维·服务器·云计算·电商
极客猴子1 天前
能提取抖音视频文案的APP推荐:短视频文案工具合集
人工智能·智能手机·音视频·语音识别