1.引入核心依赖

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>4.5.3</version>
</dependency>

2.编写接口

/**
 * <p>
 * Description:
 * </p>
 *
 * @author songzixian
 * @version v1.0.0
 * @create 2020-07-22 14:13
 * @see com.greatdata.msm.service
 */
public interface AliSmsService {
    Boolean SendBatchSms(String mobile,String code);
}

3.编写实现类

/**
 * <p>
 * Description:
 * </p>
 *
 * @author songzixian
 * @version v1.0.0
 * @create 2020-07-22 14:13
 * @see com.greatdata.sms.service.impl
 */
@Service
public class AliSmsServiceImpl implements AliSmsService {

    /**
     * 短信发送
     * Description:
     * @Param: [mobile 手机号码, code 验证码]
     * @Return: {@link CommonRequest}
     */
    @Override
    public Boolean SendBatchSms(String mobile,String code) {

        DefaultProfile profile = DefaultProfile.getProfile("地域ID", "填AccessKey ID", "填AccessKey Secret");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysAction("SendSms");
        request.setSysVersion("2017-05-25");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", "签名名称");
        request.putQueryParameter("TemplateCode", "模板名称");
        //这里可以传入map,如request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));
        request.putQueryParameter("TemplateParam", "{\"code\":"+code+"}");

        try {
            CommonResponse response = client.getCommonResponse(request);

            String data =  response.getData();
            System.out.println(response.getData());
            //获取阿里云状态码是否为OK
            Gson gson = new Gson();
            Map<String, Object> map = new HashMap<String, Object>();
            map = gson.fromJson(data, map.getClass());
            Object statusCode = map.get("Code");
            if (statusCode.equals("OK")||statusCode=="OK"){
                //发送成功
                return true;
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        //发送失败
        return false;
    }
}

引入ResponseResult通用工具类:https://songzixian.com/javatuils/1084.html

4.编写控制层

/**
 * <p>
 * Description:
 * </p>
 *
 * @author songzixian
 * @version v1.0.0
 * @create 2020-07-22 14:09
 * @see com.greatdata.sms.controller
 */
@RestController
public class AliSmsController {

    @Autowired
    private AliSmsService aliSmsService;

    @Autowired
    private RedistUtil redistUtil;

    /**
     * Description: 短信发送
     * @Param: [mobile:手机 , code:验证码]
     * @Return: {@link com.greatdata.commons.util.ResponseResult}
     */
    @PostMapping("sendSms")
    public ResponseResult sendSms(String mobile, String code) {
        if (StringUtils.isEmpty(mobile)){
            return new ResponseResult(ResponseResult.CodeStatus.FAIL,"请11位正确的手机号码");
        }

        if (StringUtils.isEmpty(code)){
            return new ResponseResult(ResponseResult.CodeStatus.FAIL,"请传入正确的验证码,0-9 4位数字");
        }
        redistUtil.setString(mobile,code);
        code = redistUtil.getString(mobile);
        System.out.println("mobile=="+mobile);
        //发送短信
        Boolean isSuccess = aliSmsService.SendBatchSms(mobile, code);
        if (isSuccess){
            return new ResponseResult(ResponseResult.CodeStatus.OK,"短信验证码发送成功");
        }

        return new ResponseResult(ResponseResult.CodeStatus.FAIL,"短信验证码发送失败");
    }
}
Last modification:July 23, 2020
如果觉得这篇技术文章对你有用,请随意赞赏