share.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import http from './http';
  2. var share =
  3. {
  4. wechat : false,
  5. url : '',
  6. project : '',
  7. token : '',
  8. param : {
  9. title : '',
  10. pic : '',
  11. content : '',
  12. link : '',
  13. },
  14. wx : {},
  15. //微信提醒
  16. wxTip : function(type) {
  17. var wx = this.is_weixin();
  18. if (wx) {
  19. var img = 'live_weixin';
  20. if (type) {
  21. img = 'weixin-guide';
  22. }
  23. var tip = '<div id="weixin-tip" style="position: fixed; left:0; top:0; background: rgba(0,0,0,0.8); filter:alpha(opacity=80); width: 100%; height:100%; z-index: 100;" onclick="document.getElementById(\'weixin-tip\').remove()"><p style="text-align: center; margin-top: 10%; padding:0 5%;"><img src="static/dever/'+img+'.png" alt="微信打开" style="max-width: 100%; height: auto;"/></p></div>';
  24. this.html(tip);
  25. return true;
  26. }
  27. return false;
  28. },
  29. //通用的分享功能
  30. share : function(provider, scene, type, data, success, error) {
  31. if (this.source == 'h5') {
  32. var weixin = this.wxTip(1);
  33. if (!weixin) {
  34. this.alert('请点击复制按钮');
  35. }
  36. } else {
  37. var config = {
  38. provider: provider,
  39. type: type,
  40. scene: scene,
  41. href: data.link,
  42. title: data.title,
  43. summary: data.content,
  44. imageUrl: data.pic,
  45. success: function(res) {
  46. if (success) {
  47. success(res);
  48. } else {
  49. console.log("success:" + JSON.stringify(res));
  50. }
  51. },
  52. fail: function(err) {
  53. if (error) {
  54. success(err);
  55. } else {
  56. console.log("fail:" + JSON.stringify(err));
  57. }
  58. }
  59. };
  60. if (data.media) {
  61. config.mediaUrl = media;
  62. }
  63. if (this.source == 'applet') {
  64. if (!data.path) {
  65. console.log('data.path未定义!');
  66. return;
  67. }
  68. if (!data.applet_id) {
  69. console.log('data.applet_id未定义!');
  70. return;
  71. }
  72. config.miniProgram = {};
  73. config.miniProgram.id = data.applet_id;
  74. config.miniProgram.path = data.path;
  75. config.miniProgram.type = 0;
  76. config.miniProgram.webUrl = data.link;
  77. }
  78. uni.share(config);
  79. }
  80. },
  81. init : function(weixin, wx, project, host, param)
  82. {
  83. var self = this;
  84. self.wx = wx;
  85. self.wechat = weixin;
  86. self.project = project;
  87. self.url = host;
  88. if (!host) {
  89. self.url = config.share.host;
  90. }
  91. self.token = http.getToken();
  92. self.param = param;
  93. self.param.timelineCallback = function() {};
  94. self.param.sendMessageCallback = function() {};
  95. self.param.shareQQCallback = function() {};
  96. if (!self.param.source_link) {
  97. self.param.source_link = location.href;
  98. }
  99. if (self.wechat) {
  100. self.wechatInit();
  101. }
  102. self.reflux();
  103. }
  104. ,wechatInit: function()
  105. {
  106. var self = this;
  107. var url = this.url + 'init?callback=?';
  108. var param = this.param;
  109. var project = this.project;
  110. var options = {};
  111. options.project = project;
  112. options.url = location.href.split('#')[0];
  113. options.noloading = 1;
  114. options.token = this.token;
  115. http.request('post', url, options, function(data) {
  116. self.wx.config({
  117. //debug:true,
  118. appId: data.appId,
  119. timestamp: data.timestamp,
  120. nonceStr: data.nonceStr,
  121. signature: data.signature,
  122. jsApiList: [
  123. 'onMenuShareTimeline',
  124. 'onMenuShareAppMessage',
  125. 'onMenuShareQQ',
  126. 'updateAppMessageShareData',
  127. 'updateTimelineShareData'
  128. ]
  129. });
  130. self.wx.ready(function() {
  131. self.wx.updateAppMessageShareData({
  132. title: param.title,
  133. desc: param.content,
  134. link: param.source_link,
  135. imgUrl: param.pic,
  136. success: function(res) {
  137. },
  138. });
  139. self.wx.updateTimelineShareData({
  140. title: param.title,
  141. link: param.source_link,
  142. imgUrl: param.img,
  143. success: function(res) {
  144. },
  145. });
  146. self.wx.onMenuShareAppMessage({
  147. title: param.title,
  148. desc: param.content,
  149. link: param.source_link,
  150. imgUrl: param.pic,
  151. success: function(res) {
  152. self.shareLog(1, 1);
  153. },
  154. cancel: function(res) {
  155. self.shareLog(1, 2);
  156. },
  157. fail: function(res) {
  158. self.shareLog(1, 3);
  159. }
  160. });
  161. self.wx.onMenuShareTimeline({
  162. title: param.title,
  163. link: param.source_link,
  164. imgUrl: param.img,
  165. success: function(res) {
  166. self.shareLog(2, 1);
  167. },
  168. cancel: function(res) {
  169. self.shareLog(2, 2);
  170. },
  171. fail: function(res) {
  172. self.shareLog(2, 3);
  173. }
  174. });
  175. self.wx.onMenuShareQQ({
  176. title: param.title,
  177. link: param.source_link,
  178. imgUrl: param.img,
  179. success: function(res) {
  180. self.shareLog(3, 1);
  181. },
  182. cancel: function(res) {
  183. self.shareLog(3, 2);
  184. },
  185. fail: function(res) {
  186. self.shareLog(3, 3);
  187. }
  188. });
  189. });
  190. });
  191. }
  192. ,reflux: function() {
  193. var refer = document.referrer,
  194. url = encodeURIComponent(document.location.href),
  195. param = location.search.substr(1),
  196. ua = encodeURIComponent(navigator.userAgent),
  197. project = this.project,
  198. token = this.token,
  199. wechat = this.wechat;
  200. if (param.indexOf("tsina-") > -1 || param.indexOf("timeline") > -1 || param.indexOf("singlemessage") > -1 || param.indexOf("groupmessage") > -1) {
  201. var url = this.url + 'reflux&callback=?' + '&project='+project+'&url=' + url + '&ua=' + ua + '&param=' + encodeURIComponent(param) + '&token=' + token + '&type=' + wechat;
  202. http.request('post', url, {noloading:1});
  203. }
  204. }
  205. ,shareLog: function(actType, actResult) {
  206. var project = this.project;
  207. var token = this.token;
  208. var wechat = this.wechat;
  209. var ua = encodeURIComponent(navigator.userAgent);
  210. var url = encodeURIComponent(document.location.href);
  211. var url = this.url + 'collect&callback=?' + '&project='+project+'&url=' + url + '&actType=' + actType + '&actResult=' + actResult + '&ua=' + ua + '&token=' + token + '&type=' + wechat
  212. http.request('post', url, {noloading:1});
  213. }
  214. }
  215. export default share;