(1)首页要有一个自己的微信测试号的appid和秘钥
(2)公司里都是后台传code(接口),获取openid(接口)
请求code接口:/Wechat/GetUserInfo/getCode
//判断是否是移动设备打开
function Isweixin(){
var userAgentInfo = navigator.userAgent.toLowerCase();
// var demainUrl ='http://faw-vw.ftms-wechat.com';
var returnUrl =chineseTransfer(window.location);
if (userAgentInfo.match(/MicroMessenger/i) == "micromessenger") {
var code = getUrlParam("code");
if (!code) {
window.location = "/Wechat/GetUserInfo/getCode?callbackurl="+returnUrl;
}else{
getOpenId(code);
}
}else {
//网页打开的
// window.location = demainUrl+"/Wechat/GetUserInfo/getCode?callbackurl="+returnUrl;
window.location = "/Wechat/GetUserInfo/getCode?callbackurl="+returnUrl;
}
} //获取code
function getUrlParam(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]);
return null;
} // 请求openid
getOpenId = function (code) {
$.ajax({
type: 'POST',
url:"/Wechat/GetUserInfo/getUserInfo",
data: { code: code },
success: function (res) {
var data = res.data;
console.log(data);
var param = {
ass_openid :data.openid,
ass_name :data.nickname,
ass_head_img : data.headimgurl
};
localStorage.setItem("fromOpenid",param.ass_openid);
});
}