留言板当页留言的代码,后来更改了

var util = require('../utils/utils.js');

Page({

  data: {

    board: [],

    liuyan: '',

    userInfo: [],

    canIUseGetUserProfile: false,

    hasUserInfo: false,

    info: '',

    // chooseimage之后临时存放图片的地址:

    imagetemppath: []

  },


 

  onLoad: function (options) {

    // 获取留言数据

    this.againget()

  },


 

  areaclear: function (e) {

    console.log('这是?', e.detail.value)

  },

  // 重新获取留言数据的函数

  againget() {

    const db = wx.cloud.database({

      env: 'tomflair-8g3qw97b9482a49f'

    });

    db.collection('board').orderBy('ids', 'desc').limit(5).get()

      .then(res => {

        this.setData({

          board: res.data,

        })

      })

  },


 

  async postboard(e) {

    let userInfo = wx.getStorageSync('userInfo')

    if (e.detail.value.textarea == '') {

      wx.showToast({

        title: '留言为空',

      })

      return

    }


 

    // 先判断userinfo是不是空,如果是空,就弹出登录请求

    if (userInfo.length == 0) {

      this.get()

    } else {

      await wx.cloud.callFunction({

        name: 'postboard',

        data: {

          nickName: userInfo.nickName,

          liuyan: e.detail.value.textarea,

          touxiang: userInfo.avatarUrl,

          time:util.formatTime(new Date())

        },

        success: res => {

          console.log('是否到了这一步', e.detail.value.textarea)

          wx.showToast({

            title: '留言成功',

          })

          this.againget()

          this.setData({

            info: ''

          })

        },

        fail: console.error

      })

    }

  },


 

  get() {

    wx.getUserProfile({

      desc: '用户获取用户资料',

      success: (res) => {

        console.log(res)

        this.setData({

          userInfo: res.userInfo,

          hasUserInfo: true

        })

        // 把得到的个人信息放入缓存里面

        console.log(this.data.userInfo)

        wx.setStorageSync('userInfo', this.data.userInfo)

      },

      fail: console.error

    })

  },

  // 这是提交留言的内容,用表单来提交;

  write(e) {

    this.setData({

      liuyan: e.detail.value

    })

  },


 

  // 这是上传图片的函数

  upload() {

    wx.chooseImage({

      count: 3,

      sizeType: ['original', 'compressed'],

      sourceType: ['album', 'camera'],

      success: res => {

        wx.showToast({

          title: '正在上传...',

          icon: 'loading',

          mask: true,

          duration: 1000

        })

        //打印一下图片的临时存放

        console.log(res.tempFilePaths)

        this.setData({

          imagetemppath: res.tempFilePaths

        })

        console.log(this.data.imagetemppath)

      }

    })

  },

  // 点击确定之后再上传图片。要不然不加预览图片;


 

  onShow: function () {

  },

  onReady: function () {

  },


 

  onHide: function () {

  },

  onUnload: function () {

  },


 

  // 这是下拉刷新的相关代码。

  onPullDownRefresh: function () {

    wx.showNavigationBarLoading();

    const db = wx.cloud.database({

      env: 'tomflair-8g3qw97b9482a49f'

    });

    db.collection('board').orderBy('ids', 'desc').skip(1).limit(5).get({

      success: res => {

        this.setData({

          board: res.data,

        })

      },

      fail: console.error,

      complete: function () {

        wx.hideNavigationBarLoading() //完成停止加载

        wx.stopPullDownRefresh() //停止下拉刷新

      }

    })

  },

  onReachBottom: function () {

  },

  onShareAppMessage: function () {

  }

})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值