tanzhongheY/pages/product/product.js

371 lines
7.9 KiB
JavaScript

// pages/product/produst.js
var urlPath = require('../../config.js');
Page({
/**
* 页面的初始数据
*/
data: {
host:urlPath.host,
list: {},
threeList: {},
curCaseIndex: 0,
curThreeCode: [],
carbon: 0,
paid_money: '0',
id: 0,
tree_num: {},
icon: "/img/nav/menu1.png",
priceList: {},
carbon_neutralization:0,
carbon_efficiency:'0.00',
},
casesChange(event){
this.setData({
curCaseIndex:event.detail.value,
priceList: {},
curThreeCode: [],
tree_num: {},
paid_money: 0,
carbon_neutralization:0,
carbon_efficiency:"0.00",
})
}
,
numChange(event) {
const {
num,
nameId
} = event.detail
const {
price,
name,
carbon_base
} = event.mark
let all_list = {}
console.log(num,'11')
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] = {
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] = {
price: price,
num: num,
name: name,
all: price * num,
carbon_base: carbon_base * num
}
}
// return
}
console.log(all_list)
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) {
//数量= 排放量 / 植物的碳汇量基数
// ,价格 = 每棵树价格 * 数量
var that = this
var {
money,
name,
carbon_base
} = 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] = {
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
// carbon:e.mark.carbon
})
console.log(that.data.priceList)
// 重新计算数目的结果
this.setMoneyNum(this.data.priceList)
//console.log(this.data.priceList);
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
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) {
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.getThreeList,
header: {
'content-type': 'application/x-www-form-urlencoded'
},
method: 'post',
dataType: 'json', // 添加这个配置
data: {
id: id,
'openid': wx.getStorageSync('openid')
},
success(res) {
if (res.data.result == 'success') {
var threeList = JSON.parse(res.data.data);
console.log(threeList)
// var money = threeList.trees[0].price
that.setData({
threeList: threeList,
//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 = []
for(var p in this.data.priceList)
{
tree_items.push({
coding:p,
amount:this.data.priceList[p].num
})
}
console.log(tree_items,'tree_items')
wx.request({
url: urlPath.postPayData, //仅为示例,并非真实的接口地址
data: {
'openid': wx.getStorageSync('openid'),
footmark_id: this.data.list.id,
case_coding: this.data.threeList.cases[this.data.curCaseIndex].coding,
tree_items:JSON.stringify(tree_items),
// tree_coding: this.data.curThreeCode[0], //TODO 这里获取数组[0]
// tree_amount: this.data.list.discharge_amount / this.data.carbon,
paid_money: this.data.paid_money
},
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,
})
// wx.showLoading({
// title: 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.apiNeutralizationPaid, //查询
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
})
console.log(res, '2222')
}
})
}
}
})
}
})