index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. $.ajax({
  40. url: Api.composeApiPath('order/product'),
  41. dataType: "json",
  42. async: true,
  43. data: {
  44. "channel": channel
  45. },
  46. headers: {
  47. uid: uid
  48. },
  49. type: "GET",
  50. success: function (res) {
  51. renderProductList(res.data)
  52. }
  53. });
  54. // 渲染页面
  55. const renderProductList = (list) => {
  56. const box = $("<div class='product-list'></div>")
  57. list.forEach((item, index) => {
  58. if (index === 0) {
  59. productId = item.id;
  60. $('.pay-price').html(`¥${item.price / 100}元`)
  61. }
  62. box.append(
  63. $(`<div data-price="${item.price / 100}" data-id="${item.id}" class="item ${index === 0 ? 'focus' : ''}">
  64. <span class="time">${item.payType === 'YEAR' ? '12个月':item.payType === 'MONTH'?'1个月':'连续包月'}</span>
  65. <span class="price">¥${item.price / 100}元</span>
  66. <span class="origin-price">原价: ¥${item.originalPrice / 100}元</span>
  67. </div>`).on('click', chooseProduct)
  68. )
  69. })
  70. $('.product-box').append(box);
  71. }
  72. // 修改支付方式
  73. const changePayWay = (flag) => {
  74. // if (typeof system !== 'undefined') {
  75. // system.postMessage(window.location.search)
  76. // }
  77. if (flag === 'wx' && payWay !== 'wx') {
  78. payWay = 'wx';
  79. $('.zfb-choose').removeClass('choose');
  80. $('.wx-choose').addClass('choose');
  81. }
  82. if (flag === 'zfb' && payWay !== 'zfb') {
  83. payWay = 'zfb';
  84. $('.wx-choose').removeClass('choose');
  85. $('.zfb-choose').addClass('choose');
  86. }
  87. }
  88. // 生成订单
  89. const createOrder = (method, id) => {
  90. let str = method === 'zfb' ? '/aliPay/prePay' : '/wxPay/prePay';
  91. $.ajax({
  92. url: Api.composeApiPath(str),
  93. dataType: "json",
  94. async: true,
  95. data: JSON.stringify({
  96. "channel": channel,
  97. "productId": id
  98. }),
  99. type: "POST",
  100. headers: {
  101. uid: uid
  102. },
  103. contentType: "application/json",
  104. success: res => {
  105. $.cookie('orderId', res.data.orderId);
  106. $.cookie('isPayBack', 1);
  107. if (method === 'zfb') {
  108. $('body').append(
  109. $(`<div>${res.data.payInfo}</div>`)
  110. )
  111. } else if (method === 'wx') {
  112. $.cookie('isWx', 1);
  113. window.location.href = res.data.payInfo
  114. }
  115. }
  116. });
  117. }
  118. const chooseProduct = (e) => {
  119. e.stopPropagation();
  120. $('.item').removeClass('focus');
  121. $(e.currentTarget).addClass('focus');
  122. productId = e.currentTarget.dataset.id.toString();
  123. $('.pay-price').html(`¥${e.currentTarget.dataset.price}元`)
  124. }
  125. // 绑定事件
  126. $(document).ready(() => {
  127. if ($.cookie('isPayBack') === '1' && $.cookie('isWx') === '1') {
  128. $('.buy-dialog').css('display', 'flex')
  129. }
  130. if (Utils.GetQueryString('payBack') === '1') {
  131. getStatus($.cookie('orderId'))
  132. }
  133. $('.wx-pay').on('click', () => {
  134. changePayWay('wx');
  135. });
  136. $('.zfb-pay').on('click', () => {
  137. changePayWay('zfb');
  138. });
  139. $('.pay-btn').on('click', () => {
  140. if (payWay && productId) {
  141. createOrder(payWay, productId)
  142. }
  143. });
  144. $('.yes').on('click', () => {
  145. getStatus($.cookie('orderId'))
  146. });
  147. $('.no').on('click', () => {
  148. getStatus($.cookie('orderId'))
  149. });
  150. });