plusShare.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. (function() {
  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. plus.nativeUI.alert('无效的分享服务[' + id + ']');
  27. callback && callback(false);
  28. return;
  29. }
  30. var _share = function() {
  31. service.send(msg, function() {
  32. plus.nativeUI.toast("分享到\"" + service.description + "\"成功!");
  33. callback && callback(true);
  34. }, function(e) {
  35. plus.nativeUI.toast("分享到\"" + service.description + "\"失败!");
  36. callback && callback(false);
  37. })
  38. };
  39. if (service.authenticated) {
  40. _share(service, msg, callback);
  41. } else {
  42. service.authorize(function() {
  43. _share(service, msg, callback);
  44. }, function(e) {
  45. plus.nativeUI.alert("认证授权失败");
  46. callback && callback(false);
  47. })
  48. }
  49. };
  50. function openSystem(msg, callback) {
  51. if (plus.share.sendWithSystem) {
  52. plus.share.sendWithSystem(msg, function() {
  53. //TODO 系统分享暂不支持回调
  54. //callback && callback(true);
  55. }, function() {
  56. //TODO 系统分享暂不支持回调
  57. //callback && callback(false);
  58. });
  59. } else {
  60. callback && callback(false);
  61. }
  62. }
  63. var open = function(msg, callback) {
  64. if (shareServices.weixin && isWechatInstalled()) {
  65. plus.nativeUI.actionSheet({
  66. title: '分享到',
  67. cancel: "取消",
  68. buttons: [{
  69. title: "微信消息"
  70. }, {
  71. title: "微信朋友圈"
  72. }, {
  73. title: "更多分享"
  74. }]
  75. }, function(e) {
  76. var index = e.index;
  77. switch (index) {
  78. case 1: //分享到微信好友
  79. msg.extra = {
  80. scene: 'WXSceneSession'
  81. };
  82. share('weixin', msg, callback);
  83. break;
  84. case 2: //分享到微信朋友圈
  85. msg.title = msg.content;
  86. msg.extra = {
  87. scene: 'WXSceneTimeline'
  88. };
  89. share('weixin', msg, callback);
  90. break;
  91. case 3: //更多分享
  92. var url = msg.href ? ('( ' + msg.href + ' )') : '';
  93. msg.title = msg.title + url;
  94. msg.content = msg.content + url;
  95. openSystem(msg, callback);
  96. break;
  97. }
  98. })
  99. } else {
  100. //系统分享
  101. var url = msg.href ? ('( ' + msg.href + ' )') : '';
  102. msg.title = msg.title + url;
  103. msg.content = msg.content + url;
  104. openSystem(msg, callback);
  105. }
  106. };
  107. plusReady(init);
  108. window.plusShare = open;
  109. })();