C端小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 line
3.4 KiB

  1. var config = require('../../../config/config.js');
  2. var app = getApp()
  3. const Http = require("../../../utils/HttpBasics")
  4. Page({
  5. data: {
  6. data: {},
  7. couponId: null,
  8. orderId: ''
  9. },
  10. onLoad(e) {
  11. console.log(e)
  12. wx.showLoading({
  13. title: '加载中...',
  14. })
  15. Http.get({
  16. url: Http.config.api.couponDetail,
  17. data: {
  18. id: e.id
  19. }
  20. }).then(res => {
  21. wx.hideLoading();
  22. this.setData({
  23. data: res,
  24. couponId: e.id
  25. })
  26. })
  27. },
  28. /**
  29. * 支付订单更新
  30. */
  31. payOrderUpdate: (orderId, payOrderId, status, reason) => {
  32. // 支付成功
  33. return Http.post({
  34. url: Http.config.api.payOrderUpdate, data: {
  35. payOrderId: payOrderId,
  36. orderId: orderId,
  37. status: status,
  38. reason: reason
  39. }
  40. })
  41. },
  42. newOrderfunc() {
  43. wx.showLoading({
  44. title: '加载中...',
  45. })
  46. Http.post({
  47. url: Http.config.api.orderSave,
  48. data: {
  49. couponId: this.data.couponId
  50. }
  51. }).then(res => {
  52. const orderId = '' + res.id;
  53. this.setData({
  54. orderId: orderId
  55. })
  56. if (res.payment > 0) {
  57. return Http.post({ url: Http.config.api.payOrderCreate, data: { orderId: orderId } })
  58. } else {
  59. // 免费券
  60. this.payOrderUpdate(orderId, "0", 1) // 支付成功
  61. .then(res => {
  62. wx.showToast({
  63. title: "支付成功",
  64. duration: 3000
  65. })
  66. })
  67. }
  68. }).then(res => {
  69. console.log(res)
  70. if (res) {
  71. var payOrderId = '' + res.payOrderId;
  72. wx.hideLoading();
  73. wx.requestPayment({
  74. timeStamp: res.timeStamp,
  75. nonceStr: res.nonceStr,
  76. package: res.package,
  77. signType: 'MD5',
  78. paySign: res.paySign,
  79. 'success': (res) => {
  80. this.payOrderUpdate(this.data.orderId, payOrderId, 1)// 支付成功
  81. console.log(res);
  82. console.log('支付成功');
  83. wx.showToast({
  84. title: '购买成功',
  85. duration: 2500
  86. })
  87. wx.navigateBack({
  88. delta: 2
  89. })
  90. },
  91. 'fail': (res) => {
  92. this.payOrderUpdate(this.data.orderId, payOrderId, 2)// 支付失败
  93. console.log(res)
  94. console.log('支付失败');
  95. return;
  96. },
  97. 'complete': (res) => {
  98. console.log(res);
  99. console.log('支付完成');
  100. var url = this.data.url;
  101. console.log('get url', url)
  102. if (res.errMsg == 'requestPayment:ok') {
  103. wx.showModal({
  104. title: '提示',
  105. content: '支付成功'
  106. });
  107. if (url) {
  108. setTimeout(function () {
  109. wx.redirectTo({
  110. url: '/pages' + url
  111. });
  112. }, 2000)
  113. } else {
  114. setTimeout(() => {
  115. wx.navigateBack()
  116. }, 2000)
  117. }
  118. } else {
  119. wx.showModal({
  120. title: '错误提示',
  121. content: res.errMsg
  122. });
  123. }
  124. return;
  125. }
  126. })
  127. }
  128. })
  129. },
  130. })