Browse Source

ADD

消息通知功能
RegMs If 4 years ago
parent
commit
29a45e0693

+ 10 - 8
cloudfunctions/listMessages/index.js

@@ -58,14 +58,16 @@ exports.main = async (event, context) => {
       .unwind('$publisher')
       .end()
 
-    await db.collection('search')
-      .add({
-        data: {
-          user_id: OPENID,
-          keyword: event.keyword || '',
-          search_time: new Date()
-        }
-      })
+    if (event.page_token === 0) {
+      await db.collection('search')
+        .add({
+          data: {
+            user_id: OPENID,
+            keyword: event.keyword || '',
+            search_time: new Date()
+          }
+        })
+    }
 
     return {
       list: messages.list,

+ 6 - 0
cloudfunctions/listNotifications/config.json

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

+ 41 - 0
cloudfunctions/listNotifications/index.js

@@ -0,0 +1,41 @@
+// 云函数入口文件
+const cloud = require('wx-server-sdk')
+
+cloud.init()
+const db = cloud.database()
+
+// 云函数入口函数
+exports.main = async (event, context) => {
+  const {
+    OPENID
+  } = cloud.getWXContext()
+
+  const notifications = await db.collection('notification')
+    .aggregate()
+    .match({
+      user_id: OPENID
+    })
+    .skip(event.page_token)
+    .limit(event.page_size)
+    .lookup({
+      from: 'message',
+      localField: 'msg_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: notifications.list,
+    next_page_token: event.page_token + notifications.list.length,
+    status: 'OK'
+  }
+}

+ 14 - 0
cloudfunctions/listNotifications/package.json

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

+ 20 - 0
cloudfunctions/updateMessage/index.js

@@ -61,6 +61,26 @@ exports.main = async (event, context) => {
       }
     })
 
+  const favorites = await db.collection('favorite')
+    .where({
+      msg_id: event.msg_id
+    })
+    .get()
+  const notifications = []
+  for (let i = 0; i < favorites.data.length; i++) {
+    notifications.push({
+      user_id: favorites.data[i].user_id,
+      msg_id: favorites.data[i].msg_id,
+      type: '动态',
+      content: event.notification || '',
+      nofity_time: new Date()
+    })
+  }
+  await db.collection('notification')
+    .add({
+      data: notifications
+    })
+
   return {
     stats: message.stats,
     status: 'OK'

+ 14 - 0
cloudfunctions/updateQuestion/index.js

@@ -62,6 +62,20 @@ exports.main = async (event, context) => {
         }
       })
 
