plusShare.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. (function plusShare() {
  2. var plusReady = function(callback) {
  3. if(window.plus) {
  4. callback();
  5. } else {
  6. document.addEventListener('plusready', callback);
  7. }
  8. }
  9. var shareServices = {};
  10. var init = function() {
  11. plus.share.getServices(function(services) {
  12. for(var i = 0, len = services.length; i < len; i++) {
  13. shareServices[services[i].id] = services[i];
  14. }
  15. });
  16. };
  17. var isWechatInstalled = function() {
  18. return plus.runtime.isApplicationExist && plus.runtime.isApplicationExist({
  19. pname: 'com.tencent.mm',
  20. action: 'weixin://'
  21. });
  22. };
  23. function share(id, msg, callback) {
  24. var service = shareServices[id];
  25. if(!service) {
  26. callback && callback(false);
  27. return;
  28. }
  29. var _share = function() {
  30. service.send(msg, function() {
  31. plus.nativeUI.toast("分享到\"" + service.description + "\"成功!");
  32. callback && callback(true);
  33. }, function(e) {
  34. plus.nativeUI.toast("分享到\"" + service.description + "\"失败!");
  35. callback && callback(false);
  36. })
  37. };
  38. if(service.authenticated) {
  39. _share(service, msg, callback);
  40. } else {
  41. service.authorize(function() {
  42. _share(service, msg, callback);
  43. }, function(e) {
  44. callback && callback(false);
  45. })
  46. }
  47. };
  48. function openSystem(msg, callback) {
  49. if(plus.share.sendWithSystem) {
  50. plus.share.sendWithSystem(msg, function() {
  51. //TODO 系统分享暂不支持回调
  52. //callback && callback(true);
  53. }, function() {
  54. //TODO 系统分享暂不支持回调
  55. //callback && callback(false);
  56. });
  57. } else {
  58. callback && callback(false);
  59. }
  60. }
  61. var open = function(msg, callback) {
  62. /**
  63. *如下情况直接打开系统分享
  64. * 1、未配置微信分享通道
  65. * 2、用户手机未安装威胁你
  66. * 3、360浏览器下
  67. */
  68. if(shareServices.weixin && isWechatInstalled() && !/360\sAphone/.test(navigator.userAgent)) {
  69. plus.nativeUI.actionSheet({
  70. title: '分享到',
  71. cancel: "取消",
  72. buttons: [{
  73. title: "微信消息"
  74. }, {
  75. title: "微信朋友圈"
  76. }, {
  77. title: "更多分享"
  78. }]
  79. }, function(e) {
  80. var index = e.index;
  81. switch(index) {
  82. case 1: //分享到微信好友
  83. msg.extra = {
  84. scene: 'WXSceneSession'
  85. };
  86. share('weixin', msg, callback);
  87. break;
  88. case 2: //分享到微信朋友圈
  89. msg.title = msg.content;
  90. msg.extra = {
  91. scene: 'WXSceneTimeline'
  92. };
  93. share('weixin', msg, callback);
  94. break;
  95. case 3: //更多分享
  96. // var url = msg.href ? ('( ' + msg.href + ' )') : '';
  97. msg.title = msg.title
  98. msg.content = msg.content
  99. // msg.pictuers = msg.pictuers
  100. // msg.type = msg.type
  101. openSystem(msg, callback);
  102. break;
  103. }
  104. })
  105. } else {
  106. //系统分享
  107. var url = msg.href ? ('( ' + msg.href + ' )') : '';
  108. msg.title = msg.title + url;
  109. msg.content = msg.content + url;
  110. openSystem(msg, callback);
  111. }
  112. };
  113. plusReady(init);
  114. window.plusShare = open;
  115. })();
  116. export default plusShare;