微信公众号开发 常用脚本累计

关注指定公众号:

//gh_ef65912f88f1 为被添加者的微信ID
WeixinJSBridge.invoke("addContact", {webtype: "1",username: 'gh_ef65912f88f1'}, function(e) {
            WeixinJSBridge.log(e.err_msg);
            //e.err_msg:add_contact:added 已经添加
            //e.err_msg:add_contact:cancel 取消添加
            //e.err_msg:add_contact:ok 添加成功
            if(e.err_msg == 'add_contact:added' || e.err_msg == 'add_contact:ok'){
                //关注成功,或者已经关注过
            }
        })

 分享到朋友圈:

WeixinJSBridge.invoke('shareTimeline',{
        "img_url": "http://example.com/example.png",
        //"img_width": "640",
        //"img_height": "640",
        "link": "http://example.com",
        "desc": "描述",
        "title": "标题"
    },function(res){
        // 返回res.err_msg,取值
        // share_timeline:cancel 用户取消
        // share_timeline:fail 发送失败
        // share_timeline:confirm 发送成功
        WeixinJSBridge.log(res.err_msg);
         
        if(res.err_msg == 'share_timeline:confirm'){
            //转发成功
        };
    });
function weixinShareTimeline(title,desc,link,imgUrl){
WeixinJSBridge.invoke(‘shareTimeline’,{
“img_url”:imgUrl,
//”img_width”:”640″,
//”img_height”:”640″,
“link”:link,
“desc”: desc,
“title”:title
});
}

发送给好友:

function weixinSendAppMessage(title,desc,link,imgUrl){
WeixinJSBridge.invoke(‘sendAppMessage’,{
//”appid”:appId,
“img_url”:imgUrl,
//”img_width”:”640″,
//”img_height”:”640″,
“link”:link,
“desc”:desc,
“title”:title
});
}

分享到腾讯微博:

function weixinShareWeibo(title,link){
WeixinJSBridge.invoke(‘shareWeibo’,{
“content”:title + link,
“url”:link
});
}

关注指定的微信号:

function weixinAddContact(name){
WeixinJSBridge.invoke(“addContact”, {webtype: “1″,username: name},
function(e) {
WeixinJSBridge.log(e.err_msg);
//e.err_msg:add_contact:added 已经添加
//e.err_msg:add_contact:cancel 取消添加
//e.err_msg:add_contact:ok 添加成功
if(e.err_msg
== ‘add_contact:added’ || e.err_msg ==
‘add_contact:ok’){
//关注成功,或者已经关注过
}
})
}
var imgUrl = 'http://su.bdimg.com/static/superplus/img/logo_white.png';//这里是分享的时候的那个图片
var lineLink = 'http://baidu.com';//这个是分享的网址
var descContent = "这个操作的介绍,没发现他有啥用";
var shareTitle = '这是题目题目题目';
var appid = 'wxc9937e3a66af6dc8';  //这里写开发者接口里的appid
function shareFriend() {
    WeixinJSBridge.invoke('sendAppMessage',{
                            "appid": appid,
                            "img_url": imgUrl,
                            "img_width": "640",
                            "img_height": "640",
                            "link": lineLink,
                            "desc": descContent,
                            "title": shareTitle
                            }, function(res) {
                            _report('send_msg', res.err_msg);
                            })
}
function shareTimeline() {
    WeixinJSBridge.invoke('shareTimeline',{
                            "img_url": imgUrl,
                            "img_width": "640",
                            "img_height": "640",
                            "link": lineLink,
                            "desc": descContent,
                            "title": shareTitle
                            }, function(res) {
                            _report('timeline', res.err_msg);
                            });
}
function shareWeibo() {
    WeixinJSBridge.invoke('shareWeibo',{
                            "content": descContent,
                            "url": lineLink,
                            }, function(res) {
                            _report('weibo', res.err_msg);
                            });
}
// 当微信内置浏览器完成内部初始化后会触发WeixinJSBridgeReady事件。
document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
        // 发送给好友
        WeixinJSBridge.on('menu:share:appmessage', function(argv){
            shareFriend();
            });
        // 分享到朋友圈
        WeixinJSBridge.on('menu:share:timeline', function(argv){
            shareTimeline();
            });
        // 分享到微博
        WeixinJSBridge.on('menu:share:weibo', function(argv){
            shareWeibo();
            });
        }, false);
原文地址:https://www.cnblogs.com/xyzhuzhou/p/4094207.html