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.

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