From 5e2aafeede13d337380f736567caf74f49713be7 Mon Sep 17 00:00:00 2001
From: linwe <872216996@qq.com>
Date: Fri, 09 Aug 2024 14:38:32 +0800
Subject: [PATCH] 代码优化

---
 src/store/modules/user.js |  557 ++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 385 insertions(+), 172 deletions(-)

diff --git a/src/store/modules/user.js b/src/store/modules/user.js
index f92f525..cd9ce27 100644
--- a/src/store/modules/user.js
+++ b/src/store/modules/user.js
@@ -1,272 +1,483 @@
-import {setToken, setRefreshToken, removeToken, removeRefreshToken} from '@/util/auth'
-import {Message} from 'element-ui'
-import {setStore, getStore} from '@/util/store'
-import {isURL, validatenull} from '@/util/validate'
-import {deepClone} from '@/util/util'
-import website from '@/config/website'
-import {loginByUsername, loginBySocial, loginBySso, getUserInfo, logout, refreshToken, getButtons} from '@/api/user'
-import {getTopMenu, getRoutes} from '@/api/system/menu'
-import md5 from 'js-md5'
+import {
+  setToken,
+  setRefreshToken,
+  removeToken,
+  removeRefreshToken,
+} from "@/util/auth";
+import {
+  Alert,
+  Message
+} from "element-ui";
+import {
+  setStore,
+  getStore
+} from "@/util/store";
+import {
+  isURL,
+  validatenull
+} from "@/util/validate";
+import {
+  deepClone
+} from "@/util/util";
+import website from "@/config/website";
+import {
+  loginByUsername,
+  loginBySocial,
+  loginBySso,
+  getUserInfo,
+  logout,
+  refreshToken,
+  getButtons,
+} from "@/api/user";
+import {
+  getTopMenu,
+  getRoutes
+} from "@/api/system/menu";
+import md5 from "js-md5";
 
+function findChildrenUrl(data) {
+  if ("children" in data[0] && data[0].children.length > 0) {
+    return findChildrenUrl(data[0].children);
+  } else {
+    return data[0];
+  }
+}
+
+function findChildrenRole(data, str) {
+  return data.split(",").find((item) => item == str);
+}
 
 function addPath(ele, first) {
   const menu = website.menu;
   const propsConfig = menu.props;
   const propsDefault = {
-    label: propsConfig.label || 'name',
-    path: propsConfig.path || 'path',
-    icon: propsConfig.icon || 'icon',
-    children: propsConfig.children || 'children'
-  }
+    label: propsConfig.label || "name",
+    path: propsConfig.path || "path",
+    icon: propsConfig.icon || "icon",
+    children: propsConfig.children || "children",
+  };
   const icon = ele[propsDefault.icon];
   ele[propsDefault.icon] = validatenull(icon) ? menu.iconDefault : icon;
-  const isChild = ele[propsDefault.children] && ele[propsDefault.children].length !== 0;
+  const isChild =
+    ele[propsDefault.children] && ele[propsDefault.children].length !== 0;
   if (!isChild) ele[propsDefault.children] = [];
   if (!isChild && first && !isURL(ele[propsDefault.path])) {
-    ele[propsDefault.path] = ele[propsDefault.path] + '/index'
+    ele[propsDefault.path] = ele[propsDefault.path] + "/index";
   } else {
-    ele[propsDefault.children].forEach(child => {
+    ele[propsDefault.children].forEach((child) => {
       addPath(child);
-    })
+    });
   }
