plusShare.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. console.log("认证授权失败");
  45. callback && callback(false);
  46. })
  47. }
  48. };
  49. function openSystem(msg, callback) {
  50. if(plus.share.sendWithSystem) {
  51. plus.share.sendWithSystem(msg, function() {
  52. //TODO 系统分享暂不支持回调
  53. //callback && callback(true);
  54. }, function() {
  55. //TODO 系统分享暂不支持回调
  56. //callback && callback(false);
  57. });
  58. } else {
  59. callback && callback(false);
  60. }
  61. }
  62. var open = function(msg, callback) {
  63. /**
  64. *如下情况直接打开系统分享
  65. * 1、未配置微信分享通道
  66. * 2、用户手机未安装威胁你
  67. * 3、360浏览器下
  68. */
  69. if(shareServices.weixin && isWechatInstalled() && !/360\sAphone/.test(navigator.userAgent)) {
  70. plus.nativeUI.actionSheet({
  71. title: '分享到',
  72. cancel: "取消",
  73. buttons: [{
  74. title: "微信消息"
  75. }, {
  76. title: "微信朋友圈"
  77. }, {
  78. title: "更多分享"
  79. }]
  80. }, function(e) {
  81. var index = e.index;
  82. switch(index) {
  83. case 1: //分享到微信好友
  84. msg.extra = {
  85. scene: 'WXSceneSession'
  86. };
  87. share('weixin', msg, callback);
  88. break;
  89. case 2: //分享到微信朋友圈
  90. msg.title = msg.content;
  91. msg.extra = {
  92. scene: 'WXSceneTimeline'
  93. };
  94. share('weixin', msg, callback);
  95. break;
  96. case 3: //更多分享
  97. // var url = msg.href ? ('( ' + msg.href + ' )') : '';
  98. msg.title = msg.title
  99. msg.content = msg.content
  100. // msg.pictuers = msg.pictuers
  101. // msg.type = msg.type
  102. console.log(msg)
  103. openSystem(msg, callback);
  104. break;
  105. }
  106. })
  107. } else {
  108. //系统分享
  109. var url = msg.href ? ('( ' + msg.href + ' )') : '';
  110. msg.title = msg.title + url;
  111. msg.content = msg.content + url;
  112. openSystem(msg, callback);
  113. }
  114. };
  115. plusReady(init);
  116. window.plusShare = open;
  117. })();
  118. export default plusShare;