C端小程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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