-
 }
 
 const user = {
   state: {
-    tenantId: getStore({name: 'tenantId'}) || '',
-    userInfo: getStore({name: 'userInfo'}) || [],
-    permission: getStore({name: 'permission'}) || {},
+    tenantId: getStore({
+      name: "tenantId"
+    }) || "",
+    userInfo: getStore({
+      name: "userInfo"
+    }) || [],
+    permission: getStore({
+      name: "permission"
+    }) || {},
     roles: [],
     menuId: {},
-    menu: getStore({name: 'menu'}) || [],
-    menuAll: getStore({name: 'menuAll'}) || [],
-    token: getStore({name: 'token'}) || '',
-    refreshToken: getStore({name: 'refreshToken'}) || '',
+    menu: getStore({
+      name: "menu"
+    }) || [],
+    menuAll: getStore({
+      name: "menuAll"
+    }) || [],
+    token: getStore({
+      name: "token"
+    }) || "",
+    refreshToken: getStore({
+      name: "refreshToken"
+    }) || "",
+    homeFirstPage: getStore({
+      name: "firstPage"
+    }) || "",
+    homeFirstTagPage: getStore({
+      name: "firstTagPage"
+    }) || {},
   },
   actions: {
     //根据用户名登录
-    LoginByUsername({commit}, userInfo) {
+    LoginByUsername({
+      dispatch,
+      commit
+    }, userInfo) {
       return new Promise((resolve, reject) => {
-        loginByUsername(userInfo.tenantId, userInfo.deptId, userInfo.roleId, userInfo.username, md5(userInfo.password), userInfo.type, userInfo.key, userInfo.code).then(res => {
-          const data = res.data;
-          if (data.error_description) {
-            Message({
-              message: data.error_description,
-              type: 'error'
-            })
-          } else {
-            commit('SET_TOKEN', data.access_token);
-            commit('SET_REFRESH_TOKEN', data.refresh_token);
-            commit('SET_TENANT_ID', data.tenant_id);
-            commit('SET_USER_INFO', data);
-            commit('DEL_ALL_TAG');
-            commit('CLEAR_LOCK');
-          }
-          resolve();
-        }).catch(error => {
-          reject(error);
-        })
-      })
+        loginByUsername(
+            userInfo.tenantId,
+            userInfo.deptId,
+            userInfo.roleId,
+            userInfo.username,
+            md5(userInfo.password),
+            userInfo.type,
+            userInfo.key,
+            userInfo.code
+          )
+          .then((res) => {
+            const data = res.data;
+            if (data.error_description) {
+              Message({
+                message: data.error_description,
+                type: "error",
+              });
+              resolve({
+                error: "error"
+              });
+            } else {
+              commit("SET_TOKEN", data.access_token);
+              commit("SET_REFRESH_TOKEN", data.refresh_token);
+              commit("SET_TENANT_ID", data.tenant_id);
+              commit("SET_USER_INFO", data);
+              commit("CLEAR_LOCK");
+              commit("CLEAR_LOCK");
+
+              dispatch("GetMenu").then((menuData) => {
+                let fistMenu = findChildrenUrl(menuData);
+                if (
+                  findChildrenRole(data.role_name, "admin") ||
+                  findChildrenRole(data.role_name, "administrator") ||
+                  findChildrenRole(data.role_name, "gagly") ||
+                  findChildrenRole(data.role_name, "jdgly") ||
+                  findChildrenRole(data.role_name, "sqgly") ||
+                  findChildrenRole(data.role_name, "mj")
+                ) {
+                  setStore({
+                    name: "showMenu",
+                    content: true
+                  });
+
+                  commit("SET_HOMEFIRSTTAGPAGE", {
+                    label: "首页",
+                    value: "/wel/index",
+                    params: {},
+                    query: {},
+                    meta: {
+                      i18n: "dashboard",
+                    },
+                    group: [],
+                    close: false,
+                  });
+
+                  commit("SET_HOMEFIRSTPAGE", "/wel");
+
+                  resolve({
+                    fistMenu: {
+                      path: "/wel",
+                    },
+                    menuData,
+                  });
+                } else {
+                  setStore({
+                    name: "showMenu",
+                    content: false
+                  });
+
+                  commit("SET_HOMEFIRSTTAGPAGE", {
+                    label: fistMenu.name,
+                    value: fistMenu.path,
+                    params: {},
+                    query: {},
+                    meta: {
+                      i18n: "dashboard",
+                    },
+                    group: [],
+                    close: false,
+                  });
+
+                  commit("SET_HOMEFIRSTPAGE", fistMenu.path);
+
+                  resolve({
+                    fistMenu,
+                    menuData
+                  });
+                }
+
+                commit("DEL_ALL_TAG");
+              });
+            }
+          })
+          .catch((error) => {
+            reject(error);
+          });
+      });
     },
     //根据手机号登录
-    LoginByPhone({commit}, userInfo) {
+    LoginByPhone({
+      commit
+    }, userInfo) {
       return new Promise((resolve) => {
-        loginByUsername(userInfo.phone, userInfo.code).then(res => {
+        loginByUsername(userInfo.phone, userInfo.code).then((res) => {
           const data = res.data.data;
-          commit('SET_TOKEN', data);
-          commit('DEL_ALL_TAG');
-          commit('CLEAR_LOCK');
+          commit("SET_TOKEN", data);
+          commit("DEL_ALL_TAG");
+          commit("CLEAR_LOCK");
           resolve();
-        })
-      })
+        });
+      });
     },
     //根据第三方信息登录
-    LoginBySocial({commit}, userInfo) {
+    LoginBySocial({
+      commit
+    }, userInfo) {
       return new Promise((resolve) => {
-        loginBySocial(userInfo.tenantId, userInfo.source, userInfo.code, userInfo.state).then(res => {
+        loginBySocial(
+          userInfo.tenantId,
+          userInfo.source,
+          userInfo.code,
+          userInfo.state
+        ).then((res) => {
           const data = res.data;
           if (data.error_description) {
             Message({
               message: data.error_description,
-              type: 'error'
-            })
+              type: "error",
+            });
           } else {
-            commit('SET_TOKEN', data.access_token);
-            commit('SET_REFRESH_TOKEN', data.refresh_token);
-            commit('SET_USER_INFO', data);
-            commit('SET_TENANT_ID', data.tenant_id);
-            commit('DEL_ALL_TAG');
-            commit('CLEAR_LOCK');
+            commit("SET_TOKEN", data.access_token);
+            commit("SET_REFRESH_TOKEN", data.refresh_token);
+            commit("SET_USER_INFO", data);
+            commit("SET_TENANT_ID", data.tenant_id);
+            commit("DEL_ALL_TAG");
+            commit("CLEAR_LOCK");
           }
           resolve();
-        })
-      })
+        });
+      });
     },
     //根据单点信息登录
-    LoginBySso({commit}, userInfo) {
+    LoginBySso({
+      commit
+    }, userInfo) {
       return new Promise((resolve) => {
-        loginBySso(userInfo.state, userInfo.code).then(res => {
+        loginBySso(userInfo.state, userInfo.code).then((res) => {
           const data = res.data;
           if (data.error_description) {
             Message({
               message: data.error_description,
-              type: 'error'
-            })
+              type: "error",
+            });
           } else {
-            commit('SET_TOKEN', data.access_token);
-            commit('SET_REFRESH_TOKEN', data.refresh_token);
-            commit('SET_USER_INFO', data);
-            commit('SET_TENANT_ID', data.tenant_id);
-            commit('DEL_ALL_TAG');
-            commit('CLEAR_LOCK');
+            commit("SET_TOKEN", data.access_token);
+            commit("SET_REFRESH_TOKEN", data.refresh_token);
+            commit("SET_USER_INFO", data);
+            commit("SET_TENANT_ID", data.tenant_id);
+            commit("DEL_ALL_TAG");
+            commit("CLEAR_LOCK");
           }
           resolve();
-        })
-      })
+        });
+      });
     },
     //获取用户信息
-    GetUserInfo({commit}) {
+    GetUserInfo({
+      commit
+    }) {
       return new Promise((resolve, reject) => {
-        getUserInfo().then((res) => {
-          const data = res.data.data;
-          commit('SET_ROLES', data.roles);
-          resolve(data);
-        }).catch(err => {
-          reject(err);
-        })
-      })
+        getUserInfo()
+          .then((res) => {
+            const data = res.data.data;
+            commit("SET_ROLES", data.roles);
+            resolve(data);
+          })
+          .catch((err) => {
+            reject(err);
+          });
+      });
     },
     //刷新token
-    refreshToken({state, commit}, userInfo) {
-      window.console.log('handle refresh token');
+    refreshToken({
+      state,
+      commit
+    }, userInfo) {
       return new Promise((resolve, reject) => {
-        refreshToken(state.refreshToken, state.tenantId,
-          !validatenull(userInfo) ? userInfo.deptId : state.userInfo.dept_id,
-          !validatenull(userInfo) ? userInfo.roleId : state.userInfo.role_id
-        ).then(res => {
-          const data = res.data;
-          commit('SET_TOKEN', data.access_token);
-          commit('SET_REFRESH_TOKEN', data.refresh_token);
-          commit('SET_USER_INFO', data);
-          resolve();
-        }).catch(error => {
-          reject(error)
-        })
-      })
+        refreshToken(
+            state.refreshToken,
+            state.tenantId,
+            !validatenull(userInfo) ? userInfo.deptId : state.userInfo.dept_id,
+            !validatenull(userInfo) ? userInfo.roleId : state.userInfo.role_id
+          )
+          .then((res) => {
+            const data = res.data;
+            commit("SET_TOKEN", data.access_token);
+            commit("SET_REFRESH_TOKEN", data.refresh_token);
+            commit("SET_USER_INFO", data);
+            resolve();
+          })
+          .catch((error) => {
+            reject(error);
+          });
+      });
     },
     // 登出
-    LogOut({commit}) {
+    LogOut({
+      commit
+    }) {
       return new Promise((resolve, reject) => {
-        logout().then(() => {
-          commit('SET_TOKEN', '');
-          commit('SET_MENU', []);
-          commit('SET_MENU_ALL_NULL', []);
-          commit('SET_ROLES', []);
-          commit('SET_TAG_LIST', []);
-          commit('DEL_ALL_TAG');
-          commit('CLEAR_LOCK');
-          removeToken();
-          removeRefreshToken();
-          resolve();
-        }).catch(error => {
-          reject(error)
-        })
-      })
+        logout()
+          .then(() => {
+            commit("SET_TOKEN", "");
+            commit("SET_MENU", []);
+            commit("SET_MENU_ALL_NULL", []);
+            commit("SET_ROLES", []);
+            commit("SET_TAG_LIST", []);
+            commit("DEL_ALL_TAG");
+            commit("CLEAR_LOCK");
+
+            removeToken();
+            removeRefreshToken();
+            resolve();
+          })
+          .catch((error) => {
+            reject(error);
+          });
+      });
     },
     //注销session
-    FedLogOut({commit}) {
-      return new Promise(resolve => {
-        commit('SET_TOKEN', '');
-        commit('SET_MENU_ALL_NULL', []);
-        commit('SET_MENU', []);
-        commit('SET_ROLES', []);
-        commit('SET_TAG_LIST', []);
-        commit('DEL_ALL_TAG');
-        commit('CLEAR_LOCK');
+    FedLogOut({
+      commit
+    }) {
+      return new Promise((resolve) => {
+        commit("SET_TOKEN", "");
+        commit("SET_MENU_ALL_NULL", []);
+        commit("SET_MENU", []);
+        commit("SET_ROLES", []);
+        commit("SET_TAG_LIST", []);
+        commit("DEL_ALL_TAG");
+        commit("CLEAR_LOCK");
+
         removeToken();
         removeRefreshToken();
         resolve();
-      })
+      });
     },
     //获取顶部菜单
     GetTopMenu() {
-      return new Promise(resolve => {
+      return new Promise((resolve) => {
         getTopMenu().then((res) => {
           const data = res.data.data || [];
-          resolve(data)
-        })
-      })
+          resolve(data);
+        });
+      });
     },
     //获取系统菜单
