C端小程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

253 строки
7.4 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. couponChannelId: 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. /**
  94. * 发起支付
  95. */
  96. orderFunc() {
  97. var that = this;
  98. wx.showLoading({
  99. title: "加载中..."
  100. });
  101. if (that.data.type == 5) {
  102. // 领取 5.停车券
  103. // TODO 选取用户下的车牌
  104. } else {
  105. Http.post({
  106. url: config.api.checkPhoneStatus,
  107. data: {}
  108. })
  109. .then(res => {
  110. console.log(res);
  111. /**
  112. * orderSave 下单
  113. */
  114. return Http.post({
  115. url: config.api.orderSave,
  116. data: {
  117. couponChannelId: "" + that.data.couponChannelId,
  118. couponId: "" + that.data.couponId
  119. }
  120. });
  121. })
  122. .catch(err => {
  123. console.log(err);
  124. if (err.code == 11005) {
  125. // 用户手机未授权
  126. wx.redirectTo({
  127. url:
  128. "../../getphoneInfo/index?couponChannelId=" +
  129. that.data.couponChannelId +
  130. "&couponId=" +
  131. that.data.couponId
  132. });
  133. }
  134. if (err.code == 11006) {
  135. // 用户手机已加密
  136. wx.redirectTo({
  137. url:
  138. "../../phoneinput/phoneinput?couponChannelId=" +
  139. that.data.couponChannelId +
  140. "& couponId=" +
  141. that.data.couponId
  142. });
  143. }
  144. })
  145. .then(res => {
  146. console.log(res);
  147. console.log("点击领取卡券一直在loading");
  148. if (res != "undefined") {
  149. const orderId = "" + res.data.id;
  150. that.setData({
  151. orderId: orderId
  152. });
  153. if (res.data.payment > 0) {
  154. // 支付金额不为0
  155. /**
  156. * 支付订单创建
  157. */
  158. Http.post({
  159. url: config.api.payOrderCreate,
  160. data: {
  161. orderId: orderId
  162. }
  163. }).then(res => {
  164. console.log(res);
  165. /// Begin payment ----
  166. var payOrderId = "" + res.data.payOrderId;
  167. wx.hideLoading();
  168. wx.requestPayment({
  169. timeStamp: res.data.timeStamp,
  170. nonceStr: res.data.nonceStr,
  171. package: res.data.package,
  172. signType: "MD5",
  173. paySign: res.data.paySign,
  174. success: res => {
  175. console.log(res);
  176. console.log("姐在检查付款异常");
  177. that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  178. console.log(res);
  179. if (res.errMsg == "requestPayment:ok") {
  180. wx.showToast({
  181. title: "购买成功",
  182. duration: 2500
  183. });
  184. }
  185. wx.navigateBack({
  186. delta: 2
  187. });
  188. },
  189. fail: res => {
  190. that.payOrderUpdate(that.data.orderId, payOrderId, 2); // 支付失败
  191. console.log(res);
  192. return;
  193. },
  194. complete: res => {
  195. console.log(res);
  196. console.log("支付完成");
  197. // var url = res.data.url;
  198. // console.log("get url", url);
  199. if (res.errMsg == "requestPayment:ok") {
  200. wx.showModal({
  201. title: "提示",
  202. content: "支付成功"
  203. });
  204. // if (url) {
  205. // setTimeout(function () {
  206. // wx.redirectTo({
  207. // url: "/pages" + url
  208. // });
  209. // }, 2000);
  210. // } else {
  211. // setTimeout(() => {
  212. // wx.navigateBack();
  213. // }, 2000);
  214. // }
  215. } else {
  216. wx.showToast({
  217. title: "支付失败",
  218. image: "./../../../assets/img/fail.png",
  219. duration: 1500,
  220. mask: false
  221. });
  222. }
  223. return;
  224. }
  225. });
  226. /// End payment --------
  227. ///// virtual pay
  228. //var payOrderId = "" + res.data.out_trade_no;
  229. //that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  230. ///// end virtual pay
  231. });
  232. } else {
  233. // 免费券
  234. that.payOrderUpdate(orderId, "0", 1);
  235. wx.showToast({
  236. title: "领取成功",
  237. duration: 3000
  238. });
  239. }
  240. }
  241. })
  242. .catch(err => {
  243. console.log(err);
  244. });
  245. }
  246. }
  247. });