share.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. (function(win) {
  2. //确认签名已拿到
  3. if (typeof signPackage != "undefined") {
  4. wx.config({
  5. debug: false,
  6. appId: signPackage.appId,
  7. timestamp: signPackage.timestamp + "",
  8. nonceStr: signPackage.nonceStr,
  9. signature: signPackage.signature,
  10. jsApiList: [ 'checkJsApi',
  11. 'onMenuShareTimeline',
  12. 'onMenuShareAppMessage'
  13. ]
  14. });
  15. wxShare();
  16. }
  17. })(window);
  18. /*
  19. * 微信JS-SDK 分享类接口
  20. * @gameOver
  21. * @param {String} title 分享标题
  22. * @param {String} desc 分享描述
  23. * @param {String} link 分享链接
  24. * @param {String} imgUrl 分享图标
  25. * @allparam
  26. */
  27. function wxShare() {
  28. wx.ready(function() {
  29. //分享给朋友
  30. wx.onMenuShareAppMessage({
  31. trigger: function(res) {
  32. this.title = window.shareData.tTitle;
  33. this.desc = window.shareData.tContent;
  34. this.link = window.shareData.timeLineLink;
  35. this.imgUrl = window.shareData.imgUrl;
  36. },
  37. success: function() {
  38. statistics("onShareFriends");
  39. },
  40. });
  41. //分享到朋友圈
  42. wx.onMenuShareTimeline({
  43. trigger: function(res) {
  44. this.title = window.shareData.tTitle;
  45. this.link = window.shareData.timeLineLink;
  46. this.imgUrl = window.shareData.imgUrl;
  47. },
  48. success: function() {
  49. statistics("onShareCircle");
  50. }
  51. });
  52. });
  53. }
  54. // 判断哪种统计 cnzz , DCAgent
  55. function statistics(shareName) {
  56. if (typeof _cnz != "undefined") {
  57. //cnzz
  58. if (shareName == "onShareCircle") {
  59. _czc.push(['_trackEvent', '分享朋友圈']);
  60. } else {
  61. _czc.push(['_trackEvent', '分享好友']);
  62. }
  63. } else if (typeof DCAgent != "undefined") {
  64. //dataeye
  65. if (shareName == "onShareCircle") {
  66. DCAgent.onEvent("share_timeline", 1, {
  67. userID: 'null'
  68. });
  69. } else {
  70. DCAgent.onEvent("share_app", 1, {
  71. userID: 'null'
  72. });
  73. }
  74. }
  75. }
  76. //判断是否为微信环境
  77. function isWeiXin() {
  78. var ua = window.navigator.userAgent.toLowerCase();
  79. if (ua.match(/MicroMessenger/i) == 'micromessenger') {
  80. return true;
  81. } else {
  82. return false;
  83. }
  84. }
  85. (function(global) {
  86. var _wx = {
  87. /*
  88. * 配置微信信息
  89. @param
  90. config == {
  91. appId: signPackage.appId,
  92. timestamp: signPackage.timestamp,
  93. nonceStr: signPackage.nonceStr,
  94. signature: signPackage.signature,
  95. jsApiList: [
  96. 'onMenuShareTimeline',
  97. 'onMenuShareAppMessage'
  98. ]
  99. }
  100. */
  101. init: function(config) {
  102. wx && wx.config(config);
  103. return this;
  104. },
  105. /*
  106. * 初始化微信分享数据
  107. * @param info == { title :'title', desc : 'desc', link : 'link', imgUrl:'imgurl'}
  108. *
  109. */
  110. config: function(info) {
  111. var me = this;
  112. this.info = info || {};
  113. document.title = info.title;
  114. wx.ready(function() {
  115. //分享给朋友
  116. wx.onMenuShareAppMessage({
  117. trigger: function(res) {
  118. this.title = info.title;
  119. this.desc = infot.desc;
  120. this.link = info.link;
  121. this.imgUrl = info.imgUrl;
  122. },
  123. success: function() {
  124. me.statistics("onShareFriends");
  125. },
  126. });
  127. //分享到朋友圈
  128. wx.onMenuShareTimeline({
  129. trigger: function(res) {
  130. this.title = info.title;
  131. this.link = info.link;
  132. this.imgUrl = info.imgUrl;
  133. },
  134. success: function() {
  135. me.statistics("onShareCircle");
  136. }
  137. });
  138. });
  139. return this;
  140. },
  141. /*
  142. * 修改分享内容
  143. * @changeInfo
  144. * @param key,value 单条内容
  145. * @param object 内容对象
  146. * @usage CKShare.wx.changeInfo("url",'http://266.com')
  147. * @allparam
  148. */
  149. changeInfo: function() {
  150. var arg = arguments,
  151. len = arg.length;
  152. if (len > 1) {
  153. this.info[arg[0]] = arg[1];
  154. } else if ("[object Object]" == Object.prototype.toString.call(arg[0])) {
  155. var obj = arg[0],
  156. i;
  157. for (i in obj) {
  158. this.info[i] = obj[i];
  159. }
  160. }
  161. document.title = this.info.title;
  162. return this;
  163. },
  164. /*
  165. * 统计
  166. */
  167. statistics: function(type) {
  168. if (typeof _cnz != "undefined") {
  169. //cnzz
  170. if (type == "onShareCircle") {
  171. _czc.push(['_trackEvent', 'share_timeline']);
  172. } else {
  173. _czc.push(['_trackEvent', 'share_app']);
  174. }
  175. } else if (typeof DCAgent != "undefined") {
  176. //dataeye
  177. if (type == "onShareCircle") {
  178. DCAgent.onEvent("share_timeline", 1, {
  179. userID: 'CK_guest'
  180. });
  181. } else {
  182. DCAgent.onEvent("share_app", 1, {
  183. userID: 'CK_guest'
  184. });
  185. }
  186. }
  187. }
  188. }
  189. global.CKShare = {
  190. wx: _wx
  191. }
  192. })(window);