码迷,mamicode.com
首页 > Web开发 > 详细

JS授权

时间:2018-09-21 12:28:17      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:生成   user   lse   timestamp   sub   amp   url   lis   微信好友   

(function(){
    var origin_url = location.href;
    var oauth_url = ‘https://vx.mcilife.com/weixin/api/oauth/?callback=‘;

    var $_GET = (function(){
        var url = window.document.location.href.toString();
        var u = url.split("?");
        if(typeof(u[1]) == "string"){
            u = u[1].split("&");
            var get = {};
            for(var i in u){
                var j = u[i].split("=");
                get[j[0]] = j[1];
            }
            return get;
        } else {
            return {};
        }
    })();
    //localStroage
    var storage = {
        read:function(name){
            return localStorage.getItem(name);
        },
        write:function(name,value){
            localStorage.setItem(name, String(value));
        },
        rm:function(name){
            localStorage.removeItem(name);
        },
        clear:function(){
            localStorage.clear();
        }
    }
    //鐢ㄦ埛
    var user = {
        login:function(){

            if(storage.read(‘token‘) === null && !$_GET[‘token‘]){
                //token鍙傛暟銆佸瓨鍌ㄩ兘涓嶅瓨鍦? 鎺堟潈
                user.oauth();
            }else if($_GET[‘token‘] && storage.read(‘token‘) === null){
                //token瀛樺湪銆佸瓨鍌ㄤ笉瀛樺湪 鍐欏叆瀛樺偍 璺宠浆
                storage.write(‘token‘,$_GET[‘token‘]);
                user.setInfo();//鑾峰彇鍒皌oken涔嬪悗鏇存柊鐢ㄦ埛淇℃伅
                location.href = origin_url;
            }else if(storage.read(‘token‘) !== null){

            }
        },
        oauth:function(){
            var url = encodeURIComponent(origin_url);
            location.href= oauth_url + url;
        },
        setInfo:function(){
            if(storage.read(‘openid‘)
                && storage.read(‘unionid‘)
                && storage.read(‘nickname‘)
                && storage.read(‘headimgurl‘)
                && storage.read(‘isSubscribe‘)) return false;

            var isApp = getQueryString("isApp");
            if(isApp=="10000"||isApp=="10001"){

            }else if(isApp=="20000"){

                $.ajax({
                    url:‘https://vx.mcilife.com/weixin/api/getinfo/?token=‘+storage.read(‘token‘),
                    type:‘get‘,
                    async:false,
                    dataType:‘json‘,
                    success:function(data){
                        if(data.code == 0){

                            if(‘openid‘ in data.data) storage.write(‘openid‘,data.data.openid);
                            if(‘unionid‘ in data.data) storage.write(‘unionid‘,data.data.unionid);
                            if(‘nickname‘ in data.data) storage.write(‘nickname‘,data.data.nickname);
                            if(‘headimgurl‘ in data.data) storage.write(‘headimgurl‘,data.data.headimgurl);
                            if(‘isSubscribe‘ in data.data) storage.write(‘isSubscribe‘,data.data.isSubscribe);

                        }else{
                            user.oauth();//鑾峰彇淇℃伅澶辫触锛岄噸鏂版巿鏉?
                        }
                    }
                })
            }
        }
    };

    if(isApp=="20000"){
        user.login();
    }

    //授权成功
    var shareimg = ‘http://gcw.mcilife.com/Square/image/mc.jpg‘;//分享的图片
    $.ajax({
        url : "http://vx.mcilife.com/weixin/jssdk/share/",//分享接口地址
        type : ‘post‘,
        data:{url:location.href},
        cache: false,
        dataType : ‘json‘,
        success : function(data) {
            console.log(data);
            wx.config( {
                appId : data.data.appId,// 必填,公众号的唯一标识
                timestamp : data.data.timestamp,// 必填,生成签名的时间戳
                nonceStr : data.data.nonceStr,// 必填,生成签名的随机串
                signature : data.data.signature,// 必填,签名,见附录1
                jsApiList : [ ‘onMenuShareTimeline‘, ‘onMenuShareAppMessage‘, ‘showOptionMenu‘ ,‘onMenuShareQQ‘ ,‘onMenuShareWeibo‘ ]
                // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
            });

            wx.ready(function() {
                wx.showOptionMenu();//打开分享按钮

                //分享到朋友圈
                wx.onMenuShareTimeline( {
                    title : "微信朋友圈:山屿海广场舞活动",// 分享标题
                    desc: ‘活动快乐,广场舞......快来参加!!‘,  //描述
                    link : window.location.href,// 分享链接
                    imgUrl : shareimg,// 分享图标
                    success : function(data) {

                        $.ajax({
                            url: ‘http://‘+setIp+‘:8080/mcilife-inter/gcw/insertUserVoteNum‘,
                            type:‘post‘,
                            dataType:‘json‘,
                            cache: false,
                            data:{
                                userId:userId,
                                num:2,
                                isTime:0
                            },
                            success: function(data) {
                            },
                            error: function () {

                            }
                        })

                    },
                    cancel : function() {

                    }
                });

                //分享到朋友
                wx.onMenuShareAppMessage( {
                    title : ‘微信好友:山屿海广场舞活动‘,
                    desc : ‘活动快乐,广场舞......快来参加!!‘,
                    link : window.location.href,
                    imgUrl : shareimg,
                    success : function(data) {// 用户确认分享后执行的回调函数
                        $.ajax({
                            url: ‘http://‘+setIp+‘:8080/mcilife-inter/gcw/insertUserVoteNum‘,
                            type:‘post‘,
                            dataType:‘json‘,
                            cache: false,
                            data:{
                                userId:userId,
                                num:2,
                                isTime:0
                            },
                            success: function(data) {
                            },
                            error: function () {

                            }
                        })
                    },cancel : function() {}
                });

                wx.onMenuShareQQ({
                    title: ‘QQ:山屿海广场舞活动‘,
                    desc: ‘活动快乐,广场舞......快来参加!!‘,
                    link:window.location.href,
                    imgUrl: shareimg,
                    success: function () {
                        $.ajax({
                            url: ‘http://‘+setIp+‘:8080/mcilife-inter/gcw/insertUserVoteNum‘,
                            type:‘post‘,
                            dataType:‘json‘,
                            cache: false,
                            data:{
                                userId:userId,
                                num:2,
                                isTime:0
                            },
                            success: function(data) {
                            },
                            error: function () {

                            }
                        })
                    },
                    cancel: function () {

                    }
                });

                wx.onMenuShareWeibo({
                    title: ‘微博:山屿海广场舞活动‘,
                    desc: ‘活动快乐,广场舞......快来参加!!‘,
                    link: window.location.href,
                    imgUrl: shareimg,
                    success: function () {
                        $.ajax({
                            url: ‘http://‘+setIp+‘:8080/mcilife-inter/gcw/insertUserVoteNum‘,
                            type:‘post‘,
                            dataType:‘json‘,
                            cache: false,
                            data:{
                                userId:userId,
                                num:2,
                                isTime:0
                            },
                            success: function(data) {
                            },
                            error: function () {

                            }
                        })
                    },
                    cancel: function () {

                    }
                });
            })
        }
    })

})()

JS授权

标签:生成   user   lse   timestamp   sub   amp   url   lis   微信好友   

原文地址:https://www.cnblogs.com/xieyongbin/p/9685881.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!