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.

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