liyongli 3 anos atrás
pai
commit
8a3aeacfd5

+ 2 - 1
miniprogram/app.json

@@ -17,5 +17,6 @@
   "style": "v2",
   "useExtendedLib": {
     "weui": true
-  }
+  },
+  "onReachBottomDistance": 10
 }

+ 0 - 337
miniprogram/components/chatroom/chatroom.js

@@ -1,337 +0,0 @@
-const FATAL_REBUILD_TOLERANCE = 10
-const SETDATA_SCROLL_TO_BOTTOM = {
-  scrollTop: 100000,
-  scrollWithAnimation: true,
-}
-
-Component({
-  properties: {
-    envId: String,
-    collection: String,
-    groupId: String,
-    groupName: String,
-    userInfo: Object,
-    onGetUserInfo: {
-      type: Function,
-    },
-    getOpenID: {
-      type: Function,
-    },
-  },
-
-  data: {
-    chats: [],
-    textInputValue: '',
-    openId: '',
-    scrollTop: 0,
-    scrollToMessage: '',
-    hasKeyboard: false,
-  },
-
-  methods: {
-    onGetUserInfo(e) {
-      this.properties.onGetUserInfo(e)
-    },
-
-    getOpenID() { 
-      return this.properties.getOpenID() 
-    },
-
-    mergeCommonCriteria(criteria) {
-      return {
-        groupId: this.data.groupId,
-        ...criteria,
-      }
-    },
-
-    async initRoom() {
-      this.try(async () => {
-        await this.initOpenID()
-
-        const { envId, collection } = this.properties
-        this.db = wx.cloud.database({
-          env: envId,
-        })
-        const db = this.db
-        const _ = db.command
-
-        const { data: initList } = await db.collection(collection).where(this.mergeCommonCriteria()).orderBy('sendTimeTS', 'desc').get()
-
-        console.log('init query chats', initList)
-
-        this.setData({
-          chats: initList.reverse(),
-          scrollTop: 10000,
-        })
-
-        this.initWatch(initList.length ? {
-          sendTimeTS: _.gt(initList[initList.length - 1].sendTimeTS),
-        } : {})
-      }, '初始化失败')
-    },
-
-    async initOpenID() {
-      return this.try(async () => {
-        const openId = await this.getOpenID()
-
-        this.setData({
-          openId,
-        })
-      }, '初始化 openId 失败')
-    },
-
-    async initWatch(criteria) {
-      this.try(() => {
-        const { collection } = this.properties
-        const db = this.db
-        const _ = db.command
-
-        console.warn(`开始监听`, criteria)
-        this.messageListener = db.collection(collection).where(this.mergeCommonCriteria(criteria)).watch({
-          onChange: this.onRealtimeMessageSnapshot.bind(this),
-          onError: e => {
-            if (!this.inited || this.fatalRebuildCount >= FATAL_REBUILD_TOLERANCE) {
-              this.showError(this.inited ? '监听错误,已断开' : '初始化监听失败', e, '重连', () => {
-                this.initWatch(this.data.chats.length ? {
-                  sendTimeTS: _.gt(this.data.chats[this.data.chats.length - 1].sendTimeTS),
-                } : {})
-              })
-            } else {
-              this.initWatch(this.data.chats.length ? {
-                sendTimeTS: _.gt(this.data.chats[this.data.chats.length - 1].sendTimeTS),
-              } : {})
-            }
-          },
-        })
-      }, '初始化监听失败')
-    },
-
-    onRealtimeMessageSnapshot(snapshot) {
-      console.warn(`收到消息`, snapshot)
-
-      if (snapshot.type === 'init') {
-        this.setData({
-          chats: [
-            ...this.data.chats,
-            ...[...snapshot.docs].sort((x, y) => x.sendTimeTS - y.sendTimeTS),
-          ],
-        })
-        this.scrollToBottom()
-        this.inited = true
-      } else {
-        let hasNewMessage = false
-        let hasOthersMessage = false
-        const chats = [...this.data.chats]
-        for (const docChange of snapshot.docChanges) {
-          switch (docChange.queueType) {
-            case 'enqueue': {
-              hasOthersMessage = docChange.doc._openid !== this.data.openId
-              const ind = chats.findIndex(chat => chat._id === docChange.doc._id)
-              if (ind > -1) {
-                if (chats[ind].msgType === 'image' && chats[ind].tempFilePath) {
-                  chats.splice(ind, 1, {
-                    ...docChange.doc,
-                    tempFilePath: chats[ind].tempFilePath,
-                  })
-                } else chats.splice(ind, 1, docChange.doc)
-              } else {
-                hasNewMessage = true
-                chats.push(docChange.doc)
-              }
-              break
-            }
-          }
-        }
-        this.setData({
-          chats: chats.sort((x, y) => x.sendTimeTS - y.sendTimeTS),
-        })
-        if (hasOthersMessage || hasNewMessage) {
-          this.scrollToBottom()
-        }
-      }
-    },
-
-    async onConfirmSendText(e) {
-      this.try(async () => {
-        if (!e.detail.value) {
-          return
-        }
-
-        const { collection } = this.properties
-        const db = this.db
-        const _ = db.command
-
-        const doc = {
-          _id: `${Math.random()}_${Date.now()}`,
-          groupId: this.data.groupId,
-          avatar: this.data.userInfo.avatarUrl,
-          nickName: this.data.userInfo.nickName,
-          msgType: 'text',
-          textContent: e.detail.value,
-          sendTime: new Date(),
-          sendTimeTS: Date.now(), // fallback
-        }
-
-        this.setData({
-          textInputValue: '',
-          chats: [
-            ...this.data.chats,
-            {
-              ...doc,
-              _openid: this.data.openId,
-              writeStatus: 'pending',
-            },
-          ],
-        })
-        this.scrollToBottom(true)
-
-        await db.collection(collection).add({
-          data: doc,
-        })
-
-        this.setData({
-          chats: this.data.chats.map(chat => {
-            if (chat._id === doc._id) {
-              return {
-                ...chat,
-                writeStatus: 'written',
-              }
-            } else return chat
-          }),
-        })
-      }, '发送文字失败')
-    },
-
-    async onChooseImage(e) {
-      wx.chooseImage({
-        count: 1,
-        sourceType: ['album', 'camera'],
-        success: async res => {
-          const { envId, collection } = this.properties
-          const doc = {
-            _id: `${Math.random()}_${Date.now()}`,
-            groupId: this.data.groupId,
-            avatar: this.data.userInfo.avatarUrl,
-            nickName: this.data.userInfo.nickName,
-            msgType: 'image',
-            sendTime: new Date(),
-            sendTimeTS: Date.now(), // fallback
-          }
-
-          this.setData({
-            chats: [
-              ...this.data.chats,
-              {
-                ...doc,
-                _openid: this.data.openId,
-                tempFilePath: res.tempFilePaths[0],
-                writeStatus: 0,
-              },
-            ]
-          })
-          this.scrollToBottom(true)
-
-          const uploadTask = wx.cloud.uploadFile({
-            cloudPath: `${this.data.openId}/${Math.random()}_${Date.now()}.${res.tempFilePaths[0].match(/\.(\w+)$/)[1]}`,
-            filePath: res.tempFilePaths[0],
-            config: {
-              env: envId,
-            },
-            success: res => {
-              this.try(async () => {
-                await this.db.collection(collection).add({
-                  data: {
-                    ...doc,
-                    imgFileID: res.fileID,
-                  },
-                })
-              }, '发送图片失败')
-            },
-            fail: e => {
-              this.showError('发送图片失败', e)
-            },
-          })
-
-          uploadTask.onProgressUpdate(({ progress }) => {
-            this.setData({
-              chats: this.data.chats.map(chat => {
-                if (chat._id === doc._id) {
-                  return {
-                    ...chat,
-                    writeStatus: progress,
-                  }
-                } else return chat
-              })
-            })
-          })
-        },
-      })
-    },
-
-    onMessageImageTap(e) {
-      wx.previewImage({
-        urls: [e.target.dataset.fileid],
-      })
-    },
-
-    scrollToBottom(force) {
-      if (force) {
-        console.log('force scroll to bottom')
-        this.setData(SETDATA_SCROLL_TO_BOTTOM)
-        return
-      }
-
-      this.createSelectorQuery().select('.body').boundingClientRect(bodyRect => {
-        this.createSelectorQuery().select(`.body`).scrollOffset(scroll => {
-          if (scroll.scrollTop + bodyRect.height * 3 > scroll.scrollHeight) {
-            console.log('should scroll to bottom')
-            this.setData(SETDATA_SCROLL_TO_BOTTOM)
-          }
-        }).exec()
-      }).exec()
-    },
-
-    async onScrollToUpper() {
-      if (this.db && this.data.chats.length) {
-        const { collection } = this.properties
-        const _ = this.db.command
-        const { data } = await this.db.collection(collection).where(this.mergeCommonCriteria({
-          sendTimeTS: _.lt(this.data.chats[0].sendTimeTS),
-        })).orderBy('sendTimeTS', 'desc').get()
-        this.data.chats.unshift(...data.reverse())
-        this.setData({
-          chats: this.data.chats,
-          scrollToMessage: `item-${data.length}`,
-          scrollWithAnimation: false,
-        })
-      }
-    },
-
-    async try(fn, title) {
-      try {
-        await fn()
-      } catch (e) {
-        this.showError(title, e)
-      }
-    },
-
-    showError(title, content, confirmText, confirmCallback) {
-      console.error(title, content)
-      wx.showModal({
-        title,
-        content: content.toString(),
-        showCancel: confirmText ? true : false,
-        confirmText,
-        success: res => {
-          res.confirm && confirmCallback()
-        },
-      })
-    },
-  },
-
-  ready() {
-    global.chatroom = this
-    this.initRoom()
-    this.fatalRebuildCount = 0
-  },
-})

