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.

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