C端小程序
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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