h5+分享到微信、朋友圈代码示例
来源:互联网 发布:怎么查域名的ip地址 编辑:程序博客网 时间:2024/04/30 08:33
在使用分享功能的时候会莫名的分享失败,debug时发现是图片过大的问题。图片过大时ios平台上返回错误码-8,安卓上返回错误码-3(我测试是这样)因此如果第一次分享失败时递归重新获取默认图片进行分享,这样就可以分享成功了。如果使用七牛等云服务器存放图片,可以设置下载图片的大小,下载图片时在链接后加"!(你定义的大小或名称)",建议微信分享图片大小60*60,一般这样就能分享成功了
http://www.cnblogs.com/phillyx/
(function(window) { var Share={}; Share.info = { id: '', name: '', head_image: "_www/images/icon/A/144.png", introduce: '' }; /** * 更新分享服务 */ var shares = null; function getSerivces() { plus.share.getServices(function(s) { shares = {}; for (var i in s) { var t = s[i]; shares[t.id] = t; } }, function(e) { console.log("获取分享服务列表失败:" + e.message); }); }; function shareAction(id, ex) { var s = null; if (!id || !(s = shares[id])) { console.log("无效的分享服务!"); return; } if (s.authenticated) { console.log("---已授权---"); shareMessage(s, ex); } else { console.log("---未授权---"); //TODO 授权无法回调,有bug s.authorize(function() { console.log('授权成功...') shareMessage(s, ex); }, function(e) { console.log("认证授权失败:" + e.code + " - " + e.message); }); } }; var sharecount = 0; /** * 发送分享消息 * @param */ function shareMessage(s, ex) { plus.nativeUI.showWaiting(); setTimeout(plus.nativeUI.closeWaiting,5000);//TODO 5秒后自动关闭等待,否则如果用户分享出去后选择‘留在微信’,再手动回到app的时候,waiting无法关闭 var msg = { extra: { scene: ex } }; msg.href = "分享的网址" + "share?hid=" + Share.info.id; msg.title = "我在xxxx等你——" + Share.info._name; msg.content = Share.info.introduce; //取本地图片 var img = plus.io.convertAbsoluteFileSystem(Share.info.head_image.replace('file://', '')); console.log(img); msg.thumbs = [img]; if (sharecount > 0) { //如果本地图片过大,导致分享失败,递归时重新分享获取默认图片 msg.thumbs = ["_www/images/icon/A/144.png"]; } console.log(JSON.stringify(msg)); s.send(msg, function() { plus.nativeUI.closeWaiting(); var strtmp = "分享到\"" + s.description + "\"成功! "; console.log(strtmp); plus.nativeUI.toast(strtmp, { verticalAlign: 'center' }); sharecount = 0; }, function(e) { plus.nativeUI.closeWaiting(); if (e.code == -2) { plus.nativeUI.toast('已取消分享', { verticalAlign: 'center' }); sharecount = 0; } else if (e.code == -3 || e.code == -8) { console.log(e.code); if (++sharecount < 2) { //TODO 分享失败可能是图片过大的问题,递归取默认图片重新分享 shareMessage(s, ex); } else { sharecount = 0; plus.nativeUI.toast('分享失败', { verticalAlign: 'center' }); } }else{ console.error('分享失败:'+JSON.stringify(e)) } console.log("分享到\"" + s.description + "\"失败: " + e.code + " - " + e.message); }); }; function share() { bhref = true; var ids = [{ id: "weixin", ex: "WXSceneSession" }, { id: "weixin", ex: "WXSceneTimeline" }], bts = [{ title: "发送给微信好友" }, { title: "分享到微信朋友圈" }]; plus.nativeUI.actionSheet({ cancel: "取消", buttons: bts }, function(e) { var i = e.index; if (i > 0) { shareAction(ids[i - 1].id, ids[i - 1].ex); } } ); }; Share.share=share; window.Share = Share; mui.plusReady(function() { getSerivces(); }); })(window)
阅读全文
0 0
- h5+分享到微信、朋友圈代码示例
- h5 微信分享 分享网页到微信朋友圈 缩略图 php后台 签名算法----实战
- 微信分享代码,朋友圈分享代码
- php分享朋友圈的代码
- 定制微信分享朋友圈JS代码
- Jquery微信朋友圈分享代码
- 关于微信中分享到朋友圈代码
- 微信分享到朋友圈JS代码
- 微信分享到朋友圈JS代码
- Jquery微信朋友圈分享代码
- 在网页中添加“分享到微信朋友圈”按钮的代码分享
- 友盟分享到微信,朋友圈
- 分享图片+文字到微信朋友圈
- Android分享到微信好友、朋友圈
- 实现分享到微信,微博,朋友圈
- 分享到微信好友或朋友圈
- Android分享到微信好友、朋友圈
- 微信分享到朋友圈,分享给朋友JS代码
- 【原创整理】织梦dedecms仿站调用代码大全
- RabbitMQ之路由 direct方式
- solr 页面配置访问权限
- 友盟推送成功但是收不到
- hadoop框架学习
- h5+分享到微信、朋友圈代码示例
- 捕获海康威视IPCamera图像,转成OpenCV可以处理的图像(一)
- Android ToolBar的meun自定义位置遇到的几个坑
- go语言 自定义类型的时候ReadFull如何使用
- CentOS7 安装Zookeeper
- react .less 样式无法加载
- Spring Cloud微服务架构在互联网中应用
- 解决libc.so.6: version `GLIBC_2.14' not found问题
- Java 基础 一文看懂Java垃圾回收机制