index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // src/store/index.js
  2. import { createStore } from 'vuex';
  3. import { House, Setting, Help } from '@element-plus/icons-vue';
  4. import { ServerActionUserLogin, ServerActionCheckLogin } from '../../wailsjs/go/main/App'
  5. import ruleList from './modules/ruleList'
  6. const iconComponents = {
  7. house: House,
  8. setting: Setting,
  9. help: Help,
  10. };
  11. export default createStore({
  12. state: {
  13. isAuthenticated: false,
  14. userInfo:{},
  15. menuConfig: {
  16. admin: [
  17. { title: '首页', icon:iconComponents.house, path: '/' },
  18. { title: '设置', icon:iconComponents.setting, path: '/setting' },
  19. { title: '帮助', icon:iconComponents.help, path: '/help' },
  20. // 更多管理员菜单项
  21. ],
  22. user: [
  23. { title: '首页', icon:iconComponents.house, path: '/' },
  24. { title: '帮助', icon:iconComponents.help, path: '/help' },
  25. // 更多普通用户菜单项
  26. ],
  27. },
  28. },
  29. mutations: {
  30. SET_AUTHENTICATED(state, payload) {
  31. state.isAuthenticated = payload;
  32. },
  33. SET_USER_INFO(state, payload) {
  34. state.userInfo = payload;
  35. },
  36. },
  37. actions: {
  38. async login({ commit }, credentials) {
  39. const res = await ServerActionUserLogin({
  40. username: credentials.username,
  41. password: credentials.password
  42. })
  43. if (res) {
  44. const userInfo = res.data
  45. if (userInfo && Object.keys(userInfo).length > 0) {
  46. // 登录成功
  47. commit('SET_USER_INFO', userInfo);
  48. commit('SET_AUTHENTICATED', true);
  49. } else {
  50. console.log('login error')
  51. }
  52. }
  53. return res
  54. },
  55. async checkLogin({ commit }) {
  56. const res = await ServerActionCheckLogin()
  57. if (res) {
  58. const userInfo = res.data
  59. if (userInfo && Object.keys(userInfo).length > 0) {
  60. // 登录成功
  61. commit('SET_USER_INFO', userInfo);
  62. commit('SET_AUTHENTICATED', true);
  63. } else {
  64. console.log('login error')
  65. }
  66. }
  67. },
  68. logout({ commit }) {
  69. commit('SET_AUTHENTICATED', false);
  70. },
  71. },
  72. getters: {
  73. // 定义一个 Getter 来获取当前登录用户的信息
  74. getCurrentUser: (state) => state.userInfo,
  75. },
  76. modules: {
  77. ruleList
  78. }
  79. });