C端小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
5.8 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. if (that.data.type == 5) {
  152. // 领取 5.停车券
  153. // TODO 选取用户下的车牌
  154. } else {
  155. Http.post({
  156. url: config.api.checkPhoneStatus,
  157. data: {}
  158. })
  159. .then(res => {
  160. return Http.post({
  161. url: config.api.orderSave,
  162. data: {
  163. couponId: that.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?couponId=" + that.data.couponId
  173. });
  174. }
  175. if (err.code == 11006) {
  176. // 用户手机已加密
  177. wx.redirectTo({
  178. url: "../../phoneinput/phoneinput?couponId=" + that.data.couponId
  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
  205. .payOrderUpdate(orderId, "0", 1) // 支付成功
  206. .then(res => {
  207. wx.showToast({
  208. title: "支付成功",
  209. duration: 3000
  210. });
  211. });
  212. }
  213. }
  214. })
  215. .catch(err => {
  216. console.log(err);
  217. });
  218. }
  219. }
  220. });