import { moment } from '../../utils/moment';
const db = wx.cloud.database();
Page({
data: {
isShowModal: false,
is_open:true, //性别筛选是否打开
isShowSelectModal: false,
ageList: ["0-18", "19-25", "26-30", "31-99"],
selectedAge: "",
price:0.01, //默认1元
newfetch:null,
meget:[],
wxcode:"",
checksex:""
},
onLoad(){
// this.getMyfetch()
},
// 选择年龄
selectAgeChange: function (e) {
this.setData({
selectedAge: e.detail.value
});
},
//是否按年龄搜索
publicContact(e){
this.data.is_open = e.detail.value
},
//年龄确认按钮
confirmAgeChange: function () {
let checksex = this.data.checksex;
if(this.data.selectedAge != ""){
console.log(this.data.selectedAge);
let age = this.data.selectedAge.split("-");
let minage = age[0];
let maxage = age[1];
this.getDataByAge(checksex == "男" ? "boys":"girls",minage,maxage);
this.setData({
isShowSelectModal: !1,
price:0.02
})
}else{
this.setData({
isShowSelectModal: !1
})
}
},
//关闭年龄弹窗
hideAgeModal: function () {
this.setData({
isShowSelectModal: !1
});
},
//复制弹窗的关闭
hideModal: function () {
this.setData({
isShowModal: false
});
},
//判断是否选择年龄范围
showSelectAgeModal: function (t) {
this.setData({
checksex:t.currentTarget.dataset.gender
})
let checksex = this.data.checksex;
let is_open = this.data.is_open;
if(is_open){
// console.log("选择按年龄")
this.setData({
isShowSelectModal: true
});
}else{
// console.log("未选择按年龄")
this.getData(checksex == "男" ? "boys":"girls");
}
},
//支付方法
goPay(tsex){
let that = this;
const orderid = Math.floor(Math.random() * 9999) * Date.now() + ""; //String 商户订单号 确保唯一
// wx.setStorageSync('"orderid"',orderid);
// 小程序代码
wx.cloud.callFunction({
name: 'pay',
data: {
orderid:orderid,
body:"cp小纸条-取小纸条",
totalFee:this.data.price * 100,
},
success: res => {
const payment = res.result.payment
wx.requestPayment({
...payment,
success (res) {
console.log('支付成功', res)
let myget = that.data.meget;
console.log(myget);
db.collection(tsex).doc(myget._id).update({
data:{
state:true
}
})
that.updateMyfetch(myget);
that.setData({
isShowModal: true,
wxcode:myget.wxcode
});
},
fail (err) {
console.error('支付失败', err)
}
})
},
fail: console.error,
})
},
//获取myfetch数据库纸条数量信息
getMyfetch(){
db.collection('myfetch').where({
_openid:wx.getStorageSync('myopenid')
}).get().then(res=>{
console.log(res,'myfetch')
this.data.myfetch = res.data
})
},
//获取数据
getData(sex){
let tsex = sex+"";
db.collection(tsex).aggregate().sample({
size: 10
}).match({
state:false
})
.end().then(res=>{
// console.log(res.list,"res")
if(res.list.length > 0){
//myget 就是取出的数据
let myget = res.list[0];
this.data.meget = myget;
this.goPay(tsex);
}else{
wx.showModal({
cancelColor: 'cancelColor',
title:'提示',
content:'已经没有小纸条啦!等会儿再来吧~',
showCancel:false
})
}
})
},
getDataByAge(sex,minage,maxage){
let tsex = sex+"";
db.collection(tsex).aggregate().sample({
size: 10
}).match({
state:false,
age:db.command.gte(minage).and(db.command.lte(maxage))
})
.end().then(res=>{
// console.log(res.list,"res")
if(res.list.length > 0){
//myget 就是取出的数据
let myget = res.list[0];
this.data.meget = myget;
this.goPay(tsex);
// console.log(myget);
}else{
wx.showModal({
cancelColor: 'cancelColor',
title:'提示',
content:'没有这个年龄段的啦!换一个年龄段或者等一会儿再取吧~',
showCancel:false
})
}
})
},
//将取出来的数据存入myfetch库
updateMyfetch(sdata){
db.collection('myfetch').add({
data:{
wxcode:sdata.wxcode,
sex:sdata.sex,
age:sdata.age,
getdate:moment('YYYY-MM-DD hh:mm:ss'),
getopenid:sdata._openid,
}
}).then(res=>{
console.log(res)
})
},
/* 复制按钮 */
clickcopy: function () {
var t = this;
wx.setClipboardData({
data: this.data.wxcode,
success: function (e) {
t.setData({
isShowModal: !1
});
}
});
},
/* 分享 */
onShareAppMessage:function(res){
if(res.from === 'menu'){
console.log(res.target);
}
return{
title:'处CP吗?盲盒的那种!',
imageUrl:'https://z3.ax1x.com/2021/10/01/47mTQe.png'
}
}
});