码迷,mamicode.com
首页 > 微信 > 详细

jQuery插件之微信分享

时间:2014-12-27 17:21:59      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:

  在这里给大家介绍一款的jQuery插件,它的目的是方便大家在使用微信分享页面时调用。如有不当之处,请大家不吝赐教.

; (function ($) {
    $.fn.extend({
        "weixinShare": function (options) {

            var firstImg = $("img:first");
            var firstLink = window.location.href;
            var firstTitle = $("title").html();

            options = $.extend({
                img_width: firstImg.height() || "200",//
                img_height: firstImg.width() || "200",//
                imgUrl: firstLink,
                lineLink: firstLink,
                shareTitle: firstTitle,
                descContent: firstTitle,
                appid: "aaaaaaaaaaaaaaaaaa",
                shareFriend: true,
                shareTimeline: true,
                shareWeibo: true,
                success: function () { return true; },
                err: function () { return false; }
            }, options);

            var datas = {
                "appid": options.appid,
                "img_url": options.imgUrl,
                "img_width": options.img_width,
                "img_height": options.img_height,
                "link": options.lineLink,
                "desc": options.descContent,
                "title": options.shareTitle
            };

            document.addEventListener(WeixinJSBridgeReady, function onBridgeReady() {
                // 发送给好友
                WeixinJSBridge.on(menu:share:appmessage, function (argv) {
                    if (options.shareFriend) shareOptions(appmessage);
                });
                // 分享到朋友圈
                WeixinJSBridge.on(menu:share:timeline, function (argv) {
                    if (options.shareTimeline) shareOptions(timeline);
                });
                //绑定‘分享到微博’按钮
                WeixinJSBridge.on(menu:share:weibo, function (argv) {
                    if (options.shareWeibo) shareOptions(weibo);
                });
            }, false);

            function shareOptions(shareType) {
                var inVokeType;
                switch (shareType) {
                    case "appmessage": inVokeType = sendAppMessage; break;
                    case "timeline": inVokeType = shareTimeline; break;
                    case "weibo": inVokeType = shareWeibo; break;
                }
                share(inVokeType);
            }

            function share(inVokeType) {
                WeixinJSBridge.invoke(inVokeType, datas, function (res) {
                    executeFun(res);
                })
            }

            function executeFun(res) {
                if (res.err_msg.indexOf(confirm) > -1 || res.err_msg.indexOf(ok) > -1) options.success.apply();
                else options.err.apply();
            }
        }
    });
})(jQuery);

 

jQuery插件之微信分享

标签:

原文地址:http://www.cnblogs.com/ManBlog/p/4188540.html

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