| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- publisherInfo: [],
- mainDatas: [],
- like: -1,
- likeEnable: true
- },
- handleDate: function(date) {
- var now = new Date().getTime()
- var diffValue = now - date.getTime()
- if (diffValue < 0) {
- console.log("时间不同步")
- return "刚刚"
- }
- var result = ""
- var minute = 1000 * 60
- var hour = minute * 60
- var day = hour * 24
- var minC = diffValue / minute
- var hourC = diffValue / hour
- var dayC = diffValue / day
- if (parseInt(dayC) > 30) {
- result += date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate()
- } else if (parseInt(dayC) > 1) {
- result += parseInt(dayC) + "天前"
- } else if (parseInt(dayC) == 1) {
- result += "昨天"
- } else if (hourC >= 1) {
- result += parseInt(hourC) + "小时前"
- } else if (minC >= 5) {
- result += parseInt(minC) + "分钟前"
- } else {
- result += "刚刚"
- }
- return result
- },
- processData: function (start, arr) {
- for (let i = start; i < arr.length; i++) {
- arr[i].time = this.handleDate(arr[i].time)
- }
- this.setData({
- mainDatas: arr
- })
- wx.hideLoading()
- },
- toggleLike: function () {
- if (!this.data.likeEnable) return
- this.setData({
- likeEnable: false
- })
- const db = wx.cloud.database()
- if (this.data.like == 0) {
- db.collection("likeData").add({
- data: {
- type: "publisher",
- id: this.data.publisherInfo._id
- },
- success: function() {
- this.setData({
- like: 1,
- likeEnable: true
- })
- wx.showToast({
- title: "已关注",
- })
- }.bind(this),
- fail: function() {
- wx.showToast({
- title: "网络错误",
- icon: "none"
- })
- }
- })
- } else {
- db.collection("likeData").where({
- type: "publisher",
- id: this.data.publisherInfo._id
- }).remove({
- success: function() {
- this.setData({
- like: 0,
- likeEnable: true
- })
- wx.showToast({
- title: "已取消关注",
- })
- }.bind(this),
- fail: function() {
- wx.showToast({
- title: "网络错误",
- icon: "none"
- })
- }
- })
- }
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- wx.showLoading({
- title: "加载中"
- })
- const db = wx.cloud.database()
- db.collection("publisherInfoData").doc(options.id).get({
- success: function (res) {
- this.setData({
- publisherInfo: res.data
- })
- db.collection("mainData").where({
- publisherName: this.data.publisherInfo.publisherName
- }).orderBy("time", "desc").limit(20).get({
- success: function (res) {
- this.processData(0, res.data)
- }.bind(this)
- })
- }.bind(this)
- })
- db.collection("likeData").where({
- type: "publisher",
- id: options.id
- }).get({
- success: function(res) {
- this.setData({
- like: res.data.length
- })
- }.bind(this)
- })
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
- }
- })
|