Jelajahi Sumber

ADD

热搜后端对接
mulioid 4 tahun lalu
induk
melakukan
8d107f47d5

+ 32 - 0
cloudfunctions/listMessages/index.js

@@ -72,6 +72,38 @@ exports.main = async (event, context) => {
       next_page_token: event.page_token + messages.list.length,
       status: 'OK'
     }
+  } else if (event.hot) {
+    const reads = await db.collection('read')
+      .aggregate()
+      .match(_.expr($.lt([$.subtract([new Date(), '$read_time']), 1000 * 60 * 60 * 24])))
+      .group({
+        _id: '$msg_id',
+        total: $.sum(1)
+      })
+      .sort({
+        total: -1
+      })
+      .limit(10)
+      .lookup({
+        from: 'message',
+        localField: '_id',
+        foreignField: '_id',
+        as: 'message'
+      })
+      .unwind('$message')
+      .lookup({
+        from: 'publisher',
+        localField: 'message.pub_id',
+        foreignField: '_id',
+        as: 'message.publisher'
+      })
+      .unwind('$message.publisher')
+      .end()
+
+    return {
+      list: reads.list,
+      status: 'OK'
+    }
   } else if (event.favorite) {
     const messages = await db.collection('message')
       .aggregate()

+ 6 - 0
cloudfunctions/listSearches/config.json

@@ -0,0 +1,6 @@
+{
+  "permissions": {
+    "openapi": [
+    ]
+  }
+}

+ 32 - 0
cloudfunctions/listSearches/index.js

@@ -0,0 +1,32 @@
+// 云函数入口文件
+const cloud = require('wx-server-sdk')
+
+cloud.init()
+const db = cloud.database()
+const _ = db.command
+const $ = db.command.aggregate
+
+// 云函数入口函数
+exports.main = async (event, context) => {
+  const {
+    OPENID
+  } = cloud.getWXContext()
+
+  const searches = await db.collection('search')
+    .aggregate()
+    .match(_.expr($.lt([$.subtract([new Date(), '$search_time']), 1000 * 60 * 60 * 24])))
+    .group({
+      _id: '$keyword',
+      total: $.sum(1)
+    })
+    .sort({
+      total: -1
+    })
+    .limit(8)
+    .end()
+
+  return {
+    list: searches.list,
+    status: 'OK'
+  }
+}

+ 14 - 0
cloudfunctions/listSearches/package.json

@@ -0,0 +1,14 @@
+{
+  "name": "listSearches",
+  "version": "1.0.0",
+  "description": "",
+  "main": "index.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "author": "",
+  "license": "ISC",
+  "dependencies": {
+    "wx-server-sdk": "~2.4.0"
+  }
+}

+ 1 - 1
miniprogram/components/itemCard/itemCard.wxml

@@ -1,5 +1,5 @@
 <!-- 为首页消息通知的组件,数据在mainData.js -->
-<view wx:if="{{show}}" class="card" bindtap="getActivityInfo">
+<view wx:if="{{show}}" class="item-card" bindtap="getActivityInfo">
   <view class="profile">
     <view class="avatar-name" catchtap="getPublisherInfo">
       <image class="publisher-avatar" src="{{item.publisher.avatar}}" mode="aspectFill"></image>

+ 2 - 2
miniprogram/components/itemCard/itemCard.wxss

@@ -1,8 +1,8 @@
-.card {
+.item-card {
   display: flex;
   flex-direction: column;
   width: 700rpx;
-  margin: 50rpx 0rpx;
+  margin: 30rpx 0rpx;
   background-color: #ffffff;
   border-radius: 28rpx;
   box-shadow: 5rpx 5rpx 8rpx 0rpx rgba(0, 0, 0, 0.16);

+ 36 - 36
miniprogram/pages/activity/activity.js

@@ -101,6 +101,42 @@ Page({
     })
   },
 
+  comment: function () {
+    if (this.data.commentText.length < 5) {
+      wx.showToast({
+        title: '提问字数至少为5',
+        icon: 'none'
+      })
+    } else {
+      wx.showLoading({
+        title: '发送中'
+      })
+      wx.cloud.callFunction({
+        name: 'createQuestion',
+        data: {
+          msg_id: this.data.messageId,
+          question: this.data.commentText
+        }
+      }).then(res => {
+        wx.hideLoading()
+        if (res.result.status !== 'OK') {
+          wx.showToast({
+            title: res.result.errMsg,
+            icon: 'none'
+          })
+          return
+        }
+        wx.showToast({
+          title: '发送成功,请等待发布者回复',
+          icon: 'none'
+        })
+        this.setData({
+          commentText: ''
+        })
+      })
+    }
+  },
+
   processCommonData: function (data) {
     let edit = false
     for (let i = 0; i < app.globalData.pubInfo.length; i++) {
@@ -152,42 +188,6 @@ Page({
     })
   },
 
-  comment: function () {
-    if (this.data.commentText.length < 5) {
-      wx.showToast({
-        title: '提问字数至少为5',
-        icon: 'none'
-      })
-    } else {
-      wx.showLoading({
-        title: '发送中'
-      })
-      wx.cloud.callFunction({
-        name: 'createQuestion',
-        data: {
-          msg_id: this.data.messageId,
-          question: this.data.commentText
-        }
-      }).then(res => {
-        wx.hideLoading()
-        if (res.result.status !== 'OK') {
-          wx.showToast({
-            title: res.result.errMsg,
-            icon: 'none'
-          })
-          return
-        }
-        wx.showToast({
-          title: '发送成功,请等待发布者回复',
-          icon: 'none'
-        })
-        this.setData({
-          commentText: ''
-        })
-      })
-    }
-  },
-
   /**
    * 生命周期函数--监听页面加载
    */

+ 0 - 2
miniprogram/pages/activity/activity.wxss

@@ -1,9 +1,7 @@
 .page {
   display: flex;
   flex-direction: column;
-  justify-content: flex-start;
   align-items: center;
-  width: 100%;
 }
 
 .card {

+ 1 - 1
miniprogram/pages/main/main.wxml

@@ -49,7 +49,7 @@
       </view>
     </view>
   </view>
-  <view style="margin-top: 60rpx;">
+  <view class="message-view">
     <view wx:for="{{mainDatas}}" wx:for-item="msg" wx:key="_id">
       <itemCard wx:if="{{msg.show}}" item="{{msg}}" bindfiltermsgtype="filterMsgType" bindfiltertag="filterTag" />
     </view>

+ 7 - 1
miniprogram/pages/main/main.wxss

@@ -1,7 +1,6 @@
 .page {
   display: flex;
   flex-direction: column;
-  align-items: center;
 }
 
 .filter {
@@ -81,4 +80,11 @@
   line-height: 50rpx;
   margin-left: 25rpx;
   margin-right: 25rpx;
+}
+
+.message-view {
+  margin-top: 80rpx;
+  display: flex;
+  flex-direction: column;
+  align-items: center;
 }

+ 37 - 31
miniprogram/pages/publisher/publisher.js

@@ -14,6 +14,12 @@ Page({
     mainDatas: []
   },
 
+  viewAvatar: function () {
+    wx.previewImage({
+      urls: [this.data.publisherInfo.avatar]
+    })
+  },
+
   detail: function () {
     wx.navigateTo({
       url: '/pages/publisherDetail/publisherDetail',
@@ -24,6 +30,37 @@ Page({
     })
   },
 
+  toggleLike: function () {
+    if (this.data.likeDisabled) {
+      return
+    }
+    wx.showLoading({
+      title: this.data.like ? '取消关注' : '关注中'
+    })
+    wx.cloud.callFunction({
+      name: this.data.like ? 'deleteFollow' : 'createFollow',
+      data: {
+        pub_id: this.data.publisherId
+      }
+    }).then(res => {
+      wx.hideLoading()
+      if (res.result.status !== 'OK') {
+        wx.showToast({
+          title: res.result.errMsg,
+          icon: 'none'
+        })
+        return
+      }
+      wx.showToast({
+        title: this.data.like ? '取消关注成功' : '关注成功',
+        icon: 'none'
+      })
+      this.setData({
+        like: !this.data.like
+      })
+    })
+  },
+
   loadExtraData: function () {
     wx.showNavigationBarLoading()
     const arr = []
@@ -63,37 +100,6 @@ Page({
     })
   },
 
-  toggleLike: function () {
-    if (this.data.likeDisabled) {
-      return
-    }
-    wx.showLoading({
-      title: this.data.like ? '取消关注' : '关注中'
-    })
-    wx.cloud.callFunction({
-      name: this.data.like ? 'deleteFollow' : 'createFollow',
-      data: {
-        pub_id: this.data.publisherId
-      }
-    }).then(res => {
-      wx.hideLoading()
-      if (res.result.status !== 'OK') {
-        wx.showToast({
-          title: res.result.errMsg,
-          icon: 'none'
-        })
-        return
-      }
-      wx.showToast({
-        title: this.data.like ? '取消关注成功' : '关注成功',
-        icon: 'none'
-      })
-      this.setData({
-        like: !this.data.like
-      })
-    })
-  },
-
   /**
    * 生命周期函数--监听页面加载
    */

+ 5 - 3
miniprogram/pages/publisher/publisher.wxml

@@ -2,7 +2,7 @@
 <view class="top">
   <view class="brief-info">
     <view class="brief-info-left">
-      <image class="publisher-avatar" src="{{publisherInfo.avatar}}" mode="aspectFill" />
+      <image class="publisher-avatar" src="{{publisherInfo.avatar}}" mode="aspectFill" bindtap="viewAvatar" />
       <text class="publisher-name">{{publisherInfo.name}}</text>
     </view>
     <image class="like" src="/images/like/{{like ? 'heart' : 'hollowheart'}}.png" bindtap="toggleLike" />
@@ -18,6 +18,8 @@
   </view>
 </view>
 
-<view class="card-list" wx:for="{{mainDatas}}" wx:for-item="msg" wx:key="_id">
-  <itemCard item="{{msg}}" />
+<view class="card-list">
+  <view wx:for="{{mainDatas}}" wx:for-item="msg" wx:key="_id">
+    <itemCard item="{{msg}}" />
+  </view>
 </view>

+ 1 - 1
miniprogram/pages/publisher/publisher.wxss

@@ -72,7 +72,7 @@
 }
 
 .card-list {
-  width: 100%;
+  margin-top: 20rpx;
   display: flex;
   flex-direction: column;
   align-items: center;

+ 60 - 62
miniprogram/pages/search/search.js

@@ -18,45 +18,6 @@ Page({
     searchHistory: []
   },
 
-  onLoad: function () {
-    wx.getStorage({
-      key: 'searchHistory',
-      success: function (res) {
-        this.setData({
-          searchHistory: res.data
-        })
-      }.bind(this)
-    })
-    const db = wx.cloud.database()
-    db.collection('search').orderBy('time', 'desc').limit(20).get({
-      success: function (res) {
-        var count = {},
-          hotTag = []
-        for (let j = 0; j < res.data.length; j++) {
-          if (count[res.data[j].key] == undefined) {
-            count[res.data[j].key] = 0
-          }
-          count[res.data[j].key]++
-        }
-        for (let key in count) {
-          hotTag.push({
-            tag: key
-          })
-        }
-        hotTag.sort(function (a, b) {
-          return count[b] - count[a]
-        })
-        hotTag.splice(10)
-        this.setData({
-          hotTagData: hotTag
-        })
-      }.bind(this)
-    })
-    this.setData({
-      hotBarData: hotBarData.hotBarData
-    })
-  },
-
   update: function (e) {
     this.setData({
       searchText: e.detail.value,
@@ -97,18 +58,16 @@ Page({
   },
 
   search: function () {
-    var history = this.data.searchHistory
+    let history = this.data.searchHistory
     if (history === undefined) history = []
-    for (let i = 0; i < history.length; i++) {
-      if (history[i] == this.data.searchText) {
-        history.splice(i, 1)
-        break
-      }
+    const index = history.indexOf(this.data.searchText)
+    if (index !== -1) {
+      history.splice(index, 1)
     }
     if (history.length > 9) {
       history.splice(9, history.length - 9)
     }
-    history = [this.data.searchText].concat(history)
+    history.splice(0, 0, this.data.searchText)
     this.setData({
       searchHistory: history
     })
@@ -175,28 +134,18 @@ Page({
 
   searchTag: function (e) {
     this.setData({
-      searchText: e.target.dataset.searchTag
-    })
-    this.focus()
-    this.search()
-  },
-
-  searchAgain: function (e) {
-    this.setData({
-      searchText: e.target.dataset.history
+      searchText: e.currentTarget.dataset.searchTag
     })
     this.focus()
     this.search()
   },
 
   removeHistory: function (e) {
-    var history = this.data.searchHistory
+    let history = this.data.searchHistory
     if (history === undefined) history = []
-    for (let i = 0; i < history.length; i++) {
-      if (history[i] == e.target.dataset.history) {
-        history.splice(i, 1)
-        break
-      }
+    const index = history.indexOf(e.currentTarget.dataset.searchTag)
+    if (index !== -1) {
+      history.splice(index, 1)
     }
     this.setData({
       searchHistory: history
@@ -209,7 +158,56 @@ Page({
 
   viewActivity: function (e) {
     wx.navigateTo({
-      url: '/pages/activity/activity?id=' + e.target.dataset.activityId
+      url: '/pages/activity/activity'
+    }).then(res => {
+      res.eventChannel.emit('loadCommonData', {
+        data: e.currentTarget.dataset.activity
+      })
+    })
+  },
+
+  loadHotData: function () {
+    wx.showNavigationBarLoading()
+    const arr = []
+    arr.push(wx.cloud.callFunction({
+      name: 'listSearches'
+    }))
+    arr.push(wx.cloud.callFunction({
+      name: 'listMessages',
+      data: {
+        hot: true
+      }
+    }))
+    Promise.all(arr).then(res => {
+      wx.hideNavigationBarLoading()
+      if (res[0].result.status !== 'OK' || res[1].result.status !== 'OK') {
+        wx.showToast({
+          title: res[0].result.errMsg || res[1].result.errMsg,
+          icon: 'none'
+        })
+        return
+      }
+      for (let i = 0; i < res[1].result.list.length; i++) {
+        res[1].result.list[i].message = util.dbToMsg(res[1].result.list[i].message)
+        const name = res[1].result.list[i].message.name
+        res[1].result.list[i].message.hot_name = name.substr(0, 20) + (name.length > 20 ? '...' : '')
+      }
+      this.setData({
+        hotTagData: res[0].result.list,
+        hotBarData: res[1].result.list
+      })
+    })
+  },
+
+  onLoad: function () {
+    wx.getStorage({
+      key: 'searchHistory',
+      success: function (res) {
+        this.setData({
+          searchHistory: res.data
+        })
+      }.bind(this)
     })
+    this.loadHotData()
   }
 });

+ 12 - 10
miniprogram/pages/search/search.wxml

@@ -20,18 +20,18 @@
   <view class="hot-tag-block">
     <view class="hot-tag-title primary-text-color">热门搜索</view>
     <view class="hot-tags secondary-text-color">
-      <view class="hot-tag" wx:for="{{hotTagData}}" wx:for-item="item" wx:key="tag" data-search-tag="{{item.tag}}"
-        bindtap="searchTag">{{item.tag}}</view>
+      <view class="hot-tag" wx:for="{{hotTagData}}" wx:for-item="tag" wx:key="_id" data-search-tag="{{tag._id}}"
+        bindtap="searchTag">{{tag._id}}</view>
     </view>
   </view>
 
   <view class="hot-bar-block">
     <text class="hot-bar-title primary-background-color white-text-color">实时热点</text>
-    <view class="hot-bar" wx:for="{{hotBarData}}" wx:key="rank" wx:for-item="item" wx:key="_id"
-      data-activity-id="{{item._id}}" bindtap="viewActivity">
+    <view class="hot-bar" wx:for="{{hotBarData}}" wx:key="rank" wx:for-item="bar" wx:for-index="index" wx:key="_id"
+      data-activity="{{bar.message}}" bindtap="viewActivity">
       <view>
-        <text class="primary-text-color" space="nbsp">{{item.rank}}{{" "}}{{" "}}{{" "}}</text><text
-          class="secondary-text-color">{{item.title}}</text>
+        <text class="primary-text-color" space="nbsp">{{index + 1}}{{' '}}{{' '}}{{' '}}</text><text
+          class="secondary-text-color">{{bar.message.hot_name}}</text>
       </view>
       <image class="more" src="/images/more.png" mode="aspectFill"></image>
     </view>
@@ -39,12 +39,14 @@
 </scroll-view>
 
 <scroll-view wx:if="{{searchEnable}}" class="search-block secondary-background-color" scroll-y bindtap="blur">
-  <view class="result-bar" wx:for="{{searchResult}}" wx:for-item="msg" wx:key="_id">
-    <itemCard item="{{msg}}" />
+  <view class="result-bar">
+    <view wx:for="{{searchResult}}" wx:for-item="msg" wx:key="_id">
+      <itemCard item="{{msg}}" />
+    </view>
   </view>
   <view wx:if="{{searchText == '' && searchResult.length == 0}}" wx:for="{{searchHistory}}" wx:for-item="keyword"
-    wx:key="*this" class="search-history" data-history="{{keyword}}" catchtap="searchAgain">
+    wx:key="*this" class="search-history" data-search-tag="{{keyword}}" catchtap="searchTag">
     <view>{{keyword}}</view>
-    <view data-history="{{keyword}}" catchtap="removeHistory">×</view>
+    <view data-search-tag="{{keyword}}" catchtap="removeHistory">×</view>
   </view>
 </scroll-view>

+ 4 - 1
miniprogram/pages/search/search.wxss

@@ -94,7 +94,10 @@
 }
 
 .result-bar {
-  padding: 0rpx 20rpx;
+  margin-top: 20rpx;
+  display: flex;
+  flex-direction: column;
+  align-items: center;
 }
 
 .search-history {