pay.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. var smoney = '';
  2. var sbookid = commont.getQueryString('book_id', window.location.search, '&') || '';
  3. var schapterid = commont.getQueryString('chapter_id', window.location.search, '&') || '';
  4. $(function () {
  5. /**
  6. * 头像点击
  7. */
  8. var n = 0;
  9. $('.re_head_box img').click(function () {
  10. n++;
  11. if (n > 4) {
  12. window.location.href = '/index/index/dev'
  13. }
  14. });
  15. $(document).on('click','.select_pay',function(){
  16. $('input[name="payment_id"]').removeAttr("checked");
  17. $(this).find('input[name="payment_id"]').prop("checked", 'checked');
  18. });
  19. /**
  20. * 选项卡点击
  21. */
  22. $('.re_list_ul a').click(function () {
  23. var $this = $(this),
  24. goodsId = $this.data('goods-id'),
  25. goodsCategory = $this.data('goods-category'),
  26. goodsMoney = $this.data('goods-money');
  27. $this.parent().addClass('hover').siblings('li').removeClass('hover');
  28. var but = $('#pay_btn');
  29. but.data('goods-id', goodsId);
  30. but.data('goods-category', goodsCategory);
  31. but.data('money', goodsMoney);
  32. but.text('立即充值:' + goodsMoney);
  33. });
  34. /**
  35. * 充值按钮
  36. */
  37. var paystop = true;
  38. $('.re_but_box a').click(function () {
  39. if (!paystop) {
  40. return false
  41. }
  42. paystop = false;
  43. var $this = $(this),
  44. goodsId = $this.data('goods-id'),
  45. goodsCategory = $this.data('goods-category');
  46. smoney = $this.data('money');
  47. business_line = parseInt($this.data('business_line'));
  48. payment_id = $('input[name="payment_id"]:checked').val();
  49. sbookid = commont.getQueryString('book_id', window.location.search, '&') || '';
  50. schapterid = commont.getQueryString('chapter_id', window.location.search, '&') || '';
  51. cpslog([702, {
  52. adid: goodsId,
  53. map: {
  54. book_id: sbookid,
  55. chapter_id: schapterid,
  56. money: smoney
  57. }
  58. }, business_line]);
  59. var data = {
  60. id: goodsId,
  61. category: goodsCategory,
  62. business:business_line,
  63. payment_method:payment_id,
  64. };
  65. if(sbookid){
  66. data.book_id = sbookid;
  67. }
  68. if(schapterid){
  69. data.chapter_id = schapterid;
  70. }
  71. $('.js_loading').show();
  72. $.ajax({
  73. url: '/clientappapi/recharge/pay',
  74. data: data,
  75. type: 'POST',
  76. timeout: 35000,
  77. success: function (data) {
  78. paystop = true;
  79. if (data.code) {
  80. console.log(data);
  81. if (data.data) {
  82. window.location.href=data.data.redirect;
  83. cpslog([703, {
  84. map: {
  85. package: oJsonObj.package,
  86. xdtype: goodsCategory,
  87. result: 1,
  88. book_id: sbookid,
  89. adid: goodsId,
  90. business:business_line,
  91. chapter_id: schapterid,
  92. money: smoney
  93. }
  94. }, 'xdjg']);
  95. } else {
  96. consoleMain(data.msg);
  97. }
  98. } else {
  99. consoleMain(data.msg);
  100. cpslog([703, {
  101. map: {
  102. xdtype: goodsCategory,
  103. result: 2,
  104. book_id: sbookid,
  105. adid: goodsId,
  106. chapter_id: schapterid,
  107. business:business_line,
  108. money: smoney
  109. }
  110. }, 'xdjg']);
  111. }
  112. },
  113. error: function (XMLHttpRequest, textStatus, errorThrown) {
  114. consoleMain('异常错误,请刷新页面重试');
  115. cpslog([703, {
  116. map: {
  117. xdtype: goodsCategory,
  118. result: 4,
  119. book_id: sbookid,
  120. adid: goodsId,
  121. chapter_id: schapterid,
  122. business:business_line,
  123. money: smoney,
  124. XMLHttpRequest: XMLHttpRequest.status,
  125. textStatus: textStatus,
  126. errorThrown: errorThrown
  127. }
  128. }, 'xdjg']);
  129. paystop = true;
  130. },
  131. complete: function () {
  132. $('.js_loading').hide();
  133. }
  134. })
  135. });
  136. /**
  137. * 处理默认充值商品自动下单
  138. */
  139. var check = $('.re_but_box a.check');
  140. if(check.length){
  141. check.click();
  142. }
  143. });
  144. /**
  145. * 微信公众号支付
  146. * @param json 微信支付相关数据
  147. * @param goodsId 商品id
  148. */
  149. function wechatPay(json, goodsId) {
  150. if (typeof WeixinJSBridge == "undefined") {
  151. if (document.addEventListener) {
  152. document.addEventListener('WeixinJSBridgeReady', function () {
  153. onBridgeReady(json, goodsId);
  154. }, false);
  155. } else if (document.attachEvent) {
  156. document.attachEvent('WeixinJSBridgeReady', function () {
  157. onBridgeReady(json, goodsId);
  158. });
  159. document.attachEvent('onWeixinJSBridgeReady', function () {
  160. onBridgeReady(json, goodsId);
  161. });
  162. }
  163. } else {
  164. onBridgeReady(json, goodsId);
  165. }
  166. }
  167. // 倒计时跳转链接
  168. function innerJumpTime(redirect){
  169. 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>';
  170. var C_TIME = 3;
  171. $('body').append(_dialog_html);
  172. var interval = setInterval(function () {
  173. console.log(C_TIME);
  174. var time = --C_TIME;
  175. $('.J_dialog_pay_div').html('充值成功,'+ time +'秒后跳转');
  176. if (time <= 0) {
  177. clearInterval(interval);
  178. $('.J_dialog_pay_div').remove();
  179. window.location.href = redirect;
  180. }
  181. }, 1000);
  182. }
  183. /**
  184. * 微信内唤起充值窗口
  185. * @param json
  186. * @param goodsId
  187. */
  188. function onBridgeReady(json, goodsId) {
  189. WeixinJSBridge.invoke(
  190. 'getBrandWCPayRequest', json,
  191. function (res) {
  192. switch (res.err_msg) {
  193. case 'get_brand_wcpay_request:ok': //成功
  194. consoleMain('充值成功');
  195. cpslog([703, {
  196. map: {
  197. cztype: 'wechat',
  198. package: json.package,
  199. result: 1,
  200. book_id: sbookid,
  201. chapter_id: schapterid,
  202. money: smoney,
  203. res: res
  204. }
  205. }, 'czjg']);
  206. if (redirect = commont.getQueryString('redirect')) { //回跳链接
  207. innerJumpTime(redirect)
  208. }else{
  209. // 没有回跳地址,弹窗窗口
  210. $('.bind_alert_box').show();
  211. }
  212. break;
  213. case 'get_brand_wcpay_request:cancel': //取消
  214. consoleMain('您已取消充值');
  215. wechatCancel(goodsId);
  216. cpslog([703, {
  217. map: {
  218. cztype: 'wechat',
  219. package: json.package,
  220. result: 2,
  221. book_id: sbookid,
  222. chapter_id: schapterid,
  223. money: smoney,
  224. res: res
  225. }
  226. }, 'czjg']);
  227. break;
  228. case 'get_brand_wcpay_request:fail': //失败
  229. consoleMain('充值失败,请尝试重新充值或试试其他充值方式');
  230. wechatCancel(goodsId);
  231. cpslog([703, {
  232. map: {
  233. cztype: 'wechat',
  234. package: json.package,
  235. result: 3,
  236. book_id: sbookid,
  237. chapter_id: schapterid,
  238. money: smoney,
  239. res: res
  240. }
  241. }, 'czjg']);
  242. break;
  243. default:
  244. console.log(res);
  245. wechatCancel(goodsId);
  246. if (typeof res.err_msg == 'undefined' && res.errMsg == 'chooseWXPay:fail, the permission value is offline verifying') {
  247. consoleMain('充值失败,原因:模拟器无法充值');
  248. } else {
  249. consoleMain('异常错误,请刷新页面重试');
  250. cpslog([703, {
  251. map: {
  252. cztype: 'wechat',
  253. package: json.package,
  254. result: 4,
  255. book_id: sbookid,
  256. chapter_id: schapterid,
  257. money: smoney,
  258. res: res
  259. }
  260. }, 'czjg']);
  261. }
  262. break;
  263. }
  264. }
  265. );
  266. }
  267. //region 弹框事件
  268. $('.bind_alert_off').click(function () {
  269. var search = window.location.search.replace('?', '');
  270. var aParams = search.split('&');
  271. var aParamsNoGoods = [];
  272. for (var i = 0; i < aParams.length; i++) {
  273. if (aParams[i].indexOf('goods_id=') == -1) {
  274. aParamsNoGoods.push(aParams[i]);
  275. }
  276. }
  277. var strParams = aParamsNoGoods.join('&');
  278. var href = window.location.pathname + '?' + strParams;
  279. window.location.href = href;
  280. });
  281. $('.alert_but_1').click(function () {
  282. window.location.href = $('.js_recent_url').val();
  283. });
  284. //endregion
  285. function wechatCancel(goodsId) {
  286. $.ajax({
  287. url: '/api/recharge/wechatCancel',
  288. data: {
  289. url: window.location.href,
  290. goods_id: goodsId
  291. },
  292. type: 'POST',
  293. timeout: 10000,
  294. success: function (data) {
  295. },
  296. error: function (err) {
  297. }
  298. })
  299. }