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.

220 rader
5.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. orderId: "",
  9. day: "",
  10. hour: "",
  11. minute: ""
  12. },
  13. onLoad(options) {
  14. let that = this;
  15. console.log(options.id);
  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.endTime);
  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. });
  59. });
  60. }
  61. },
  62. /**
  63. * 支付订单更新
  64. */
  65. payOrderUpdate: (orderId, payOrderId, status, reason) => {
  66. // 支付成功
  67. Http.post({
  68. url: config.api.payOrderUpdate,
  69. data: {
  70. payOrderId: payOrderId,
  71. orderId: orderId,
  72. status: status,
  73. reason: reason
  74. }
  75. })
  76. .then(res => {
  77. console.log("payOrderUpdate then", res);
  78. wx.showToast({
  79. title: "购买成功",
  80. duration: 2500
  81. });
  82. })
  83. .catch(err => {
  84. console.log("payOrderUpdate catch", err);
  85. });
  86. },
  87. payment: res => {
  88. var that = this;
  89. var payOrderId = "" + res.data.out_trade_no;
  90. wx.requestPayment({
  91. timeStamp: res.timeStamp,
  92. nonceStr: res.nonceStr,
  93. package: res.package,
  94. signType: "MD5",
  95. paySign: res.paySign,
  96. success: res => {
  97. that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  98. console.log(res);
  99. console.log("支付成功");
  100. wx.showToast({
  101. title: "购买成功",
  102. duration: 2500
  103. });
  104. wx.navigateBack({
  105. delta: 2
  106. });
  107. },
  108. fail: res => {
  109. that.payOrderUpdate(that.data.orderId, payOrderId, 2); // 支付失败
  110. console.log(res);
  111. console.log("支付失败");
  112. return;
  113. },
  114. complete: res => {
  115. console.log(res);
  116. console.log("支付完成");
  117. var url = this.data.url;
  118. console.log("get url", url);
  119. if (res.errMsg == "requestPayment:ok") {
  120. wx.showModal({
  121. title: "提示",
  122. content: "支付成功"
  123. });
  124. if (url) {
  125. setTimeout(function() {
  126. wx.redirectTo({
  127. url: "/pages" + url
  128. });
  129. }, 2000);
  130. } else {
  131. setTimeout(() => {
  132. wx.navigateBack();
  133. }, 2000);
  134. }
  135. } else {
  136. wx.showModal({
  137. title: "错误提示",
  138. content: res.errMsg
  139. });
  140. }
  141. return;
  142. }
  143. });
  144. },
  145. orderFunc() {
  146. var that = this;
  147. wx.showLoading({
  148. title: "加载中..."
  149. });
  150. if (that.data.type == 5) {
  151. // 领取 5.停车券
  152. // TODO 选取用户下的车牌
  153. } else {
  154. Http.post({
  155. url: config.api.checkPhoneStatus,
  156. data: {}
  157. })
  158. .then(res => {
  159. return Http.post({
  160. url: config.api.orderSave,
  161. data: {
  162. couponChannelId: '' + that.data.data.id,
  163. couponId: '' + that.data.data.couponId
  164. }
  165. });
  166. })
  167. .catch(err => {
  168. console.log(err);
  169. if (err.code == 11005) {
  170. // 用户手机未授权
  171. wx.redirectTo({
  172. url: "../../getphoneInfo/index?couponChannelId=" + that.data.id
  173. });
  174. }
  175. if (err.code == 11006) {
  176. // 用户手机已加密
  177. wx.redirectTo({
  178. url: "../../phoneinput/phoneinput?couponChannelId=" + that.data.id
  179. });
  180. }
  181. })
  182. .then(res => {
  183. if (res != "undefined") {
  184. const orderId = "" + res.data.id;
  185. that.setData({
  186. orderId: orderId
  187. });
  188. if (res.data.payment > 0) {
  189. // 支付金额不为0
  190. Http.post({
  191. url: config.api.payOrderCreate,
  192. data: {
  193. orderId: orderId
  194. }
  195. }).then(res => {
  196. console.log(res);
  197. var payOrderId = "" + res.data.out_trade_no;
  198. wx.hideLoading();
  199. //payment(res);
  200. that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  201. });
  202. } else {
  203. // 免费券
  204. that.payOrderUpdate(orderId, "0", 1) // 支付成功
  205. .then(res => {
  206. wx.showToast({
  207. title: "支付成功",
  208. duration: 3000
  209. });
  210. });
  211. }
  212. }
  213. })
  214. .catch(err => {
  215. console.log(err);
  216. });
  217. }
  218. }
  219. });