C端小程序
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

228 行
6.9 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. couponChannelId: options.id,
  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. id: options.id,
  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. orderFunc() {
  94. var that = this;
  95. wx.showLoading({
  96. title: "加载中..."
  97. });
  98. if (that.data.type == 5) {
  99. // 领取 5.停车券
  100. // TODO 选取用户下的车牌
  101. } else {
  102. Http.post({
  103. url: config.api.checkPhoneStatus,
  104. data: {}
  105. })
  106. .then(res => {
  107. return Http.post({
  108. url: config.api.orderSave,
  109. data: {
  110. couponChannelId: '' + that.data.couponChannelId,
  111. couponId: '' + that.data.couponId
  112. }
  113. });
  114. })
  115. .catch(err => {
  116. console.log(err);
  117. if (err.code == 11005) {
  118. // 用户手机未授权
  119. wx.redirectTo({
  120. url: "../../getphoneInfo/index?couponChannelId=" + that.data.couponChannelId + "&couponId=" + that.data.couponId
  121. });
  122. }
  123. if (err.code == 11006) {
  124. // 用户手机已加密
  125. wx.redirectTo({
  126. url: "../../phoneinput/phoneinput?couponChannelId=" + that.data.couponChannelId + "& couponId=" + that.data.couponId
  127. });
  128. }
  129. })
  130. .then(res => {
  131. if (res != "undefined") {
  132. const orderId = "" + res.data.id;
  133. that.setData({
  134. orderId: orderId
  135. });
  136. if (res.data.payment > 0) {
  137. // 支付金额不为0
  138. Http.post({
  139. url: config.api.payOrderCreate,
  140. data: {
  141. orderId: orderId
  142. }
  143. }).then(res => {
  144. console.log(res);
  145. /*
  146. /// Begin payment ----
  147. var payOrderId = "" + res.data.payOrderId;
  148. wx.hideLoading();
  149. wx.requestPayment({
  150. timeStamp: res.data.timeStamp,
  151. nonceStr: res.data.nonceStr,
  152. package: res.data.package,
  153. signType: "MD5",
  154. paySign: res.data.paySign,
  155. success: res => {
  156. that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  157. console.log(res);
  158. console.log("支付成功");
  159. wx.showToast({
  160. title: "购买成功",
  161. duration: 2500
  162. });
  163. wx.navigateBack({
  164. delta: 2
  165. });
  166. },
  167. fail: res => {
  168. that.payOrderUpdate(that.data.orderId, payOrderId, 2); // 支付失败
  169. console.log(res);
  170. console.log("支付失败");
  171. return;
  172. },
  173. complete: res => {
  174. console.log(res);
  175. console.log("支付完成");
  176. var url = res.data.url;
  177. console.log("get url", url);
  178. if (res.errMsg == "requestPayment:ok") {
  179. wx.showModal({
  180. title: "提示",
  181. content: "支付成功"
  182. });
  183. if (url) {
  184. setTimeout(function () {
  185. wx.redirectTo({
  186. url: "/pages" + url
  187. });
  188. }, 2000);
  189. } else {
  190. setTimeout(() => {
  191. wx.navigateBack();
  192. }, 2000);
  193. }
  194. } else {
  195. wx.showModal({
  196. title: "错误提示",
  197. content: res.errMsg
  198. });
  199. }
  200. return;
  201. }
  202. });
  203. /// End payment --------
  204. */
  205. ///// virtual pay
  206. var payOrderId = "" + res.data.out_trade_no;
  207. that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  208. ///// end virtual pay
  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. });