special_pay.js 11 KB

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