|
- var config = require("../../../config/config.js");
- var app = getApp();
- const Http = require("../../../utils/HttpBasics");
- const util = require("../../../utils/util");
- Page({
- data: {
- data: {},
- carList: [],
- couponChannelId: null,
- couponId: null,
- orderId: "",
- day: "",
- hour: "",
- minute: ""
- },
- onLoad(options) {
- let that = this;
- console.log(options.couponChannelId);
- console.log(options.couponId);
- that.init();
- /**
- * 首页banner detail couponlist传过来的值
- * iambannercoupon
- */
-
- that.setData({
- couponChannelId: options.couponChannelId,
- couponId: options.couponId
- });
-
- wx.showLoading({
- title: "加载中..."
- });
-
- if (options.flag) {
- console.log('111111111111111'+JSON.stringify(options))
- this.orderFunc();
- } else {
-
- if(options.couponChannelId=='iambannercoupon'){
- /**
- * 待解决
- */
- var details ={
- couponChannelId: options.couponId
- }
- }else{
- var details ={
- couponChannelId: options.couponChannelId
- }
- }
-
- Http.get({
- url: config.api.couponDetail,
- data: details
- }).then(res => {
- console.log(res);
- console.log("details")
- //当前时间与优惠券下架时间做计算
- var startTime = util.fmtDate(res.data.endTime);
- console.log(startTime);
- var s1 = new Date(startTime.replace(/-/g, "/"));
- var s2 = new Date();
- var runTime = parseInt((s1.getTime() - s2.getTime()) / 1000);
- var year = Math.floor(runTime / 86400 / 365);
- var runTime = runTime % (86400 * 365);
- var month = Math.floor(runTime / 86400 / 30);
- var runTime = runTime % (86400 * 30);
- var day = Math.floor(runTime / 86400);
- var runTime = runTime % 86400;
- var hour = Math.floor(runTime / 3600);
- var runTime = runTime % 3600;
- var minute = Math.floor(runTime / 60);
- var runTime = runTime % 60;
- var second = runTime;
- console.log(year, month, day, hour, minute, second);
- that.setData({
- year: year,
- month: month,
- day: day,
- hour: hour,
- minute: minute
- });
- console.log(that.data.day);
- wx.hideLoading();
- that.setData({
- data: res.data
- });
- });
- }
- },
- /**
- * 支付订单更新
- */
- payOrderUpdate: (orderId, payOrderId, status, reason) => {
- // 支付成功
- Http.post({
- url: config.api.payOrderUpdate,
- data: {
- payOrderId: payOrderId,
- orderId: orderId,
- status: status,
- reason: reason
- }
- })
- .then(res => {
- console.log("payOrderUpdate then", res);
- // wx.showToast({
- // title: "购买成功",
- // duration: 2500
- // });
- })
- .catch(err => {
- console.log("payOrderUpdate catch", err);
- });
- },
- /**
- * 拉取车牌列表
- */
- init: function () {
- var that = this
-
- app.parkInitCallback = token => {
- that.initPark()
- that.initUsrCarList()
- if (!app.globalData.carLogin) {
- // 共同登录
- Http.post({
- url: config.api.carInit,
- data: {
- phone: app.globalData.phone
- }
- }).then(res => {
- app.globalData.carLogin = true
- app.globalData.parkVendor = res.data.vendor
- if (res.data.token != "undefined") {
- app.globalData.etcpToken = res.data.token
- console.log("etcpToken", app.globalData.etcpToken)
- }
- // 获取 停车费
- that.getStopFee()
- })
- }
- }
- if (app.globalData.token && app.globalData.token != null) {
- app.parkInitCallback(app.globalData.token)
- }
- },
- getStopFee: function () {
- var that = this
- // carLogin
- for (var i = 0; i < that.data.carList.length; i++) {
- that.getStopFeeItem(that.data.carList[i], i)
- }
- // console.log(JSON.stringify(getStopFeeItem))
- },
- initPark: function () {
- var that = this
- // 车场信息获取
- Http.get({
- url: config.api.getParkInfo,
- data: {}
- })
- .then(res => {
- console.log(res)
- that.setData({
- park: res.data,
- })
- })
- },
- initUsrCarList: function () {
- var that = this
- // 绑定车获取
- Http.get({
- url: config.api.getUserCarList,
- data: {}
- }).then(res => {
- console.log('>>>>>>>>>>>>>>>>>1' + JSON.stringify(res));
- that.setData({
- carList: res.data
- })
- })
- },
- /**
- * 发起支付
- */
- orderFunc() {
- var that = this;
- wx.showLoading({
- title: "加载中..."
- });
- if (that.data.data.type == 5) {
- this.initUsrCarList()
- // 领取 5.停车券
- // TODO 选取用户下的车牌
- console.log(11111111111111111111111111111111)
- } else {
- Http.post({
- url: config.api.checkPhoneStatus,
- data: {}
- })
- .then(res => {
- console.log(res);
- /**
- * orderSave 下单
- */
- return Http.post({
- url: config.api.orderSave,
- data: {
- couponChannelId: "" + that.data.couponChannelId,
- couponId: "" + that.data.couponId
- }
- });
- })
- .catch(err => {
- console.log(err);
- if (err.code == 11005) {
- // 用户手机未授权
- wx.redirectTo({
- url:
- "../../getphoneInfo/index?couponChannelId=" +
- that.data.couponChannelId +
- "&couponId=" +
- that.data.couponId
- });
- }
- if (err.code == 11006) {
- // 用户手机已加密
- wx.redirectTo({
- url:
- "../../phoneinput/phoneinput?couponChannelId=" +
- that.data.couponChannelId +
- "& couponId=" +
- that.data.couponId
- });
- }
- })
- .then(res => {
- console.log(res);
- console.log("点击领取卡券一直在loading");
- if (res != "undefined") {
- const orderId = "" + res.data.id;
- that.setData({
- orderId: orderId
- });
- if (res.data.payment > 0) {
- // 支付金额不为0
- /**
- * 支付订单创建
- */
- Http.post({
- url: config.api.payOrderCreate,
- data: {
- orderId: orderId
- }
- }).then(res => {
- console.log(res);
-
- /// Begin payment ----
- var payOrderId = "" + res.data.payOrderId;
- wx.hideLoading();
- wx.requestPayment({
- timeStamp: res.data.timeStamp,
- nonceStr: res.data.nonceStr,
- package: res.data.package,
- signType: "MD5",
- paySign: res.data.paySign,
- success: res => {
- console.log(res);
- console.log("姐在检查付款异常");
- that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
- console.log(res);
- if (res.errMsg == "requestPayment:ok") {
- wx.showToast({
- title: "购买成功",
- duration: 2500
- });
- }
-
- wx.navigateBack({
- delta: 2
- });
- },
-
- fail: res => {
- that.payOrderUpdate(that.data.orderId, payOrderId, 2); // 支付失败
- console.log(res);
- return;
- },
- complete: res => {
- console.log(res);
- console.log("支付完成");
- // var url = res.data.url;
- // console.log("get url", url);
- if (res.errMsg == "requestPayment:ok") {
- wx.showModal({
- title: "提示",
- content: "支付成功"
- });
- // if (url) {
- // setTimeout(function () {
- // wx.redirectTo({
- // url: "/pages" + url
- // });
- // }, 2000);
- // } else {
- // setTimeout(() => {
- // wx.navigateBack();
- // }, 2000);
- // }
- } else {
- wx.showToast({
- title: "支付失败",
- image: "./../../../assets/img/fail.png",
- duration: 1500,
- mask: false
- });
- }
- return;
- }
- });
- /// End payment --------
-
- ///// virtual pay
- //var payOrderId = "" + res.data.out_trade_no;
- //that.payOrderUpdate(that.data.orderId, payOrderId, 1); // 支付成功
- ///// end virtual pay
- });
- } else {
- // 免费券
- that.payOrderUpdate(orderId, "0", 1);
- wx.showToast({
- title: "领取成功",
- duration: 3000
- });
- }
- }
- })
- .catch(err => {
- console.log(err);
- });
- }
- }
- });
|