No Description

payment.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. var qs = parseQueryString(window.location.href),token,rechargeMoney = 0,goodsId,user_id,order_id;
  2. $().ready(function(){
  3. if(qs.order_id){
  4. order_id = qs.order_id;
  5. $(".wx_payment_mask").show();
  6. }
  7. getTokenAddUserIdEvent()
  8. wxPaymentResult();
  9. })
  10. function app_h5_getOrderId (data) {//支付是否成功,解ios微信白页bug
  11. if(navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)){
  12. if(data){
  13. var dataes = JSON.parse(data)
  14. order_id = dataes.order_id
  15. $(".wx_payment_mask").show();
  16. // window.location.href="./paymentResult.html?type=3&order_id=" + order_id
  17. }
  18. }
  19. }
  20. function getTokenAddUserIdEvent () {
  21. //获取token 和 user_id
  22. var param = '{"js_callback" : "getTokenAddUserId"}';
  23. try{
  24. if(navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)){
  25. window.webkit.messageHandlers.app_h5_getTokenAndUserid.postMessage(param);
  26. }
  27. if(navigator.userAgent.match(/android/i)){
  28. obj4H5.app_h5_getTokenAndUserid(param);
  29. }
  30. }catch(e){
  31. console.log("兼容")
  32. // token = 'c1c63ee904336d416627ac09c85fa5cb'
  33. // user_id = '10'
  34. // rechargeListData()
  35. }
  36. }
  37. function getTokenAddUserId (data) {//app token 回调
  38. var dataes = JSON.parse(data)
  39. token = dataes.token;
  40. user_id = dataes.user_id;
  41. rechargeListData()
  42. }
  43. function rechargeListData () {
  44. //充值套餐列表--
  45. $(".loading").show();
  46. $.ajax({
  47. type: 'get',
  48. url: '/user/rechargeList',
  49. data:{
  50. user_id:user_id
  51. },
  52. headers:{
  53. token: token
  54. },
  55. success: function (res) {
  56. $(".loading").hide();
  57. console.log(res)
  58. if(res.errno == 200){
  59. var rechargeListHtml = '',recharge_type_html;
  60. res.rst.forEach(function (item,index) {
  61. //recharge_type": 0, //套餐类型 1--首充套餐 2--一般套餐',
  62. recharge_type_html = '',free_gold_html = '';
  63. //free_gold充值是否送金币 0的时候不送
  64. if(item.recharge_type == 1){
  65. recharge_type_html = '<img src="img/payment_one.png"/>'
  66. }
  67. if(item.free_gold != 0){
  68. free_gold_html = '<span>送' + item.free_gold + '江湖币</span>'
  69. }
  70. rechargeListHtml += '<li onclick="packageSelection(' + item.id + ',' + item.money + ')"><div class="rechargeLi rechargeLi_' + item.id + '"><p>充' + item.recharge_gold + '江湖币</p>' + free_gold_html + recharge_type_html + '</div></li>'
  71. });
  72. $(".rechargeList").html(rechargeListHtml)
  73. }else{
  74. showMsg(res.rst.msg)
  75. }
  76. },
  77. error: function () {
  78. showMsg('请稍后再试~')
  79. $(".loading").hide();
  80. }
  81. })
  82. }
  83. function payEvent () {
  84. //立即支付按钮事件
  85. var type = $(".payment_mode li input[name='mode']:checked").val();//1为支付宝 2为微信
  86. if(!goodsId){
  87. showMsg('请选择您需要的套餐')
  88. return;
  89. }
  90. if(!type){
  91. showMsg('请选择您的支付方式')
  92. return;
  93. }
  94. window.location.href = '/credit/pay?goods_id=' + goodsId + '&pay_type=' + type + '&user_id=' + user_id
  95. }
  96. function wxPaymentResult () {
  97. //微信支付结果
  98. $("#paySuccess").on('click',function () {
  99. console.log('支付成功')
  100. wxCheckOrder();
  101. $(".wx_payment_mask").hide();
  102. })
  103. $("#payError").on('click',function () {
  104. console.log('支付失败')
  105. wxCheckOrder();
  106. $(".wx_payment_mask").hide();
  107. })
  108. }
  109. function wxCheckOrder () {
  110. //查询订单是否支付成功
  111. $(".loading").show();
  112. $.ajax({
  113. url:'/user/wx_check_order',
  114. type:'post',
  115. dataType:'json',
  116. data:{
  117. client:1,
  118. order_id:order_id
  119. },
  120. success: function (res){
  121. $(".loading").hide();
  122. if(res && res.errno == 0){
  123. showMsg('恭喜您,订单支付成功!')
  124. }else{
  125. showMsg('订单支付失败,请重新支付!')
  126. }
  127. },
  128. error: function (err){
  129. $(".loading").hide();
  130. }
  131. })
  132. }
  133. function packageSelection (goods_id,money){
  134. //套餐选择事件
  135. goodsId = goods_id;
  136. rechargeMoney = money;
  137. $(".payment_bottom .total span").html('¥' + rechargeMoney)
  138. $(".rechargeLi").removeClass('rechargeLiActive')
  139. $(".rechargeLi_" + goods_id).addClass('rechargeLiActive')
  140. }
  141. //获取地址栏参数
  142. function parseQueryString(url) {
  143. var urlObj = {};
  144. var reg = /([^?=&]+)=([^?=&]+)/g;
  145. url.replace(reg, function($0, $1, $2) {
  146. urlObj[$1] = decodeURIComponent($2);
  147. })
  148. return urlObj;
  149. }
  150. // 展示错误提示信息
  151. function showMsg(msg) {
  152. var msgBox = document.getElementsByClassName('alert-info')[0];
  153. msgBox.getElementsByTagName("p")[0].innerHTML=msg;
  154. msgBox.style.display="block";
  155. setTimeout(function() {
  156. msgBox.style.display="none";
  157. }, 2000);
  158. }