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.

284 line
8.2 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);
  18. console.log(options.couponChannelId);
  19. console.log(options.couponId);
  20. console.log("传的变量的名字叫Id");
  21. /**
  22. * 首页banner detail couponlist传过来的值
  23. * iambannercoupon
  24. */
  25. wx.showLoading({
  26. title: "加载中..."
  27. });
  28. if (options.flag) {
  29. this.orderFunc();
  30. } else {
  31. if (options.couponChannelId == "iambannercoupon") {
  32. that.setData({
  33. couponId: options.couponId
  34. });
  35. /**
  36. * 待解决
  37. */
  38. var parmer = {
  39. url: config.api.bannercoupondetai,
  40. data: {
  41. couponId: options.couponId
  42. }
  43. };
  44. } else {
  45. that.setData({
  46. couponChannelId: options.couponChannelId,
  47. couponId: options.couponId
  48. });
  49. var parmer = {
  50. url: config.api.couponDetail,
  51. data: {
  52. couponChannelId: options.couponChannelId
  53. }
  54. };
  55. }
  56. Http.get(parmer).then(res => {
  57. console.log(res);
  58. console.log("details");
  59. //当前时间与优惠券下架时间做计算
  60. var startTime = util.fmtDate(res.data.endTime);
  61. console.log(startTime);
  62. var s1 = new Date(startTime.replace(/-/g, "/"));
  63. var s2 = new Date();
  64. var runTime = parseInt((s1.getTime() - s2.getTime()) / 1000);
  65. var year = Math.floor(runTime / 86400 / 365);
  66. var runTime = runTime % (86400 * 365);
  67. var month = Math.floor(runTime / 86400 / 30);
  68. var runTime = runTime % (86400 * 30);
  69. var day = Math.floor(runTime / 86400);
  70. var runTime = runTime % 86400;
  71. var hour = Math.floor(runTime / 3600);
  72. var runTime = runTime % 3600;
  73. var minute = Math.floor(runTime / 60);
  74. var runTime = runTime % 60;
  75. var second = runTime;
  76. console.log(year, month, day, hour, minute, second);
  77. that.setData({
  78. year: year,
  79. month: month,
  80. day: day,
  81. hour: hour,
  82. minute: minute
  83. });
  84. console.log(that.data.day);
  85. wx.hideLoading();
  86. that.setData({
  87. data: res.data
  88. });
  89. });
  90. }
  91. },
  92. /**
  93. * 支付订单更新
  94. */
  95. payOrderUpdate: (orderId, payOrderId, status, reason) => {
  96. // 支付成功
  97. Http.post({
  98. url: config.api.payOrderUpdate,
  99. data: {
  100. payOrderId: payOrderId,
  101. orderId: orderId,
  102. status: status,
  103. reason: reason
  104. }
  105. })
  106. .then(res => {
  107. console.log("payOrderUpdate then", res);
  108. // wx.showToast({
  109. // title: "购买成功",
  110. // duration: 2500
  111. // });
  112. })
  113. .catch(err => {
  114. console.log("payOrderUpdate catch", err);
  115. });
  116. },
  117. /**
  118. * 发起支付
  119. */
  120. orderFunc() {
  121. var that = this;
  122. wx.showLoading({
  123. title: "加载中..."
  124. });
  125. if (that.data.type == 5) {
  126. // 领取 5.停车券
  127. // TODO 选取用户下的车牌
  128. } else {
  129. Http.post({
  130. url: config.api.checkPhoneStatus,
  131. data: {}
  132. })
  133. .then(res => {
  134. console.log(res);
  135. var data = {
  136. couponChannelId: "" + that.data.couponChannelId,
  137. couponId: "" + that.data.couponId
  138. }
  139. if (that.data.couponChannelId == null) {
  140. data = {
  141. couponId: "" + that.data.couponId
  142. }
  143. }
  144. /**
  145. * orderSave 下单
  146. */
  147. return Http.post({
  148. url: config.api.orderSave,
  149. data: data
  150. });
  151. })
  152. .catch(err => {
  153. console.log(err);
  154. if (err.code == 11005) {
  155. // 用户手机未授权
  156. wx.redirectTo({
  157. url:
  158. "../../getphoneInfo/index?couponChannelId=" +
  159. that.data.couponChannelId +
  160. "&couponId=" +
  161. that.data.couponId
  162. });
  163. }
  164. if (err.code == 11006) {
  165. // 用户手机已加密
  166. wx.redirectTo({
  167. url:
  168. "../../phoneinput/phoneinput?couponChannelId=" +
  169. that.data.couponChannelId +
  170. "& couponId=" +
  171. that.data.couponId
  172. });
  173. }
  174. })
  175. .then(res => {
  176. console.log(res);
  177. console.log("点击领取卡券一直在loading");
  178. if (res != "undefined") {
  179. const orderId = "" + res.data.id;
  180. that.setData({
  181. orderId: orderId
  182. });
  183. if (res.data.payment > 0) {
  184. // 支付金额不为0
  185. /**
  186. * 支付订单创建
  187. */
  188. Http.post({
  189. url: config.api.payOrderCreate,
  190. data: {
  191. orderId: orderId
  192. }
  193. }).then(res => {
  194. console.log(res);
  195. /// Begin payment ----
  196. var payOrderId = "" + res.data.payOrderId;
  197. wx.hideLoading();
  198. wx.requestPayment({
  199. timeStamp: res.data.timeStamp,
  200. nonceStr: res.data.nonceStr,
  201. package: res.data.package,
  202. signType: "MD5",
  203. paySign: res.data.paySign,
  204. success: res => {
  205. console.log(res);
  206. console.log("姐在检查付款异常");
  207. that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  208. console.log(res);
  209. if (res.errMsg == "requestPayment:ok") {
  210. wx.showToast({
  211. title: "购买成功",
  212. duration: 2500
  213. });
  214. }
  215. wx.navigateBack({
  216. delta: 2
  217. });
  218. },
  219. fail: res => {
  220. that.payOrderUpdate(that.data.orderId, payOrderId, 2); // 支付失败
  221. console.log(res);
  222. return;
  223. },
  224. complete: res => {
  225. console.log(res);
  226. console.log("支付完成");
  227. // var url = res.data.url;
  228. // console.log("get url", url);
  229. if (res.errMsg == "requestPayment:ok") {
  230. wx.showModal({
  231. title: "提示",
  232. content: "支付成功"
  233. });
  234. // if (url) {
  235. // setTimeout(function () {
  236. // wx.redirectTo({
  237. // url: "/pages" + url
  238. // });
  239. // }, 2000);
  240. // } else {
  241. // setTimeout(() => {
  242. // wx.navigateBack();
  243. // }, 2000);
  244. // }
  245. } else {
  246. wx.showToast({
  247. title: "支付失败",
  248. image: "./../../../assets/img/fail.png",
  249. duration: 1500,
  250. mask: false
  251. });
  252. }
  253. return;
  254. }
  255. });
  256. /// End payment --------
  257. ///// virtual pay
  258. //var payOrderId = "" + res.data.out_trade_no;
  259. //that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
  260. ///// end virtual pay
  261. });
  262. } else {
  263. // 免费券
  264. that.payOrderUpdate(orderId, "0", 1);
  265. wx.showToast({
  266. title: "领取成功",
  267. duration: 3000
  268. });
  269. }
  270. }
  271. })
  272. .catch(err => {
  273. console.log(err);
  274. });
  275. }
  276. }
  277. });