Jelajahi Sumber

接入消息列表接口

LinWuTai 1 Minggu lalu
induk
melakukan
9175e5249d
3 mengubah file dengan 115 tambahan dan 7 penghapusan
  1. 46 0
      api/chat.js
  2. 67 5
      pages/chat/chat.vue
  3. 2 2
      utils/request.js

+ 46 - 0
api/chat.js

@@ -0,0 +1,46 @@
+import request from '@/utils/request'
+
+/**
+ * 消息列表
+ * @param {Object} data 参数
+ */
+export function query(data) {
+	return request({
+		'url': '/chat/list',
+		headers: {
+			isToken: true
+		},
+		'method': 'post',
+		'data': data
+	})
+}
+
+/**
+ * 消息条数
+ * @param {Object} data 参数
+ */
+export function count() {
+	return request({
+		'url': '/chat/amount',
+		headers: {
+			isToken: true
+		},
+		'method': 'get',
+		'data': null
+	})
+}
+
+/**
+ * 消息详情
+ * @param {Number} id 消息ID
+ */
+export function detail(id) {
+	return request({
+		'url': `/chat/${id}`,
+		headers: {
+			isToken: true
+		},
+		'method': 'get',
+		'data': null
+	})
+}

+ 67 - 5
pages/chat/chat.vue

@@ -39,15 +39,22 @@
 
 <script setup>
 	import { ref } from 'vue'
-	import { onLoad } from '@dcloudio/uni-app'
+	import { onLoad, onReachBottom } from '@dcloudio/uni-app'
+	import { query, count } from '@/api/chat.js'
 	
 	const uToast = ref()
 	
 	const searchInputStyle = {
 		backgroundColor: '#E5E5E5'
 	}
+	const pageNum = ref(1)
+	const pageSize = ref(10)
+	const visualLoadMore = ref(false)
+	const loadMoreStatus = ref('more')
 	const searchForm = ref({
-		keyword: ''
+		keyword: '',
+		pageNumber: 1,
+		pageSize: 10
 	})
 	const list = ref([
 		{
@@ -83,16 +90,71 @@
 			isRead: false
 		},
 	])
+	
+	function onSearch() {
+		loadMoreStatus.value = 'more'
+		query(searchForm.value).then(res => {
+			if (res && res.message === 'success') {
+				list.value = res.data
+				if (res.count === 0) {
+					visualLoadMore.value = false
+				}
+			}
+		})
+	}
+	
 	function onChatClick(chat) {
 		uni.navigateTo({
 			url: `/pages/chatDetail/chatDetail?id=${chat.id}&title=${chat.title}`
 		})
 	}
+	
+	
+	onReachBottom(async () => {
+		if (loadMoreStatus.value === 'noMore') {
+			uni.showToast({
+				title: '没有更多啦>﹏<',
+				icon: 'none'
+			})
+			return
+		}
+		visualLoadMore.value = true
+		loadMoreStatus.value = 'loading'
+		searchForm.value.pageNum++
+		query(searchForm.value).then(res => {
+			if (res && res.message === 'success') {
+				if (res.data) {
+					if (res.count >= list.value.length) {
+						if (list.value.length === res.count) {
+							loadMoreStatus.value = 'noMore'
+							visualLoadMore.value = true
+						} else {
+							list.value.push(...res.data)
+							loadMoreStatus.value = 'more'
+							visualLoadMore.value = false
+						}							
+					} else {
+						loadMoreStatus.value = 'noMore'
+						visualLoadMore.value = true
+					}
+				} else {
+					loadMoreStatus.value = 'noMore'
+					visualLoadMore.value = true
+				}
+			}
+		})
+	})
 	onLoad(() => {
-		uni.setTabBarBadge({ //显示数字  
-			index: 1, //tabbar下标
-			text: '999' //数字
+		onSearch()
+		count().then(res => {
+			if (res && res.message === 'success') {
+				uni.setTabBarBadge({ //显示数字
+					index: 1, //tabbar下标
+					text: `${res.data.amount}` ?? '0' //数字
+				})
+			}
 		})
+		
 	})
 </script>
 

+ 2 - 2
utils/request.js

@@ -15,7 +15,7 @@ const request = config => {
 	config.headers = config.headers || {};
 
 	if (getToken() && !isToken) {
-		config.headers['Authorization'] = 'Bearer ' + getToken();
+		config.headers['Authorization'] = getToken();
 	}
 
 	if (config.params) {
@@ -51,7 +51,7 @@ const request = config => {
 					toast(msg);
 					reject(code);
 				}
-				resolve(res.data.data);
+				resolve(res.data);
 			})
 			.catch(error => {
 				let {