收藏
评论

模拟UA

/**
 * 模拟一个小程序的UA,至少在能让一些统计平台区分是ios还是android
 * iphonex ua: Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1
 * galaxy ua : Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Mobile Safari/537.36
 * ua列表: http://www.cnblogs.com/yannisc/articles/3284231.html
 */
export function getUserAgent() {
  let systemInfo = wx.getSystemInfoSync() || {};
  let model = systemInfo.model; // 手机品牌 如iPhone 6S Plus
  let system = systemInfo.system || ""; // 系统信息 如 iOS 10.3.3
  let systemVersion = system.split(" ")[1] || "";
  let platform = systemInfo.platform; // ios or android
  if (platform === "ios") {
    let iosSystemVersion = systemVersion.replace(/\./g, "_");
    return `Mozilla/5.0 (iPhone; CPU iPhone OS ${iosSystemVersion} like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1`;
  } else {
    return `Mozilla/5.0 (Linux; Android ${systemVersion}; ${model} Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Mobile Safari/537.36`;
  }
}
最后一次编辑于  2018-11-30  (未经腾讯允许,不得转载)
赞 0
收藏