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.

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