Sfoglia il codice sorgente

ADD

搜索功能对接
RegMs If 4 anni fa
parent
commit
7e575e4502

+ 46 - 0
cloudfunctions/listMessages/index.js

@@ -3,6 +3,7 @@ const cloud = require('wx-server-sdk')
 
 cloud.init()
 const db = cloud.database()
+const _ = db.command
 
 // 云函数入口函数
 exports.main = async (event, context) => {
@@ -23,6 +24,51 @@ exports.main = async (event, context) => {
       .limit(event.page_size)
       .end()
 
+    return {
+      list: messages.list,
+      next_page_token: event.page_token + messages.list.length,
+      status: 'OK'
+    }
+  } else if (event.keyword) {
+    const regexp = db.RegExp({
+      regexp: event.keyword,
+      options: 'i'
+    })
+    const messages = await db.collection('message')
+      .aggregate()
+      .match(_.or([{
+        name: regexp
+      }, {
+        brief: regexp
+      }, {
+        detail: regexp
+      }]))
+      .sort({
+        publish_time: -1
+      })
+      .skip(event.page_token)
+      .limit(event.page_size)
+      .lookup({
+        from: 'publisher',
+        localField: 'pub_id',
+        foreignField: '_id',
+        as: 'publisher'
+      })
+      .end()
+
+    for (let i = 0; i < messages.list.length; i++) {
+      messages.list[i].publisher = messages.list[i].publisher[0]
+    }
+
+    await db.collection('search')
+      .add({
+        data: {
+          user_id: OPENID,
+          keyword: event.keyword || '',
+          search_time: new Date()
+        }
+      })
+
     return {
       list: messages.list,
       next_page_token: event.page_token + messages.list.length,

+ 2 - 2
miniprogram/pages/publisher/publisher.js

@@ -10,7 +10,7 @@ Page({
     publisherInfo: {},
     pageToken: 0,
     mainDatas: [],
-    like: -1,
+    like: false,
     likeEnable: true
   },
 
@@ -40,7 +40,7 @@ Page({
       }
       for (let i = 0; i < res.result.list.length; i++) {
         res.result.list[i] = util.dbToMsg(res.result.list[i])
-        res.result.list[i].publisher = [this.data.publisherInfo]
+        res.result.list[i].publisher = this.data.publisherInfo
       }
       this.setData({
         mainDatas: this.data.mainDatas.concat(res.result.list),

+ 1 - 7
miniprogram/pages/publisher/publisher.wxml

@@ -5,11 +5,9 @@
       <image class="publisher-avatar" src="{{publisherInfo.avatar}}" mode="aspectFill" />
       <text class="publisher-name">{{publisherInfo.name}}</text>
     </view>
-    <image wx:if="{{like != -1}}" class="like" src="/images/like/{{like ? 'heart' : 'hollowheart'}}.png"
-      bindtap="toggleLike" />
+    <image class="like" src="/images/like/{{like ? 'heart' : 'hollowheart'}}.png" bindtap="toggleLike" />
   </view>
 
-
   <view class="detailed-info">
     <view class="publisher-intro secondary-text-color"><text
         class="primary-text-color">简介:</text>{{publisherInfo.intro}}</view>
@@ -20,10 +18,6 @@
   </view>
 </view>
 
-
-
-
-
 <view class="card" wx:for="{{mainDatas}}" wx:for-item="item" wx:key="_id">
   <itemCard item="{{item}}" />
 </view>

+ 34 - 44
miniprogram/pages/search/search.js

@@ -1,17 +1,17 @@
-const hotTagData = require("../../data/hotTagData.js")
-const hotBarData = require("../../data/hotBarData.js")
+const hotTagData = require('../../data/hotTagData.js')
+const hotBarData = require('../../data/hotBarData.js')
 const util = require('../../utils/util.js')
 
 Page({
   options: {
-    styleIsolation: "apply-shared"
+    styleIsolation: 'apply-shared'
   },
 
   // 页面的初始数据
   data: {
     toggleEnable: true,
     searchEnable: false,
-    searchText: "",
+    searchText: '',
     hotTagData: [],
     hotBarData: [],
     searchResult: [],
@@ -20,7 +20,7 @@ Page({
 
   onLoad: function () {
     wx.getStorage({
-      key: "searchHistory",
+      key: 'searchHistory',
       success: function (res) {
         this.setData({
           searchHistory: res.data
@@ -28,7 +28,7 @@ Page({
       }.bind(this)
     })
     const db = wx.cloud.database()
-    db.collection("search").orderBy("time", "desc").limit(20).get({
+    db.collection('search').orderBy('time', 'desc').limit(20).get({
       success: function (res) {
         var count = {},
           hotTag = []
@@ -92,13 +92,13 @@ Page({
   },
 
   blur: function () {
-    if (this.data.searchText != "") return
+    if (this.data.searchText !== '') return
     this.cancel()
   },
 
   search: function () {
     var history = this.data.searchHistory
-    if (history == undefined) history = []
+    if (history === undefined) history = []
     for (let i = 0; i < history.length; i++) {
       if (history[i] == this.data.searchText) {
         history.splice(i, 1)
@@ -113,44 +113,34 @@ Page({
       searchHistory: history
     })
     wx.setStorage({
-      key: "searchHistory",
+      key: 'searchHistory',
       data: history
     })
     wx.showLoading({
-      title: "搜索中"
+      title: '搜索中'
     })
-    const db = wx.cloud.database()
-    const _ = db.command
-    db.collection("search").add({
+    wx.cloud.callFunction({
+      name: 'listMessages',
       data: {
-        key: this.data.searchText,
-        time: new Date()
-      },
-      success: function () {
-        var reg = new RegExp(this.data.searchText, "i")
-        db.collection("message").where(
-          _.or([{
-              title: reg
-            },
-            {
-              subTitle: reg
-            },
-            {
-              details: reg
-            }
-          ])
-        ).orderBy("time", "desc").limit(20).get({
-          success: function (res) {
-            for (let i = 0; i < res.data.length; i++) {
-              res.data[i].time = util.handleDate(res.data[i].time)
-            }
-            this.setData({
-              searchResult: res.data
-            })
-            wx.hideLoading()
-          }.bind(this)
+        keyword: this.data.searchText,
+        page_token: 0,
+        page_size: 25
+      }
+    }).then(res => {
+      wx.hideLoading()
+      if (res.result.status !== 'OK') {
+        wx.showToast({
+          title: res.result.errMsg,
+          icon: 'none'
         })
-      }.bind(this)
+        return
+      }
+      for (let i = 0; i < res.result.list.length; i++) {
+        res.result.list[i] = util.dbToMsg(res.result.list[i])
+      }
+      this.setData({
+        searchResult: res.result.list
+      })
     })
   },
 
@@ -158,7 +148,7 @@ Page({
     if (!this.data.toggleEnable || !this.data.searchEnable) return
     this.data.toggleEnable = false
     this.setData({
-      searchText: "",
+      searchText: '',
     })
     this.animate('.cancel-button', [{
         opacity: 1
@@ -201,7 +191,7 @@ Page({
 
   removeHistory: function (e) {
     var history = this.data.searchHistory
-    if (history == undefined) history = []
+    if (history === undefined) history = []
     for (let i = 0; i < history.length; i++) {
       if (history[i] == e.target.dataset.history) {
         history.splice(i, 1)
@@ -212,14 +202,14 @@ Page({
       searchHistory: history
     })
     wx.setStorage({
-      key: "searchHistory",
+      key: 'searchHistory',
       data: history
     })
   },
 
   viewActivity: function (e) {
     wx.navigateTo({
-      url: "/pages/activity/activity?id=" + e.target.dataset.activityId
+      url: '/pages/activity/activity?id=' + e.target.dataset.activityId
     })
   }
 });