C端小程序
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
4.0 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,
  35. data: {
  36. payOrderId: payOrderId,
  37. orderId: orderId,
  38. status: status,
  39. reason: reason
  40. }
  41. })
  42. },
  43. payment: (res) => {
  44. var that = this;
  45. var payOrderId = '' + res.payOrderId;
  46. wx.requestPayment({
  47. timeStamp: res.timeStamp,
  48. nonceStr: res.nonceStr,
  49. package: res.package,
  50. signType: 'MD5',
  51. paySign: res.paySign,
  52. 'success': (res) => {
  53. that.payOrderUpdate(that.data.orderId, payOrderId, 1) // 支付成功
  54. console.log(res);
  55. console.log('支付成功');
  56. wx.showToast({
  57. title: '购买成功',
  58. duration: 2500
  59. })
  60. wx.navigateBack({
  61. delta: 2
  62. })
  63. },
  64. 'fail': (res) => {
  65. that.payOrderUpdate(that.data.orderId, payOrderId, 2) // 支付失败
  66. console.log(res)
  67. console.log('支付失败');
  68. return;
  69. },
  70. 'complete': (res) => {
  71. console.log(res);
  72. console.log('支付完成');
  73. var url = this.data.url;
  74. console.log('get url', url)
  75. if (res.errMsg == 'requestPayment:ok') {
  76. wx.showModal({
  77. title: '提示',
  78. content: '支付成功'
  79. });
  80. if (url) {
  81. setTimeout(function() {
  82. wx.redirectTo({
  83. url: '/pages' + url
  84. });
  85. }, 2000)
  86. } else {
  87. setTimeout(() => {
  88. wx.navigateBack()
  89. }, 2000)
  90. }
  91. } else {
  92. wx.showModal({
  93. title: '错误提示',
  94. content: res.errMsg
  95. });
  96. }
  97. return;
  98. }
  99. })
  100. },
  101. orderFunc() {
  102. var that = this;
  103. wx.showLoading({
  104. title: '加载中...',
  105. })
  106. Http.post({
  107. url: config.api.checkPhoneStatus,
  108. data: {}
  109. })
  110. .then(res => {
  111. return Http.post({
  112. url: Http.config.api.orderSave,
  113. data: {
  114. couponId: that.data.couponId
  115. }
  116. })
  117. }).catch(err => {
  118. console.log(err)
  119. if (err.code == 11005) {
  120. // 用户手机未授权
  121. wx.redirectTo({
  122. url: '../../getphoneInfo/index',
  123. })
  124. }
  125. if (err.code == 11006) {
  126. // 用户手机已加密
  127. wx.redirectTo({
  128. url: '../../phoneinput/phoneinput',
  129. })
  130. }
  131. })
  132. .then(res => {
  133. const orderId = '' + res.data.id;
  134. that.setData({
  135. orderId: orderId
  136. })
  137. if (res.data.payment > 0) {
  138. // real pay
  139. return Http.post({
  140. url: Http.config.api.payOrderCreate,
  141. data: {
  142. orderId: orderId
  143. }
  144. })
  145. } else {
  146. // 免费券
  147. that.payOrderUpdate(orderId, "0", 1) // 支付成功
  148. .then(res => {
  149. wx.showToast({
  150. title: "支付成功",
  151. duration: 3000
  152. })
  153. })
  154. }
  155. }).then(res => {
  156. console.log(res)
  157. if (res) {
  158. var payOrderId = '' + res.payOrderId;
  159. wx.hideLoading();
  160. //payment(res);
  161. that.payOrderUpdate(that.data.orderId, payOrderId, 1) // 支付成功
  162. }
  163. })
  164. },
  165. })