mshare.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. function WX_share(b,c,d,e){var f=navigator.userAgent,g=-1<f.indexOf("Android")||-1<f.indexOf("Adr");f.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)&&(function(a){window.WebViewJavascriptBridge?a(WebViewJavascriptBridge):document.addEventListener("WebViewJavascriptBridgeReady",function(){a(WebViewJavascriptBridge)},!1)}(function(a){a.init(function(a,b){alert("Received message: "+a);b&&b("Right back atcha")});a.send({url:b,typeid:8,depict:c,title:d,picture:e});a.send("Please respond to this",function(a){alert("Javascript got its response",
  2. a)})}),window.webkit.messageHandlers.JS_Call_OC_login.postMessage({url:b,typeid:8,depict:c,title:d,picture:e}));g&&Android.share(b,c,d,e)};
  3. ;(function($, window, document, undefined) {
  4. function regist(mName, module) {
  5. if (typeof window.brickjs === "undefined") {
  6. window.brickjs = {};
  7. }
  8. brickjs[mName] = module;
  9. }
  10. ;/**
  11. * @file 微信,微博分享
  12. * @author jie.qin@condenast.com.cn
  13. * @version 0.1.0
  14. * @requires http://js.selfimg.com.cn/jquery/jquery-1.7.1.min.js|http://js.selfimg.com.cn/gallery/zepto/1.1.4/zepto.min.js
  15. * @requires http://js.selfimg.com.cn/lib/jweixin-1.0.0.js
  16. * @example <a href="../mshare/examples/demo.html">demo.html</a>
  17. * @see http://js.selfimg.com.cn/brickjs/mshare/0.1.0/mshare.min.js
  18. */
  19. var isWeixin = navigator.userAgent.toLowerCase().indexOf("micromessenger") > -1;
  20. //isWeixin = true;
  21. /**
  22. * 微信,微博分享定义组件。(微博分享功能,可以在按钮上配置属性实现动态替换分享属性,属性名称:data-desc,data-sUrl,data-imgUrl)
  23. * @class
  24. * @author jie.qin@condenast.com.cn
  25. * @version 0.1.0
  26. * @requires http://js.selfimg.com.cn/jquery/jquery-1.7.1.min.js|http://js.selfimg.com.cn/gallery/zepto/1.1.4/zepto.min.js
  27. * @requires http://js.selfimg.com.cn/lib/jweixin-1.0.0.js
  28. * @example <a href="../mshare/examples/demo.html">demo.html</a>
  29. * @see http://js.selfimg.com.cn/brickjs/mshare/0.1.0/mshare.min.js
  30. * @param {Object} cfgs 配置项
  31. * @param {String} [cfgs.site=""] 1、vogue 2、self 3、gq 4、adstyle 5、cnt 6、subscribe (必须,用于微信获得签名和统计使用)
  32. * @param {String} [cfgs.catalog=""] 栏目id(仅对微博有效)
  33. * @param {String} [cfgs.sUrl=window.location.href] 分享地址url
  34. * @param {String} [cfgs.title=document.title] 分享标题(仅对微信有效,微博分享使用desc)
  35. * @param {String} [cfgs.desc=""] 分享说明
  36. * @param {String} [cfgs.imgUrl=""] 分享时携带图片地址
  37. * @param {String} [cfgs.ralateUid=""] (仅对微博有效)
  38. * @param {String} [cfgs.timelineCallback] 分享到朋友圈回调(仅对微信有效)
  39. * @param {String} [cfgs.sendMessageCallback] 发送给朋友回调(仅对微信有效)
  40. * @param {String} [cfgs.appKey] 分享到微信和微博对应的appkey
  41. * @param {String} [cfgs.appKey.tsina] 分享到微博对应的appkey
  42. * @param {String} [cfgs.wxGuide] 分享到微信引导(仅对微信有效)
  43. * @param {String} [cfgs.wxGuide.tpl] 分享到微信引导弹层自定义模板
  44. * @param {String} [cfgs.wxGuide.imgUrl] 分享到微信引导图片地址
  45. * @param {String} [cfgs.wxGuide.width] 分享到微信引导图片宽度
  46. * @param {String} [cfgs.wxGuide.height] 分享到微信引导图片高度
  47. * @param {Object} [cfgs.wxGuide.style={top:0,right:0}] 分享到微信引导弹层样式
  48. * @param {Object} selector 绑定的对象
  49. */
  50. function MShare(cfgs, selector) {
  51. this._cfgs = {
  52. site: "",
  53. catalog: "",
  54. sUrl: window.location.href,
  55. title: document.title,
  56. imgUrl: "",
  57. desc: "",
  58. appKey: "",
  59. ralateUid: "",
  60. source_id:"",
  61. source_table :"",
  62. timelineCallback: function() {},
  63. sendMessageCallback: function() {},
  64. shareQQCallback: function() {},
  65. wxGuide: {
  66. tpl: '',
  67. imgUrl: 'http://css.selfimg.com.cn/vogue/mdiscovery/images/weixin-guide.png',
  68. width: 295,
  69. height: 273,
  70. style: {
  71. right: 0,
  72. top: 0
  73. }
  74. }
  75. };
  76. this.weixinSigned = 0;
  77. this.trigger = $(selector);
  78. this._baseUrl = "/collect.";
  79. this._tsinaParam = "get?platform=tsina&title={title}&url={url}&pic={pic}&site={site}&source_id={source_id}&source_table={source_table}&ralateUid={ralateUid}&appkey={appkey}&hash={hash}";
  80. this._init(cfgs, selector);
  81. this._reflux();
  82. }
  83. MShare.prototype = {
  84. _init: function(cfgs, selector) {
  85. var self = this;
  86. $.extend(true, this._cfgs, cfgs)
  87. if (selector) {
  88. $(selector).on('click', function() {
  89. if (isWeixin) {
  90. self._weixinGuide();
  91. } else {
  92. self._weiboShare($(this));
  93. }
  94. })
  95. }
  96. if (isWeixin) {
  97. this.weixinInit();
  98. }
  99. },
  100. _weixinGuide: function() {
  101. var cfg = this._cfgs,
  102. weixinCfg = cfg.wxGuide,
  103. defImg = weixinCfg.imgUrl,
  104. tpl = weixinCfg.tpl || '<div id="weixin_guide" style="position: fixed;background:url(http://css.selfimg.com.cn/cnt/mobile/images/blk80.png) repeat;left:0;top:0;right:0;bottom:0;z-index:1000;"><img src="' + defImg + '" style="position: absolute;z-index:100;" width="' + weixinCfg.width + '" height="' + weixinCfg.height + '"/></div>';
  105. $('#weixin_guide').remove();
  106. $(tpl).attr('id', 'weixin_guide').appendTo($('body'));
  107. $('#weixin_guide img').css(weixinCfg.style || {})
  108. setTimeout(function() {
  109. $('#weixin_guide').click(function() {
  110. $(this).remove();
  111. })
  112. }, 300)
  113. },
  114. /**
  115. * 对微信分享自定义配置
  116. * @param {Object} cfgs 配置项
  117. * @param {String} [cfgs.site=""] 1、vogue 2、self 3、gq 4、adstyle 5、cnt 6、subscribe(必须,用于微信获得签名和统计使用)
  118. * @param {String} [cfgs.sUrl=window.location.href] 分享地址url
  119. * @param {String} [cfgs.title=document.title] 分享标题
  120. * @param {String} [cfgs.desc=""] 分享说明
  121. * @param {String} [cfgs.imgUrl=""] 分享时携带图片地址
  122. * @param {String} [cfgs.timelineCallback] 分享到朋友圈回调
  123. * @param {String} [cfgs.sendMessageCallback] 发送给朋友回调
  124. * @param {String} [cfgs.shareQQCallback] 发送到QQ回调
  125. */
  126. weixinInit: function(cfgs) {
  127. var self = this,
  128. wxCfgs;
  129. cfgs && $.extend(true, this._cfgs, cfgs);
  130. wxCfgs = {
  131. "site": this._cfgs.website,
  132. "img_url": this._cfgs.imgUrl || "",
  133. "link": this._cfgs.sUrl || location.href,
  134. "desc": this._cfgs.desc || "",
  135. "title": this._cfgs.title || document.title,
  136. timelineCallback: this._cfgs.timelineCallback,
  137. sendMessageCallback: this._cfgs.sendMessageCallback,
  138. shareQQCallback: this._cfgs.shareQQCallback
  139. }
  140. //var url = VG.conf.root_domain + '?c=WeixinJsSDK&a=AjaxConfigData&site=' + wxCfgs.site + '&thisPageUrl=' + encodeURIComponent(location.href) + '&callback=?';
  141. var url = VG.conf.root_domain + '?c=WeixinJsSDK&a=AjaxConfigData&thisPageUrl=' + location.href.split('#')[0];
  142. var state = false;
  143. if (location.href.indexOf('test') > -1) {
  144. var state = true;
  145. }
  146. //alert(url)
  147. $.ajax({
  148. type:'post',
  149. dataType:'jsonp',
  150. jsonp: 'jsonp_cb',
  151. url: url,
  152. data: {"thisPageUrl":location.href.split('#')[0]},
  153. success:function(data){
  154. var d = data.msg;
  155. //alert(d.appId)
  156. wx.config({
  157. //debug:true,
  158. appId: d.appId,
  159. timestamp: d.timestamp,
  160. nonceStr: d.nonceStr,
  161. signature: d.signature,
  162. jsApiList: [
  163. 'onMenuShareTimeline',
  164. 'onMenuShareAppMessage',
  165. 'onMenuShareQQ'
  166. ]
  167. });
  168. //alert(wxCfgs.link);
  169. wx.ready(function() {
  170. //alert(wxCfgs.img_url)
  171. if (state) {
  172. //alert(wxCfgs.img_url);
  173. }
  174. //alert(wxCfgs.link);
  175. wx.onMenuShareAppMessage({
  176. title: wxCfgs.title,
  177. desc: wxCfgs.desc,
  178. link: wxCfgs.link,
  179. imgUrl: wxCfgs.img_url,
  180. success: function(res) {
  181. if (state) {
  182. alert(1);
  183. }
  184. wxCfgs.sendMessageCallback('success', res)
  185. self._shareLog(0, 1);
  186. },
  187. cancel: function(res) {
  188. if (state) {
  189. alert(2);
  190. }
  191. wxCfgs.sendMessageCallback('cancel', res)
  192. self._shareLog(0, 2);
  193. },
  194. fail: function(res) {
  195. if (state) {
  196. alert(3);
  197. }
  198. wxCfgs.sendMessageCallback('fail', res)
  199. self._shareLog(0, 0);
  200. }
  201. });
  202. wx.onMenuShareTimeline({
  203. title: wxCfgs.title,
  204. link: wxCfgs.link,
  205. imgUrl: wxCfgs.img_url,
  206. success: function(res) {
  207. if (state) {
  208. alert(1);
  209. }
  210. wxCfgs.timelineCallback('success', res)
  211. self._shareLog(1, 1);
  212. },
  213. cancel: function(res) {
  214. if (state) {
  215. alert(2);
  216. }
  217. wxCfgs.timelineCallback('cancel', res)
  218. self._shareLog(1, 2);
  219. },
  220. fail: function(res) {
  221. if (state) {
  222. alert(3);
  223. }
  224. wxCfgs.timelineCallback('fail', res)
  225. self._shareLog(1, 0);
  226. }
  227. });
  228. wx.onMenuShareQQ({
  229. title: wxCfgs.title,
  230. link: wxCfgs.link,
  231. imgUrl: wxCfgs.img_url,
  232. success: function(res) {
  233. wxCfgs.shareQQCallback('success', res)
  234. self._shareLog(2, 1);
  235. },
  236. cancel: function(res) {
  237. wxCfgs.shareQQCallback('cancel', res)
  238. self._shareLog(2, 2);
  239. },
  240. fail: function(res) {
  241. wxCfgs.shareQQCallback('fail', res)
  242. self._shareLog(2, 0);
  243. }
  244. });
  245. });
  246. },
  247. async:'true',
  248. timeout: 3000
  249. });
  250. /*
  251. $.getJSON(VG.conf.root_domain + '?c=WeixinJsSDK&a=AjaxConfigData&site=' + wxCfgs.site + '&thisPageUrl=' + encodeURIComponent(location.href) + '&callback=?', function(d) {
  252. var d = d.msg;
  253. if (d) {
  254. wx.config({
  255. //debug:true,
  256. appId: d.appId,
  257. timestamp: d.timestamp,
  258. nonceStr: d.nonceStr,
  259. signature: d.signature,
  260. jsApiList: [
  261. 'onMenuShareTimeline',
  262. 'onMenuShareAppMessage',
  263. 'onMenuShareQQ'
  264. ]
  265. });
  266. wx.ready(function() {
  267. wx.onMenuShareAppMessage({
  268. title: wxCfgs.title,
  269. desc: wxCfgs.desc,
  270. link: wxCfgs.link,
  271. imgUrl: wxCfgs.img_url,
  272. success: function(res) {
  273. wxCfgs.sendMessageCallback('success', res)
  274. self._shareLog(0, 1);
  275. },
  276. cancel: function(res) {
  277. wxCfgs.sendMessageCallback('cancel', res)
  278. self._shareLog(0, 2);
  279. },
  280. fail: function(res) {
  281. wxCfgs.sendMessageCallback('fail', res)
  282. self._shareLog(0, 0);
  283. }
  284. });
  285. wx.onMenuShareTimeline({
  286. title: wxCfgs.title,
  287. link: wxCfgs.link,
  288. imgUrl: wxCfgs.img_url,
  289. success: function(res) {
  290. wxCfgs.timelineCallback('success', res)
  291. self._shareLog(1, 1);
  292. },
  293. cancel: function(res) {
  294. wxCfgs.timelineCallback('cancel', res)
  295. self._shareLog(1, 2);
  296. },
  297. fail: function(res) {
  298. wxCfgs.timelineCallback('fail', res)
  299. self._shareLog(1, 0);
  300. }
  301. });
  302. wx.onMenuShareQQ({
  303. title: wxCfgs.title,
  304. link: wxCfgs.link,
  305. imgUrl: wxCfgs.img_url,
  306. success: function(res) {
  307. wxCfgs.shareQQCallback('success', res)
  308. self._shareLog(2, 1);
  309. },
  310. cancel: function(res) {
  311. wxCfgs.shareQQCallback('cancel', res)
  312. self._shareLog(2, 2);
  313. },
  314. fail: function(res) {
  315. wxCfgs.shareQQCallback('fail', res)
  316. self._shareLog(2, 0);
  317. }
  318. });
  319. });
  320. }
  321. })
  322. */
  323. },
  324. /**
  325. * 重置分享自定义配置
  326. * @param {Object} cfg 自定义配置对象
  327. * @param {String} [cfg.sUrl=window.location.href] 分享地址url
  328. * @param {String} [cfg.title=document.title] 分享标题
  329. * @param {String} [cfg.desc=""] 分享说明
  330. * @param {String} [cfg.imgUrl] 分享时携带图片地址
  331. */
  332. reset: function(cfg) {
  333. var self = this;
  334. if (!isWeixin) {
  335. cfg.sUrl && self.trigger.data('sUrl', cfg.sUrl);
  336. cfg.desc && self.trigger.data('desc', cfg.desc);
  337. cfg.imgUrl && self.trigger.data("imgUrl", cfg.imgUrl);
  338. } else {
  339. this.weixinInit({
  340. sUrl: cfg.sUrl || self._cfgs.sUrl,
  341. desc: cfg.desc || self._cfgs.desc,
  342. imgUrl: cfg.imgUrl || self._cfgs.imgUrl,
  343. title: cfg.title || self._cfgs.title
  344. });
  345. }
  346. },
  347. _reflux: function() {
  348. var refer = document.referrer,
  349. url = document.location.href,
  350. param = location.search.substr(1),
  351. ua = navigator.userAgent;
  352. if (param.indexOf("tsina-") > -1 || param.indexOf("timeline") > -1 || param.indexOf("singlemessage") > -1 || param.indexOf("groupmessage") > -1) {
  353. $.getJSON(this._baseUrl + "reflux?" + "callback=?" + "&url=" + encodeURIComponent(url) + '&ua=' + ua + '&param=' + encodeURIComponent(param));
  354. }
  355. },
  356. _shareLog: function(actType, actResult) {
  357. var site = this._cfgs.website;
  358. //$.get('http://application.self.com.cn/wxapi/interface/sharecollect/collectdata?mp=' + site + '&url=' + encodeURIComponent(location.href) + '&ac=' + actType + '&sc=' + actResult + '&ua=' + encodeURIComponent(navigator.userAgent))
  359. },
  360. _weiboShare: function(trigger) {
  361. var self = this;
  362. var cfg = this._cfgs,
  363. params = {
  364. title: cfg.desc,
  365. url: cfg.sUrl,
  366. pic: cfg.imgUrl,
  367. site: cfg.site,
  368. cat: cfg.catalog,
  369. source_id: cfg.source_id,
  370. source_table: cfg.source_table,
  371. ralateUid: cfg.ralateUid,
  372. appkey: cfg.appKey.tsina
  373. },
  374. tgUrl = trigger.data('sUrl'),
  375. tgTitle = trigger.data('desc'),
  376. tgPic = trigger.data("imgUrl");
  377. params.title = encodeURIComponent(tgTitle ? tgTitle : params.title);
  378. params.url = encodeURIComponent(tgUrl ? tgUrl : params.url);
  379. params.pic = encodeURIComponent(tgPic ? tgPic : params.pic);
  380. var cmdUrl = this._baseUrl + self._formatUri(this._tsinaParam, params);
  381. window.open(cmdUrl);
  382. },
  383. _formatUri: function(uri, params) {
  384. var reg, uri = uri;
  385. for (var k in params) {
  386. reg = new RegExp("{" + k + "}", "g");
  387. uri = uri.replace(reg, params[k]);
  388. }
  389. return uri;
  390. }
  391. }
  392. ;regist("MShare",MShare);
  393. })((typeof Zepto !== 'undefined' ? Zepto : jQuery), window, document)