index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. require('./index.less');
  2. import 'jquery.cookie'
  3. import './helper/response';
  4. import Api from './helper/const';
  5. import Utils from './helper/utils';
  6. const channel = Utils.GetQueryString('channel');
  7. const uid = Utils.GetQueryString('uid');
  8. let payWay = 'wx';
  9. let productId = '';
  10. const getStatus = (orderId) => {
  11. // 查询是否成功
  12. $.ajax({
  13. url: Api.composeApiPath(`order/${orderId}`),
  14. dataType: "json",
  15. async: true,
  16. headers: {
  17. uid: uid
  18. },
  19. type: "GET",
  20. success: function (res) {
  21. $.removeCookie('orderId');
  22. $.removeCookie('isPayBack');
  23. if ($.cookie('isWx')) {
  24. $.removeCookie('isWx');
  25. $('.buy-dialog').css('display', 'none')
  26. }
  27. if (res.data.status === 2) {
  28. console.log('支付成功')
  29. system.postMessage('success')
  30. } else {
  31. console.log('支付失败')
  32. system.postMessage('close')
  33. }
  34. }
  35. });
  36. }
  37. // 默认微信支付
  38. $('.wx-choose').addClass('choose');
  39. // 渲染页面
  40. // 修改支付方式
  41. const changePayWay = (flag) => {
  42. // if (typeof system !== 'undefined') {
  43. // system.postMessage(window.location.search)
  44. // }
  45. if (flag === 'wx' && payWay !== 'wx') {
  46. payWay = 'wx';
  47. $('.zfb-choose').removeClass('choose');
  48. $('.wx-choose').addClass('choose');
  49. }
  50. if (flag === 'zfb' && payWay !== 'zfb') {
  51. payWay = 'zfb';
  52. $('.wx-choose').removeClass('choose');
  53. $('.zfb-choose').addClass('choose');
  54. }
  55. }
  56. // 生成订单
  57. const createOrder = (method, id) => {
  58. let str = method === 'zfb' ? '/aliPay/prePay' : '/wxPay/prePay';
  59. $.ajax({
  60. url: Api.composeApiPath(str),
  61. dataType: "json",
  62. async: true,
  63. data: JSON.stringify({
  64. "channel": channel,
  65. "productId": id
  66. }),
  67. type: "POST",
  68. headers: {
  69. uid: uid
  70. },
  71. contentType: "application/json",
  72. success: res => {
  73. $.cookie('orderId', res.data.orderId);
  74. $.cookie('isPayBack', 1);
  75. if (method === 'zfb') {
  76. $('body').append(
  77. $(`<div>${res.data.payInfo}</div>`)
  78. )
  79. } else if (method === 'wx') {
  80. $.cookie('isWx', 1);
  81. window.location.href = res.data.payInfo
  82. }
  83. }
  84. });
  85. }
  86. const chooseProduct = (e) => {
  87. e.stopPropagation();
  88. $('.item').removeClass('focus');
  89. $(e.currentTarget).addClass('focus');
  90. productId = e.currentTarget.dataset.id.toString();
  91. $('.pay-price').html(`¥${e.currentTarget.dataset.price}元`)
  92. }
  93. const getSign = () => {
  94. console.log(123)
  95. }
  96. // 绑定事件
  97. $(document).ready(() => {
  98. if (Utils.GetQueryString('payBack') === '1') {
  99. getStatus($.cookie('orderId'))
  100. }
  101. // 获取验证码
  102. $('#sign_btn').on('click', () => {
  103. getSign();
  104. });
  105. $('.zfb-pay').on('click', () => {
  106. changePayWay('zfb');
  107. });
  108. $('.pay-btn').on('click', () => {
  109. if (payWay && productId) {
  110. createOrder(payWay, productId)
  111. }
  112. });
  113. $('.yes').on('click', () => {
  114. getStatus($.cookie('orderId'))
  115. });
  116. $('.no').on('click', () => {
  117. getStatus($.cookie('orderId'))
  118. });
  119. });