share.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. 'onMenuShareTimeline',
  107. 'onMenuShareAppMessage',
  108. 'onMenuShareQQ'
  109. ]
  110. });
  111. wx.ready(function() {
  112. wx.onMenuShareAppMessage({
  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. cancel: function(res) {
  122. param.sendMessageCallback('cancel', res)
  123. self.ShareLog(1, 2);
  124. },
  125. fail: function(res) {
  126. param.sendMessageCallback('fail', res)
  127. self.ShareLog(1, 3);
  128. }
  129. });
  130. wx.onMenuShareTimeline({
  131. title: param.title,
  132. link: param.url,
  133. imgUrl: param.img,
  134. success: function(res) {
  135. param.timelineCallback('success', res)
  136. self.ShareLog(2, 1);
  137. },
  138. cancel: function(res) {
  139. param.timelineCallback('cancel', res)
  140. self.ShareLog(2, 2);
  141. },
  142. fail: function(res) {
  143. param.timelineCallback('fail', res)
  144. self.ShareLog(2, 3);
  145. }
  146. });
  147. wx.onMenuShareQQ({
  148. title: param.title,
  149. link: param.url,
  150. imgUrl: param.img,
  151. success: function(res) {
  152. param.shareQQCallback('success', res)
  153. self.ShareLog(3, 1);
  154. },
  155. cancel: function(res) {
  156. param.shareQQCallback('cancel', res)
  157. self.ShareLog(3, 2);
  158. },
  159. fail: function(res) {
  160. param.shareQQCallback('fail', res)
  161. self.ShareLog(3, 3);
  162. }
  163. });
  164. });
  165. },
  166. async:'true',
  167. timeout: 3000
  168. });
  169. }
  170. ,Reflux: function() {
  171. var refer = document.referrer,
  172. url = encodeURIComponent(document.location.href),
  173. param = location.search.substr(1),
  174. ua = encodeURIComponent(navigator.userAgent),
  175. project = this.project,
  176. uid = this.uid,
  177. wechat = this.wechat;
  178. if (param.indexOf("tsina-") > -1 || param.indexOf("timeline") > -1 || param.indexOf("singlemessage") > -1 || param.indexOf("groupmessage") > -1) {
  179. $.getJSON(this.url + 'reflux?callback=?' + '&project='+project+'&url=' + url + '&ua=' + ua + '&param=' + encodeURIComponent(param) + '&uid=' + uid + '&type=' + wechat);
  180. }
  181. }
  182. ,ShareLog: function(actType, actResult) {
  183. var project = this.project;
  184. var uid = this.uid;
  185. var wechat = this.wechat;
  186. var ua = encodeURIComponent(navigator.userAgent);
  187. var url = encodeURIComponent(document.location.href);
  188. $.getJSON(this.url + 'collect?callback=?' + '&project='+project+'&url=' + url + '&actType=' + actType + '&actResult=' + actResult + '&ua=' + ua + '&uid=' + uid + '&type=' + wechat);
  189. }
  190. }
  191. module.exports = {
  192. Dever_Share: Dever_Share,
  193. }