From 08f4b877ce42c996f5dcb09e0b738bb6e9e1faa4 Mon Sep 17 00:00:00 2001
From: RuoYi <yzz_ivy@163.com>
Date: Wed, 20 Nov 2024 11:13:24 +0800
Subject: [PATCH] 优化代码

---
 ruoyi-ui/src/components/TopNav/index.vue       |    8 +++-----
 ruoyi-ui/src/utils/validate.js                 |   22 ++++++++++++++++++++++
 ruoyi-ui/src/store/modules/user.js             |    4 +++-
 ruoyi-ui/src/components/HeaderSearch/index.vue |    8 +++-----
 4 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/ruoyi-ui/src/components/HeaderSearch/index.vue b/ruoyi-ui/src/components/HeaderSearch/index.vue
index a01325a..5dd4ca9 100644
--- a/ruoyi-ui/src/components/HeaderSearch/index.vue
+++ b/ruoyi-ui/src/components/HeaderSearch/index.vue
@@ -22,6 +22,7 @@
 // make search results more in line with expectations
 import Fuse from 'fuse.js/dist/fuse.min.js'
 import path from 'path'
+import { isHttp } from '@/utils/validate'
 
 export default {
   name: 'HeaderSearch',
@@ -72,7 +73,7 @@
     change(val) {
       const path = val.path;
       const query = val.query;
-      if(this.ishttp(val.path)) {
+      if(isHttp(val.path)) {
         // http(s):// 路径新窗口打开
         const pindex = path.indexOf("http");
         window.open(path.substr(pindex, path.length), "_blank");
@@ -115,7 +116,7 @@
         if (router.hidden) { continue }
 
         const data = {
-          path: !this.ishttp(router.path) ? path.resolve(basePath, router.path) : router.path,
+          path: !isHttp(router.path) ? path.resolve(basePath, router.path) : router.path,
           title: [...prefixTitle]
         }
 
@@ -149,9 +150,6 @@
       } else {
         this.options = []
       }
-    },
-    ishttp(url) {
-      return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1
     }
   }
 }
diff --git a/ruoyi-ui/src/components/TopNav/index.vue b/ruoyi-ui/src/components/TopNav/index.vue
index 86a91c4..f214245 100644
--- a/ruoyi-ui/src/components/TopNav/index.vue
+++ b/ruoyi-ui/src/components/TopNav/index.vue
@@ -33,6 +33,7 @@
 
 <script>
 import { constantRoutes } from "@/router";
+import { isHttp } from "@/utils/validate";
 
 // 隐藏侧边栏路由
 const hideList = ['/index', '/user/profile'];
@@ -78,7 +79,7 @@
             if(router.path === "/") {
               router.children[item].path = "/" + router.children[item].path;
             } else {
-              if(!this.ishttp(router.children[item].path)) {
+              if(!isHttp(router.children[item].path)) {
                 router.children[item].path = router.path + "/" + router.children[item].path;
               }
             }
@@ -126,7 +127,7 @@
     handleSelect(key, keyPath) {
       this.currentIndex = key;
       const route = this.routers.find(item => item.path === key);
-      if (this.ishttp(key)) {
+      if (isHttp(key)) {
         // http(s):// 路径新窗口打开
         window.open(key, "_blank");
       } else if (!route || !route.children) {
@@ -160,9 +161,6 @@
       } else {
         this.$store.dispatch('app/toggleSideBarHide', true);
       }
-    },
-    ishttp(url) {
-      return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1
     }
   },
 };
diff --git a/ruoyi-ui/src/store/modules/user.js b/ruoyi-ui/src/store/modules/user.js
index 7c9b0c7..6c5e2b5 100644
--- a/ruoyi-ui/src/store/modules/user.js
+++ b/ruoyi-ui/src/store/modules/user.js
@@ -1,5 +1,7 @@
 import { login, logout, getInfo, refreshToken } from '@/api/login'
 import { getToken, setToken, setExpiresIn, removeToken } from '@/utils/auth'
+import { isEmpty } from "@/utils/validate"
+import defAva from '@/assets/images/profile.jpg'
 
 const user = {
   state: {
@@ -61,7 +63,7 @@
       return new Promise((resolve, reject) => {
         getInfo().then(res => {
           const user = res.user
-          const avatar = (user.avatar == "" || user.avatar == null) ? require("@/assets/images/profile.jpg") : user.avatar;
+          const avatar = (isEmpty(user.avatar)) ? defAva : user.avatar
           if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
             commit('SET_ROLES', res.roles)
             commit('SET_PERMISSIONS', res.permissions)
diff --git a/ruoyi-ui/src/utils/validate.js b/ruoyi-ui/src/utils/validate.js
index 7986433..287d518 100644
--- a/ruoyi-ui/src/utils/validate.js
+++ b/ruoyi-ui/src/utils/validate.js
@@ -1,4 +1,26 @@
 /**
+ * 判断value字符串是否为空 
+ * @param {string} value
+ * @returns {Boolean}
+ */
+export function isEmpty(value) {
+  if (value == null || value == "" || value == undefined || value == "undefined") {
+    return true;
+  }
+  return false;
+}
+
+/**
+ * 判断url是否是http或https 
+ * @param {string} url
+ * @returns {Boolean}
+ */
+export function isHttp(url) {
+  return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1
+}
+
+/**
+ * 判断path是否为外链
  * @param {string} path
  * @returns {Boolean}
  */

--
Gitblit v1.9.3