225ba874aa7b2b63b23a80d0107330291678dc49.svn-base 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package com.iamberry.app.api.util;
  2. import java.io.Serializable;
  3. import org.apache.commons.lang3.StringUtils;
  4. /**
  5. * @company 深圳爱贝源科技有限公司
  6. * @website www.iamberry.com
  7. * @author 献
  8. * @tel 18271840547
  9. * @date 2016年12月8日
  10. * @explain APP版本,用于定位接口版本
  11. */
  12. public class AppVersion implements Serializable {
  13. private static final long serialVersionUID = 6519521054213394115L;
  14. /**
  15. * 手机系统:
  16. * 1、Android
  17. * 2、iOS
  18. * 3、其他
  19. */
  20. private int os;
  21. /**
  22. * 手机型号
  23. */
  24. private int version;
  25. public int getOs() {
  26. return os;
  27. }
  28. public void setOs(int os) {
  29. this.os = os;
  30. }
  31. public int getVersion() {
  32. return version;
  33. }
  34. public void setVersion(int version) {
  35. this.version = version;
  36. }
  37. public AppVersion(int os, int version) {
  38. super();
  39. this.os = os;
  40. this.version = version;
  41. }
  42. /**
  43. * 解析APP版本
  44. * @param userAgent
  45. * @return
  46. * @author 献
  47. * @Time 2016年12月8日
  48. */
  49. public static AppVersion parseUserAgent(String userAgent) {
  50. userAgent = userAgent.toLowerCase();
  51. if (userAgent.indexOf("ios") != -1) {
  52. // iOS
  53. return new AppVersion(2, parseVersion(userAgent));
  54. } else if (userAgent.indexOf("android") != -1) {
  55. // Android
  56. return new AppVersion(1, parseVersion(userAgent));
  57. }
  58. // 其他
  59. return new AppVersion(3, parseVersion(userAgent));
  60. }
  61. /**
  62. * 解析版本号
  63. * @param agent
  64. * @return
  65. * @author 献
  66. * @Time 2016年12月8日
  67. */
  68. private static int parseVersion(String agent) {
  69. int index = StringUtils.indexOf(agent, "/");
  70. agent = StringUtils.substring(agent, index+1);
  71. index = StringUtils.indexOf(agent, " ");
  72. index = index == -1 ? StringUtils.indexOf(agent, "(") : index;
  73. try {return Integer.parseInt(StringUtils.replace(StringUtils.substring(agent, 0, index), ".", ""));} catch (Exception e) {return 0;}
  74. }
  75. public String toString() {
  76. return "{\"os\":\"" + os + "\", \"version\":\"" + version + "\"}";
  77. }
  78. }