阿里云短信接口实现模板

1、首先我们需要前端传送的手机号phone,每次请求接口时都去redis缓存中以phone作为key去查有没有对应的code,如果存在,则不发送验证消息;如果code为空,则自动生成一个四位数验证码,调用send()方法,其中“SMS_154161”是我们在阿里云生成的一个templateCode,然后我们吧phone和code以key、value的形式存入我们的redis缓存中,同时设置过期时间。

import com.aliyuncs.utils.StringUtils;
import com.sms.service.SendSms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@RestController
@CrossOrigin //跨域支持
public class SendSmsController {
     

    @Autowired
    private SendSms sendSms;

    private RedisTemplate<String,String> redisTemplate;

    @GetMapping("/send/{phone}")
    public String code(@PathVariable("phone") String phone){
     
            String code = redisTemplate.opsForValue().get(phone);
        if(!StringUtils.isEmpty(code)){
     
            return phone + ":" +code + "已存在,还没有过期";
        }
        //生成验证码并存储到redis中
        code = UUID.randomUUID().toString().substring(0,4);
        HashMap<String,Object> param = new HashMap<>();
        param.put("code",code);
        boolean isSend = sendSms.send(phone,"SMS_154161",param);
        if(isSend){
     
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.SECONDS);
            return phone + ":" + code + "发送成功!";
        }else {
     
            return "发送失败!";
        }

    }

}

2、Service层

import java.util.Map;
public interface SendSmsService {
     

    public boolean send(String phoneNum, String templateCode, Map<String,Object> code);
}

3、ServiceImpl实现层

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.sms.service.SendSms;
import org.springframework.stereotype.Service;
import java.util.Map;

@Service
public class SendSmsServiceImpl  implements SendSmsService {
     
    @Override
    public boolean send(String phoneNum, String templateCode, Map<String, Object> code) {
     
        // 设置公共请求参数,初始化Client、连接阿里云
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-hangzhou",          // 地域ID
                "账号",      // RAM账号的AccessKey ID
                "密码"); // RAM账号AccessKey Secret
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest addSmsTemplateRequest = new CommonRequest();
        addSmsTemplateRequest.setSysDomain("dysmsapi.aliyuncs.com");//不要动
        addSmsTemplateRequest.setSysVersion("2017-05-25");//不要动
        addSmsTemplateRequest.setSysAction("SendSms");

        //自定义的参数(手机号,验证码,签名,模板!)
        addSmsTemplateRequest.putQueryParameter("PhoneNumbers", phoneNum);
        addSmsTemplateRequest.putQueryParameter("SignName", "签名,阿里云生成");
        addSmsTemplateRequest.putQueryParameter("TemplateCode", templateCode);
        addSmsTemplateRequest.putQueryParameter("TemplateParam", JSONObject.toJSONString(code));

        try {
     
            CommonResponse response = client.getCommonResponse(addSmsTemplateRequest);
            return response.getHttpResponse().isSuccess();
        }catch (ServerException e){
     
            e.printStackTrace();
        }
        catch (ClientException e) {
     
            e.printStackTrace();
        }
        return false;
    }
}

你可能感兴趣的:(微服务)