+ 0 - 4
miniprogram/components/chatroom/chatroom.json

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

+ 0 - 85
miniprogram/components/chatroom/chatroom.wxml

@@ -1,85 +0,0 @@
-<view class="chatroom">
-  <view class="header">
-    <!-- display number of people in the room -->
-    <view class="left"></view>
-    <!-- room name -->
-    <view class="middle">{{groupName}}</view>
-    <!-- reserved -->
-    <view class="right"></view>
-  </view>
-
-  <!-- chats -->
-  <scroll-view 
-    class="body" 
-    scroll-y 
-    scroll-with-animation="{{scrollWithAnimation}}"
-    scroll-top="{{scrollTop}}" 
-    scroll-into-view="{{scrollToMessage}}"
-    bindscrolltoupper="onScrollToUpper"
-  >
-    <view 
-      wx:for="{{chats}}"
-      wx:key="{{item._id}}"
-      id="item-{{index}}"
-      class="message {{openId == item._openid ? 'message__self' : ''}}"
-    >
-      <image 
-        class="avatar"
-        src="{{item.avatar}}"
-        mode="aspectFit"
-      ></image> 
-      <view class="main">
-        <view class="nickname">{{item.nickName}}</view>
-        <block wx:if="{{item.msgType === 'image'}}">
-          <view class="image-wrapper">
-            <view class="loading" wx:if="{{item.writeStatus > -1}}">{{item.writeStatus}}%</view>
-            <image 
-              src="{{item.tempFilePath || item.imgFileID}}" 
-              data-fileid="{{item.tempFilePath || item.imgFileID}}" 
-              class="image-content" 
-              style="{{item.imgStyle}}"
-              mode="aspectFit" 
-              bindtap="onMessageImageTap"></image>
-          </view>
-        </block>
-        <block wx:else>
-          <view class="text-wrapper">
-            <view class="loading" wx:if="{{item.writeStatus === 'pending'}}">···</view>
-            <view class="text-content">{{item.textContent}}</view>
-          </view>
-        </block>
-      </view>
-    </view>
-  </scroll-view>
-
-  <!-- message sender -->
-  <view class="footer">
-    <view class="message-sender" wx:if="{{userInfo}}">
-      <input 
-        class="text-input"
-        type="text"
-        confirm-type="send"
-        bindconfirm="onConfirmSendText"
-        cursor-spacing="20"
-        value="{{textInputValue}}"
-      ></input>
-
-      <image 
-        src="./photo.png" 
-        class="btn-send-image" 
-        mode="aspectFit"
-        bindtap="onChooseImage"
-      ></image>
-    </view>
-
-    <view class="message-sender" wx:if="{{!userInfo}}">
-      <button 
-        open-type="getUserInfo" 
-        bindgetuserinfo="onGetUserInfo"
-        class="userinfo"
-      >请先登录后参与聊天</button>
-    </view>
-  </view>
-
-</view>
-

