feat(头像处理): 添加统一头像URL处理函数并替换硬编码路径

This commit is contained in:
yangzhe 2026-01-20 11:44:28 +08:00
parent 07a649b4be
commit d2cf481a67
5 changed files with 45 additions and 12 deletions

View File

@ -14,3 +14,36 @@ export const Debounce = (fn, wait) => {
if (callNow) fn.apply(this, args) 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;
}

View File

@ -2,6 +2,7 @@ import Vue from 'vue';
import App from './App'; import App from './App';
import "./static/common/css/font.css" import "./static/common/css/font.css"
import common from './static/common/js/common' import common from './static/common/js/common'
import { getHeadImgUrl } from '@/common/utils.js';
import jweixin from 'jweixin-module' import jweixin from 'jweixin-module'
import { import {
HubConnectionBuilder, HubConnectionBuilder,
@ -27,6 +28,13 @@ var tips = function(title, type, time) {
// 导入公用js // 导入公用js
Vue.prototype.$tips = tips Vue.prototype.$tips = tips
Vue.prototype.common = common 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 // 导入微信sdk
Vue.prototype.jweixin = jweixin Vue.prototype.jweixin = jweixin
// 引入全局uView // 引入全局uView

View File

@ -212,19 +212,11 @@ export default {
}, },
receiverHeadSculptureUrl() { receiverHeadSculptureUrl() {
if (this.vuex_msgUser.headSculptureUrl) { return this.$getHeadImgUrl(this.vuex_msgUser.headSculptureUrl);
return this.baseUrl + "/" + this.vuex_msgUser.headSculptureUrl;
}
return "/static/common/images/avatar_default2.png";
}, },
headSculptureUrl() { headSculptureUrl() {
if (this.vuex_user.HeadSculptureUrl) { return this.$getHeadImgUrl(this.vuex_user.HeadSculptureUrl);
return this.baseUrl + "/" + this.vuex_user.HeadSculptureUrl;
}
return "/static/common/images/avatar_default2.png";
}, },
}, },

View File

@ -26,7 +26,7 @@
> >
<image <image
class="teacher-avatar" class="teacher-avatar"
src="/static/common/images/avatar.png" :src="$getHeadImgUrl(teacher.headSculptureUrl)"
></image> ></image>
<view class="teacher-info"> <view class="teacher-info">
<view class="teacher-detail"> <view class="teacher-detail">

View File

@ -251,7 +251,7 @@ export default {
this.imageUrl = ""; this.imageUrl = "";
this.imageValue = [ this.imageValue = [
{ {
url: "/static/common/images/avatar_default.jpg", url: "/static/common/images/avatar_default2.png",
extname: "jpg", extname: "jpg",
name: "", name: "",
}, },