notice.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { getNoticeApi } from "@/api";
  2. export default {
  3. namespaced: true,
  4. state() {
  5. return {
  6. isOPen: false,
  7. data: {
  8. id: "",
  9. tipEndTime: "",
  10. tipStartTime: "",
  11. imageUrl: "",
  12. imageUrlEn: "",
  13. title: "",
  14. titleEn: "",
  15. description: "",
  16. descriptionEn: "",
  17. infoUrl: "",
  18. infoUrlEn: "",
  19. version: "",
  20. },
  21. };
  22. },
  23. getters: {
  24. content: (state) => state.data,
  25. status: (state) => state.isOPen,
  26. },
  27. mutations: {
  28. setData(state, payload) {
  29. state.data = payload;
  30. },
  31. setStatus(state, payload) {
  32. state.isOPen = payload;
  33. },
  34. },
  35. actions: {
  36. async getNotice({ commit, state }) {
  37. const data = await getNoticeApi();
  38. console.log("state", state);
  39. const preState = state.data;
  40. data.data &&
  41. Object.keys(data.data).forEach((i) => {
  42. if (i in preState) {
  43. preState[i] = data.data[i];
  44. }
  45. });
  46. if (preState.tipStartTime && preState.tipEndTime) {
  47. const start = new Date(preState.tipStartTime);
  48. const end = new Date(preState.tipEndTime);
  49. const date = Date.now();
  50. const noticeIds = localStorage.getItem("noticeIds")
  51. ? Array.isArray(JSON.parse(localStorage.getItem("noticeIds")))
  52. ? JSON.parse(localStorage.getItem("noticeIds"))
  53. : []
  54. : [];
  55. if (date > start && date < end) {
  56. if (!Array.from(noticeIds).includes(preState.id)) {
  57. noticeIds.push(preState.id);
  58. localStorage.setItem("noticeIds", JSON.stringify(noticeIds));
  59. commit("setStatus", true);
  60. }
  61. } else {
  62. commit("setStatus", false);
  63. }
  64. }
  65. console.log("preState", preState);
  66. commit("setData", preState);
  67. },
  68. },
  69. };