chat.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. {
  83. id: '1',
  84. title: '会费缴交成功通知',
  85. content: '尊敬的用户,您在2023年8月16日15:00成交缴交会费……',
  86. type: 'zhifu',
  87. createTime: '8/21',
  88. read: true
  89. },
  90. {
  91. id: '2',
  92. title: '继续教育通知',
  93. content: '尊敬的用户,您预约2023年8月16日《广州市存量房网……',
  94. type: 'jiaoyu',
  95. createTime: '8/20',
  96. read: true
  97. },
  98. {
  99. id: '3',
  100. title: '课程购买成功通知',
  101. content: '尊敬的用户,您在成功购买2023年8月16日《广州市存……',
  102. type: 'goumai',
  103. createTime: '8/19',
  104. read: true
  105. },
  106. {
  107. id: '4',
  108. title: '个人会员生日祝福',
  109. content: '尊敬的用户,今日是您的生日,协会祝你在……',
  110. type: 'shengri',
  111. createTime: '8/16',
  112. read: false
  113. },
  114. ])
  115. function onSearchConfirm() {
  116. searchForm.value.pageNumber = 1
  117. pageNum.value = 1
  118. onSearch()
  119. }
  120. function onSearchClear() {
  121. searchForm.value.keyword = null
  122. searchForm.value.pageNumber = 1
  123. pageNum.value = 1
  124. onSearch()
  125. }
  126. function onSearch() {
  127. loadMoreStatus.value = 'more'
  128. query(searchForm.value).then(res => {
  129. if (res && res.message === 'success') {
  130. list.value = res.data
  131. if (res.count === 0) {
  132. visualLoadMore.value = false
  133. }
  134. }
  135. })
  136. }
  137. function onChatClick(chat) {
  138. uni.navigateTo({
  139. url: `/pages/chatDetail/chatDetail?id=${chat.id}&title=${chat.title}`
  140. })
  141. }
  142. onPullDownRefresh((e) => {
  143. searchForm.value.pageNumber = 1
  144. loadMoreStatus.value = 'more'
  145. query(searchForm.value).then(res => {
  146. if (res && res.message === 'success') {
  147. list.value = res.data
  148. if (res.count === 0) {
  149. visualLoadMore.value = false
  150. }
  151. uni.showToast({
  152. title: '刷新成功',
  153. icon: 'success',
  154. duration: 2000
  155. })
  156. }
  157. uni.stopPullDownRefresh()
  158. })
  159. })
  160. onReachBottom(async () => {
  161. if (loadMoreStatus.value === 'noMore') {
  162. uni.showToast({
  163. title: '没有更多啦>﹏<',
  164. icon: 'none'
  165. })
  166. return
  167. }
  168. visualLoadMore.value = true
  169. loadMoreStatus.value = 'loading'
  170. searchForm.value.pageNum++
  171. query(searchForm.value).then(res => {
  172. if (res && res.message === 'success') {
  173. if (res.data) {
  174. if (res.count >= list.value.length) {
  175. if (list.value.length === res.count) {
  176. loadMoreStatus.value = 'noMore'
  177. visualLoadMore.value = true
  178. } else {
  179. list.value.push(...res.data)
  180. loadMoreStatus.value = 'more'
  181. visualLoadMore.value = false
  182. }
  183. } else {
  184. loadMoreStatus.value = 'noMore'
  185. visualLoadMore.value = true
  186. }
  187. } else {
  188. loadMoreStatus.value = 'noMore'
  189. visualLoadMore.value = true
  190. }
  191. }
  192. })
  193. })
  194. onLoad(() => {
  195. onSearch()
  196. count().then(res => {
  197. if (res && res.message === 'success') {
  198. tabbarStore.setMessageCount(res.data.amount)
  199. uni.setTabBarBadge({ //显示数字
  200. index: 1, //tabbar下标
  201. text: `${res.data.amount}` ?? '0' //数字
  202. })
  203. }
  204. })
  205. })
  206. </script>
  207. <style lang="scss" scoped>
  208. .container {
  209. height: 100vh;
  210. width: 100vw;
  211. background-color: $uni-bg-color;
  212. padding: 0 20rpx;
  213. .search-box {
  214. margin-bottom: 20rpx;
  215. @include topMagnet();
  216. ::v-deep(.u-search) {
  217. background-color: #e5e5e5;
  218. border-radius: 50rpx;
  219. .u-action {
  220. width: 18%;
  221. background-color: $uni-color-primary;
  222. border-radius: 50rpx;
  223. color: $uni-text-color-inverse;
  224. margin-right: 8rpx;
  225. font-size: 28rpx;
  226. line-height: 50rpx;
  227. letter-spacing: 3rpx;
  228. text-align: center;
  229. }
  230. }
  231. }
  232. .list-box {
  233. padding: 0 20rpx;
  234. .list-item-box {
  235. .main-box {
  236. display: flex;
  237. justify-content: space-between;
  238. align-items: center;
  239. padding: 30rpx 0;
  240. &:active {
  241. background-color: $uni-bg-color-hover;
  242. }
  243. // .icon-box {
  244. // width: 15%;
  245. // display: flex;
  246. // justify-content: center;
  247. // align-items: center;
  248. // position: relative;
  249. // .icon {
  250. // width: 70rpx;
  251. // height: 70rpx;
  252. // }
  253. // .tag {
  254. // position: absolute;
  255. // right: 5rpx;
  256. // top: -3rpx;
  257. // width: 15rpx;
  258. // height: 15rpx;
  259. // background-color: $uni-color-error;
  260. // border-radius: 50%;
  261. // }
  262. // }
  263. .content-box {
  264. width: 85%;
  265. display: flex;
  266. flex-direction: column;
  267. justify-content: space-around;
  268. gap: 10rpx;
  269. position: relative;
  270. .title {
  271. height: calc(70% - 5rpx);
  272. font-size: $uni-title-font-size-2;
  273. font-weight: bold;
  274. }
  275. .text {
  276. height: calc(30% - 5rpx);
  277. font-size: $uni-font-size-2;
  278. color: $uni-text-color-grey;
  279. @include text-overflow();
  280. }
  281. .tag {
  282. position: absolute;
  283. left: -14rpx;
  284. top: -3rpx;
  285. width: 15rpx;
  286. height: 15rpx;
  287. background-color: $uni-color-error;
  288. border-radius: 50%;
  289. }
  290. }
  291. .other-box {
  292. width: 15%;
  293. display: flex;
  294. flex-direction: column;
  295. justify-content: flex-end;
  296. align-items: center;
  297. gap: 10rpx;
  298. .date {
  299. font-size: $uni-font-size-2;
  300. color: $uni-text-color-grey;
  301. }
  302. }
  303. }
  304. .line-box {
  305. height: 1rpx;
  306. width: 80%;
  307. background-color: #E5E5E5;
  308. margin: 0 auto;
  309. }
  310. }
  311. }
  312. }
  313. </style>