C端小程序
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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