123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- // src/store/index.js
- import { createStore } from 'vuex';
- import { House, Setting, Help } from '@element-plus/icons-vue';
- import { ServerActionUserLogin, ServerActionCheckLogin } from '../../wailsjs/go/main/App'
- import ruleList from './modules/ruleList'
- const iconComponents = {
- house: House,
- setting: Setting,
- help: Help,
- };
- export default createStore({
- state: {
- isAuthenticated: false,
- userInfo:{},
- menuConfig: {
- admin: [
- { title: '首页', icon:iconComponents.house, path: '/' },
- { title: '设置', icon:iconComponents.setting, path: '/setting' },
- { title: '帮助', icon:iconComponents.help, path: '/help' },
- // 更多管理员菜单项
- ],
- user: [
- { title: '首页', icon:iconComponents.house, path: '/' },
- { title: '帮助', icon:iconComponents.help, path: '/help' },
- // 更多普通用户菜单项
- ],
- },
- },
- mutations: {
- SET_AUTHENTICATED(state, payload) {
- state.isAuthenticated = payload;
- },
- SET_USER_INFO(state, payload) {
- state.userInfo = payload;
- },
- },
- actions: {
- async login({ commit }, credentials) {
- const res = await ServerActionUserLogin({
- username: credentials.username,
- password: credentials.password
- })
- if (res) {
- const userInfo = res.data
- if (userInfo && Object.keys(userInfo).length > 0) {
- // 登录成功
- commit('SET_USER_INFO', userInfo);
- commit('SET_AUTHENTICATED', true);
- } else {
- console.log('login error')
- }
- }
- return res
- },
- async checkLogin({ commit }) {
- const res = await ServerActionCheckLogin()
- if (res) {
- const userInfo = res.data
- if (userInfo && Object.keys(userInfo).length > 0) {
- // 登录成功
- commit('SET_USER_INFO', userInfo);
- commit('SET_AUTHENTICATED', true);
- } else {
- console.log('login error')
- }
- }
- },
- logout({ commit }) {
- commit('SET_AUTHENTICATED', false);
- },
- },
- getters: {
- // 定义一个 Getter 来获取当前登录用户的信息
- getCurrentUser: (state) => state.userInfo,
- },
- modules: {
- ruleList
- }
- });
|