+    const notification = await db.collection('question')
+      .doc(event.que_id)
+      .get()
+    await db.collection('notification')
+      .add({
+        data: {
+          user_id: notification.data.user_id,
+          msg_id: notification.data.msg_id,
+          type: '回复',
+          content: event.answer || '',
+          notify_time: new Date()
+        }
+      })
+
     return {
       stats: question.stats,
       status: 'OK'

+ 30 - 30
miniprogram/components/itemmyNews/itemmyNews.js → miniprogram/components/itemNotification/itemNotification.js

@@ -1,31 +1,31 @@
-Component({
-  options: {
-    styleIsolation: "apply-shared"
-  },
-
-  properties: {
-    item: {
-      type: Object,
-      value: {}
-    }
-  },
-
-  data: {
-    ellipsis: true
-  },
-
-  methods: {
-    getPublisherInfo: function (e) {
-      wx.navigateTo({
-        url: "/pages/publisher/publisher?id=" + e.target.dataset.publisherId
-      })
-    },
-
-    elli: function () {
-      this.setData({
-        ellipsis: !this.data.ellipsis
-      })
-    }
-  }
-
+Component({
+  options: {
+    styleIsolation: "apply-shared"
+  },
+
+  properties: {
+    item: Object
+  },
+
+  data: {
+    ellipsis: true
+  },
+
+  methods: {
+    getMessageInfo: function () {
+      wx.navigateTo({
+        url: '/pages/message/message'
+      }).then(res => {
+        res.eventChannel.emit('loadCommonData', {
+          data: this.data.item.message
+        })
+      })
+    },
+
+    toggleEllipsis: function () {
+      this.setData({
+        ellipsis: !this.data.ellipsis
+      })
+    }
+  }
 })

+ 3 - 3
miniprogram/components/itemmyNews/itemmyNews.json → miniprogram/components/itemNotification/itemNotification.json

@@ -1,4 +1,4 @@
-{
-  "component": true,
-  "usingComponents": {}
+{
+  "component": true,
+  "usingComponents": {}
 }

+ 19 - 0
miniprogram/components/itemNotification/itemNotification.wxml

@@ -0,0 +1,19 @@
+<!-- 为消息-我的消息里通知的组件,组件中数据来自data:newNewsData.js -->
+<view class="card">
+  <view class="top">
+    <view class="profile" wx:if="{{item.type === '回复'}}">
+      <text class="primary-text-color" style="font-size: 24rpx;">
+        <text class="publisher-name" style="color: #000000;">{{item.message.publisher.name}}</text> 对您的评论进行了回复
+      </text>
+    </view>
+    <view class="profile" wx:if="{{item.type === '动态'}}">
+      <text class="primary-text-color" style="font-size: 24rpx;">您收藏的信息 <text class="publisher-name"
+          style="color: #000000;">{{item.message.name}}</text> 有新动态</text>
+    </view>
+    <text class="primary-text-color detail" bindtap="getMessageInfo">查看</text>
+  </view>
+  <view class="bottom">
+    <view class="text secondary-text-color {{ellipsis ? 'ellipsis' : 'unellipsis'}}">{{item.content}}</view>
+    <view class="detail primary-text-color" bindtap="toggleEllipsis">{{ellipsis ? '展开' : '收起'}}</view>
+  </view>
+</view>

+ 54 - 54
miniprogram/components/itemmyNews/itemmyNews.wxss → miniprogram/components/itemNotification/itemNotification.wxss

@@ -1,55 +1,55 @@
-.card {
-  height: auto;
-  margin: 30rpx 20rpx 0rpx 20rpx;
-  background-color: #ffffff;
-  box-shadow: 5rpx 5rpx 8rpx 0rpx rgba(0, 0, 0, 0.16);
-  border-radius: 20rpx;
-  padding-top: 20rpx;
-}
-
-.publisher-name {
-  font-size: 30rpx;
-}
-
-.text {
-  font-size: 24rpx;
-  display: -webkit-box;
-  -webkit-box-orient: vertical;
-  text-overflow: ellipsis;
-  overflow: hidden;
-}
-
-.ellipsis {
-  -webkit-line-clamp: 2;
-}
-
-.unellipsis {
-  -webkit-line-clamp: 0;
-}
-
-.top {
-  display: flex;
-  justify-content: space-between;
-  align-items: flex-end;
-  margin: 0rpx 30rpx 0rpx 30rpx;
-}
-
-.bottom {
-  display: flex;
-  justify-content: space-between;
-  align-items: flex-end;
-  margin: 20rpx 30rpx 0rpx 30rpx;
-  padding-bottom: 25rpx;
-
-}
-
-.profile {
-  display: flex;
-}
-
-.detail {
-  font-size: 24rpx;
-  text-align: right;
-  white-space: nowrap;
-  margin-left: 40rpx;
+.card {
+  height: auto;
+  margin: 30rpx 20rpx 0rpx 20rpx;
+  background-color: #ffffff;
+  box-shadow: 5rpx 5rpx 8rpx 0rpx rgba(0, 0, 0, 0.16);
+  border-radius: 20rpx;
+  padding-top: 20rpx;
+}
+
+.publisher-name {
+  font-size: 30rpx;
+}
+
+.text {
+  font-size: 24rpx;
+  display: -webkit-box;
+  -webkit-box-orient: vertical;
+  text-overflow: ellipsis;
+  overflow: hidden;
+}
+
+.ellipsis {
+  -webkit-line-clamp: 2;
+}
+
+.unellipsis {
+  -webkit-line-clamp: none;
+}
+
+.top {
+  display: flex;
+  justify-content: space-between;
+  align-items: flex-end;
+  margin: 0rpx 30rpx 0rpx 30rpx;
+}
+
+.bottom {
+  display: flex;
+  justify-content: space-between;
+  align-items: flex-end;
+  margin: 20rpx 30rpx 0rpx 30rpx;
+  padding-bottom: 25rpx;
+
+}
+
+.profile {
+  display: flex;
+}
+
+.detail {
+  font-size: 24rpx;
+  text-align: right;
+  white-space: nowrap;
+  margin-left: 40rpx;
 }

+ 0 - 20
miniprogram/components/itemmyNews/itemmyNews.wxml

@@ -1,20 +0,0 @@
-<!-- 为消息-我的消息里通知的组件,组件中数据来自data:newNewsData.js -->
-<view class="card">
-  <view class="top">
-    <view class="profile" wx:if="{{item.publishType == '回复'}}">
-      <text class="primary-text-color" style="font-size: 24rpx;">
-        <text class="publisher-name" style="color: #000000;">{{item.publisherName}}</text>对您的评论进行了回复
-      </text>
-    </view>
-    <view class="profile" wx:if="{{item.publishType == '动态'}}">
-      <text class="primary-text-color" style="font-size: 24rpx;">您收藏的信息 <text class="publisher-name"
-          style="color: #000000;">{{item.publisherName}}</text> 有新动态</text>
-    </view>
-    <text class="primary-text-color detail">查看</text>
-  </view>
-  <view class="bottom">
-    <view class="text secondary-text-color  {{ellipsis?'ellipsis':'unellipsis'}}">{{item.title}}</view>
-
-    <view class="detail primary-text-color" bindtap="elli">{{ellipsis?'展开':'收起'}}</view>
-  </view>
-</view>

+ 3 - 3
miniprogram/pages/messagePublish/messagePublish.wxml

@@ -77,7 +77,7 @@
     </view>
     <view wx:if="{{messageId}}" class="block" style="margin-top: 20rpx;">
       <view class="cate primary-text-color" style="align-self: flex-start;">消息通知</view>
-      <textarea class="input" style="height: 200rpx;" placeholder="提交后会以消息的形式通知关注者" name="notify"></textarea>
+      <textarea class="input" style="height: 200rpx;" placeholder="提交后会以消息的形式通知关注者" name="notification"></textarea>
     </view>
     <myButton class="button" type="primary" form-type="submit">提交</myButton>
   </form>
@@ -130,7 +130,7 @@
     </view>
     <view wx:if="{{messageId}}" class="block" style="margin-top: 20rpx;">
       <view class="cate primary-text-color" style="align-self: flex-start;">消息通知</view>
-      <textarea class="input" style="height: 200rpx;" placeholder="提交后会以消息的形式通知关注者" name="notify"></textarea>
+      <textarea class="input" style="height: 200rpx;" placeholder="提交后会以消息的形式通知关注者" name="notification"></textarea>
     </view>
     <myButton class="button" type="primary" form-type="submit">提交</myButton>
   </form>
@@ -176,7 +176,7 @@
     </view>
     <view wx:if="{{messageId}}" class="block" style="margin-top: 20rpx;">
       <view class="cate primary-text-color" style="align-self: flex-start;">消息通知</view>
-      <textarea class="input" style="height: 200rpx;" placeholder="提交后会以消息的形式通知关注者" name="notify"></textarea>
+      <textarea class="input" style="height: 200rpx;" placeholder="提交后会以消息的形式通知关注者" name="notification"></textarea>
     </view>
     <myButton class="button" type="primary" form-type="submit">提交</myButton>
   </form>

+ 69 - 9
miniprogram/pages/notification/notification.js

@@ -1,20 +1,80 @@
-var newNews = require("../../data/newNewsData.js");
-Component({
+const app = getApp()
+const util = require('../../utils/util.js')
+
+Page({
   data: {
-    newNews:{
+    hasUserInfo: false,
+    pageToken: 0,
+    notificationDatas: [],
+    loading: true
+  },
 
-    }
+  loadNotificationData: function (refresh) {
+    this.setData({
+      loading: true
+    })
+    wx.showNavigationBarLoading()
+    wx.cloud.callFunction({
+      name: 'listNotifications',
+      data: {
+        page_token: refresh ? 0 : this.data.pageToken,
+        page_size: 20
+      }
+    }).then(res => {
+      wx.hideNavigationBarLoading()
+      wx.stopPullDownRefresh()
+      if (res.result.status !== 'OK') {
+        wx.showToast({
+          title: res.result.errMsg,
+          icon: 'none'
+        })
+        return
+      }
+      for (let i = 0; i < res.result.list.length; i++) {
+        res.result.list[i].message = util.dbToMsg(res.result.list[i].message)
+      }
+      this.setData({
+        notificationDatas: refresh ? res.result.list : this.data.notificationDatas.concat(res.result.list),
+        pageToken: res.result.next_page_token,
+        loading: false
+      })
+    })
   },
 
-  lifetimes: {
-    ready: function() {
+  onShow: function () {
+    if (!this.data.hasUserInfo && app.globalData.hasUserInfo) {
+      this.loadNotificationData(true)
+    } else {
       this.setData({
-        newNews:newNews.newNews
+        loading: false
       })
     }
+    this.setData({
+      hasUserInfo: app.globalData.hasUserInfo
+    })
   },
 
-  methods: {
+  onPullDownRefresh: function () {
+    if (this.data.loading) {
+      return
+    }
+    this.setData({
+      hasUserInfo: app.globalData.hasUserInfo
+    })
+    if (this.data.hasUserInfo) {
+      this.loadNotificationData(true)
+    }
+  },
 
-  }
+  onReachBottom: function () {
+    if (this.data.loading) {
+      return
+    }
+    this.setData({
+      hasUserInfo: app.globalData.hasUserInfo
+    })
+    if (this.data.hasUserInfo) {
+      this.loadNotificationData(false)
+    }
+  },
 })

+ 2 - 2
miniprogram/pages/notification/notification.json

@@ -1,6 +1,6 @@
 {
-  "component":true,
+  "enablePullDownRefresh": true,
   "usingComponents": {
-    "itemmyNews":"/components/itemmyNews/itemmyNews"
+    "itemNotification": "/components/itemNotification/itemNotification"
   }
 }

+ 12 - 2
miniprogram/pages/notification/notification.wxml

@@ -1,7 +1,17 @@
 <!-- 为消息的导航栏 两个component界面:“关注动态”followNews “我的消息”myNews-->
 <!-- 修改:将关注动态转移到首页,消息页面删除导航栏-->
 <view class="page">
-  <view class="card" wx:for="{{newNews}}" wx:for-item="item" wx:key="_id" wx:index="index">
-    <itemmyNews item="{{item}}" />
+  <view wx:if="{{hasUserInfo}}">
+    <view wx:if="{{notificationDatas.length}}">
+      <view class="card" wx:for="{{notificationDatas}}" wx:for-item="item" wx:key="_id">
+        <itemNotification item="{{item}}" />
+      </view>
+    </view>
+    <view wx:else class="title primary-text-color" style="text-align: center;">
+      这里空空如也
+    </view>
+  </view>
+  <view wx:else class="title primary-text-color" style="text-align: center;">
+    请先登录
   </view>
 </view>

+ 5 - 0
miniprogram/pages/notification/notification.wxss

@@ -2,3 +2,8 @@
   display: flex;
   flex-direction: column;
 }
+
+.title {
+  margin: 20rpx 20rpx 20rpx 40rpx;
+  font-size: 35rpx;
+}

+ 0 - 1
miniprogram/pages/publisherQuestion/publisherQuestion.js

@@ -67,7 +67,6 @@ Page({
         answer: this.data.questions[e.currentTarget.dataset.index].answer
       }
     }).then(res => {
-      console.log(res)
       wx.hideLoading()
       if (res.result.status !== 'OK') {
         wx.showToast({