|
- var config = require('../../../config/config.js');
- var app = getApp()
- const Http = require("../../../utils/HttpBasics")
- Page({
- data: {
- data: {},
- couponId: null,
- orderId: ''
- },
- onLoad(e) {
- console.log(e)
- wx.showLoading({
- title: '加载中...',
- })
- if (e.flag == 'pay') {
- this.orderFunc();
- } else {
- Http.get({
- url: config.api.couponDetail,
- data: {
- id: e.id
- }
- }).then(res => {
- wx.hideLoading();
- this.setData({
- data: res,
- couponId: e.id
- })
- })
- }
- },
- /**
- * 支付订单更新
- */
- 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);
- })
- },
- payment: (res) => {
- var that = this;
- var payOrderId = '' + res.data.out_trade_no;
- wx.requestPayment({
- timeStamp: res.timeStamp,
- nonceStr: res.nonceStr,
- package: res.package,
- signType: 'MD5',
- paySign: res.paySign,
- 'success': (res) => {
- that.payOrderUpdate(that.data.orderId, payOrderId, 1) // 支付成功
- console.log(res);
- console.log('支付成功');
- wx.showToast({
- title: '购买成功',
- duration: 2500
- })
- wx.navigateBack({
- delta: 2
- })
- },
- 'fail': (res) => {
- that.payOrderUpdate(that.data.orderId, payOrderId, 2) // 支付失败
- console.log(res)
- console.log('支付失败');
- return;
- },
- 'complete': (res) => {
- console.log(res);
- console.log('支付完成');
- var url = this.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.showModal({
- title: '错误提示',
- content: res.errMsg
- });
- }
- return;
- }
- })
- },
- orderFunc() {
- var that = this;
- wx.showLoading({
- title: '加载中...',
- })
- Http.post({
- url: config.api.checkPhoneStatus,
- data: {}
- })
- .then(res => {
- return Http.post({
- url: config.api.orderSave,
- data: {
- couponId: that.data.couponId
- }
- })
- }).catch(err => {
- console.log(err)
- if (err.code == 11005) {
- // 用户手机未授权
- wx.redirectTo({
- url: '../../getphoneInfo/index?couponId=' + that.data.couponId,
- })
- }
- if (err.code == 11006) {
- // 用户手机已加密
- wx.redirectTo({
- url: '../../phoneinput/phoneinput?couponId=' + that.data.couponId,
- })
- }
- })
- .then(res => {
- 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)
- var payOrderId = '' + res.data.out_trade_no;
- wx.hideLoading();
- //payment(res);
- that.payOrderUpdate(that.data.orderId, payOrderId, 1) // 支付成功
- })
- } else {
- // 免费券
- that.payOrderUpdate(orderId, "0", 1) // 支付成功
- .then(res => {
- wx.showToast({
- title: "支付成功",
- duration: 3000
- })
- })
- }
- }
- }).catch(err => {
- console.log(err)
- })
- },
- })
|