singlepage_pay.js 12 KB

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