Files
cardtest/pages/car/index.js
zhangmanman 34590d9f72 [更新]
2021-06-30 09:06:58 +08:00

299 lines
10 KiB
JavaScript

/*
* 本时生活
*/
Page({
/**
* 页面的初始数据
*/
data: {
rightId : '', //权益id
welfareId : '', //活动id
address : '', //默认地址
allAddress : '', //收货地址列表
detail : '', //权益详情
amount : '', //总金额
moreAmount : '', //产品金额
platIndex : 0, //选择提交方式下标
isdeliver : -1,
platformCp : [], //选择提交数组
num : 1, //购买的数量
content : '', //内容介绍
notification: '', //重要提示
remark : '', //使用须知
score : '', //应付总积分
freight : '', //运费
noticeShow : false, //须知显示状态
addressShow : false, //收货地址显示
payWayIndex : 0,
payWay :[
{value: 0, name: "微信支付"}
// {value: 1, name: "沃钱包支付"}
]
},
/**
* 生命周期函数--监听页面加载
*/
onLoad (options) {
this.setData({
rightId : options.right_id,
welfareId: options.welfare_id
})
// 获取卡券列表
this.couponInfo()
},
/**
* 生命周期函数--监听页面显示
*/
onShow () {
},
/**
* 卡券列表
*/
couponInfo() {
wx.$api.user.washcarBuy(this.data.welfareId, this.data.rightId, this.data.num, this.data.address.id).then(res=>{
let obj = res.data.detail.express
let defGet = res.data.detail.def_get
let arr = new Array
arr = Object.keys(obj).map(val=>{
return { ...obj[val], ...{key: val} }
})
if(this.data.isdeliver == -1){
if(res.data.detail.type == 'physical') {
this.setData({
isdeliver : res.data.detail.def_get
})
}
if(res.data.detail.type == 'virtual') this.setData({isdeliver : 1})
}
this.setData({
address : res.data.address,
allAddress : res.data.all_address,
detail : res.data.detail,
num : res.data.qty,
freight : res.data.freight,
platformCp : arr,
amount : res.data.total,
moreAmount : res.data.amount,
score : res.data.score,
remark : res.data.detail.remark.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;"'),
content : res.data.detail.content.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;"')
})
}).catch(err=>{
if(!err.login){
wx.showModal({
title : '用户登录已过期',
content : '请重新登录',
showCancel : false,
success : res => {
if (res.confirm) {
wx.redirectTo({
url: '/pages/login/login'
})
}
}
})
}
})
},
/**
* 须知展开收起状态
*/
noticeTap() {
this.setData({
noticeShow : !this.data.noticeShow
})
},
/**
* 收货地址弹出
*/
addressTap() {
this.setData({
addressShow : !this.data.addressShow
})
},
/**
* 选择收货地址
*/
selectAddress(e){
let new_addressId = e.currentTarget.dataset.id,
addressId = this.data.address.id
if (new_addressId != addressId) {
this.setData({
address : e.currentTarget.dataset.index,
addressShow : false
})
}else{
this.setData({
addressShow : false
})
}
// 获取卡券列表
this.couponInfo()
},
/**
* 支付选择
*/
payBind(e) {
this.setData({
payWayIndex: e.detail.value
})
},
/**
* 商品数量加减
*/
goodsNumber(e){
let num = this.data.num
if (e.currentTarget.dataset.type == 'plus'){
num ++;
}else{
if (num > 1){
num --;
}else{
wx.showToast({
title : '商品数量不能小于1',
icon : 'none'
})
}
}
this.setData({
num : num
})
// 获取详情
this.couponInfo()
},
/**
* 选择提交方式
*/
platBind(e) {
this.setData({
platIndex : e.detail.value,
isdeliver : this.data.platformCp[e.detail.value].value
})
// 获取详情
this.couponInfo()
},
/**
* 支付提交
*/
submitOrder() {
if(this.data.isdeliver.length == 0) {
this.setData({
isdeliver: this.data.detail.def_get
})
}
let right_id = this.data.rightId,
welfare_id = this.data.welfareId,
address_id = this.data.address.id,
is_deliver = this.data.isdeliver,
qty = this.data.num
wx.$api.user.washcarCreate(welfare_id, right_id, qty, address_id, is_deliver).then(res=>{
let dataUrl = '', //定义接口来源名称
type = res.data.type //订单来源
// welfare为福利活动购买
if(type == 'welfare') dataUrl = wx.$api.index.fridayInfo(welfare_id, address_id, is_deliver)
// right为权益购买
if(type == 'right') dataUrl = wx.$api.index.rightStore(right_id, address_id, is_deliver, qty)
// 第一步接口调取
dataUrl.then(dataRes=>{
let payUrl = '', //定义接口来源名称
Newtype = dataRes.data.type //订单来源
// welfare为福利活动购买
if(Newtype == 'welfare') payUrl = wx.$api.index.fridayPay(dataRes.data.trade_no)
// right为权益购买
if(Newtype == 'right') payUrl = wx.$api.index.wechat(dataRes.data.trade_no)
// 第一步接口调取
payUrl.then(payEes=>{
let payInfo = JSON.parse(payEes.data)
wx.requestPayment({
timeStamp: payInfo.timeStamp,
nonceStr : payInfo.nonceStr,
package : payInfo.package,
paySign : payInfo.paySign,
signType : payInfo.signType,
success : res=>{
if(res.errMsg == "requestPayment:ok"){
wx.showToast({
title: '支付成功',
icon : 'success'
})
if(Newtype == 'welfare'){
setTimeout(()=>{
if(this.data.isdeliver == 1) {
wx.reLaunch({
url: '/pages/order/order?orderType=welfareGoods'
})
} else {
wx.reLaunch({
url: '/pages/order/order?orderType=welfare'
})
}
},3000)
}
if(Newtype == 'right'){
setTimeout(()=>{
if(this.data.isdeliver == 1) {
wx.reLaunch({
url: '/pages/order/order?orderType=rightsCoupons'
})
} else {
wx.reLaunch({
url: '/pages/order/order?orderType=rights'
})
}
},3000)
}
}
},
fail : res=>{
if(Newtype == 'welfare'){
if(this.data.isdeliver == 1) {
wx.reLaunch({
url: '/pages/order/order?orderType=welfareGoods&stateType=unpay'
})
} else {
wx.reLaunch({
url: '/pages/order/order?orderType=welfare&stateType=unpay'
})
}
}
if(Newtype == 'right'){
if(this.data.isdeliver == 1) {
wx.reLaunch({
url: '/pages/order/order?orderType=rightsCoupons&stateType=unpay'
})
} else {
wx.reLaunch({
url: '/pages/order/order?orderType=rights&stateType=unpay'
})
}
}
}
})
})
})
})
}
})