mshare.js 16 KB

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