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.

226 line
6.0 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. console.log(options.id);
  17. that.setData({
  18. couponId:options.id
  19. })
  20. console.log(options.targetAd);
  21. wx.showLoading({
  22. title: "加载中..."
  23. });
  24. if (options.flag) {
  25. this.orderFunc();
  26. } else {
  27. Http.get({
  28. url: config.api.couponDetail,
  29. data: {
  30. couponId: options.id,
  31. targetAd:options.targetAd
  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. couponId: that.data.couponId
  169. }
  170. });
  171. })
  172. .catch(err => {
  173. console.log(err);
  174. if (err.code == 11005) {
  175. // 用户手机未授权
  176. wx.redirectTo({
  177. url: "../../getphoneInfo/index?couponId=" + that.data.couponId
  178. });
  179. }
  180. if (err.code == 11006) {
  181. // 用户手机已加密
  182. wx.redirectTo({
  183. url: "../../phoneinput/phoneinput?couponId=" + that.data.couponId
  184. });
  185. }
  186. })
  187. .then(res => {
  188. if (res != "undefined") {
  189. const orderId = "" + res.data.id;
  190. that.setData({
  191. orderId: orderId
  192. });
  193. if (res.data.payment > 0) {
  194. // 支付金额不为0
  195. Http.post({
  196. url: config.api.payOrderCreate,
  197. data: {
  198. orderId: orderId
  199. }
  200. }).then(res => {
  201. console.log(res);
  202. var payOrderId = "" + res.data.out_trade_no;
  203. wx.hideLoading();
  204. //payment(res);
  205. that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  206. });
  207. } else {
  208. // 免费券
  209. that
  210. .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. });