tanzhongheY/pages/gproduct/gproduct.js

464 lines
11 KiB
JavaScript

// pages/product/produst.js
var urlPath = require('../../config.js');
Page({
/**
* 页面的初始数据
*/
data: {
host: urlPath.host,
list: {},
flag:false,
isXiangm: false,
newDo_type: '',
threeList: [],
curCaseIndex: 0,
addressIndex: 0,
curThreeCode: [],
carbon: 0,
paid_money: '0',
id: 0,
tree_num: {},
icon: "/img/nav/menu1.png",
priceList: {},
carbon_neutralization: 0,
carbon_efficiency: '0.00',
senIndex: 0,
senList: [{
name: '补植补造',
type: 'afforestation'
},
{
name: '森林管护',
type: 'forest_management'
},
],
newType: '',
isShif: false,
cases_position_id: ''
},
senChange(event) {
this.setData({
senIndex: event.detail.value,
newType: this.data.senList[event.detail.value].type,
priceList: {},
curThreeCode: [],
tree_num: {},
flag:true,
})
if (this.data.senList[event.detail.value].type == 'forest_management') {
this.setData({
isShif: true,
newDo_type: this.data.senList[this.data.senIndex].type,
senIndex: 1,
})
} else {
this.setData({
isShif: false,
newDo_type: this.data.senList[this.data.senIndex].type,
senIndex: 0,
})
}
},
casesChange(event) {
if(this.data.threeList[event.detail.value].type=='forest_management'){
if(this.data.threeList[event.detail.value].default_type=='forest_management'){
this.setData({
isShif:true,
isXiangm:true,
senIndex: 1,
})
} else {
this.setData({
isShif:false,
isXiangm:true,
senIndex: 0,
})
}
} else {
this.setData({
isShif:false,
isXiangm:false,
senIndex: 0,
})
}
this.setData({
curCaseIndex: event.detail.value,
addressIndex:0,
priceList: {},
curThreeCode: [],
tree_num: {},
paid_money: 0,
flag:true,
carbon_neutralization: 0,
carbon_efficiency: "0.00",
})
if(this.data.senList[event.detail.value].name=='补植补造'){
this.setData({
newDo_type:'afforestation',
})
}
if (event.detail.value == 1) {
this.setData({
newDo_type: this.data.senList[this.data.senIndex].type,
cases_position_id:this.data.threeList[this.data.curCaseIndex].address[0].id
})
} else {
this.setData({
newDo_type: this.data.senList[this.data.curCaseIndex].type,
cases_position_id:this.data.threeList[this.data.curCaseIndex].address[0].id
})
}
},
addrressChange(event) {
this.setData({
addressIndex: event.detail.value,
cases_position_id: this.data.threeList[this.data.curCaseIndex].address[event.detail.value]
.id,
priceList: {},
curThreeCode: [],
tree_num: {},
flag:true,
})
},
numChange(event) {
const {
num,
nameId
} = event.detail
const {
price,
name,
carbon_base
} = event.mark
let all_list = {}
all_list = Object.assign({}, this.data.priceList, all_list)
if (this.data.curThreeCode.indexOf(nameId) != -1) {
if (num == 0) {
delete all_list[nameId]
this.data.curThreeCode.splice(this.data.curThreeCode.indexOf(nameId), 1)
this.setData({
curThreeCode: this.data.curThreeCode
})
} else {
all_list[nameId] = {
id: nameId,
price: price,
num: num,
name: name,
all: price * num,
carbon_base: carbon_base * num
}
}
} else {
if (num > 0) {
var data = this.data.curThreeCode
data.push(nameId)
this.setData({
curThreeCode: data
})
all_list[nameId] = {
id: nameId,
price: price,
num: num,
name: name,
all: price * num,
carbon_base: carbon_base * num
}
}
}
this.setMoneyNum(all_list);
},
setMoneyNum: function(all_list) {
var all_price = 0
var carbon_neutralization = 0,
carbon_efficiency = 100
for (var p in all_list) {
all_price = all_price + all_list[p].all
carbon_neutralization = parseFloat(carbon_neutralization) + all_list[p].carbon_base;
}
carbon_efficiency = (parseFloat(carbon_neutralization) / parseFloat(this.data.carbon)) * 100
this.setData({
priceList: all_list,
paid_money: all_price,
carbon_neutralization: parseInt((carbon_neutralization * 100)) / 100,
carbon_efficiency: carbon_efficiency.toFixed(2),
})
},
setMoney: function(e) {
console.log('e',e);
//数量= 排放量 / 植物的碳汇量基数
// ,价格 = 每棵树价格 * 数量
var that = this
var {
money,
name,
carbon_base,
id
} = e.currentTarget.dataset;
let i = that.data.curThreeCode.indexOf(e.currentTarget.dataset.code);
this.data.tree_num[e.currentTarget.dataset.code] = 1
if (i == -1) {
that.data.curThreeCode.push(e.currentTarget.dataset.code);
} else {
//取消选中
that.data.curThreeCode.splice(i, 1);
delete this.data.priceList[e.currentTarget.dataset.code]
this.data.tree_num[e.currentTarget.dataset.code] = 0
}
var new_att = []
// 拿出已经存在的价格数量判断取消的内容
for (var p in this.data.priceList) {
new_att.push(p)
}
// 计算不在列表的数据加入默认数量1科
that.data.curThreeCode.map(item => {
if (new_att.indexOf(item) == -1) {
this.data.priceList[item] = {
id: id,
price: money,
num: 1,
name: name,
all: money * 1,
carbon_base: parseFloat(carbon_base)
}
}
})
this.setData({
curThreeCode: that.data.curThreeCode,
priceList: that.data.priceList,
tree_num: that.data.tree_num,
carbon_neutralization: 0
})
// 重新计算数目的结果
this.setMoneyNum(this.data.priceList)
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {
console.log('options',options);
if (typeof this.getTabBar === 'function' &&
this.getTabBar()) {
this.getTabBar().setData({
selected: 2
})
}
if (options.icon != undefined) {
this.setData({
icon: options.icon,
})
}
this.getOne(options)
},
moneyset: function(e) {
this.setData({
paid_money: e.detail.value
})
},
getOne: function(options) {
var that = this
var carbon = (options.carbon != undefined ? options.carbon : "");
var id = (options.id != undefined ? options.id : "");
wx.request({
url: urlPath.gMarketDetail,
header: {
'content-type': 'application/x-www-form-urlencoded'
},
method: 'post',
dataType: 'json', // 添加这个配置
data: {
id: id,
'openid': wx.getStorageSync('openid'),
},
success(res) {
console.log('res213',res);
if (res.data.result == 'success') {
that.setData({
list: JSON.parse(res.data.data),
carbon: carbon,
id: id
})
} else {
console.log(res.data.msg); //失败
}
}
})
wx.request({
url: urlPath.getapiCasesOnlyForestManagementV2,
header: {'content-type': 'application/x-www-form-urlencoded'},
method: 'post',
dataType: 'json', // 添加这个配置
data: {
id: id,
'openid': wx.getStorageSync('openid')
},
success(res) {
console.log(JSON.parse(res.data.data),777777777);
if (res.data.result == 'success') {
var dataList = JSON.parse(res.data.data).cases;
if(dataList[0].type=='forest_management'){
that.setData({
isShif:true,
isXiangm:true,
senIndex:1
})
}
// let arr = that.data.senList
// arr.forEach((item,index)=>{
// if(item.type == dataList[that.data.curCaseIndex].default_type){
// let obj = arr[index];
// arr.splice(index,1)
// arr.unshift(obj)
// }
// })
// console.log(arr,666666);
// if(arr[0].type=='forest_management'){
// that.setData({
// isShif:true,
// isXiangm:false,
// senIndex:1
// })
// }
// that.setData({
// senList:arr
// })
that.setData({
threeList: dataList,
cases_position_id: dataList[that.data.curCaseIndex].address[that.data.addressIndex].id,
newDo_type:that.data.senList[0].type,
//TODO 这里赋值应该用数组
// curThreeCode: [JSON.parse(res.data.data).trees[0].coding],
// carbon:JSON.parse(res.data.data).trees[0].carbon_base,
// paid_money: money.toFixed(2)
})
} else {
console.log(res.data.msg); //失败
}
}
});
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function() {},
/**
* 生命周期函数--监听页面显示
*/
onShow: function() {},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function() {},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function() {},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function() {},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function() {},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function() {},
toPay() {
var that = this
var tree_items = []
var management_items = []
if (this.data.newDo_type == 'afforestation') {
for (var p in this.data.priceList) {
tree_items.push({
coding: p,
amount: this.data.priceList[p].num
})
}
} else {
for (var p in this.data.priceList) {
management_items.push({
id: this.data.priceList[p].id,
amount: this.data.priceList[p].num
})
}
}
wx.request({
url: urlPath.postPayDataV2,
data: {
'openid': wx.getStorageSync('openid'),
footmark_id: this.data.list.id,
do_type: this.data.newDo_type,
case_coding: this.data.threeList[this.data.curCaseIndex].coding,
tree_items: JSON.stringify(tree_items) || '',
forest_management_items: JSON.stringify(management_items) || '',
paid_money: this.data.paid_money,
cases_position_id: this.data.cases_position_id
},
header: {
'content-type': 'application/x-www-form-urlencoded'
},
dataType: 'json', // 添加这个配置
method: 'post',
success(res) {
if (res.data.result == 'fail') {
wx.showModal({
title: '提示',
content: res.data.message,
})
} else {
var data = JSON.parse(res.data.data)
wx.requestPayment({
timeStamp: String(data.prepay_data.timeStamp),
nonceStr: data.prepay_data.nonceStr,
package: data.prepay_data.package,
signType: data.prepay_data.signType,
paySign: data.prepay_data.paySign,
success(res) {
//支付成功
wx.request({
url: urlPath.apiNeutralizationPaidV2, //查询
data: {
'openid': wx.getStorageSync('openid'),
neutralization_id: data.neutralization_id,
},
header: {
'content-type': 'application/x-www-form-urlencoded'
},
dataType: 'json', // 添加这个配置
method: 'post',
success(res) {
if (res.data.result == "success") {
wx.navigateTo({
url: '/pages/payment/payment?paid_money=' +
that.data.paid_money + "&&id=" +
that.data.id
})
}
}
})
},
fail(res) {
wx.showToast({
title: '支付失败',
icon: 'error',
duration: 2000
})
}
})
}
}
})
}
})