微信的授权及调用微信的注意事项

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
    <META HTTP-EQUIV="Expires" CONTENT="0">
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <title></title>
    <script type="text/javascript" src="js/zepto_1.1.3.js"></script>
</head>

<body style="color:gray;margin:0px;text-align:center;padding-top:20%" onload="GetParams()">
    <script type="text/javascript">
    ! function(factory) {
        if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {

            var target = module['exports'] || exports;
            factory(target);
        } else if (typeof define === 'function' && define['amd']) {
            define(['exports'], factory);
        } else {
            factory(window['activity'] = {});
        }

    }(function(BaseExports) {
        var activity = typeof BaseExports !== 'undefined' ? BaseExports : {};
        //获取地址栏查询参数
        activity.GetQueryString = function(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var newsid = window.location.search.substr(1).match(reg);
            if (newsid !== 'undefined' && newsid !== undefined && newsid !== null && newsid !== "") {
                localStorage.setItem(name, decodeURI(newsid[2]));
                return newsid[2];
            }
            return null;
        }
        activity.isBlank = function(value) {
            if (value === null || value === undefined || value === "null" || value === "undefined" || value === "") {
                return true;
            }
        }
    });

    localStorage.removeItem("openid");
    localStorage.removeItem("error");
    localStorage.removeItem("subscribe");




    var error = activity.GetQueryString("error");

    if (activity.isBlank("error")) {
        $.ajax({
            type: "post",
            headers: {
                "Content-Type": "application/json;charset=utf-8"
            },
            url: "http://***/openweixin/user/currentVisitCount",
            async: false,
            timeout: 1000,
            dataType: "application/json",
            success: function(data) {
                if (data === "false" || data === false) {
                    //TODO 提示 当前人数过多
                }

            },
            error: function(xhr, type) {
                //TODO 
            }
        });
    }

    if (error === "true") {


    } else {

        var openid = activity.GetQueryString("openid");
        if (activity.isBlank(openid)) {
            window.location.href = "http://****/openweixin/user/getCode?scope=snsapi_base&redirect_url=当前页面地址";
        }

        var subscribe = activity.GetQueryString("subscribe");
        if (subscribe === "true") {
            //TODO 跳转活动页面
        } else if (subscribe === "false") {
            //TODO 跳转关注公众号二维码
        }

    }

    ///-------------------------------------------------------------------------------------------------------------------
    ///
    ///获取页面JSSDK授权
    ///
    $.ajax({
        type: "post",
        url: "http://****/openweixin/jsapi/getJsapiSignature",
        async: false,
        dataType: 'json',
        contentType: "application/json;charset=utf-8", //发送信息至服务器时内容编码类型。
        data: "{\"url\":\"" + location.href.split('#')[0] + "\"}",
        success: function(data) {
            // var wxJsapiSignature = eval("(" + data + ")");
            console.info(data)

            //以下结构可以移出Ajax返回函数
            wx.config({

                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
                appId: data.appid, // 必填,公众号的唯一标识
                timestamp: parseInt(data.timestamp), // 必填,生成签名的时间戳
                nonceStr: data.noncestr, // 必填,生成签名的随机串
                signature: data.signature, // 必填,签名,见附录1
                jsApiList: [
                        'onMenuShareAppMessage'
                    ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
            });

            wx.ready(function() {
                // 2. 分享接口
                // 2.1 监听“分享给朋友”,按钮点击、自定义分享内容及分享结果接口
                wx.onMenuShareAppMessage({
                    title: '互联网之子', //标题
                    desc: '少时诵诗书所0', //描述
                    link: 'http://****/?parent=' + openid, //连接地址指打开分享时页面地址
                    imgUrl: 'http://****/jssdk/images/p2166127561.jpg', //图片
                    trigger: function(res) {
                        // 不要尝试在trigger中使用ajax异步请求修改本次分享的内容,因为客户端分享操作是一个同步操作,这时候使用ajax的回包会还没有返回
                        alert('用户点击发送给朋友');
                    },
                    success: function(res) {
                        alert('已分享');
                    },
                    cancel: function(res) {
                        alert('已取消');
                    },
                    fail: function(res) {
                        alert(JSON.stringify(res));
                    }
                });
            });

            wx.error(function(res) {
                //  alert(res.errMsg);
            });

        },
        error: function(data) {
            // alert(date)
        }
    })
    </script>
</body>

</html>

判断访问地址是否带有openid或error等参数

    如果带有相关参数则页面继续执行

    否则 1.请求验证当前访问人数  http://****/openweixin/user/currentVisitCount   
        2.请求授权地址  http://****&redirect_url=当前页面地址


调用微信时需要-调用微信的js--
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>

猜你喜欢

转载自blog.csdn.net/lyj168997/article/details/82630270