+ 0 - 161
miniprogram/components/chatroom/chatroom.wxss

@@ -1,161 +0,0 @@
-.chatroom {
-  width: 100%;
-  height: 100%;
-  display: flex;
-  flex-direction: column;
-}
-
-.chatroom .header {
-  flex-basis: fit-content;
-  display: flex;
-  flex-direction: row;
-  border-bottom: 1px solid #ddd;
-  padding: 20rpx 0 30rpx;
-  font-size: 30rpx;
-  /* background: rgb(34, 187, 47);
-  color: rgba(255, 255, 255, 1) */
-  /* font-family: 'Microsoft YaHei' */
-}
-
-.chatroom .header .left {
-  flex: 1;
-}
-
-.chatroom .header .middle {
-  flex: 2;
-  text-align: center;
-}
-
-.chatroom .header .right {
-  flex: 1;
-}
-
-.chatroom .body {
-  flex: 2;
-  display: flex;
-  flex-direction: column;
-  background: rgb(237,237,237);
-  padding-bottom: 16rpx;
-}
-
-.body .message {
-  display: flex;
-  flex-direction: row;
-  position: relative;
-  margin: 12rpx 0;
-}
-
-.body .message.message__self {
-  flex-direction: row-reverse;
-}
-
-.body .message .avatar {
-  position: relative;
-  top: 5rpx;
-  width: 60rpx;
-  height: 60rpx;
-  border-radius: 5rpx;
-  margin: 15rpx;
-}
-
-.body .message .main {
-  flex: 1;
-  display: flex;
-  flex-direction: column;
-  align-items: flex-start;
-}
-
-.body .message.message__self .main {
-  align-items: flex-end;
-}
-
-.body .message .nickname {
-  font-size: 24rpx;
-  color: #444;
-}
-
-.body .message .text-content {
-  border: 1px solid transparent;
-  border-radius: 3px;
-  background-color: #fff;
-  margin: 2px 0 0 0;
-  padding: 4px 10px;
-  font-size: 30rpx;
-  display: inline-block;
-}
-
-.body .message.message__self .text-content {
-  background-color: paleturquoise;
-}
-
-.body .message .text-wrapper {
-  display: flex;
-  flex-direction: row;
-  align-items: center;
-  max-width: 80%;
-}
-
-.body .message.message__self .text-wrapper .loading{
-  font-size: 16rpx;
-  margin-right: 18rpx;
-}
-
-.body .message .image-wrapper {
-  display: flex;
-  flex-direction: row;
-  align-items: center;
-}
-
-.body .message .image-content {
-  max-width: 240rpx;
-  max-height: 240rpx;
-}
-
-.body .message.message__self .image-wrapper .loading {
-  font-size: 20rpx;
-  margin-right: 18rpx;
-}
-
-.chatroom .footer {
-  flex-basis: fit-content;
-  display: flex;
-  flex-direction: row;
-  border-top: 1px solid #ddd;
-  font-size: 10rpx;
-  padding: 20rpx 30rpx;
-  background: rgb(246,246,246);
-}
-
-.chatroom .footer .message-sender {
-  flex: 1;
-  display: flex;
-  flex-direction: row;
-}
-
-.message-sender .text-input {
-  flex: 1;
-  font-size: 16px;
-  border: 1px solid transparent;
-  border-radius: 5px;
-  padding: 3px 6px;
-  margin: 0 10px 0 5px;
-  background: #fff;
-}
-
-.message-sender .btn-send-image {
-  width: 50rpx;
-  height: 50rpx;
-  align-self: center;
-}
-
-button {
-  font-size: 30rpx;
-}
-
-button.userinfo {
-  background: darkturquoise;
-  color: aliceblue;
-  padding: 0 100rpx;
-  border: 1px solid #ddd;
-  border-radius: 20px;
-}

