|
@@ -0,0 +1,96 @@
|
|
|
+package com.zanxiang.sdk.common.util;
|
|
|
+
|
|
|
+import com.zanxiang.common.enums.PayDeviceEnum;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 通过user-agent匹配用户访问设备类型
|
|
|
+ *
|
|
|
+ * @author xufeng
|
|
|
+ * @date 2022/6/22 10:45
|
|
|
+ */
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class DeviceCheckUtil {
|
|
|
+ //手机
|
|
|
+ static String phoneReg = "\\b(ip(hone|od)|android|opera m(ob|in)i"
|
|
|
+ + "|windows (phone|ce)|blackberry|symbianos"
|
|
|
+ + "|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp"
|
|
|
+ + "|laystation portable)|nokia|fennec|htc[-_]"
|
|
|
+ + "|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"
|
|
|
+ + "|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";
|
|
|
+ //苹果设备
|
|
|
+ static String appleReg = "\\b(ip(hone|od|ad))\\b";
|
|
|
+
|
|
|
+ //苹果设备
|
|
|
+ static String wxMiniReg = "\\b(miniProgram)\\b";
|
|
|
+
|
|
|
+
|
|
|
+ //移动设备正则匹配:手机端、平板、苹果设备、微信小程序
|
|
|
+ static Pattern phonePat = Pattern.compile(phoneReg, Pattern.CASE_INSENSITIVE);
|
|
|
+ static Pattern tablePat = Pattern.compile(tableReg, Pattern.CASE_INSENSITIVE);
|
|
|
+ static Pattern applePat = Pattern.compile(appleReg, Pattern.CASE_INSENSITIVE);
|
|
|
+ static Pattern wxMiniPat = Pattern.compile(wxMiniReg, Pattern.CASE_INSENSITIVE);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检测是否是移动设备访问
|
|
|
+ *
|
|
|
+ * @param userAgent 浏览器标识
|
|
|
+ * @return true:移动设备接入,false:pc端接入
|
|
|
+ * @Title: check
|
|
|
+ */
|
|
|
+ public static boolean isMobile(String userAgent) {
|
|
|
+ if (null == userAgent) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ // 匹配
|
|
|
+ Matcher matcherPhone = phonePat.matcher(userAgent);
|
|
|
+ Matcher matcherTable = tablePat.matcher(userAgent);
|
|
|
+ if (matcherPhone.find() || matcherTable.find()) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过userAgent获取支付设备类型id
|
|
|
+ *
|
|
|
+ * @param userAgent
|
|
|
+ * @return PayDeviceEnum
|
|
|
+ */
|
|
|
+ public static Integer getType(String userAgent) {
|
|
|
+ //todo app判断场景待加入
|
|
|
+ //todo 微信小程序场景待校验
|
|
|
+ boolean isMobile = isMobile(userAgent);
|
|
|
+ if (!isMobile) {
|
|
|
+ return PayDeviceEnum.PC.getCode();
|
|
|
+ }
|
|
|
+ //验证是否为微信小程序环境
|
|
|
+ Matcher matcherWxMini = wxMiniPat.matcher(userAgent);
|
|
|
+ if (matcherWxMini.find()) {
|
|
|
+ return PayDeviceEnum.MP.getCode();
|
|
|
+ }
|
|
|
+ return PayDeviceEnum.H5.getCode();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查是否为苹果IOS设备
|
|
|
+ *
|
|
|
+ * @param userAgent
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Boolean isIOS(String userAgent) {
|
|
|
+ Matcher matcherApple = applePat.matcher(userAgent);
|
|
|
+ if (matcherApple.find()) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|