PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > HBuilderX教程 > 正文

    HBuilder mui h5+分享到微信、朋友圈代码示例

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:在使用分享功能的时候会莫名的分享失败,debug时发现是图片过大的问题。图片过大时ios平台上返回错误码-8,安卓上返回错误码-3(我测试是这...
    在使用分享功能的时候会莫名的分享失败,debug时发现是图片过大的问题。
    图片过大时ios平台上返回错误码-8,安卓上返回错误码-3(我测试是这样)
    因此如果第一次分享失败时递归重新获取默认图片进行分享,这样就可以分享成功了。
    如果使用七牛等云服务器存放图片,可以设置下载图片的大小,下载图片时在链接后加"!(你定义的大小或名称)",建议微信分享图片大小60*60,一般这样就能分享成功了

    1. (function(window) { 
    2.     var Share={}; 
    3.     Share.info = { 
    4.         id: ''
    5.         name: ''
    6.         head_image: "_www/images/icon/A/144.png"
    7.         introduce: '' 
    8.     }; 
    9.     /** 
    10.      * 更新分享服务 
    11.      */ 
    12.     var shares = null
    13.  
    14.     function getSerivces() {         
    15.         plus.share.getServices(function(s) { 
    16.              
    17.             shares = {}; 
    18.             for (var i in s) { 
    19.                 var t = s[i]; 
    20.                 shares[t.id] = t; 
    21.             } 
    22.         }, function(e) { 
    23.             console.log("获取分享服务列表失败:" + e.message); 
    24.         }); 
    25.     }; 
    26.  
    27.     function shareAction(id, ex) { 
    28.         var s = null
    29.          
    30.         if (!id || !(s = shares[id])) { 
    31.             console.log("无效的分享服务!"); 
    32.             return
    33.         } 
    34.         if (s.authenticated) { 
    35.             console.log("---已授权---"); 
    36.             shareMessage(s, ex); 
    37.         } else { 
    38.             console.log("---未授权---"); 
    39.             //TODO 授权无法回调,有bug 
    40.             s.authorize(function() {     
    41.                 console.log('授权成功...')               
    42.                 shareMessage(s, ex); 
    43.             }, function(e) {         
    44.                 console.log("认证授权失败:" + e.code + " - " + e.message); 
    45.             }); 
    46.         } 
    47.     }; 
    48.     var sharecount = 0; 
    49.     /** 
    50.      * 发送分享消息 
    51.      * @param 
    52.      */ 
    53.     function shareMessage(s, ex) { 
    54.         plus.nativeUI.showWaiting(); 
    55.         var msg = { 
    56.             extra: { 
    57.                 scene: ex 
    58.             } 
    59.         }; 
    60.         msg.href = "分享的网址" + "share?hid=" + Share.info.id; 
    61.         msg.title = "我在xxxx等你——" + Share.info._name; 
    62.         msg.content = Share.info.introduce; 
    63.         //取本地图片 
    64.         var img = plus.io.convertAbsoluteFileSystem(Share.info.head_image.replace('file://''')); 
    65.         console.log(img); 
    66.         msg.thumbs = [img]; 
    67.         if (sharecount > 0) { 
    68.             //如果本地图片过大,导致分享失败,递归时重新分享获取默认图片 
    69.             msg.thumbs = ["_www/images/icon/A/144.png"]; 
    70.         } 
    71.         console.log(JSON.stringify(msg)); 
    72.         s.send(msg, function() { 
    73.             plus.nativeUI.closeWaiting(); 
    74.             var strtmp = "分享到\"" + s.description + "\"成功! "
    75.             console.log(strtmp); 
    76.             plus.nativeUI.toast(strtmp, { 
    77.                 verticalAlign: 'center' 
    78.             }); 
    79.             sharecount = 0; 
    80.         }, function(e) { 
    81.             plus.nativeUI.closeWaiting();            
    82.             if (e.code == -2) { 
    83.                 plus.nativeUI.toast('已取消分享', { 
    84.                     verticalAlign: 'center' 
    85.                 }); 
    86.                 sharecount = 0; 
    87.             } else if (e.code == -3 || e.code == -8) { 
    88.                 console.log(e.code); 
    89.                 if (++sharecount < 2) { 
    90.                     //TODO 分享失败可能是图片过大的问题,递归取默认图片重新分享 
    91.                     shareMessage(s, ex); 
    92.                 } else { 
    93.                     sharecount = 0; 
    94.                     plus.nativeUI.toast('分享失败', { 
    95.                         verticalAlign: 'center' 
    96.                     }); 
    97.                 } 
    98.             }else
    99.                 console.error('分享失败:'+JSON.stringify(e)) 
    100.             } 
    101.             console.log("分享到\"" + s.description + "\"失败: " + e.code + " - " + e.message); 
    102.         }); 
    103.     }; 
    104.  
    105.     function share() { 
    106.         bhref = true
    107.         var ids = [{ 
    108.                 id: "weixin"
    109.                 ex: "WXSceneSession" 
    110.             }, { 
    111.                 id: "weixin"
    112.                 ex: "WXSceneTimeline" 
    113.             }], 
    114.             bts = [{ 
    115.                 title: "发送给微信好友" 
    116.             }, { 
    117.                 title: "分享到微信朋友圈" 
    118.             }]; 
    119.         plus.nativeUI.actionSheet({ 
    120.                 cancel: "取消"
    121.                 buttons: bts 
    122.             }, 
    123.             function(e) { 
    124.                 var i = e.index; 
    125.                 if (i > 0) { 
    126.                     shareAction(ids[i - 1].id, ids[i - 1].ex); 
    127.                 } 
    128.             } 
    129.         ); 
    130.     }; 
    131.     Share.share=share; 
    132.     window.Share = Share; 
    133.     mui.plusReady(function() {       
    134.         getSerivces(); 
    135.     });  
    136. })(window) 

     

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-146-2581-1.html
    相关热词搜索: