From e82ee73704c140127ed46c8f4fd1183acbb7e97f Mon Sep 17 00:00:00 2001 From: RuoYi <yzz_ivy@163.com> Date: 星期三, 20 十一月 2024 10:47:31 +0800 Subject: [PATCH] 用户头像http(s)链接支持 --- src/store/modules/user.js | 7 +++++-- src/utils/validate.js | 20 ++++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/store/modules/user.js b/src/store/modules/user.js index d127d9e..24e6871 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -1,5 +1,6 @@ import { login, logout, getInfo } from '@/api/login' import { getToken, setToken, removeToken } from '@/utils/auth' +import { isHttp, isEmpty } from "@/utils/validate" import defAva from '@/assets/images/profile.jpg' const useUserStore = defineStore( @@ -35,8 +36,10 @@ return new Promise((resolve, reject) => { getInfo().then(res => { const user = res.user - const avatar = (user.avatar == "" || user.avatar == null) ? defAva : import.meta.env.VITE_APP_BASE_API + user.avatar; - + let avatar = user.avatar || "" + if (!isHttp(avatar)) { + avatar = (isEmpty(avatar)) ? defAva : import.meta.env.VITE_APP_BASE_API + avatar + } if (res.roles && res.roles.length > 0) { // 楠岃瘉杩斿洖鐨剅oles鏄惁鏄竴涓潪绌烘暟缁� this.roles = res.roles this.permissions = res.permissions diff --git a/src/utils/validate.js b/src/utils/validate.js index 71d0d06..8d32dc0 100644 --- a/src/utils/validate.js +++ b/src/utils/validate.js @@ -1,9 +1,21 @@ /** - * 鍒ゆ柇url鏄惁鏄痟ttp鎴杊ttps - * @param {string} path + * 鍒ゆ柇value瀛楃涓叉槸鍚︿负绌� + * @param {string} value * @returns {Boolean} */ - export function isHttp(url) { +export function isEmpty(value) { + if (value == null || value == "" || value == undefined || value == "undefined") { + return true; + } + return false; +} + +/** + * 鍒ゆ柇url鏄惁鏄痟ttp鎴杊ttps + * @param {string} url + * @returns {Boolean} + */ +export function isHttp(url) { return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1 } @@ -12,7 +24,7 @@ * @param {string} path * @returns {Boolean} */ - export function isExternal(path) { +export function isExternal(path) { return /^(https?:|mailto:|tel:)/.test(path) } -- Gitblit v1.9.3