1.引入Js <script src="https://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script>
2.function wxLoad() {
var url = window.location.pathname + window.location.search;
var objd = new WxLogin({
id: "wxlogin",
appid: "网站APPID",
scope: "snsapi_login",
redirect_uri:扫码后跳入的网址,
state: "STATE",
style: "black",
href: "https://www.ansying.com/css/weixin/weixin.css"
});
}
PS:在页面加载中加入wxLoad方法 用来绑定二维码
3.function WX_Login() {
var code = GetRequest()['code'];
var href = window.location.href;
if (href.indexOf('&code') > 0) {
href = window.location.href.split('&code')[0];
}
else if (href.indexOf('?code') > 0) {
href = window.location.href.split('?code')[0];
}
if (code != "undefined" && code != null) {
$.ajax({
type: "Post",
url: "Controller方法",
data: { "code": code, "href": href },
success: function (result) {
},
error: function () {
}
});
}
}
PS:加入到页面加载中,扫码调用
后台:
public string wxt(string code, string href)
{
try
{
string APPID = "网站APPID";
string secret = "密码";
string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + APPID + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
//请求url以获取数据
string result = HttpService.Get(url);
JsonData jd = JsonMapper.ToObject(result);
if (((IDictionary)jd).Contains("errmsg"))
{
WriteContent("errmsg" + (string)jd["errmsg"]);
WriteContent("href" + href);
return href;
}
string unionid = (string)jd["unionid"];
string openid = (string)jd["openid"];
string access_token = (string)jd["access_token"];
//判断是否有UNIONID
if (unionid != "")
{
}
else
{
return "0";
}
}
catch (Exception e)
{
}
}
==============================================================================================
微信中调用
1.判断是否为微信浏览器
function Isweixn() {
var ua = navigator.userAgent.toLowerCase();
if (ua.match(/MicroMessenger/i) == "micromessenger") {
return true;
} else {
return false;
}
}
2.二维码换成a标签加链接
https://open.weixin.qq.com/connect/oauth2/authorize?appid=网站APPID&redirect_uri=后台方法?href='" + herf + "'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"
PS:可以传参数href
调用后台如上面的wxt方法