import config from '@/config/index.js' // 统一处理图片地址,接口返回相对路径时自动补全前缀 export const formatImageUrl = (url) => { if (!url) { return '' } if (/^https?:\/\//.test(url)) { return url } const imageBaseUrl = config.imageUrl || '' const normalizedBaseUrl = imageBaseUrl.endsWith('/') ? imageBaseUrl : `${imageBaseUrl}/` const normalizedPath = String(url).replace(/^\/+/, '') return `${normalizedBaseUrl}${normalizedPath}` } export const saveImage = (url) => { uni.showLoading({ title: '正在保存图片...' }); uni.getSetting({ success: async (res) => { if (!res.authSetting["scope.writePhotosAlbum"]) { uni.authorize({ scope: "scope.writePhotosAlbum", success: async () => { const res = await uni.getImageInfo({ src: url }); uni.saveImageToPhotosAlbum({ filePath: res.path, success: () => { uni.hideLoading(); uni.showToast({ title: "保存成功!" }); }, fail: (err) => { console.error("保存失败:", err); uni.showToast({ title: "保存失败,请重试!", icon: 'error' }); } }); }, fail: () => { uni.hideLoading(); uni.showModal({ title: "权限未授权", content: "请前往设置中开启相册权限", success: (res) => { if (res.confirm) { uni.openSetting(); } } }); } }); } else { const res = await uni.getImageInfo({ src: url }); uni.saveImageToPhotosAlbum({ filePath: res.path, success: () => { uni.hideLoading(); uni.showToast({ title: "保存成功!" }); }, fail: (err) => { console.error("保存失败:", err); uni.showToast({ title: "保存失败,请重试!", icon: 'error' }); } }); } }, fail: () => { uni.hideLoading(); uni.showToast({ title: "获取权限失败!", icon: 'error' }); } }); };