废话不多说直接上代码:
验证码生成代码:
String code = KeyGenerater.getRandomNumberKey(6);
String data = FastJsonUtil.of("code", code, "product", "网").toJSONString();
if (sendSms(phoneNum, data, codeModel)) {
redisService.set(cacheKey, code, GlobalCacheManageEnum.PHONE_SMS_KEY.getExpireTime());
resultVo.setData(total);
return resultVo;
}
调用发送消息:
配置:
远程调用:
private Boolean sendSms(String phoneNum, String data, String codeModel) {
if (StringUtils.isBlank(phoneNum) || StringUtils.isBlank(data) || StringUtils.isBlank(codeModel)) {
throw new OpBusinessException(PublicExceptionCodeEnum.EX_PARAM_NOT_NULL.getCode(), PublicExceptionCodeEnum.EX_PARAM_NOT_NULL.getMsg());
}
if (redisService.exists(GlobalCacheManageEnum.PHONE_SMS_KEY.getCacheKey(codeModel, phoneNum))) {
throw new OpBusinessException(ExceptionCodeEnum.EX_PHONE_CODE_IS_USING.getCode(), ExceptionCodeEnum.EX_PHONE_CODE_IS_USING.getMsg());
}
// 1:组装发送信息
String randomKey = KeyGenerater.getRandomKey(32);
Map param = new HashMap();
param.put("appId", phoneSmsAppId);
param.put("mobile", phoneNum);
param.put("data", data);
param.put("codeModel", codeModel);
param.put("randomKey", randomKey);
String sign = MD5Facade.getFormDataParamMD5(param, phoneSmsSecretKey, null);
param.put("sign", sign);
// 4:获取验证码
String result;
try {
result = HttpClientUtil.doGet(phoneSmsUrl, param);
} catch (Exception e) {
throw new RpcInvokeException("sendSms-------" + phoneSmsUrl, e);
}
if (StringUtils.isEmpty(result)) {
// 短信服务器异常
throw new OpBusinessException(ExceptionCodeEnum.EX_PHONE_SMS_SERVICE_ERROR.getCode(), ExceptionCodeEnum.EX_PHONE_SMS_SERVICE_ERROR.getMsg());
}
// 5:解析结果
ResultVo
远程代码:
@RequestMapping(value = "/sendSms", method = RequestMethod.GET)
@ResponseBody
public ResultVo
配置文件:
@Override
public ResultVo
短信模板类:
public enum SmsCodeModelEnum {
VERIFICATION_MODEL("SMS_********","********通验证码模版"),
SERVER_ALARM_MODEL("SMS_********","服务器监控报警模板"),
BASE_VERIFICATION_MODEL("SMS_********","验证码模版"),
IDENTITY_MODEL("SMS_1********","身份验证模版"),
LOGIN_CONFIRMATION("SMS_********","登录确认模版"),
USER_REGISTER("SMS_********","用户注册模版"),
;
private String code;
private String description;
SmsCodeModelEnum(String code, String description) {
this.code = code;
this.description = description;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public static SmsCodeModelEnum eval(String code) {
for (SmsCodeModelEnum type : SmsCodeModelEnum.values()) {
if (type.getCode().equals(code)) {
return type;
}
}
return null;
}
public static boolean contains(String code) {
for (SmsCodeModelEnum type : SmsCodeModelEnum.values()) {
if (type.getCode().equals(code)) {
return true;
}
}
return false;
}
}