一、确定微信浏览器的user-agent
// js 获取是否为微信浏览器访问
var is_we = (function(){return navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1})();
二、引入设置cookie的方法
这里可以将其封装到js中,根据判断第一步结果进行处理是否加载
//设置cookie
function setCookie(key, value, expiredays){
var exdate = new Date();
exdate.setDate(exdate.getDate() + expiredays);
document.cookie = key + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString());
}
//取回cookie
function getCookie(key){
if(document.cookie.length > 0){
c_start = document.cookie.indexOf(key + "=");
if(c_start != -1){
c_start = c_start + key.length + 1;
c_end = document.cookie.indexOf(";", c_start);
if(c_end == -1){
c_end = document.cookie.length;
}
return unescape(document.cookie.substring(c_start, c_end));
}
}
return "";
}
感谢沧海龙腾LV博主编写的微信内置浏览器缓存第三方网页数据到Cookie
三、异步登录提交之后,使用下面的方法进行处理
// 如果是微信端,将用户名和密码存入cookie中,注意一定要登录成功后处理
if (!(typeof(is_we) == "undefined")) {
//设置cookie
setCookie("we", loginName+"@leemeea@"+loginPWD, 356*24*60*60*1000);
}
四、页面加载的时候做下处理
// 如果是微信端
if (!(typeof(is_we) == "undefined")) {
$(function(){
// 获取微信中cookie的账号和密码
var we_coo = getCookie("we");
// 判定账号和密码是否在cookie中
if(!we_coo.length < 1 && we_coo.indexOf("@leemeea@") != -1){
// 有cookie,那就处理到登录框中吧
$("#loginname").val(we_coo.split("@leemeea@")[0]);
$("#loginpwd").val(we_coo.split("@leemeea@")[1]);
}
})
}