C端小程序
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

217 righe
5.6 KiB

  1. var config = require("../../../config/config.js");
  2. var app = getApp();
  3. const Http = require("../../../utils/HttpBasics");
  4. const util = require("../../../utils/util");
  5. Page({
  6. data: {
  7. data: {},
  8. couponId: null,
  9. orderId: "",
  10. day: "",
  11. hour: "",
  12. minute: ""
  13. },
  14. onLoad(options) {
  15. let that = this;
  16. wx.showLoading({
  17. title: "加载中..."
  18. });
  19. if (options.flag) {
  20. this.orderFunc();
  21. } else {
  22. Http.get({
  23. url: config.api.couponDetail,
  24. data: {
  25. id: options.id
  26. }
  27. }).then(res => {
  28. console.log(res);
  29. //当前时间与优惠券下架时间做计算
  30. var startTime = util.fmtDate(res.data.sendEndDate);
  31. console.log(startTime);
  32. var s1 = new Date(startTime.replace(/-/g, "/"));
  33. var s2 = new Date();
  34. var runTime = parseInt((s1.getTime() - s2.getTime()) / 1000);
  35. var year = Math.floor(runTime / 86400 / 365);
  36. var runTime = runTime % (86400 * 365);
  37. var month = Math.floor(runTime / 86400 / 30);
  38. var runTime = runTime % (86400 * 30);
  39. var day = Math.floor(runTime / 86400);
  40. var runTime = runTime % 86400;
  41. var hour = Math.floor(runTime / 3600);
  42. var runTime = runTime % 3600;
  43. var minute = Math.floor(runTime / 60);
  44. var runTime = runTime % 60;
  45. var second = runTime;
  46. console.log(year, month, day, hour, minute, second);
  47. that.setData({
  48. year: year,
  49. month: month,
  50. day: day,
  51. hour: hour,
  52. minute: minute
  53. });
  54. console.log(that.data.day);
  55. wx.hideLoading();
  56. that.setData({
  57. data: res.data,
  58. couponId: options.id
  59. });
  60. });
  61. }
  62. },
  63. /**
  64. * 支付订单更新
  65. */
  66. payOrderUpdate: (orderId, payOrderId, status, reason) => {
  67. // 支付成功
  68. Http.post({
  69. url: config.api.payOrderUpdate,
  70. data: {
  71. payOrderId: payOrderId,
  72. orderId: orderId,
  73. status: status,
  74. reason: reason
  75. }
  76. })
  77. .then(res => {
  78. console.log("payOrderUpdate then", res);
  79. wx.showToast({
  80. title: "购买成功",
  81. duration: 2500
  82. });
  83. })
  84. .catch(err => {
  85. console.log("payOrderUpdate catch", err);
  86. });
  87. },
  88. payment: res => {
  89. var that = this;
  90. var payOrderId = "" + res.data.out_trade_no;
  91. wx.requestPayment({
  92. timeStamp: res.timeStamp,
  93. nonceStr: res.nonceStr,
  94. package: res.package,
  95. signType: "MD5",
  96. paySign: res.paySign,
  97. success: res => {
  98. that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  99. console.log(res);
  100. console.log("支付成功");
  101. wx.showToast({
  102. title: "购买成功",
  103. duration: 2500
  104. });
  105. wx.navigateBack({
  106. delta: 2
  107. });
  108. },
  109. fail: res => {
  110. that.payOrderUpdate(that.data.orderId, payOrderId, 2); // 支付失败
  111. console.log(res);
  112. console.log("支付失败");
  113. return;
  114. },
  115. complete: res => {
  116. console.log(res);
  117. console.log("支付完成");
  118. var url = this.data.url;
  119. console.log("get url", url);
  120. if (res.errMsg == "requestPayment:ok") {
  121. wx.showModal({
  122. title: "提示",
  123. content: "支付成功"
  124. });
  125. if (url) {
  126. setTimeout(function() {
  127. wx.redirectTo({
  128. url: "/pages" + url
  129. });
  130. }, 2000);
  131. } else {
  132. setTimeout(() => {
  133. wx.navigateBack();
  134. }, 2000);
  135. }
  136. } else {
  137. wx.showModal({
  138. title: "错误提示",
  139. content: res.errMsg
  140. });
  141. }
  142. return;
  143. }
  144. });
  145. },
  146. orderFunc() {
  147. var that = this;
  148. wx.showLoading({
  149. title: "加载中..."
  150. });
  151. Http.post({
  152. url: config.api.checkPhoneStatus,
  153. data: {}
  154. })
  155. .then(res => {
  156. return Http.post({
  157. url: config.api.orderSave,
  158. data: {
  159. couponId: that.data.couponId
  160. }
  161. });
  162. })
  163. .catch(err => {
  164. console.log(err);
  165. if (err.code == 11005) {
  166. // 用户手机未授权
  167. wx.redirectTo({
  168. url: "../../getphoneInfo/index?couponId=" + that.data.couponId
  169. });
  170. }
  171. if (err.code == 11006) {
  172. // 用户手机已加密
  173. wx.redirectTo({
  174. url: "../../phoneinput/phoneinput?couponId=" + that.data.couponId
  175. });
  176. }
  177. })
  178. .then(res => {
  179. if (res != "undefined") {
  180. const orderId = "" + res.data.id;
  181. that.setData({
  182. orderId: orderId
  183. });
  184. if (res.data.payment > 0) {
  185. // 支付金额不为0
  186. Http.post({
  187. url: config.api.payOrderCreate,
  188. data: {
  189. orderId: orderId
  190. }
  191. }).then(res => {
  192. console.log(res);
  193. var payOrderId = "" + res.data.out_trade_no;
  194. wx.hideLoading();
  195. //payment(res);
  196. that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  197. });
  198. } else {
  199. // 免费券
  200. that
  201. .payOrderUpdate(orderId, "0", 1) // 支付成功
  202. .then(res => {
  203. wx.showToast({
  204. title: "支付成功",
  205. duration: 3000
  206. });
  207. });
  208. }
  209. }
  210. })
  211. .catch(err => {
  212. console.log(err);
  213. });
  214. }
  215. });