1
0
mirror of https://github.com/chatopera/cosin.git synced 2025-08-01 16:38:02 +08:00

close #491 Wechat mobile device

This commit is contained in:
Yu 2021-11-16 11:02:56 +08:00
parent e7c45782e7
commit ff7ab98bcf

View File

@ -17,52 +17,55 @@
package com.chatopera.cc.util; package com.chatopera.cc.util;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
* 检测是否为移动端设备访问 , 网络共享源码 * 检测是否为移动端设备访问 , 网络共享源码
* *
* @author : Cuichenglong * @author : Cuichenglong
* @group : tgb8 * @group : tgb8
* @Version : 1.00 * @Version : 1.00
* @Date : 2014-7-7 下午01:34:31 * @Date : 2014-7-7 下午01:34:31
*/ */
public class MobileDevice { public class MobileDevice {
// \b 是单词边界(连着的两个(字母字符 非字母字符) 之间的逻辑上的间隔), // \b 是单词边界(连着的两个(字母字符 非字母字符) 之间的逻辑上的间隔),
// 字符串在编译时会被转码一次,所以是 "\\b" // 字符串在编译时会被转码一次,所以是 "\\b"
// \B 是单词内部逻辑间隔(连着的两个字母字符之间的逻辑上的间隔) // \B 是单词内部逻辑间隔(连着的两个字母字符之间的逻辑上的间隔)
static String phoneReg = "\\b(ip(hone|od)|android|opera m(ob|in)i" static String phoneReg = "\\b(ip(hone|od)|android|opera m(ob|in)i"
+"|windows (phone|ce)|blackberry" + "|windows (phone|ce)|blackberry"
+"|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp" + "|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp"
+"|laystation portable)|nokia|fennec|htc[-_]" + "|laystation portable)|nokia|fennec|htc[-_]"
+"|mobile|up.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\\b"; + "|mobile|up.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";
static String tableReg = "\\b(ipad|tablet|(Nexus 7)|up.browser" static String tableReg = "\\b(ipad|tablet|(Nexus 7)|up.browser"
+"|[1-4][0-9]{2}x[1-4][0-9]{2})\\b"; + "|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";
static String wechatReg = "MicroMessenger";
//移动设备正则匹配手机端平板 //移动设备正则匹配手机端平板
static Pattern phonePat = Pattern.compile(phoneReg, Pattern.CASE_INSENSITIVE); static Pattern phonePat = Pattern.compile(phoneReg, Pattern.CASE_INSENSITIVE);
static Pattern tablePat = Pattern.compile(tableReg, Pattern.CASE_INSENSITIVE); static Pattern tablePat = Pattern.compile(tableReg, Pattern.CASE_INSENSITIVE);
static Pattern wechatPat = Pattern.compile(wechatReg, Pattern.CASE_INSENSITIVE);
/**
* 检测是否是移动设备访问 /**
* * 检测是否是移动设备访问
*
* @param userAgent 浏览器标识
* @return true:移动设备接入false:pc端接入
* @Title: isMobile * @Title: isMobile
* @Date : 2014-7-7 下午01:29:07 * @Date : 2014-7-7 下午01:29:07
* @param userAgent 浏览器标识 */
* @return true:移动设备接入false:pc端接入 public static boolean isMobile(String userAgent) {
*/ if (null == userAgent) {
public static boolean isMobile(String userAgent){ userAgent = "";
if(null == userAgent){ }
userAgent = "";
}
// 匹配 // 匹配
Matcher matcherPhone = phonePat.matcher(userAgent); Matcher matcherPhone = phonePat.matcher(userAgent);
Matcher matcherTable = tablePat.matcher(userAgent); Matcher matcherTable = tablePat.matcher(userAgent);
if(matcherPhone.find() || matcherTable.find()){ Matcher matcherWechat = wechatPat.matcher(userAgent);
return true; if (matcherPhone.find() || matcherTable.find() || matcherWechat.find()) {
} else { return true;
return false; } else {
} return false;
} }
}
} }