background

Determining ua to determine different execution strategies is a business necessity. Here is how to determine UA in detail.

Note: The first two are the code of the simple version of JUDGING UA given by wechat official.

Distinguish between wechat and wechat codes

var isWXWork = ua.match(/wxwork/i) == 'wxwork'; var isWeixin = ! isWXWork && ua.match(/micromessenger/i) == 'micromessenger';Copy the code

Distinguish between mobile and PC



var isMobile = false;
var isDesktop = false;
if(navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|IEMobile)/i)) {
          isMobile = true;
    } else {
          isDesktop = true;
}
Copy the code

Personal details return to ua source

* @param {*} ua * @returns */ export function getSource(UA) {const isWx = ua.match(/MicroMessenger/i); let source = ""; if (isWx && !! isWx.length) { if (ua.includes("wxwork")) { source = "wxwork"; } else { source = "wx"; } } else if (ua.includes("DingTalk")) { source = "dd"; } else { source = "other"; } const isPhone = /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS |Symbian|Windows Phone)/i; if (! (ua.match(isPhone) && ua.match(isPhone).length)) { if (source ! == "other") { source += "-pc"; } else { source = "pc"; } } else if (source === "other") { source = "h5"; } return source; }Copy the code

Parameter description:

logo instructions
wxwork Enterprise wechat mobile terminal
wxwork-pc Enterprise wechat computer terminal
wx Wechat mobile app
wx-pc Wechat computer terminal
dd Nail nail mobile terminal
dd-pc Nail nail computer end
h5 Mobile terminal (except above)
pc The computer side