通过user-agent判断用户的请求是用QQ、微信还是支付宝

2019-11-06 00:34:09 浏览数 (1)

通过user-agent判断用户是用QQ、微信还是支付宝打开链接或二维码

代码语言:javascript复制
function is_weixn_qq(){
var ua = navigator.userAgent.toLowerCase();
if(ua.match(/MicroMessenger/i)=="micromessenger") {
    return "weixin";
} else if (ua.match(/QQ/i) == "qq") {
    return "QQ";
}else if(ua.match(/AlipayClient/i) == 'alipayclient'){
    return "Alipay";
}else{
    ...
}

后台代码中判断请求来源

代码语言:javascript复制
//从httpServerletRequest中获得user-agent   
String userAgent = req.getHeader("user-agent"); 
if (userAgent != null && userAgent.contains("AlipayClient")) { 
  return "支付宝"; 
}else if (userAgent != null && userAgent.contains("MicroMessenger")) { 
  return "微信"; 
}else{ 
  return "其它"; 
}

0 人点赞