From d2cf481a6765e7b6d7abd0e818d762e2351f4f66 Mon Sep 17 00:00:00 2001 From: yangzhe Date: Tue, 20 Jan 2026 11:44:28 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=A4=B4=E5=83=8F=E5=A4=84=E7=90=86):=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BB=9F=E4=B8=80=E5=A4=B4=E5=83=8FURL?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=87=BD=E6=95=B0=E5=B9=B6=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E7=A1=AC=E7=BC=96=E7=A0=81=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/utils.js | 33 ++++++++++++++++++++++++++++++++ main.js | 8 ++++++++ pages/chat/index.vue | 12 ++---------- pages/home/admissions/index.vue | 2 +- pages/home/userSetting/index.vue | 2 +- 5 files changed, 45 insertions(+), 12 deletions(-) diff --git a/common/utils.js b/common/utils.js index 6f5da6b..ddafdba 100644 --- a/common/utils.js +++ b/common/utils.js @@ -14,3 +14,36 @@ export const Debounce = (fn, wait) => { if (callNow) fn.apply(this, args) } } + +/** + * 处理头像/图片 URL + * @param {String} url 图片路径 + * @param {String} baseUrl 基础路径 + * @param {String} defaultUrl 默认图片路径 + */ +export const getHeadImgUrl = (url, baseUrl, defaultUrl = "/static/common/images/avatar_default2.png") => { + if (!url) return defaultUrl; + if (url.startsWith('http') || url.startsWith('https') || url.startsWith('blob:')) return url; + + // 统一处理反斜杠:将所有的 \ 替换为 / + let cleanUrl = url.replace(/\\/g, '/'); + + // 处理 baseUrl: 替换反斜杠并去除末尾斜杠 + let cleanBaseUrl = ''; + if (baseUrl) { + cleanBaseUrl = baseUrl.replace(/\\/g, '/').replace(/\/$/, ''); + } + + // 情况三:headSculptureUrl = "/UploadImages/tx.jpg" + if (cleanUrl.startsWith('/')) { + return cleanBaseUrl + cleanUrl; + } + + // 情况二:headSculptureUrl = "UploadImages/tx.jpg" + if (cleanUrl.includes('/')) { + return cleanBaseUrl + '/' + cleanUrl; + } + + // 情况一:headSculptureUrl = "tx.jpg" + return cleanBaseUrl + '/UploadImages/' + cleanUrl; +} diff --git a/main.js b/main.js index 33b03ad..38e3cf6 100644 --- a/main.js +++ b/main.js @@ -2,6 +2,7 @@ import Vue from 'vue'; import App from './App'; import "./static/common/css/font.css" import common from './static/common/js/common' +import { getHeadImgUrl } from '@/common/utils.js'; import jweixin from 'jweixin-module' import { HubConnectionBuilder, @@ -27,6 +28,13 @@ var tips = function(title, type, time) { // 导入公用js Vue.prototype.$tips = tips Vue.prototype.common = common +Vue.prototype.$getHeadImgUrl = function (url, defaultUrl) { + let baseUrl = ''; + if (this.$u && this.$u.http && this.$u.http.config && this.$u.http.config.baseUrl) { + baseUrl = this.$u.http.config.baseUrl; + } + return getHeadImgUrl(url, baseUrl, defaultUrl); +} // 导入微信sdk Vue.prototype.jweixin = jweixin // 引入全局uView diff --git a/pages/chat/index.vue b/pages/chat/index.vue index f2ed716..22b4f95 100644 --- a/pages/chat/index.vue +++ b/pages/chat/index.vue @@ -212,19 +212,11 @@ export default { }, receiverHeadSculptureUrl() { - if (this.vuex_msgUser.headSculptureUrl) { - return this.baseUrl + "/" + this.vuex_msgUser.headSculptureUrl; - } - - return "/static/common/images/avatar_default2.png"; + return this.$getHeadImgUrl(this.vuex_msgUser.headSculptureUrl); }, headSculptureUrl() { - if (this.vuex_user.HeadSculptureUrl) { - return this.baseUrl + "/" + this.vuex_user.HeadSculptureUrl; - } - - return "/static/common/images/avatar_default2.png"; + return this.$getHeadImgUrl(this.vuex_user.HeadSculptureUrl); }, }, diff --git a/pages/home/admissions/index.vue b/pages/home/admissions/index.vue index 255eaf9..feea492 100644 --- a/pages/home/admissions/index.vue +++ b/pages/home/admissions/index.vue @@ -26,7 +26,7 @@ > diff --git a/pages/home/userSetting/index.vue b/pages/home/userSetting/index.vue index 289711a..92b8fc1 100644 --- a/pages/home/userSetting/index.vue +++ b/pages/home/userSetting/index.vue @@ -251,7 +251,7 @@ export default { this.imageUrl = ""; this.imageValue = [ { - url: "/static/common/images/avatar_default.jpg", + url: "/static/common/images/avatar_default2.png", extname: "jpg", name: "", },