C端小程序
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

248 рядки
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: "../../getphoneInfo/index?couponChannelId=" + that.data.couponChannelId + "&couponId=" + that.data.couponId
  128. });
  129. }
  130. if (err.code == 11006) {
  131. // 用户手机已加密
  132. wx.redirectTo({
  133. url: "../../phoneinput/phoneinput?couponChannelId=" + that.data.couponChannelId + "& couponId=" + that.data.couponId
  134. });
  135. }
  136. })
  137. .then(res => {
  138. if (res != "undefined") {
  139. const orderId = "" + res.data.id;
  140. that.setData({
  141. orderId: orderId
  142. });
  143. if (res.data.payment > 0) {
  144. // 支付金额不为0
  145. /**
  146. * 支付订单创建
  147. */
  148. Http.post({
  149. url: config.api.payOrderCreate,
  150. data: {
  151. orderId: orderId
  152. }
  153. }).then(res => {
  154. console.log(res);
  155. /// Begin payment ----
  156. var payOrderId = "" + res.data.payOrderId;
  157. wx.hideLoading();
  158. wx.requestPayment({
  159. timeStamp: res.data.timeStamp,
  160. nonceStr: res.data.nonceStr,
  161. package: res.data.package,
  162. signType: "MD5",
  163. paySign: res.data.paySign,
  164. success: res => {
  165. console.log(res);
  166. console.log("姐在检查付款异常");
  167. that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  168. console.log(res);
  169. if(res.errMsg=='requestPayment:ok'){
  170. wx.showToast({
  171. title: "购买成功",
  172. duration: 2500
  173. });
  174. }
  175. wx.navigateBack({
  176. delta: 2
  177. });
  178. },
  179. fail: res => {
  180. that.payOrderUpdate(that.data.orderId, payOrderId, 2); // 支付失败
  181. console.log(res);
  182. return;
  183. },
  184. complete: res => {
  185. console.log(res);
  186. console.log("支付完成");
  187. // var url = res.data.url;
  188. // console.log("get url", url);
  189. if (res.errMsg == "requestPayment:ok") {
  190. wx.showModal({
  191. title: "提示",
  192. content: "支付成功"
  193. });
  194. // if (url) {
  195. // setTimeout(function () {
  196. // wx.redirectTo({
  197. // url: "/pages" + url
  198. // });
  199. // }, 2000);
  200. // } else {
  201. // setTimeout(() => {
  202. // wx.navigateBack();
  203. // }, 2000);
  204. // }
  205. }
  206. else {
  207. wx.showToast({
  208. title:'支付失败',
  209. image:'./../../../assets/img/fail.png',
  210. duration: 1500,
  211. mask:false
  212. })
  213. }
  214. return;
  215. }
  216. });
  217. /// End payment --------
  218. ///// virtual pay
  219. //var payOrderId = "" + res.data.out_trade_no;
  220. //that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  221. ///// end virtual pay
  222. });
  223. } else {
  224. // 免费券
  225. that.payOrderUpdate(orderId, "0", 1) // 支付成功
  226. .then(res => {
  227. wx.showToast({
  228. title: "支付成功",
  229. duration: 3000
  230. });
  231. });
  232. }
  233. }
  234. })
  235. .catch(err => {
  236. console.log(err);
  237. });
  238. }
  239. }
  240. });