场景
对接微信小程序消息订阅发送消息的文档是有明确标识文本的规则的,但是当本文的格式问题时微信的出参并不会返回详细的原因,所以最好在调用方做一次阻拦。
文档位置:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-message-management/subscribe-message/sendMessage.html
代码
定义正则
//==== 小程序模板校验正则 ====
/**
* 32位以内字母,只能字母
*/
private static final String letter = "^[a-zA-Z]{1,32}$";
/**
* 5位以内符号,只能符号
*/
private static final String symbol = "^[`~!@#$%^&*()_\\-+=<>?:\"{}|,.\\/;'\\\\[\\]·~!@#¥%……&*()——\\-+={}|《》?:“”【】、;‘',。、]{1,5}$";
/**
* 32位以内数字、字母或符号
*/
private static final String character_string = "^[A-Za-z0-9`~!@#$%^&*()_\\-+=<>?:\"{}|,.\\/;'\\\\[\\]·~!@#¥%……&*()——\\-+={}|《》?:“”【】、;‘',。、]{1,32}$";
//年月日加24小时制时间格式(有空格);例如:2019年10月01日 15:01、2019-10-01日
private static final String year_month_day_one = "([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})年(((0[13578]|1[02])月(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))日";
private static final String year_month_day_two = "([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))";
private static final String twenty_four_hours_one = "([01]?[0-9]|2[0-3]):[0-5][0-9]";
private static final String twenty_four_hours_two = "([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]";
private static final String time_date = year_month_day_one + " " + twenty_four_hours_one + "|" + year_month_day_one + " " + twenty_four_hours_two + "|" + year_month_day_two + " " + twenty_four_hours_one + "|" + year_month_day_two + " " + twenty_four_hours_two;
/**
* 24小时制时间格式(支持+年月日); 例如:15:01,或:2019年10月1日 15:01
*/
private static final String time = twenty_four_hours_one + "|" + twenty_four_hours_two + "|" + time_date;
/**
* 年月日格式(支持+24小时制时间),支持填时间段,两个时间点之间用“~”符号连接 例如:2019年10月1日,或:2019年10月1日 15:01
*/
private static final String date = year_month_day_one + "|" + year_month_day_two + "|" + time_date;
/**
* 1个币种符号+纯数字,可带小数,结尾可带“元”,可带两位小数(没限定几位数)
*/
private static final String amount = "^[\\$\\xA2-\\xA5\\u058F\\u060B\\u09F2\\u09F3\\u09FB\\u0AF1\\u0BF9\\u0E3F\\u17DB\\u20A0-\\u20BD\\uA838\\uFDFC\\uFE69\\uFF04\\uFFE0\\uFFE1\\uFFE5\\uFFE6]([1-9][0-9]*)+(\\.[0-9]{1,2})?元*$";
/**
* 电话,17位以内,数字、符号
*/
private static final String phone_number = "^[0-9+-]{1,17}$";
/**
* 车牌,8位以内,第一位与最后一位可为汉字,其余为字母或数字
*/
private static final String car_number = "^[\\u4e00-\\u9fa5A-Z0-9]{1}[A-Z0-9]{6}[\\u4e00-\\u9fa5A-Z0-9]{1}$";
/**
* 姓名,中文名10个汉字内;纯英文名20个字母内;中文和字母混合按中文名算,10个字内
*/
private static final String name = "^[\\u4e00-\\u9fa5]{1,10}$|^[a-zA-Z]{1,20}$|^[a-zA-Z\\u4e00-\\u9fa5]{1,10}$";
/**
* 汉字,5个以内汉字
*/
private static final String phrase = "^[\\u4e00-\\u9fa5]{1,5}$";
//==== 公众号模板校验正则 ====
/**
* 短事物,5个以内字符,不支持换行
*/
private static final String short_thing = "^.{1,5}$";
/**
* 副标题,10个以内字符,不支持换行
*/
private static final String first = "^.{1,10}$";
/**
* 标题,20以内字符,不支持换行
*/
private static final String remark = "^.{1,20}$";
正则判断
/**
* 根据指定正则表达式判断目标字符串是否符合规则
*
* @param str 需要判断的字符串
* @param regex 正则表达式
* @return 是否符合目标正则规则
*/
private static boolean judgeRegular(@NotNull String str, @NotNull String regex) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
return matcher.matches();
}
文本校验
- 这里
judgeText
为字段的类型,foxbaseValue
为需要校验的文本。
switch (judgeText) {
case "thing":
if (foxbaseValue.length() > 20) {
throw new BusinessException(titleName + ":" + "内容不正确,该字段限制20个字符以内,内容格式不限,符号不能带有换行等控制字符");
}
break;
case "number":
if (!(NumberUtils.isNumber(foxbaseValue) && foxbaseValue.length() <= 32)) {
throw new BusinessException(titleName + ":" + "内容不正确,该字段内容限制32位以内,只可输入数字,可带小数");
}
break;
case "letter":
if (!judgeRegular(foxbaseValue, letter)) {
throw new BusinessException(titleName + ":" + "内容不正确,该字段内容限制32位以内,只可输入字母");
}
break;
case "symbol":
if (!judgeRegular(foxbaseValue, symbol)) {
throw new BusinessException(titleName + ":" + "内容不正确,该字段内容限制5位以内,只可输入符号,不能带有换行等控制字符");
}
break;
case "character_string":
if (!judgeRegular(foxbaseValue, character_string)) {
throw new BusinessException(titleName + ":" + "内容不正确,该字段内容限制32位以内,可数字、字母或符号组合,符号不能带有换行等控制字符");
}
break;
case "time":
//按照~拆分日期范围
String[] timeStrings = foxbaseValue.split("~");
if (!(timeStrings.length == 1 || timeStrings.length == 2)) {
throw new BusinessException(titleName + ":" + "内容不正确,该字段只可填写24小时制时间格式(支持+年月日),支持填时间段,两个时间点之间用“~”符号连接");
}
for (String timeString : timeStrings) {
if (!judgeRegular(timeString, time)) {
throw new BusinessException(titleName + ":" + "内容不正确,该字段只可填写24小时制时间格式(支持+年月日),支持填时间段,两个时间点之间用“~”符号连接");
}
}
break;
case "date":
//按照~拆分日期范围
String[] dateStrings = foxbaseValue.split("~");
if (!(dateStrings.length == 1 || dateStrings.length == 2)) {
throw new BusinessException(titleName + ":" + "内容不正确,该字段只可填写年月日格式(支持24小时制时间格式),支持填时间段,两个时间点之间用“~”符号连接");
}
for (String dateString : dateStrings) {
if (!judgeRegular(dateString, date)) {
throw new BusinessException(titleName + ":" + "内容不正确,该字段只可填写年月日格式(支持24小时制时间格式),支持填时间段,两个时间点之间用“~”符号连接");
}
}
break;
case "amount":
if (!judgeRegular(foxbaseValue, amount) || foxbaseValue.length() > 10) {
throw new BusinessException(titleName + ":" + "内容不正确,该字段只可填写1个币种符号+10位以内纯数字,可带小数,结尾可带“元”");
}
break;
case "phone_number":
if (!judgeRegular(foxbaseValue, phone_number)) {
throw new BusinessException(titleName + ":" + "内容不正确,该字段只可填写17位以内数字、符号,数字可带小数,符号不能带有换行等控制字符");
}
break;
case "car_number":
if (!judgeRegular(foxbaseValue, car_number)) {
throw new BusinessException(titleName + ":" + "内容不正确,该字段内容限制8位以内,第一位与最后一位可为汉字,其余为字母或数字");
}
break;
case "name":
if (!judgeRegular(foxbaseValue, name)) {
throw new BusinessException(titleName + ":" + "内容不正确,该字段内容只可填写10个以内纯汉字或20个以内纯字母或符号,中文和字母混合按中文名算,10个字内,符号不能带有换行等控制字符");
}
break;
case "phrase":
if (!judgeRegular(foxbaseValue, phrase)) {
throw new BusinessException(titleName + ":" + "内容不正确,该字段内容限制5个字以内,只可输入纯汉字");
}
break;
case "short_thing":
if (!judgeRegular(foxbaseValue, short_thing)) {
throw new BusinessException(titleName + ":" + "内容不正确,该字段内容限制5个以内字符,不支持换行");
}
break;
case "first":
if (!judgeRegular(foxbaseValue, first)) {
throw new BusinessException(titleName + ":" + "内容不正确,该字段内容限制10个以内字符,不支持换行");
}
break;
case "remark":
if (!judgeRegular(foxbaseValue, remark)) {
throw new BusinessException(titleName + ":" + "内容不正确,该字段内容限制20个以内字符,不支持换行");
}
break;
default:
break;
}
总结
有部分正则其实还是没有完全一致的,主要是感觉文档也没有写的特别清楚,但是也是够用了