user.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { defineStore } from "pinia";
  2. const USER_INFO_KEY = "USER_INFO_KEY";
  3. const TOKEN_KEY = "TOKEN_KEY";
  4. export const useUserStore = defineStore("user", {
  5. state: () => ({
  6. userInfo: null,
  7. token: "",
  8. // lang: 'zh-CN',
  9. lang: "en-US",
  10. searchType: undefined,
  11. showLoginDialog: false,
  12. }),
  13. actions: {
  14. setUserInfo (userInfo: UserInfo | null) {
  15. this.userInfo = userInfo;
  16. let cookies = useCookie(USER_INFO_KEY);
  17. cookies.value = userInfo;
  18. setItem(USER_INFO_KEY, JSON.stringify(userInfo));
  19. },
  20. setToken (token: string) {
  21. this.token = token;
  22. let cookies = useCookie(TOKEN_KEY);
  23. cookies.value = token;
  24. setItem(TOKEN_KEY, token);
  25. },
  26. setLang (val: string) {
  27. this.lang = val;
  28. let cookies = useCookie("lang");
  29. cookies.value = val;
  30. setItem("lang", val);
  31. },
  32. resetState () {
  33. this.userInfo = null;
  34. this.token = "";
  35. removeItem(USER_INFO_KEY);
  36. removeItem(TOKEN_KEY);
  37. // removeItem("lang");
  38. removeItemCookie(USER_INFO_KEY);
  39. removeItemCookie(TOKEN_KEY);
  40. // removeItemCookie("lang");
  41. },
  42. setSearchType (val: string) {
  43. this.searchType = val;
  44. },
  45. setShowLoginDialog (bol: boolean) {
  46. this.showLoginDialog = bol;
  47. },
  48. },
  49. getters: {
  50. getUserInfo: (state) => {
  51. console.log('storage', getItem(USER_INFO_KEY))
  52. return state.userInfo || getItem(USER_INFO_KEY) || null
  53. },
  54. getToken: (state) => getItemCookie(TOKEN_KEY) || state.token || "",
  55. getLang: (state) => getItemCookie("lang") || state.lang || "",
  56. getSearchType: (state) => state.searchType || "",
  57. getShowLoginDialog: (state) => state.showLoginDialog || false,
  58. },
  59. // persist: {
  60. // storage: {
  61. // getItem: (key) => {
  62. // // if (process.client) {
  63. // // return localStorage.getItem(key)
  64. // // }
  65. // return useCookie(key).value;
  66. // },
  67. // setItem: (key, value) => {
  68. // // if (process.client) {
  69. // // localStorage.setItem(key, value)
  70. // // }
  71. // let cookies = useCookie(key);
  72. // cookies.value = value;
  73. // },
  74. // removeItem: (key) => {
  75. // let cookies = useCookie(key);
  76. // cookies.value = undefined || null;
  77. // },
  78. // },
  79. // },
  80. });
  81. const getItem = (key: string) => {
  82. return useCookie(key).value;
  83. };
  84. const setItem = (key: string, value: any) => {
  85. let cookies = useCookie(key);
  86. cookies.value = value;
  87. };
  88. const removeItem = (key: string) => {
  89. let cookies = useCookie(key);
  90. cookies.value = null;
  91. };
  92. const removeItemCookie = (key: string) => {
  93. let cookies = useCookie(key);
  94. cookies.value = null;
  95. };
  96. const getItemCookie = (key: string) => {
  97. let cookies = useCookie(key);
  98. return cookies.value;
  99. };