微信分享通用组件,用于微信浏览器内浏览网页的分享信息定义。

微信分享通用组件,用于微信浏览器内浏览网页的分享信息定义。
一、可定义的信息
1、分享时显示的LOGO;
2、分享LOGO的宽度;
3、分享LOGO的高度;
4、分享出去显示的标题(默认调用网页标题);
5、分享出去显示的描述(默认调用网页标题);
6、分享链接(默认为当前页面的URL)。
7、分享微信的APPID(一般为空)。

二、使用方法
1、引入微信分享组件js:

01 /*******************************
02  * Author:Mr.Think
03  * Description:微信分享通用代码
04  * 使用方法:_WXShare('分享显示的LOGO','LOGO宽度','LOGO高度','分享标题','分享描述','分享链接','微信APPID(一般不用填)');
05  *******************************/
06 function _WXShare(img,width,height,title,desc,url,appid){
07     //初始化参数
09     width=width||100;
10     height=height||100;
11     title=title||document.title;
12     desc=desc||document.title;
13     url=url||document.location.href;
14     appid=appid||'';
15     //微信内置方法
16     function _ShareFriend() {
17         WeixinJSBridge.invoke('sendAppMessage',{
18               'appid': appid,
19               'img_url': img,
20               'img_width': width,
21               'img_height': height,
22               'link': url,
23               'desc': desc,
24               'title': title
25               }, function(res){
26                 _report('send_msg', res.err_msg);
27           })
28     }
29     function _ShareTL() {
30         WeixinJSBridge.invoke('shareTimeline',{
31               'img_url': img,
32               'img_width': width,
33               'img_height': height,
34               'link': url,
35               'desc': desc,
36               'title': title
37               }, function(res) {
38               _report('timeline', res.err_msg);
39               });
40     }
41     function _ShareWB() {
42         WeixinJSBridge.invoke('shareWeibo',{
43               'content': desc,
44               'url': url,
45               }, function(res) {
46               _report('weibo', res.err_msg);
47               });
48     }
49     // 当微信内置浏览器初始化后会触发WeixinJSBridgeReady事件。
50     document.addEventListener('WeixinJSBridgeReady'functiononBridgeReady() {
51             // 发送给好友
52             WeixinJSBridge.on('menu:share:appmessage'function(argv){
53                 _ShareFriend();
54           });
55  
56             // 分享到朋友圈
57             WeixinJSBridge.on('menu:share:timeline'function(argv){
58                 _ShareTL();
59                 });
60  
61             // 分享到微博
62             WeixinJSBridge.on('menu:share:weibo'function(argv){
63                 _ShareWB();
64            });
65     }, false);
66 }

2、在页面底部或共用js中,初始化执行:

1 _WXShare('分享显示的LOGO','LOGO宽度','LOGO高度','分享标题','分享描述','分享链接','微信APPID(一般不用填)');

3、可通过动态定义【分享链接】,实现每次分享出去的链接不同;

原文地址:https://www.cnblogs.com/sunshq/p/4062702.html