网站接入微信扫码登录

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方法




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值