chat.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <template>
  2. <view class="container">
  3. <view class="search-box">
  4. <u-search
  5. v-model="searchForm.keyword"
  6. :clearabled="true"
  7. bg-color="#E5E5E5"
  8. :input-style="searchInputStyle"
  9. placeholder="请输入搜索内容"
  10. @search="onSearchConfirm"
  11. @custom="onSearchConfirm"
  12. @clear="onSearchClear"
  13. ></u-search>
  14. </view>
  15. <u-empty
  16. mode="data"
  17. v-if="list.length === 0"
  18. iconSize="120"
  19. textSize="58"
  20. text="暂无数据"
  21. >
  22. </u-empty>
  23. <view class="list-box">
  24. <view class="list-item-box" v-for="item in list" :key="item.id" @click="onChatClick(item)">
  25. <view class="main-box">
  26. <view class="content-box">
  27. <view class="title">
  28. {{item.title}}
  29. </view>
  30. <view class="text">
  31. {{item.content}}
  32. </view>
  33. <view class="tag" v-show="!item.read"></view>
  34. </view>
  35. <view class="other-box">
  36. <view class="date">
  37. {{item.createTime}}
  38. </view>
  39. </view>
  40. </view>
  41. <view class="line-box"></view>
  42. </view>
  43. </view>
  44. <uni-load-more v-show="visualLoadMore" :status="loadMoreStatus"></uni-load-more>
  45. <!-- 与包裹页面所有内容的元素u-page同级,且在它的下方 -->
  46. <u-tabbar v-model="tabbarCurrentIndex" :list="tabbarList" icon-size="50" :active-color="tabbarActiveColor" :inactive-color="tabbarInactiveColor"></u-tabbar>
  47. </view>
  48. </template>
  49. <script setup>
  50. import { ref, computed } from 'vue'
  51. import { onLoad, onReachBottom, onPullDownRefresh } from '@dcloudio/uni-app'
  52. import { query, count } from '@/api/chat.js'
  53. import { useTabbarStore } from '@/store/tabbarStore.js'
  54. const tabbarStore = useTabbarStore()
  55. // 底部导航栏数据
  56. const tabbarList = computed(() => {
  57. return tabbarStore.list
  58. })
  59. // 底部导航栏选中颜色
  60. const tabbarActiveColor = computed(() => {
  61. return tabbarStore.activeColor
  62. })
  63. // 底部导航栏未选中颜色
  64. const tabbarInactiveColor = computed(() => {
  65. return tabbarStore.inactiveColor
  66. })
  67. const tabbarCurrentIndex = 1
  68. const uToast = ref()
  69. const searchInputStyle = {
  70. backgroundColor: '#E5E5E5'
  71. }
  72. const pageNum = ref(1)
  73. const pageSize = ref(10)
  74. const visualLoadMore = ref(false)
  75. const loadMoreStatus = ref('more')
  76. const searchForm = ref({
  77. keyword: '',
  78. pageNumber: 1,
  79. pageSize: 10
  80. })
  81. const list = ref([])
  82. function onSearchConfirm() {
  83. searchForm.value.pageNumber = 1
  84. pageNum.value = 1
  85. onSearch()
  86. }
  87. function onSearchClear() {
  88. searchForm.value.keyword = null
  89. searchForm.value.pageNumber = 1
  90. pageNum.value = 1
  91. onSearch()
  92. }
  93. function onSearch() {
  94. loadMoreStatus.value = 'more'
  95. query(searchForm.value).then(res => {
  96. if (res && res.message === 'success') {
  97. list.value = res.data
  98. if (res.count === 0) {
  99. visualLoadMore.value = false
  100. }
  101. }
  102. })
  103. }
  104. function onChatClick(chat) {
  105. uni.navigateTo({
  106. url: `/pages/chatDetail/chatDetail?id=${chat.id}&title=${chat.title}`
  107. })
  108. }
  109. onPullDownRefresh((e) => {
  110. searchForm.value.pageNumber = 1
  111. loadMoreStatus.value = 'more'
  112. query(searchForm.value).then(res => {
  113. if (res && res.message === 'success') {
  114. list.value = res.data
  115. if (res.count === 0) {
  116. visualLoadMore.value = false
  117. }
  118. uni.showToast({
  119. title: '刷新成功',
  120. icon: 'success',
  121. duration: 2000
  122. })
  123. }
  124. uni.stopPullDownRefresh()
  125. })
  126. })
  127. onReachBottom(async () => {
  128. if (loadMoreStatus.value === 'noMore') {
  129. uni.showToast({
  130. title: '没有更多啦>﹏<',
  131. icon: 'none'
  132. })
  133. return
  134. }
  135. visualLoadMore.value = true
  136. loadMoreStatus.value = 'loading'
  137. searchForm.value.pageNum++
  138. query(searchForm.value).then(res => {
  139. if (res && res.message === 'success') {
  140. if (res.data) {
  141. if (res.count >= list.value.length) {
  142. if (list.value.length === res.count) {
  143. loadMoreStatus.value = 'noMore'
  144. visualLoadMore.value = true
  145. } else {
  146. list.value.push(...res.data)
  147. loadMoreStatus.value = 'more'
  148. visualLoadMore.value = false
  149. }
  150. } else {
  151. loadMoreStatus.value = 'noMore'
  152. visualLoadMore.value = true
  153. }
  154. } else {
  155. loadMoreStatus.value = 'noMore'
  156. visualLoadMore.value = true
  157. }
  158. }
  159. })
  160. })
  161. onLoad(() => {
  162. onSearch()
  163. count().then(res => {
  164. if (res && res.message === 'success') {
  165. tabbarStore.setMessageCount(res.data.amount)
  166. uni.setTabBarBadge({ //显示数字
  167. index: 1, //tabbar下标
  168. text: `${res.data.amount}` ?? '0' //数字
  169. })
  170. }
  171. })
  172. })
  173. </script>
  174. <style lang="scss" scoped>
  175. .container {
  176. height: 100vh;
  177. width: 100vw;
  178. background-color: $uni-bg-color;
  179. padding: 0 20rpx;
  180. .search-box {
  181. margin-bottom: 20rpx;
  182. @include topMagnet();
  183. ::v-deep(.u-search) {
  184. background-color: #e5e5e5;
  185. border-radius: 50rpx;
  186. .u-action {
  187. width: 18%;
  188. background-color: $uni-color-primary;
  189. border-radius: 50rpx;
  190. color: $uni-text-color-inverse;
  191. margin-right: 8rpx;
  192. font-size: 28rpx;
  193. line-height: 50rpx;
  194. letter-spacing: 3rpx;
  195. text-align: center;
  196. }
  197. }
  198. }
  199. .list-box {
  200. padding: 0 20rpx;
  201. .list-item-box {
  202. .main-box {
  203. display: flex;
  204. justify-content: space-between;
  205. align-items: center;
  206. padding: 30rpx 0;
  207. &:active {
  208. background-color: $uni-bg-color-hover;
  209. }
  210. // .icon-box {
  211. // width: 15%;
  212. // display: flex;
  213. // justify-content: center;
  214. // align-items: center;
  215. // position: relative;
  216. // .icon {
  217. // width: 70rpx;
  218. // height: 70rpx;
  219. // }
  220. // .tag {
  221. // position: absolute;
  222. // right: 5rpx;
  223. // top: -3rpx;
  224. // width: 15rpx;
  225. // height: 15rpx;
  226. // background-color: $uni-color-error;
  227. // border-radius: 50%;
  228. // }
  229. // }
  230. .content-box {
  231. width: 85%;
  232. display: flex;
  233. flex-direction: column;
  234. justify-content: space-around;
  235. gap: 10rpx;
  236. position: relative;
  237. .title {
  238. height: calc(70% - 5rpx);
  239. font-size: $uni-title-font-size-2;
  240. font-weight: bold;
  241. }
  242. .text {
  243. height: calc(30% - 5rpx);
  244. font-size: $uni-font-size-2;
  245. color: $uni-text-color-grey;
  246. @include text-overflow();
  247. }
  248. .tag {
  249. position: absolute;
  250. left: -14rpx;
  251. top: -3rpx;
  252. width: 15rpx;
  253. height: 15rpx;
  254. background-color: $uni-color-error;
  255. border-radius: 50%;
  256. }
  257. }
  258. .other-box {
  259. width: 15%;
  260. display: flex;
  261. flex-direction: column;
  262. justify-content: flex-end;
  263. align-items: center;
  264. gap: 10rpx;
  265. .date {
  266. font-size: $uni-font-size-2;
  267. color: $uni-text-color-grey;
  268. }
  269. }
  270. }
  271. .line-box {
  272. height: 1rpx;
  273. width: 80%;
  274. background-color: #E5E5E5;
  275. margin: 0 auto;
  276. }
  277. }
  278. }
  279. }
  280. </style>