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.

228 lignes
6.9 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.couponChannelId);
  18. that.setData({
  19. couponChannelId: options.couponChannelId,
  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.couponChannelId,
  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. orderFunc() {
  94. var that = this;
  95. wx.showLoading({
  96. title: "加载中..."
  97. });
  98. if (that.data.type == 5) {
  99. // 领取 5.停车券
  100. // TODO 选取用户下的车牌
  101. } else {
  102. Http.post({
  103. url: config.api.checkPhoneStatus,
  104. data: {}
  105. })
  106. .then(res => {
  107. return Http.post({
  108. url: config.api.orderSave,
  109. data: {
  110. couponChannelId: '' + that.data.couponChannelId,
  111. couponId: '' + that.data.couponId
  112. }
  113. });
  114. })
  115. .catch(err => {
  116. console.log(err);
  117. if (err.code == 11005) {
  118. // 用户手机未授权
  119. wx.redirectTo({
  120. url: "../../getphoneInfo/index?couponChannelId=" + that.data.couponChannelId + "&couponId=" + that.data.couponId
  121. });
  122. }
  123. if (err.code == 11006) {
  124. // 用户手机已加密
  125. wx.redirectTo({
  126. url: "../../phoneinput/phoneinput?couponChannelId=" + that.data.couponChannelId + "& couponId=" + that.data.couponId
  127. });
  128. }
  129. })
  130. .then(res => {
  131. if (res != "undefined") {
  132. const orderId = "" + res.data.id;
  133. that.setData({
  134. orderId: orderId
  135. });
  136. if (res.data.payment > 0) {
  137. // 支付金额不为0
  138. Http.post({
  139. url: config.api.payOrderCreate,
  140. data: {
  141. orderId: orderId
  142. }
  143. }).then(res => {
  144. console.log(res);
  145. /// Begin payment ----
  146. var payOrderId = "" + res.data.payOrderId;
  147. wx.hideLoading();
  148. wx.requestPayment({
  149. timeStamp: res.data.timeStamp,
  150. nonceStr: res.data.nonceStr,
  151. package: res.data.package,
  152. signType: "MD5",
  153. paySign: res.data.paySign,
  154. success: res => {
  155. that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  156. console.log(res);
  157. console.log("支付成功");
  158. wx.showToast({
  159. title: "购买成功",
  160. duration: 2500
  161. });
  162. wx.navigateBack({
  163. delta: 2
  164. });
  165. },
  166. fail: res => {
  167. that.payOrderUpdate(that.data.orderId, payOrderId, 2); // 支付失败
  168. console.log(res);
  169. console.log("支付失败");
  170. return;
  171. },
  172. complete: res => {
  173. console.log(res);
  174. console.log("支付完成");
  175. var url = res.data.url;
  176. console.log("get url", url);
  177. if (res.errMsg == "requestPayment:ok") {
  178. wx.showModal({
  179. title: "提示",
  180. content: "支付成功"
  181. });
  182. if (url) {
  183. setTimeout(function () {
  184. wx.redirectTo({
  185. url: "/pages" + url
  186. });
  187. }, 2000);
  188. } else {
  189. setTimeout(() => {
  190. wx.navigateBack();
  191. }, 2000);
  192. }
  193. } else {
  194. wx.showModal({
  195. title: "错误提示",
  196. content: res.errMsg
  197. });
  198. }
  199. return;
  200. }
  201. });
  202. /// End payment --------
  203. ///// virtual pay
  204. //var payOrderId = "" + res.data.out_trade_no;
  205. //that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  206. ///// end virtual pay
  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. });