API 文档

数据库文档捉虫大赛邀您参加,好礼多多> HOT

本文主要继续围绕主页的 index.js 进行抽取纸条功能的讲解,更多详情请参见 主页逻辑代码

操作步骤

  1. 随机抽取可以用微信开放文档提供的 开发者资源 来查找,进行随机的查询抽取,抽取的条件是“性别,学校和生命值必须大于0”
    // 随机抽取
    db.collection('body_girl_yun').aggregate().match({
    sex: 1,
    location: parseInt(that.data.indexBody3),
    life: _.gt(0) // 生命值要大于0
    }).sample({ size: 1}).end()
  2. 抽到则处理,抽不到则提示处理时要把生命值减1。
    // 数据库没有这个学校的男生的纸条就
    if(res.list.length == 0){
    return wx.showToast({
    title: '此学校暂无纸条',
    icon: 'error',
    mask: true
    })
    }
    // 抽取到了纸条,则进行操作
    // 生命值减一, 生命值为0的不会抽出来
    db.collection('body_girl_yun').where({
    _id: res.list[0]._id
    }).update({
    data: {
    life: parseInt(res.list[0].life) - 1
    }
    }).then( resultUpdate => {
    wx.showToast({
    title: '抽取成功',
    icon: 'success',
    mask: true
    })
    }).catch(err=>{
    wx.showToast({
    title: err,
    icon: 'error'
    })
    })
  3. 把数据写到 body_girl_yun_put 表中。
    // 并将数据添加到 body_girl_yun_put
    db.collection('body_girl_yun_put').add({
    data: {
    picture: res.list[0].picture,
    des: res.list[0].des,
    location: res.list[0].location,
    sex: res.list[0].sex,
    weChatId: res.list[0].weChatId,
    openId: app.globalData.openId,
    time: new Date().getTime()
    }
    }).then( resultAdd => {

    }).catch(err=>{
    wx.showToast({
    title: err,
    icon: 'error'
    })
    })
    })
  4. 数据存入成功则跳转页面 history。
    wx.switchTab({
    url: '/pages/history/history',
    success: res=>{
    wx.showToast({
    title: '抽出纸条成功',
    icon: 'success'
    })
    },
    fail: err => {

    }
    })
    that.setData({
    outBodyMask: false
    })
  5. 加上提示框与 loading 效果,wx.showLoading, wx.hideLoading(), 并添加了一些基本的错误处理。
    // 确认抽取一张男生纸条
    sureOutBodyBtn: function(){
    const that = this

    wx.showLoading({
    title: '随机抽取中',
    mask: true
    })
    // 随机抽取
    db.collection('body_girl_yun').aggregate().match({
    sex: 1,
    location: parseInt(that.data.indexBody3),
    life: _.gt(0) // 生命值要大于0
    }).sample({ size: 1}).end()
    .then(res => {
    wx.hideLoading()
    // 数据库没有这个学校的男生的纸条就
    if(res.list.length == 0){
    return wx.showToast({
    title: '此学校暂无纸条',
    icon: 'error',
    mask: true
    })
    }

    console.log(res)
    // 生命值减一, 生命值为0的不会抽出来
    db.collection('body_girl_yun').where({
    _id: res.list[0]._id
    }).update({
    data: {
    life: parseInt(res.list[0].life) - 1
    }
    }).then( resultUpdate => {
    wx.showToast({
    title: '抽取成功',
    icon: 'success',
    mask: true
    })
    }).catch(err=>{
    wx.showToast({
    title: err,
    icon: 'error'
    })
    })


    // 并将数据添加到 body_girl_yun_put
    db.collection('body_girl_yun_put').add({
    data: {
    picture: res.list[0].picture,
    des: res.list[0].des,
    location: res.list[0].location,
    sex: res.list[0].sex,
    weChatId: res.list[0].weChatId,
    openId: app.globalData.openId,
    time: new Date().getTime()
    }
    }).then( resultAdd => {

    wx.switchTab({
    url: '/pages/history/history',
    success: res=>{
    wx.showToast({
    title: '抽出纸条成功',
    icon: 'success'
    })
    },
    fail: err => {
    wx.showToast({
    title: err,
    icon: 'error'
    })
    }
    })

    // console.log("数据add"resultAdd)
    that.setData({
    outBodyMask: false
    })
    }).catch(err=>{
    wx.showToast({
    title: err,
    icon: 'error'
    })
    })
    })
    }