123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package com.iamberry.app.api.util;
- import java.io.Serializable;
- import org.apache.commons.lang3.StringUtils;
- /**
- * @company 深圳爱贝源科技有限公司
- * @website www.iamberry.com
- * @author 献
- * @tel 18271840547
- * @date 2016年12月8日
- * @explain APP版本,用于定位接口版本
- */
- public class AppVersion implements Serializable {
- private static final long serialVersionUID = 6519521054213394115L;
- /**
- * 手机系统:
- * 1、Android
- * 2、iOS
- * 3、其他
- */
- private int os;
-
- /**
- * 手机型号
- */
- private int version;
- public int getOs() {
- return os;
- }
- public void setOs(int os) {
- this.os = os;
- }
- public int getVersion() {
- return version;
- }
- public void setVersion(int version) {
- this.version = version;
- }
-
- public AppVersion(int os, int version) {
- super();
- this.os = os;
- this.version = version;
- }
- /**
- * 解析APP版本
- * @param userAgent
- * @return
- * @author 献
- * @Time 2016年12月8日
- */
- public static AppVersion parseUserAgent(String userAgent) {
- userAgent = userAgent.toLowerCase();
- if (userAgent.indexOf("ios") != -1) {
- // iOS
- return new AppVersion(2, parseVersion(userAgent));
- } else if (userAgent.indexOf("android") != -1) {
- // Android
- return new AppVersion(1, parseVersion(userAgent));
- }
- // 其他
- return new AppVersion(3, parseVersion(userAgent));
- }
-
- /**
- * 解析版本号
- * @param agent
- * @return
- * @author 献
- * @Time 2016年12月8日
- */
- private static int parseVersion(String agent) {
- int index = StringUtils.indexOf(agent, "/");
- agent = StringUtils.substring(agent, index+1);
- index = StringUtils.indexOf(agent, " ");
- index = index == -1 ? StringUtils.indexOf(agent, "(") : index;
- try {return Integer.parseInt(StringUtils.replace(StringUtils.substring(agent, 0, index), ".", ""));} catch (Exception e) {return 0;}
- }
- public String toString() {
- return "{\"os\":\"" + os + "\", \"version\":\"" + version + "\"}";
- }
- }
|