index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <template>
  2. <div class="mainContentBox">
  3. <div class="headers" style="font-size: 40px">
  4. <img
  5. src="/logo.png"
  6. object-fit="cover"
  7. width="80px"
  8. height="80px"
  9. style="margin-right: 20px"
  10. />消息中心
  11. </div>
  12. <div class="body">
  13. <div class="left">
  14. <div class="headers">未读消息({{ newMsg.length }}条)</div>
  15. <el-scrollbar class="msgs">
  16. <div class="item" v-for="item in newMsg" :key="item">
  17. <div>
  18. <div class="text">{{ item.title }} {{ item.created }}</div>
  19. <div class="text">{{ item.content }}</div>
  20. </div>
  21. <div class="action">
  22. <el-button type="success" @click="submit(item.id)" class="btn"
  23. >确认收到</el-button
  24. >
  25. </div>
  26. </div>
  27. </el-scrollbar>
  28. <div class="bottomBtn">
  29. <!-- <el-button
  30. type="primary"
  31. :disabled="setNextStatus1"
  32. @click="setPage1(1)"
  33. class="btn"
  34. >下一页</el-button
  35. >
  36. <el-button
  37. class="btn"
  38. :disabled="page == 1"
  39. type="primary"
  40. @click="setPage1(-1)"
  41. >
  42. 上一页
  43. </el-button> -->
  44. </div>
  45. </div>
  46. <div class="right">
  47. <div class="headers">已确认消息({{ totalCount2 }}条)</div>
  48. <el-scrollbar class="msgs">
  49. <div
  50. class="item"
  51. style="overflow: hidden; background-color: rgba(0, 128, 0, 0.5)"
  52. v-for="item in msgData2"
  53. :key="item"
  54. >
  55. <div>
  56. <div class="text">{{ item.title }} {{ item.created }}</div>
  57. <div class="text">{{ item.content }}</div>
  58. </div>
  59. </div>
  60. </el-scrollbar>
  61. <div class="bottomBtn">
  62. <el-button
  63. type="primary"
  64. :disabled="setNextStatus2"
  65. @click="setPage2(1)"
  66. class="btn"
  67. >下一页</el-button
  68. >
  69. <el-button
  70. class="btn"
  71. :disabled="page2 == 1"
  72. type="primary"
  73. @click="setPage2(-1)"
  74. >
  75. 上一页
  76. </el-button>
  77. </div>
  78. </div>
  79. </div>
  80. </div>
  81. </template>
  82. <script setup>
  83. import { confirmMessage, dataList } from "@/api/message";
  84. defineOptions({ name: "MessageMain" });
  85. const newMsg = ref([]);
  86. //连接地址
  87. const ws = new WebSocket(
  88. `${import.meta.env.VITE_WEBSOCKET_URL}/websocket/${localStorage.getItem("token")}`
  89. );
  90. const submit = async (id) => {
  91. const { code } = await confirmMessage([id]);
  92. if (code == "200") {
  93. ElMessage.success("操作成功");
  94. getPageInfo2();
  95. getPageInfo1();
  96. }
  97. };
  98. ws.onopen = () => {};
  99. ws.onmessage = (event) => {
  100. newMsg.value = JSON.parse(event.data).content;
  101. };
  102. ws.onclose = () => {};
  103. const page2 = ref(1);
  104. const pageSize2 = ref(5);
  105. const msgData2 = ref([]);
  106. const totalCount2 = ref(0);
  107. //获取已读消息
  108. const getPageInfo2 = async () => {
  109. const { data, code } = await dataList({
  110. pageNo: page2.value,
  111. pageSize: pageSize2.value,
  112. readState: 1,
  113. });
  114. if (code == "200") {
  115. msgData2.value = data.records;
  116. totalCount2.value = data.totalCount;
  117. }
  118. };
  119. //获取未读消息(和websocket一样的)
  120. const getPageInfo1 = async () => {
  121. const { data, code } = await dataList({
  122. pageNo: page2.value,
  123. pageSize: 999,
  124. readState: 0,
  125. });
  126. if (code == "200") {
  127. newMsg.value = data.records;
  128. }
  129. };
  130. const setNextStatus2 = computed(() => {
  131. return totalCount2.value - page2.value * pageSize2.value > 0 ? false : true;
  132. });
  133. const setPage2 = (num) => {
  134. page2.value = page2.value + num;
  135. getPageInfo2();
  136. getPageInfo1();
  137. };
  138. onMounted(() => {
  139. getPageInfo2();
  140. });
  141. </script>
  142. <style lang="scss" scoped>
  143. .bottomBtn {
  144. display: flex;
  145. justify-content: space-evenly;
  146. align-items: center;
  147. height: 40px;
  148. padding: 0 10px;
  149. margin-top: 10px;
  150. .btn {
  151. font-size: $f20;
  152. font-weight: 500;
  153. }
  154. }
  155. .headers {
  156. height: 80px;
  157. width: 100%;
  158. line-height: 80px;
  159. text-align: center;
  160. font-weight: 600;
  161. font-size: 26px;
  162. display: flex;
  163. justify-content: center;
  164. }
  165. .mainContentBox {
  166. width: 100vw;
  167. display: flex;
  168. flex-direction: column;
  169. height: calc(100vh - 100px);
  170. .body {
  171. height: calc(100vh - 184px);
  172. display: flex;
  173. .left {
  174. width: 50%;
  175. margin: 0 10px;
  176. display: flex;
  177. flex-direction: column;
  178. }
  179. .right {
  180. width: 50%;
  181. margin: 0 10px;
  182. display: flex;
  183. flex-direction: column;
  184. }
  185. }
  186. }
  187. .msgs {
  188. flex: 1;
  189. background-color: white;
  190. border-radius: 16px;
  191. padding: 10px;
  192. .item {
  193. min-height: 60px;
  194. margin-bottom: 8px;
  195. width: 100%;
  196. background-color: rgba(255, 165, 0, 0.4);
  197. border-radius: 16px;
  198. display: flex;
  199. padding: 10px 0;
  200. .text {
  201. flex: 1;
  202. padding: 3px 10px;
  203. font-size: 16px;
  204. font-weight: 500;
  205. }
  206. .action {
  207. width: 100px;
  208. display: flex;
  209. align-items: center;
  210. justify-content: center;
  211. }
  212. }
  213. }
  214. </style>