当前位置:   article > 正文

微信公众号开发授权和分享模块脚本封装

文章 分享的时候 做一个显示授权 jq

 

 

 

      在引用jquery和微信JSSDK的基础上封装一个脚本 实现大量的H5网页分享授权

1.脚本

  

/*分享模块*/
var shareData = {
    title: '分享主标题',//分享标题
    desc: '分享副标题',//分享描述
    link: location.href,//分享链接,默认是当前页
    imgUrl: 'http://wx.qqauto.cn/html/2018/1/images/theme1.png'//分享图片,必须绝对路径
};

function WxShare(options) {
    //默认参数       
    shareData = $.extend(shareData, options);
    wx.onMenuShareTimeline({
        title: shareData.title, // 分享标题
        desc: shareData.desc, // 分享描述
        link: shareData.link, // 分享链接
        imgUrl: shareData.imgUrl, // 分享图标
        success: function () {
        },
        cancel: function () {
        }
    });
    wx.onMenuShareAppMessage({
        title: shareData.title, // 分享标题
        desc: shareData.desc, // 分享描述
        link: shareData.link, // 分享链接
        imgUrl: shareData.imgUrl, // 分享图标
        success: function () {
        },
        cancel: function () {
            // 用户取消分享后执行的回调函数
        }
    });
}
;

(function ($) {
    var jurl = location.href;
    if (jurl.indexOf('#'))
        jurl = jurl.split('#')[0];
    //jurl = encodeURIComponent(jurl);   

    $.ajax({
        url: '/CommonAPI/JSSDKHelper/15',
        data: { url: jurl },
        async: false,
        success: function (d) {
            if (d && d.success) {
                wx.config({
                    debug: false,
                    appId: d.appId,
                    timestamp: d.timestamp,
                    nonceStr: d.nonceStr,
                    signature: d.signature,
                    jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage']
                });
                wx.ready(function () {
                    WxShare();
                });
            }
        }
    });
})(jQuery)

/*授权模块*/
var _Mvar = {
    myGroup: '11',
    gid: 479,
    id: 15,
    // gid: 6,
    // id: 13,
    wxNum: "",
    username: "",
    phone: "",
    nickname: "",
    headimgurl: "",
    domain: '',
    data: '',
    msg: '可能是网络原因,请刷新页面重试!',
    cantest: '',
    jsoncallback: '',
    go: 0,
    uid: 0,
    cantest: '',
    wxid: '',
    wxName: '',
    myid: '',
    shareText: '福州万国长城哈弗猜车',
    shareText1: '',
    shareText2: '',
    prize: '万精',
    chance: 0,
    times: 0,
    num: 0,
    isShare: 0,
    loadImg: 'http://wx.qqauto.cn/html/upcoin/power/22/images/img2.png'
};


function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
}

_Mvar.wxNum = getQueryString("uid");
var hostname = location.hostname;
var myUrl = "http://" + hostname + location.pathname + "?uid=" + _Mvar.wxNum + '&gid=' + _Mvar.gid;
function init2() {
    $.ajax({
        type: "get",
        url: "http://wx.qqauto.cn/CommonApi/OAuth2?id=" + _Mvar.id + "&gid=" + _Mvar.gid + "&myUrl=" + encodeURIComponent(myUrl) + "&_r=" + Math.random(),
        dataType: "json",
        async: false,
        success: function (data) {
            if (data.success == true) {
                _Mvar.wxid = data.wxnum;
                _Mvar.nickname = data.nickname;
                _Mvar.headimgurl = data.headimg;
            } else {
                window.location.href = data.msg;
            }
        },
        timeout: 15000,
        error: function (xhr, type) {
            alert("网络异常,请刷新重试。");
        }
    });
}
var isLocal = hostname && hostname.indexOf('wx.qqauto.cn') > -1;
if (isLocal)
    init2();
else {
    _Mvar.domain = 'http://192.168.5.43:8022',
   _Mvar.cantest = 'can',
   _Mvar.wxid = '',
   _Mvar.jsoncallback = '?jsoncallback=?';
    _Mvar.nickname = '花生';
    _Mvar.gid = 14;
    _Mvar.id = 13;
}

2.创建测试DEMO

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="/Scripts/jquery-1.10.2.js"></script>
    <script language="javascript" type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
    <script src="/Scripts/yhz.js"></script>

</head>
<body>
    <button id="btn1">修改主副标题</button>
    <button id="btn2">修改链接地址Link</button>
</body>
</html>
<script>
    $(function () {
        //修改主副标题
        $("#btn1").click(function () {
            shareData.title = "哈哈哈哈";
            shareData.desc = "嘿嘿";
            WxShare(shareData);
        })
        //修改链接为百度
        $("#btn2").click(function () {
            shareData.link = location.href + "?test=2";
            WxShare(shareData);
        })
    })
</script>

3.分享

ps:引用的层级

  <script src="/Scripts/jquery-1.10.2.js"></script>
    <script language="javascript" type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
    <script src="/Scripts/yhz.js"></script>


转载于:https://www.cnblogs.com/CallmeYhz/p/8889140.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/70713
推荐阅读
相关标签
  

闽ICP备14008679号