pay.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. var smoney = '';
  2. var sbookid = commont.getQueryString('book_id', window.location.search, '&') || '';
  3. var schapterid = commont.getQueryString('chapter_id', window.location.search, '&') || '';
  4. var float_id = commont.getQueryString('float_id', window.location.search, '&') || '';
  5. $(function () {
  6. /* if (typeof cpslog == 'undefined') {
  7. window.cpslog = function (arr, callback) {
  8. console.log('cps_log.js 未加载');
  9. }
  10. }*/
  11. /**
  12. * 头像点击
  13. */
  14. var n = 0;
  15. $('.re_head_box img').click(function () {
  16. n++;
  17. if (n > 4) {
  18. window.location.href = '/index/index/dev';
  19. }
  20. });
  21. /**
  22. * 选项卡点击
  23. */
  24. $('.re_list_ul a').click(function () {
  25. var $this = $(this),
  26. goodsId = $this.data('goods-id'),
  27. goodsCategory = $this.data('goods-category'),
  28. goodsMoney = $this.data('goods-money'),
  29. activityId = $this.data('activity-id');
  30. $this.parent().addClass('hover').siblings('li').removeClass('hover');
  31. var but = $('#pay_btn');
  32. but.data('goods-id', goodsId);
  33. but.data('goods-category', goodsCategory);
  34. but.data('activity-id', activityId);
  35. but.data('money', goodsMoney);
  36. but.text('立即充值:' + goodsMoney);
  37. });
  38. /**
  39. * 充值按钮
  40. */
  41. var paystop = true;
  42. var payurl = '/api/recharge/pay';
  43. $('.re_but_box a').click(function () {
  44. if (!paystop) {
  45. return false;
  46. }
  47. paystop = false;
  48. var $this = $(this),
  49. goodsId = $this.data('goods-id'),
  50. goodsCategory = $this.data('goods-category'),
  51. activity_id = $this.data('activity-id');
  52. smoney = $this.data('money');
  53. business_line = parseInt($this.data('business_line'));
  54. sbookid = commont.getQueryString('book_id', window.location.search, '&') || '';
  55. schapterid = commont.getQueryString('chapter_id', window.location.search, '&') || '';
  56. cpslog([702, {
  57. adid: goodsId,
  58. map: {
  59. book_id: sbookid,
  60. chapter_id: schapterid,
  61. business:business_line,
  62. money: smoney
  63. }
  64. }, business_line]);
  65. var data = {
  66. id: goodsId,
  67. activity_id: activity_id,
  68. business_line:business_line,
  69. category: goodsCategory,
  70. float_id: float_id,
  71. money: smoney
  72. };
  73. if(sbookid){
  74. data.book_id = sbookid;
  75. }
  76. if(schapterid){
  77. data.chapter_id = schapterid;
  78. }
  79. $('.js_loading').show();
  80. $.ajax({
  81. url: payurl,
  82. data: data,
  83. type: 'POST',
  84. timeout: 35000,
  85. success: function (data) {
  86. paystop = true;
  87. if (data.code) {
  88. switch (business_line) {
  89. case 0: //微信公众号支付
  90. if (data.data) {
  91. var oJsonObj = JSON.parse(data.data.json);
  92. wechatPay(oJsonObj, goodsId);
  93. cpslog([703, {
  94. map: {
  95. package: oJsonObj.package,
  96. xdtype: goodsCategory,
  97. result: 1,
  98. book_id: sbookid,
  99. adid: goodsId,
  100. chapter_id: schapterid,
  101. business:business_line,
  102. money: smoney
  103. }
  104. }, 'xdjg']);
  105. } else {
  106. consoleMain(data.msg);
  107. }
  108. break;
  109. default: //alipay 支付宝 //wechath5 微信H5支付
  110. break;
  111. }
  112. } else {
  113. consoleMain(data.msg);
  114. cpslog([703, {
  115. map: {
  116. xdtype: goodsCategory,
  117. result: 2,
  118. book_id: sbookid,
  119. adid: goodsId,
  120. chapter_id: schapterid,
  121. business:business_line,
  122. money: smoney
  123. }
  124. }, 'xdjg']);
  125. }
  126. },
  127. error: function (XMLHttpRequest, textStatus, errorThrown) {
  128. consoleMain('异常错误,请刷新页面重试');
  129. cpslog([703, {
  130. map: {
  131. xdtype: goodsCategory,
  132. result: 4,
  133. book_id: sbookid,
  134. adid: goodsId,
  135. chapter_id: schapterid,
  136. business:business_line,
  137. money: smoney,
  138. XMLHttpRequest: XMLHttpRequest.status,
  139. textStatus: textStatus,
  140. errorThrown: errorThrown
  141. }
  142. }, 'xdjg']);
  143. paystop = true;
  144. },
  145. complete: function () {
  146. $('.js_loading').hide();
  147. }
  148. });
  149. });
  150. /**
  151. * 处理默认充值商品自动下单
  152. */
  153. var check = $('.re_but_box a.check');
  154. if(check.length){
  155. check.click();
  156. }
  157. });
  158. /**
  159. * 微信公众号支付
  160. * @param json 微信支付相关数据
  161. * @param goodsId 商品id
  162. */
  163. function wechatPay(json, goodsId) {
  164. if (typeof WeixinJSBridge == "undefined") {
  165. if (document.addEventListener) {
  166. document.addEventListener('WeixinJSBridgeReady', function () {
  167. onBridgeReady(json, goodsId);
  168. }, false);
  169. } else if (document.attachEvent) {
  170. document.attachEvent('WeixinJSBridgeReady', function () {
  171. onBridgeReady(json, goodsId);
  172. });
  173. document.attachEvent('onWeixinJSBridgeReady', function () {
  174. onBridgeReady(json, goodsId);
  175. });
  176. }
  177. } else {
  178. onBridgeReady(json, goodsId);
  179. }
  180. }
  181. // 倒计时跳转链接
  182. function innerJumpTime(redirect){
  183. var _dialog_html = '<div class="J_dialog_pay_div" style="position:fixed;top:50%;left:50%;margin-left:-1rem;margin-top:-.25rem;width:2rem;height:.5rem;line-height:.5rem;border-radius:.05rem;background:rgba(0,0,0,0.65);text-align:center;color:#fff;font-size:.18rem;">充值成功,3秒后跳转</div>';
  184. var C_TIME = 3;
  185. $('body').append(_dialog_html);
  186. var interval = setInterval(function () {
  187. console.log(C_TIME);
  188. var time = --C_TIME;
  189. $('.J_dialog_pay_div').html('充值成功,'+ time +'秒后跳转');
  190. if (time <= 0) {
  191. clearInterval(interval);
  192. $('.J_dialog_pay_div').remove();
  193. // 广告需要,非从点击底部tab离开充值页,清除支付状态缓存
  194. window.localStorage.removeItem('payStatus');
  195. window.location.href = redirect;
  196. }
  197. }, 1000);
  198. }
  199. /**
  200. * 微信内唤起充值窗口
  201. * @param json
  202. * @param goodsId
  203. */
  204. function onBridgeReady(json, goodsId) {
  205. WeixinJSBridge.invoke(
  206. 'getBrandWCPayRequest', json,
  207. function (res) {
  208. switch (res.err_msg) {
  209. case 'get_brand_wcpay_request:ok': //成功
  210. // consoleMain('充值成功,正在跳转');
  211. cpslog([703, {
  212. map: {
  213. cztype: 'wechat',
  214. package: json.package,
  215. result: 1,
  216. book_id: sbookid,
  217. chapter_id: schapterid,
  218. money: smoney,
  219. res: res
  220. }
  221. }, 'czjg']);
  222. /* if (redirect = commont.getQueryString('redirect')) { //回跳链接
  223. innerJumpTime(redirect)
  224. }else{
  225. // 没有回跳地址,弹窗窗口
  226. $('.bind_alert_box').show();
  227. }*/
  228. // 记录支付成功状态,供广告展示使用
  229. window.payStatus = '1';
  230. window.localStorage.setItem('payStatus','true');
  231. if (redirect = commont.getQueryString('redirect')) { //回跳链接
  232. $('.js_recent_url').val(redirect);
  233. }
  234. if (typeof is_mishu_block != 'undefined' && is_mishu_block == '1') {
  235. $(".mishu_tips_box").show();
  236. } else {
  237. $('.dialoge_bg').show();
  238. }
  239. var activeId = $('.js_recent_url').attr('active-id');
  240. if ( parseInt(activeId) > 0 ){
  241. $('.dialoge_bg .suc_btn_look').text('回到活动');
  242. }
  243. $('.dialoge_bg').show();
  244. break;
  245. case 'get_brand_wcpay_request:cancel': //取消
  246. consoleMain('您已取消充值');
  247. wechatCancel(goodsId);
  248. cpslog([703, {
  249. map: {
  250. cztype: 'wechat',
  251. package: json.package,
  252. result: 2,
  253. book_id: sbookid,
  254. chapter_id: schapterid,
  255. money: smoney,
  256. res: res
  257. }
  258. }, 'czjg']);
  259. if(window.$isAdPick && window.$isAdPick == '1' && $('.ad-recharge-alert').find('.ad-image').length>0){
  260. window.simpleAD.showRechargeAlert();
  261. $('.close').unbind().bind('click',function(){
  262. $('.ad-recharge-alert').hide();
  263. });
  264. }
  265. break;
  266. case 'get_brand_wcpay_request:fail': //失败
  267. consoleMain('充值失败,请尝试重新充值或试试其他充值方式');
  268. wechatCancel(goodsId);
  269. cpslog([703, {
  270. map: {
  271. cztype: 'wechat',
  272. package: json.package,
  273. result: 3,
  274. book_id: sbookid,
  275. chapter_id: schapterid,
  276. money: smoney,
  277. res: res
  278. }
  279. }, 'czjg']);
  280. break;
  281. default:
  282. console.log(res);
  283. wechatCancel(goodsId);
  284. if (typeof res.err_msg == 'undefined' && res.errMsg == 'chooseWXPay:fail, the permission value is offline verifying') {
  285. consoleMain('充值失败,原因:模拟器无法充值');
  286. } else {
  287. consoleMain('异常错误,请刷新页面重试');
  288. cpslog([703, {
  289. map: {
  290. cztype: 'wechat',
  291. package: json.package,
  292. result: 4,
  293. book_id: sbookid,
  294. chapter_id: schapterid,
  295. money: smoney,
  296. res: res
  297. }
  298. }, 'czjg']);
  299. }
  300. break;
  301. }
  302. }
  303. );
  304. }
  305. //秘书弹窗
  306. var mishu_2_num_val = parseInt($.fn.cookie('mishu_2_num')) || 0;
  307. $(".mishu_tips_box .btn_close_2").click(function () {
  308. mishu_2_num_val++;
  309. $.fn.cookie('mishu_2_num',mishu_2_num_val,{expires: 3650, path: '/', domain: document.domain});
  310. $('.mishu_tips_box').remove();
  311. var goUrl = $('.js_recent_url').val();
  312. if (goUrl.length > 0 && goUrl.indexOf("book_id") != -1) {
  313. // 广告需要,非从点击底部tab离开充值页,清除支付状态缓存
  314. window.localStorage.removeItem('payStatus');
  315. window.location.href=goUrl;
  316. } else {
  317. window.location.reload();
  318. }
  319. });
  320. $(".suc_close").click(function () {
  321. $('.dialoge_bg').hide();
  322. window.location.reload();
  323. });
  324. //region 弹框事件
  325. $('.bind_alert_off').click(function () {
  326. var search = window.location.search.replace('?', '');
  327. var aParams = search.split('&');
  328. var aParamsNoGoods = [];
  329. for (var i = 0; i < aParams.length; i++) {
  330. if (aParams[i].indexOf('goods_id=') == -1) {
  331. aParamsNoGoods.push(aParams[i]);
  332. }
  333. }
  334. var strParams = aParamsNoGoods.join('&');
  335. // 广告需要,非从点击底部tab离开充值页,清除支付状态缓存
  336. window.localStorage.removeItem('payStatus');
  337. var href = window.location.pathname + '?' + strParams;
  338. window.location.href = href;
  339. });
  340. $('.alert_but_1, .suc_btn_look').click(function () {
  341. // 广告需要,非从点击底部tab离开充值页,清除支付状态缓存
  342. window.localStorage.removeItem('payStatus');
  343. window.location.href = $('.js_recent_url').val();
  344. });
  345. //endregion
  346. function wechatCancel(goodsId) {
  347. if ($('.fans_box').length > 0) {
  348. $('.fans_box').show();
  349. cpslog([703, {export_fans_id:$('.fans_box').data('id')}, 'export_fans_show']);
  350. $(".btn_close_3").click(function () {
  351. $('.fans_box').hide();
  352. cpslog([703, {export_fans_id:$('.fans_box').data('id')}, 'export_fans_close']);
  353. });
  354. }
  355. $.ajax({
  356. url: '/api/recharge/wechatCancel',
  357. data: {
  358. url: window.location.href,
  359. goods_id: goodsId
  360. },
  361. type: 'POST',
  362. timeout: 10000,
  363. success: function (data) {
  364. },
  365. error: function (err) {
  366. }
  367. });
  368. }