SiteMessagePopup.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <template>
  2. <transition
  3. name="fade"
  4. mode="out-in"
  5. appear
  6. :duration="{ enter: 500, leave: 300 }"
  7. >
  8. <div v-if="unreadMessage" :key="unreadMessage.id" class="popup">
  9. <h5
  10. style="
  11. padding: 5px;
  12. background: #3c8dbd;
  13. border-radius: 6px 6px 0px 0px;
  14. font-size: 14px;
  15. font-weight: 500;
  16. color: rgba(255, 255, 255, 1);
  17. line-height: 20px;
  18. "
  19. >
  20. {{ unreadMessage.title }}
  21. </h5>
  22. <p
  23. style="
  24. text-overflow: ellipsis;
  25. text-indent: 2em;
  26. text-align: left;
  27. height: 100px;
  28. overflow: hidden;
  29. margin: 20px;
  30. "
  31. >
  32. {{ unreadMessageContent }}
  33. </p>
  34. <div style="text-align: left; margin-left: 20px; margin-bottom: 10px">
  35. <span
  36. style="
  37. display: inline-block;
  38. margin-left: 20px;
  39. cursor: pointer;
  40. color: #2d8cf0;
  41. "
  42. @click="toDetail"
  43. >详情</span
  44. >
  45. <span
  46. style="
  47. display: inline-block;
  48. margin-left: 20px;
  49. cursor: pointer;
  50. color: #777;
  51. "
  52. @click="ignoreMessage(null)"
  53. >忽略</span
  54. >
  55. </div>
  56. </div>
  57. </transition>
  58. </template>
  59. <script>
  60. import { mapState } from "vuex";
  61. export default {
  62. name: "SiteMessagePopup",
  63. data() {
  64. let ignoreMessageIds = window.sessionStorage.getItem("ignoreMessageIds");
  65. if (ignoreMessageIds) {
  66. ignoreMessageIds = JSON.parse(ignoreMessageIds);
  67. } else {
  68. ignoreMessageIds = [];
  69. }
  70. return {
  71. siteMessages: [],
  72. ignoreMessageIds,
  73. };
  74. },
  75. computed: {
  76. ...mapState({ user: (state) => state.user }),
  77. unreadMessage() {
  78. const unreadMessages = this.siteMessages
  79. .filter((v) => v.hasRead === false)
  80. .filter(
  81. (v) =>
  82. this.ignoreMessageIds.includes(v.id + "-" + this.user.id) === false
  83. )
  84. .reverse();
  85. const unreads = this.siteMessages.filter((v) => v.hasRead === false);
  86. this.$emit("changeUnreadMessageCount", unreads.length);
  87. if (unreadMessages.length > 0) {
  88. return unreadMessages[0];
  89. } else {
  90. return null;
  91. }
  92. },
  93. unreadMessageContent() {
  94. const div = document.createElement("div");
  95. div.innerHTML = this.unreadMessage.content;
  96. const text = div.textContent || div.innerText || "";
  97. return text.slice(0, 90) + (text.length > 90 ? "..." : "");
  98. },
  99. },
  100. created() {
  101. this.$eventHub.$on("UnreadNoticeChange", () => this.onUnreadNoticeChange());
  102. this.getUnreadNoticeList();
  103. },
  104. beforeDestroy() {
  105. this.$eventHub.$off("UnreadNoticeChange");
  106. },
  107. methods: {
  108. toDetail() {
  109. let unid = this.unreadMessage.id;
  110. this.ignoreMessage(unid);
  111. this.$router.push({
  112. path: "/home/site-message/" + unid,
  113. });
  114. },
  115. ignoreMessage(id) {
  116. let unid = this.unreadMessage.id;
  117. if (id) {
  118. unid = id;
  119. }
  120. this.ignoreMessageIds.push(unid + "-" + this.user.id);
  121. window.sessionStorage.setItem(
  122. "ignoreMessageIds",
  123. JSON.stringify(this.ignoreMessageIds)
  124. );
  125. },
  126. async getUnreadNoticeList() {
  127. try {
  128. this.$httpWithoutBar
  129. .get("/api/ecs_exam_work/notice/getUserNoticeList?hasRead=false")
  130. .then((response) => {
  131. if (response.data.hasRecalled === true) {
  132. response.data.title = "发送者已撤回消息:" + response.data.title;
  133. response.data.content = "该消息已被发送者撤回。";
  134. }
  135. this.siteMessages = response.data;
  136. setTimeout(() => {
  137. this.getUnreadNoticeList();
  138. }, 60000);
  139. });
  140. } catch (error) {
  141. console.log("tag", error);
  142. }
  143. },
  144. async onUnreadNoticeChange() {
  145. this.$httpWithoutBar
  146. .get("/api/ecs_exam_work/notice/getUserNoticeList?hasRead=false")
  147. .then((response) => {
  148. this.siteMessages = response.data;
  149. });
  150. },
  151. },
  152. };
  153. </script>
  154. <style scoped>
  155. .popup {
  156. position: fixed;
  157. bottom: 20px;
  158. right: 20px;
  159. width: 320px;
  160. height: 200px;
  161. background: rgba(255, 255, 255, 1);
  162. box-shadow: 0px 2px 10px 0px rgba(0, 0, 0, 0.1);
  163. border-radius: 6px;
  164. border: 1px solid rgba(221, 221, 221, 1);
  165. z-index: 99;
  166. }
  167. </style>