diff --git a/main.js b/main.js index 83b5fcd1..93c8672b 100644 --- a/main.js +++ b/main.js @@ -10,6 +10,19 @@ Vue.config.productionTip = false import uView from 'uni_modules/uview-ui'; Vue.use(uView); +// 导入工具函数 +import { processImageUrl } from './utils/index.js' + +// 添加全局方法 - Vue 2 +Vue.prototype.$processImage = (url) => { + return processImageUrl(url) +} + +// 添加全局过滤器 - Vue 2 +Vue.filter('processImage', function(url) { + return processImageUrl(url) +}) + const app = new Vue({ ...App @@ -26,6 +39,14 @@ const app = createApp(App) // 安装 uView app.use(uView) +// 导入工具函数 +import { processImageUrl } from './utils/index.js' + +// 添加全局方法 - Vue 3 +app.config.globalProperties.$processImage = (url) => { + return processImageUrl(url) +} + // 挂载多个全局方法 app.config.globalProperties.$toast = $toast app.config.globalProperties.$modal = $modal diff --git a/utils/index.js b/utils/index.js index 2f33d977..0ee97b82 100644 --- a/utils/index.js +++ b/utils/index.js @@ -2,11 +2,15 @@ const environments = { development: { apiUrl: 'https://test.hshuishang.com', - picUrl: 'https://test.hshuishang.com' + picUrl: 'https://test.hshuishang.com', + aliyunOssUrl: 'https://wechat-img-file.oss-cn-beijing.aliyuncs.com', + staticUrl: 'https://wechat-img-file.oss-cn-beijing.aliyuncs.com' }, production: { apiUrl: 'https://api.hshuishang.com', - picUrl: 'https://api.hshuishang.com' + picUrl: 'https://api.hshuishang.com', + aliyunOssUrl: 'https://wechat-img-file.oss-cn-beijing.aliyuncs.com', + staticUrl: 'http://static.hshuishang.com' } }; @@ -32,6 +36,26 @@ const envConfig = environments[currentEnv] || environments.development; export const RequsetUrl = envConfig.apiUrl; // 请求地址前缀 export const picUrl = envConfig.picUrl; // 图片地址前缀 +export const aliyunOssUrl = envConfig.aliyunOssUrl; // 阿里云OSS地址 +export const staticUrl = envConfig.staticUrl; // 静态资源地址 + +/** + * 处理图片URL,根据环境自动替换 + * @param {string} url - 原始图片URL + * @returns {string} - 处理后的图片URL + */ +export const processImageUrl = (url) => { + if (!url) return url; + + // 如果URL包含阿里云OSS地址,则根据环境替换 + if (url.includes(aliyunOssUrl)) { + if (currentEnv === 'production') { + return url.replace(aliyunOssUrl, staticUrl); + } + } + + return url; +}; /** * @description 小程序跳转方法二次封装