message.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <template>
  2. <view>
  3. <u-navbar
  4. title-color="#fff"
  5. :custom-back="customBack"
  6. :border-bottom="false"
  7. back-icon-color="#CCE8FF"
  8. :background="{ background: '#008CFF' }"
  9. title="消息中心"
  10. />
  11. <mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback">
  12. <scroll-view scroll-y style="height: 100%; width: 100%" v-for="(messageItem, index) in messageList" :key="messageItem.id">
  13. <view class="time">{{ messageItem.createTime }}</view>
  14. <view class="page-box">
  15. <view class="message" @click="goDetails(messageItem)">
  16. <view class="message-top u-flex">
  17. <view class="message-top-left u-flex-1">
  18. <view class="car">{{ messageItem.title }}</view>
  19. </view>
  20. <view class="message-top-right u-flex-2">
  21. <u-badge :is-dot="true" type="error" v-if="messageItem.readFlag == '0'"></u-badge>
  22. </view>
  23. </view>
  24. <view class="message-center">
  25. <view class="message-center-item">{{ messageItem.content }}</view>
  26. </view>
  27. <view class="message-bottom">
  28. <u-cell-item title="查看详情" style="color: #008cff"></u-cell-item>
  29. </view>
  30. </view>
  31. </view>
  32. </scroll-view>
  33. </mescroll-body>
  34. <u-toast ref="uToast" />
  35. </view>
  36. </template>
  37. <script>
  38. import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js';
  39. export default {
  40. mixins: [MescrollMixin], // 使用mixin
  41. data() {
  42. return {
  43. messageList: []
  44. };
  45. },
  46. methods: {
  47. // 返回上一页
  48. customBack() {
  49. uni.getStorage({
  50. key: 'messageBack',
  51. success: (res) => {
  52. this.$u.route({
  53. type: 'switchTab',
  54. url: res.data
  55. });
  56. }
  57. });
  58. },
  59. /*下拉刷新的回调*/
  60. downCallback() {
  61. // 第2种: 下拉刷新和上拉加载调同样的接口, 则不用第1种, 直接mescroll.resetUpScroll()即可
  62. this.mescroll.resetUpScroll(); // 重置列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
  63. },
  64. /*上拉加载的回调*/
  65. upCallback(page) {
  66. let pageNum = page.num; // 页码, 默认从1开始
  67. let pageSize = page.size; // 页长, 默认每页10条
  68. this.$u.api
  69. .getMessageList({
  70. pageSize: pageSize,
  71. pageNum: pageNum
  72. })
  73. .then((res) => {
  74. if (res.code == 200) {
  75. // 接口返回的当前页数据列表 (数组)
  76. let curPageData = res.data?.rows ?? [];
  77. // 接口返回的当前页数据长度 (如列表有26个数据,当前页返回8个,则curPageLen=8)
  78. let curPageLen = curPageData.length;
  79. // 接口返回的总页数 (如列表有26个数据,每页10条,共3页; 则totalPage=3)
  80. let totalPage = res.data.pages;
  81. // 接口返回的总数据量(如列表有26个数据,每页10条,共3页; 则totalSize=26)
  82. let totalSize = res.data.total;
  83. // 接口返回的是否有下一页 (true/false)
  84. let hasNext = res.data.page < res.data.pages;
  85. //设置列表数据
  86. if (page.num == 1) this.messageList = []; // 如果是第一页需手动置空列表
  87. this.messageList = this.messageList.concat(curPageData); // 追加新数据
  88. // 后台接口有返回列表的总页数 totalPage
  89. this.mescroll.endByPage(curPageLen, totalPage);
  90. setTimeout(() => {
  91. this.mescroll.endSuccess(curPageLen);
  92. }, 20);
  93. } else {
  94. this.mescroll.endErr();
  95. }
  96. })
  97. .catch((err) => {
  98. this.$refs.uToast.show({
  99. title: err.msg,
  100. type: 'error'
  101. });
  102. });
  103. },
  104. // 跳转消息详情
  105. goDetails(item) {
  106. if (Number(item.readFlag) === 0) {
  107. this.$u.api
  108. .messageRead({
  109. newId: item.id
  110. })
  111. .then((res) => {
  112. if (res.code == 200) {
  113. this.$u.route({
  114. url: 'pages/message/messageInfo',
  115. params: {
  116. details: JSON.stringify(item)
  117. }
  118. });
  119. } else {
  120. this.$refs.uToast.show({
  121. title: res.msg,
  122. type: 'error'
  123. });
  124. }
  125. })
  126. .catch((err) => {
  127. this.$refs.uToast.show({
  128. title: '操作失败',
  129. type: 'error'
  130. });
  131. });
  132. } else {
  133. this.$u.route({
  134. url: 'pages/message/messageInfo',
  135. params: {
  136. details: JSON.stringify(item)
  137. }
  138. });
  139. }
  140. }
  141. }
  142. };
  143. </script>
  144. <style>
  145. /* #ifndef H5 */
  146. page {
  147. height: 100%;
  148. background-color: #f6f6ff;
  149. }
  150. /* #endif */
  151. </style>
  152. <style lang="scss" scoped>
  153. @import './message.scss';
  154. </style>