不单单只发验证码:123456 ;需要加一些发送方的信息,
譬如:验证码:123456,尊敬的会员,您正在使用手机号更换服务[验证码告知他人将导致账号被盗,请勿泄露]【中国银行】。
上面短信接口提供商只提供我们123456 这时需要添加相关模板,还有其他场景需要发送短信,不同场景所对应的模板是不一样的
结合我实际项目中的例子,记录下短信模板的开发
Java方法:
public Map getMessage(MessageTypeBean messageTypeBean) throws Exception {
Map map = new HashMap();
try {
if(StringUtil.isEmpty(messageTypeBean)){
throw new MessageException("发送短信请求参数为空!");
}
MemberInfoBean memberInfoBean = null;
MessageUserBean messageUserBean = null;
//短信信息
String message = "";
//验证码
String verCode = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
//短信内容类型
int authCodeType = messageTypeBean.getAuthCodeType();
//商户名称
String merchName = messageTypeBean.getMerchName();
//商户号
String merchNo = messageTypeBean.getMerchNo();
//会员编号
String memberNo = messageTypeBean.getMemberNo();
//会员手机号
String phone = messageTypeBean.getPhone();
//消费金额
String tranAmount = messageTypeBean.getTranAmount();
//门店名称
String storeName = messageTypeBean.getStoreName();
//门店联系电话
String telephone = messageTypeBean.getTelephone();
//调整方式
String adjustType = messageTypeBean.getAdjustType();
//调整金额
String adjustNum = null;
if(!StringUtil.isEmpty(messageTypeBean.getAdjustNum())){
adjustNum = messageTypeBean.getAdjustNum().toString();
}
//调整后积分值
String afterPoint = null;
if(!StringUtil.isEmpty(messageTypeBean.getAfterPoint())){
afterPoint = messageTypeBean.getAfterPoint().toString();
}
//调整后的金额
String afterAmount = null;
if(!StringUtil.isEmpty(messageTypeBean.getAfterAmount())){
afterAmount = messageTypeBean.getAfterAmount().toString();
}
//充值金额
String rechargeAmount = null;
if(!StringUtil.isEmpty(messageTypeBean.getRechargeAmount())){
rechargeAmount = messageTypeBean.getRechargeAmount().toString();
}
//通知剩余金额
String availableAmount = null;
if(!StringUtil.isEmpty(messageTypeBean.getAvailableAmount())){
availableAmount = messageTypeBean.getAvailableAmount().toString();
}
MessageRecordBean messageRecordBean = new MessageRecordBean();
logger.info("根据会员编号手机号获取短信");
if (StringUtil.isEmpty(merchNo)) {
throw new MessageException("您的商户号不正确!");
}
if (StringUtil.isEmpty(memberNo)) {
throw new MessageException("您的会员卡号不正确!");
}
if (StringUtil.isEmpty(phone)) {
throw new MessageException("您还未输入手机号,请输入手机号!");
}
// 正则表达式判断手机号码是否符合
if (!Pattern.matches(REGEX_MOBILE, phone)) {
logger.info("手机号码输入有误!");
throw new MessageException("您的手机号输入有问题,请重新输入!");
}
MemberInfoBean paramMemberInfo = new MemberInfoBean();
paramMemberInfo.setMerchNo(merchNo);
paramMemberInfo.setMemberNo(memberNo);
memberInfoBean = memberDao.getMemberInfoSelective(paramMemberInfo);
if(StringUtil.isEmpty(memberInfoBean) || StringUtil.isEmpty(memberInfoBean.getMerchNo())){
throw new MessageException("您还未注册会员,请先注册成为会员");
}else{
//判断会员是否已经被锁定
if (Constant.MEMBER_STATUS_LOCKED .equals(memberInfoBean.getStatus())) {
logger.info("会员锁定状态不能修改手机号");
throw new MessageException("会员已锁定,暂不能发送验证码");
}
//修改手机号判断是否已经存在该会员
if (Constant.AUTHCODETYPE_CHANGE == authCodeType) {
MemberInfoBean validateMemberInfo = new MemberInfoBean();
validateMemberInfo.setMerchNo(merchNo);
validateMemberInfo.setPhone(phone);
validateMemberInfo = memberDao.getMemberInfoSelective(validateMemberInfo);
if (!StringUtil.isEmpty(validateMemberInfo)) {
throw new MessageException("该手机号码已是会员!");
}
//短信内容
message = MessageInfoUtils.getMessageForCHANGE(verCode,merchName);
}
//新增会员
if (Constant.AUTHCODETYPE_ADD == authCodeType) {
message = MessageInfoUtils.getMessageForADD(merchName);
}
//会员储值消费
if (Constant.AUTHCODETYPE_CONSUME == authCodeType) {
message = MessageInfoUtils.getMessageForCONSUME(phone, tranAmount, storeName, telephone);
}
//积分调整
if (Constant.AUTHCODETYPE_POINT_ADJUST == authCodeType) {
message = MessageInfoUtils.getMessageForPOINTADJUST(adjustType,adjustNum,afterPoint,storeName,telephone);
}
//余额调整
if (Constant.AUTHCODETYPE_BALANCE_ADJUST == authCodeType) {
message = MessageInfoUtils.getMessageForBALANCEADJUST(adjustType, adjustNum,afterAmount,storeName,telephone);
}
//充值
if (Constant.AUTHCODETYPE_RECHARGE == authCodeType) {
message = MessageInfoUtils.getMessageForRECHARGE(rechargeAmount, availableAmount, storeName, telephone);
}
messageUserBean = memberDao.getMessageUser(memberInfoBean.getMerchNo());
if(StringUtil.isEmpty(messageUserBean) || StringUtil.isEmpty(messageUserBean.getAccount())
|| StringUtil.isEmpty(messageUserBean.getPassword())){
throw new MessageException("短信发送失败啦,请重试");
}
String account = messageUserBean.getAccount();
String password = messageUserBean.getPassword();
/* password =DesUtil.decrypt(password); */
String result = MessageValidateUtils.getMessage(configProperties.getMessageUrl(),
account, password, phone, message);
if (result == null) {
throw new MessageException("短信发送失败啦,请重试");
}
if ("0".equals(result)) {
//如果是发送的验证码需要存redis;如果是通知,则不需要存
if(Constant.AUTHCODETYPE_RECHARGE == authCodeType){
RedisUtils.set(merchNo + phone + authCodeType, message);
}
logger.info("短信消息发送成功!");
messageRecordBean.setStoreNo(memberInfoBean.getStoreNo());
messageRecordBean.setPlatformName(messageUserBean.getPlatformName());
messageRecordBean.setMobile(phone);
messageRecordBean.setContent(message);
messageRecordBean.setStatus(1);
messageRecordBean.setSendTime(DateUtils.getCurrentDateString());
memberDao.saveMessageRecord(messageRecordBean);
map.put("result", true);
} else {
logger.info("短信消息发送失败! status: " + result);
messageRecordBean.setStoreNo(memberInfoBean.getStoreNo());
messageRecordBean.setPlatformName(messageUserBean.getPlatformName());
messageRecordBean.setMobile(phone);
messageRecordBean.setContent(message);
messageRecordBean.setStatus(2);
messageRecordBean.setSendTime(DateUtils.getCurrentDateString());
memberDao.saveMessageRecord(messageRecordBean);
map.put("result", false);
map.put("exception", "短信发送失败,请稍后重试");
}
}
} catch (MessageException e) {
logger.error("发送短信异常:" + e.getMessage());
map.put("exception", e.getMessage());
return map;
} catch (Exception e) {
logger.error("发送短信异常:" + e.getMessage());
map.put("exception", "短信发送失败,请稍后重试");
return map;
}
return map;
}
说明:不同的场景对应不同的message,将只有需要发送验证码的存入redis里用于下一次验证,只是通知短信则不需要
MessageTypeBean 字段略过
常量:
/** 更换/绑定手机号 */ public static final Integer AUTHCODETYPE_MOD = 1; /** 付款 */ public static final Integer AUTHCODETYPE_PAY = 2; /** 新增会员 */ public static final Integer AUTHCODETYPE_ADD = 3; /** 会员储值消费 */ public static final Integer AUTHCODETYPE_CONSUME = 4; /** 更换手机号 */ public static final Integer AUTHCODETYPE_CHANGE = 5; /** 积分调整 */ public static final Integer AUTHCODETYPE_POINT_ADJUST = 6; /** 余额调整 */ public static final Integer AUTHCODETYPE_BALANCE_ADJUST = 7; /** 充值 */ public static final Integer AUTHCODETYPE_RECHARGE = 8;
模板util类:
package common.anmav.utils; public class MessageInfoUtils { /** * 新增会员 */ public static final String MESSAGE_TEMPLETE_ADD = "恭喜您开通%s会员,关注微信公众号将有更多惊喜哦。【%s】"; /** * 会员储值消费 */ public static final String MESSAGE_TEMPLETE_CONSUME = "%s会员,您使用会员储值支付%s元,如非本人操作请联系【%s】%s。"; /** * 更换手机号 */ public static final String MESSAGE_TEMPLETE_CHANGE = "验证码:%s,尊敬的会员,您正在使用手机号更换服务[验证码告知他人将导致账号被盗,请勿泄露]【%s】。"; /** * 积分调整 */ public static final String MESSAGE_TEMPLETE_POINT_ADJUST = "您的积分%s%s积分,调整后积分为:%s分,如有疑问请致电%s【%s】"; /** * 余额调整 */ public static final String MESSAGE_TEMPLETE_BALANCE_ADJUST = "您的余额%s%s元,调整后储值余额为:%s元,如有疑问请致电%s【%s】"; /** * 充值 */ public static final String MESSAGE_TEMPLETE_RECHARGE = "您已充值%s元,充值后储值余额为:%s元,如有疑问请致电%s【%s】"; /** * 新增会员 */ public static String getMessageForADD(String merchName){ return String.format(MESSAGE_TEMPLETE_ADD, merchName); } /** * 会员储值消费 */ public static String getMessageForCONSUME(String phone,String tranAmount,String storeName,String telephone){ return String.format(MESSAGE_TEMPLETE_CONSUME, phone, tranAmount, storeName, telephone); } /** * 更换手机号 */ public static String getMessageForCHANGE(String verCode,String merchName){ return String.format(MESSAGE_TEMPLETE_CHANGE, verCode, merchName); } /** * 积分调整 */ public static String getMessageForPOINTADJUST(String adjustType,String adjustNum,String afterPoint,String storeName,String telephone){ return String.format(MESSAGE_TEMPLETE_POINT_ADJUST, adjustType, adjustNum,afterPoint,storeName,telephone); } /** * 余额调整 */ public static String getMessageForBALANCEADJUST(String adjustType,String adjustNum,String afterAmount,String storeName,String telephone){ return String.format(MESSAGE_TEMPLETE_BALANCE_ADJUST, adjustType, adjustNum,afterAmount,storeName,telephone); } /** * 充值 */ public static String getMessageForRECHARGE(String rechargeAmount,String availableAmount,String storeName,String telephone){ return String.format(MESSAGE_TEMPLETE_RECHARGE, rechargeAmount, availableAmount, storeName, telephone); } }
说明:主要是利用了Sring.format()的方法