+ 29 - 10
miniprogram/pages/home/index.js

@@ -14,28 +14,47 @@ Page({
   onLoad: async function (options) {
     const db = wx.cloud.database();
     const _ = db.command;
-    let list = await db.collection('data_news').where({
+    let listNews = await db.collection('data_news').where({
       hot: true,
       type: 1
-    }).get();
+    }).count();
     let video = await db.collection("data_asset").where({
       type: "video"
-    }).get();
-    console.log(list)
-    list.data = list.data.concat(video.data);
-    list.data = list.data.sort((a,b)=>{
+    }).count();
+    const listpage = Math.ceil(listNews.total / 20)
+    const videopage = Math.ceil(video.total / 20)
+    let list = [];
+    for (let i = 0; i < listpage; i++) {
+      let end = listNews.total > (i + 1) * 20 ? (i + 1) * 20 : listNews.total;
+      let li = await db.collection('data_news').where({
+        hot: true,
+        type: 1
+      }).skip(i * 20)
+        .limit(end)
+        .get() || { data: [] };
+      list = list.concat(...li.data)
+    }
+    for (let i = 0; i < videopage; i++) {
+      let end = video.total > (i + 1) * 20 ? (i + 1) * 20 : video.total;
+      let li = await db.collection("data_asset").where({
+        type: "video"
+      }).skip(i * 20)
+        .limit(end)
+        .get() || { data: [] };
+      list = list.concat(...li.data)
+    }
+    list = list.sort((a,b)=>{
       if(a._createTime) a.create_time = a._createTime;
       if(b._createTime) b.create_time = b._createTime;
       return b.create_time - a.create_time
     })
-    console.log(list)
 
-    for (let i = 0; i < list.data.length; i++) {
-      const v = list.data[i];
+    for (let i = 0; i < list.length; i++) {
+      const v = list[i];
       v.create_time = this.format(v.create_time || 0)
     }
     this.setData({
-      newList: list.data || []
+      newList: list || []
     })
   },
 

+ 32 - 10
miniprogram/pages/interList/interList.js

@@ -23,25 +23,47 @@ Page({
     }
     const db = wx.cloud.database();
     wx.showLoading()
-    let list = await db.collection('data_news').where({
+    let listAll = await db.collection('data_news').where({
       type: 1
-    }).get();
-    let video = await db.collection("data_asset").where({
+    }).count();
+    let videoAll = await db.collection("data_asset").where({
       type: "video"
-    }).get();
-    list.data = list.data.concat(video.data);
-    list.data = list.data.sort((a,b)=>{
+    }).count();
+
+    const videopage = Math.ceil(videoAll.total / 20);
+    const listpage = Math.ceil(listAll.total / 20);
+    let list = []
+    for (let i = 0; i < videopage; i++) {
+      let end = videoAll.total > (i + 1) * 20 ? (i + 1) * 20 : videoAll.total;
+      let li = await db.collection("data_asset").where({
+        type: "video"
+      }).skip(i * 20)
+        .limit(end)
+        .get() || { data: [] };
+      list = list.concat(...li.data)
+    }
+    for (let i = 0; i < listpage; i++) {
+      let end = listAll.total > (i + 1) * 20 ? (i + 1) * 20 : listAll.total;
+      let li = await db.collection('data_news').where({
+        type: 1
+      }).skip(i * 20)
+        .limit(end)
+        .get() || { data: [] };
+      list = list.concat(...li.data)
+    }
+
+    list = list.sort((a,b)=>{
       if(a._createTime) a.create_time = a._createTime;
       if(b._createTime) b.create_time = b._createTime;
       return b.create_time - a.create_time
     })
-    for (let i = 0; i < list.data.length; i++) {
-      const v = list.data[i];
+    for (let i = 0; i < list.length; i++) {
+      const v = list[i];
       if (v.create_time) v.create_time = this.format(v.create_time || 0)
     }
-    console.log(list.data)
+    console.log(list)
     this.setData({
-      newsList: list.data || []
+      newsList: list || []
     },()=>{
     wx.hideLoading()
     })

+ 23 - 13
miniprogram/pages/marvellous/index.js

@@ -13,49 +13,59 @@ Page({
     showVideo: "",
     showAnVideo: true
   },
-
   /**
    * 生命周期函数--监听页面加载
    */
-  onLoad: async function (options) {
+  onLoad: function (options) {
     wx.setNavigationBarTitle({
       title: options.title || "精彩瞬间"
     })
-
+    this.init(options);
+  },
+  async init(options) {
     const db = wx.cloud.database();
     const _ = db.command;
-    const $ = db.command.aggregate
+    if (!options) options = { type: this.data.pageType }
     this.imgList = [];
     let pageList = {};
     wx.showLoading();
-    let list = await db.collection('data_asset').where({
+    let allList = await db.collection('data_asset').where({
       type: _.eq(options.type || "img")
-    }).get();
+    }).count();
+    const totalpage = Math.ceil(allList.total / 20)
+    let list = [];
+    for (let i = 0; i < totalpage; i++) {
+      let end = allList.total > (i + 1) * 20 ? (i + 1) * 20 : allList.total;
+      let li = await db.collection('data_asset').where({
+        type: _.eq(options.type || "img")
+      }).skip(i * 20)
+        .limit(end)
+        .get() || { data: [] };
+      list = list.concat(...li.data)
+    }
 
     wx.hideLoading();
-    if (!list.data.length) return wx.showToast({
+    if (!list.length) return wx.showToast({
       title: '暂无数据',
       icon: "none"
     })
-    list.data.sort((a, b) => {
+    list.sort((a, b) => {
       return b.create_time - a.create_time
     })
-    let li = list.data || [];
-    console.log(list, options.type)
-    for (let i = 0; i < li.length; i++) {
-      const v = li[i];
+    for (let i = 0; i < list.length; i++) {
+      const v = list[i];
       v.index = i;
       // pageList[v.create_time] ? pageList[v.create_time].list.push(v) : pageList[v.create_time] = { list: [v], time: this.format(v.create_time) };
       pageList[0] ? pageList[0].list.push(v) : pageList[0] = { list: [v], time: 0 };
       this.imgList.push(v.url);
     }
+
     this.setData({
       pageList: pageList,
       pageType: options.type || "img",
       showAnVideo: options.type === "video"
     })
   },
-
   /**
    * 生命周期函数--监听页面初次渲染完成
    */