share.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* ================================================================================
  2. * dever.js v1.0
  3. * http://git.shemic.com/dever/script
  4. * ================================================================================
  5. * Copyright 2017-2018 Dever(dever.cc)
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * ================================================================================
  19. */
  20. //<filter><script src="../js/lib/jquery.min.js"></script><script src="../js/dever.js"></script></filter>
  21. //分享 暂时只支持手机版微信分享 请同时安装wechat组件
  22. var Dever_Share =
  23. {
  24. wechat : false,
  25. url : '',
  26. project : '',
  27. uid : '-1',
  28. param : {
  29. title : '',
  30. img : '',
  31. desc : '',
  32. url : '',
  33. timelineCallback: function() {},
  34. sendMessageCallback: function() {},
  35. shareQQCallback: function() {}
  36. },
  37. Init : function(uid, project, url, param, button)
  38. {
  39. var self = this;
  40. self.wechat = self.CheckWechat();
  41. self.project = project;
  42. self.url = url;
  43. self.uid = uid;
  44. //self.param = param;
  45. $.extend(true, self.param, param)
  46. if (button) {
  47. $(button).on('click', function() {
  48. if (self.wechat == 1) {
  49. self.WechatGuide();
  50. } else {
  51. self.WeiboShare($(this));
  52. }
  53. })
  54. }
  55. if (self.wechat == 1) {
  56. self.WechatInit();
  57. }
  58. self.Reflux();
  59. }
  60. ,CheckWechat : function()
  61. {
  62. var state = navigator.userAgent.toLowerCase().indexOf("micromessenger") > -1;
  63. if (state) {
  64. return 1;
  65. } else {
  66. return 2;
  67. }
  68. }
  69. ,WechatGuide : function()
  70. {
  71. var param = this.param;
  72. var tpl = '<div id="weixin_guide" style="position: fixed;background:url(../img/background.png) repeat;left:0;top:0;right:0;bottom:0;z-index:1000;"><img src="../img/weixin-guide.png" style="position: absolute;z-index:100;" width="295" height="273"/></div>';
  73. $('#weixin_guide').remove();
  74. $(tpl).attr('id', 'weixin_guide').appendTo($('body'));
  75. $('#weixin_guide img').css(weixinCfg.style || {})
  76. setTimeout(function() {
  77. $('#weixin_guide').click(function() {
  78. $(this).remove();
  79. })
  80. }, 300)
  81. }
  82. ,WechatInit: function()
  83. {
  84. var self = this;
  85. var url = this.url + 'init?callback=?';
  86. var param = this.param;
  87. var project = this.project;
  88. $.ajax({
  89. type:'post',
  90. dataType:'jsonp',
  91. jsonp: 'jsonp_cb',
  92. url: url,
  93. data: {
  94. 'project' : project,
  95. 'url' : location.href.split('#')[0]
  96. },
  97. success:function(result) {
  98. var data = result.data;
  99. wx.config({
  100. //debug:true,
  101. appId: data.appId,
  102. timestamp: data.timestamp,
  103. nonceStr: data.nonceStr,
  104. signature: data.signature,
  105. jsApiList: [
  106. 'updateTimelineShareData',//'onMenuShareTimeline',
  107. 'updateAppMessageShareData',//'onMenuShareAppMessage',
  108. //'onMenuShareQQ'
  109. ]
  110. });
  111. wx.ready(function() {
  112. wx.updateAppMessageShareData({
  113. title: param.title,
  114. desc: param.desc,
  115. link: param.url,
  116. imgUrl: param.img,
  117. success: function(res) {
  118. //param.sendMessageCallback('success', res)
  119. //self.ShareLog(1, 1);
  120. },
  121. });
  122. wx.updateTimelineShareData({
  123. title: param.title,
  124. link: param.url,
  125. imgUrl: param.img,
  126. success: function(res) {
  127. //param.timelineCallback('success', res)
  128. //self.ShareLog(2, 1);
  129. },
  130. });
  131. /*
  132. wx.onMenuShareQQ({
  133. title: param.title,
  134. link: param.url,
  135. imgUrl: param.img,
  136. success: function(res) {
  137. param.shareQQCallback('success', res)
  138. self.ShareLog(3, 1);
  139. },
  140. cancel: function(res) {
  141. param.shareQQCallback('cancel', res)
  142. self.ShareLog(3, 2);
  143. },
  144. fail: function(res) {
  145. param.shareQQCallback('fail', res)
  146. self.ShareLog(3, 3);
  147. }
  148. });*/
  149. });
  150. },
  151. async:'true',
  152. timeout: 3000
  153. });
  154. }
  155. ,Reflux: function() {
  156. var refer = document.referrer,
  157. url = encodeURIComponent(document.location.href),
  158. param = location.search.substr(1),
  159. ua = encodeURIComponent(navigator.userAgent),
  160. project = this.project,
  161. uid = this.uid,
  162. wechat = this.wechat;
  163. if (param.indexOf("tsina-") > -1 || param.indexOf("timeline") > -1 || param.indexOf("singlemessage") > -1 || param.indexOf("groupmessage") > -1) {
  164. $.getJSON(this.url + 'reflux?callback=?' + '&project='+project+'&url=' + url + '&ua=' + ua + '&param=' + encodeURIComponent(param) + '&uid=' + uid + '&type=' + wechat);
  165. }
  166. }
  167. ,ShareLog: function(actType, actResult) {
  168. var project = this.project;
  169. var uid = this.uid;
  170. var wechat = this.wechat;
  171. var ua = encodeURIComponent(navigator.userAgent);
  172. var url = encodeURIComponent(document.location.href);
  173. $.getJSON(this.url + 'collect?callback=?' + '&project='+project+'&url=' + url + '&actType=' + actType + '&actResult=' + actResult + '&ua=' + ua + '&uid=' + uid + '&type=' + wechat);
  174. }
  175. }