-    GetMenu({commit, dispatch}, topMenuId) {
-      return new Promise(resolve => {
+    GetMenu({
+      commit,
+      dispatch
+    }, topMenuId) {
+      return new Promise((resolve) => {
         getRoutes(topMenuId).then((res) => {
-          const data = res.data.data
+          const data = res.data.data;
           let menu = deepClone(data);
-          menu.forEach(ele => {
+          menu.forEach((ele) => {
             addPath(ele, true);
           });
-          commit('SET_MENU_ALL', menu)
-          commit('SET_MENU', menu)
-          dispatch('GetButtons');
-          resolve(menu)
-        })
-      })
+          commit("SET_MENU_ALL", menu);
+          commit("SET_MENU", menu);
+          dispatch("GetButtons");
+          resolve(menu);
+        });
+      });
     },
     //获取系统按钮
-    GetButtons({commit}) {
+    GetButtons({
+      commit
+    }) {
       return new Promise((resolve) => {
-        getButtons().then(res => {
+        getButtons().then((res) => {
           const data = res.data.data;
-          commit('SET_PERMISSION', data);
+          commit("SET_PERMISSION", data);
           resolve();
-        })
-      })
+        });
+      });
     },
   },
   mutations: {
+    SET_HOMEFIRSTPAGE: (state, homeFirstPage) => {
+      setStore({
+        name: "firstPage",
+        content: homeFirstPage
+      });
+      state.homeFirstPage = homeFirstPage;
+    },
+    SET_HOMEFIRSTTAGPAGE: (state, homeFirstTagPage) => {
+      setStore({
+        name: "firstTagPage",
+        content: homeFirstTagPage,
+      });
+      state.homeFirstTagPage = homeFirstTagPage;
+    },
     SET_TOKEN: (state, token) => {
       setToken(token);
       state.token = token;
-      setStore({name: 'token', content: state.token})
+      setStore({
+        name: "token",
+        content: state.token
+      });
     },
     SET_MENU_ID(state, menuId) {
       state.menuId = menuId;
     },
     SET_MENU_ALL: (state, menuAll) => {
       let menu = state.menuAll;
-      menuAll.forEach(ele => {
-        if (!menu.find(item => item.label === ele.label && item.path === ele.path)) {
+      menuAll.forEach((ele) => {
+        if (
+          !menu.find(
+            (item) => item.label === ele.label && item.path === ele.path
+          )
+        ) {
           menu.push(ele);
         }
-      })
-      state.menuAll = menu
-      setStore({ name: 'menuAll', content: state.menuAll })
+      });
+      state.menuAll = menu;
+      setStore({
+        name: "menuAll",
+        content: state.menuAll
+      });
     },
     SET_MENU_ALL_NULL: (state) => {
-      state.menuAll = []
-      setStore({ name: 'menuAll', content: state.menuAll })
+      state.menuAll = [];
+      setStore({
+        name: "menuAll",
+        content: state.menuAll
+      });
     },
     SET_MENU: (state, menu) => {
-      state.menu = menu
-      setStore({ name: 'menu', content: state.menu })
+      state.menu = menu;
+      setStore({
+        name: "menu",
+        content: state.menu
+      });
     },
     SET_REFRESH_TOKEN: (state, refreshToken) => {
-      setRefreshToken(refreshToken)
+      setRefreshToken(refreshToken);
       state.refreshToken = refreshToken;
-      setStore({name: 'refreshToken', content: state.refreshToken})
+      setStore({
+        name: "refreshToken",
+        content: state.refreshToken
+      });
     },
     SET_TENANT_ID: (state, tenantId) => {
       state.tenantId = tenantId;
-      setStore({name: 'tenantId', content: state.tenantId})
+      setStore({
+        name: "tenantId",
+        content: state.tenantId
+      });
     },
     SET_USER_INFO: (state, userInfo) => {
       if (validatenull(userInfo.avatar)) {
         userInfo.avatar = "/img/bg/img-logo.png";
       }
       state.userInfo = userInfo;
-      setStore({name: 'userInfo', content: state.userInfo})
+      setStore({
+        name: "userInfo",
+        content: state.userInfo
+      });
     },
     SET_ROLES: (state, roles) => {
       state.roles = roles;
@@ -275,27 +486,29 @@
       let result = [];
 
       function getCode(list) {
-        list.forEach(ele => {
-          if (typeof (ele) === 'object') {
+        list.forEach((ele) => {
+          if (typeof ele === "object") {
             const chiildren = ele.children;
             const code = ele.code;
             if (chiildren) {
-              getCode(chiildren)
+              getCode(chiildren);
             } else {
               result.push(code);
             }
           }
-        })
+        });
       }
 
       getCode(permission);
       state.permission = {};
-      result.forEach(ele => {
+      result.forEach((ele) => {
         state.permission[ele] = true;
       });
-      setStore({name: 'permission', content: state.permission})
-    }
-  }
-
-}
-export default user
+      setStore({
+        name: "permission",
+        content: state.permission
+      });
+    },
+  },
+};
+export default user;
\ No newline at end of file

--
Gitblit v1.9.3