From 8f713a64c792ceae5d7d55bae6f0d040392f3d16 Mon Sep 17 00:00:00 2001 From: zoujing Date: Sun, 5 Apr 2026 16:49:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A4=84=E7=90=86=E5=8A=A0=E8=BD=BDH5?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E7=9A=84=20=E6=96=B9=E6=B3=95=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/GeneratorPhotoComponent/index.vue | 2 + src/pages/webview/h5.js | 128 ++++++++++++++++++ src/pages/webview/index.vue | 30 +++- 3 files changed, 156 insertions(+), 4 deletions(-) create mode 100644 src/pages/webview/h5.js diff --git a/src/pages/index/components/module/GeneratorPhotoComponent/index.vue b/src/pages/index/components/module/GeneratorPhotoComponent/index.vue index a87b129..b7f0214 100644 --- a/src/pages/index/components/module/GeneratorPhotoComponent/index.vue +++ b/src/pages/index/components/module/GeneratorPhotoComponent/index.vue @@ -56,6 +56,8 @@ onMounted(() => { const jumpClick = () => { const token = getAccessToken(); + navigateTo('http://192.168.1.14:8081/#/home', { token: token }); + return if (props.toolCall.componentNameParams.jumpUrl) { navigateTo(props.toolCall.componentNameParams.jumpUrl, { token: token }); } diff --git a/src/pages/webview/h5.js b/src/pages/webview/h5.js new file mode 100644 index 0000000..ba504e3 --- /dev/null +++ b/src/pages/webview/h5.js @@ -0,0 +1,128 @@ +import { navigateTo } from "@/router"; +import { getAccessToken } from "@/constant/token"; +import { updateImageFile } from "@/request/api/UpdateFile"; + +// 这个文件是为了在 H5 页面中使用 uni.navigateTo 方法,并且自动携带 token 参数 +export const navigateToNewPage = (url, options = {}) => { + const token = getAccessToken(); + navigateTo(url, { token: token }, options); +}; + +// 保存图片到相册的完整流程 +export const saveImageToAlbum = (imageUrl) => { + if (!imageUrl) { + uni.showToast({ title: '图片地址无效', icon: 'none' }); + return; + } + + // 先下载图片 + uni.showLoading({ title: '保存中...', mask: true }); + uni.downloadFile({ + url: imageUrl, + success: (res) => { + if (res.statusCode === 200) { + const tempFilePath = res.tempFilePath; + // 请求相册授权并保存 + uni.getSetting({ + success: (settingRes) => { + if (settingRes.authSetting['scope.writePhotosAlbum']) { + // 已授权,直接保存 + saveToAlbum(tempFilePath); + } else { + // 未授权,请求授权 + uni.authorize({ + scope: 'scope.writePhotosAlbum', + success: () => saveToAlbum(tempFilePath), + fail: () => { + uni.hideLoading(); + uni.showModal({ + title: '提示', + content: '需要相册权限才能保存图片,请去设置中开启', + confirmText: '去设置', + success: (modalRes) => { + if (modalRes.confirm) { + uni.openSetting(); + } + } + }); + } + }); + } + } + }); + } else { + uni.hideLoading(); + uni.showToast({ title: '图片下载失败', icon: 'none' }); + } + }, + fail: (err) => { + console.error('下载失败', err); + uni.hideLoading(); + uni.showToast({ title: '网络错误,下载失败', icon: 'none' }); + } + }); +} + +// 执行保存到相册 +function saveToAlbum(filePath) { + uni.saveImageToPhotosAlbum({ + filePath: filePath, + success: () => { + uni.hideLoading(); + uni.showToast({ title: '保存成功', icon: 'success' }); + }, + fail: (err) => { + console.error('保存失败', err); + uni.hideLoading(); + uni.showToast({ title: '保存失败', icon: 'none' }); + } + }); +} + +/** + * 选择并上传图片 + * @param {Object} options 配置项 + * @param {number} options.count 最多选择的图片数量,默认1 + * @param {string} options.sourceType 图片来源 ['album', 'camera'],默认两者都允许 + * @returns {Promise} 返回上传后的图片地址(res.data) + */ +export function chooseAndUploadImage(options = {}) { + const { count = 1, sourceType = ['album', 'camera'] } = options + + return new Promise((resolve, reject) => { + // 1. 选择图片 + uni.chooseImage({ + count, + sourceType, + success: (chooseRes) => { + const tempFilePaths = chooseRes.tempFilePaths + if (!tempFilePaths || tempFilePaths.length === 0) { + reject(new Error('未选择图片')) + return + } + const filePath = tempFilePaths[0] // 单张图片,取第一张 + + // 2. 上传图片(这里假设 updateImageFile 返回 Promise,resolve 的数据结构为 { data: '图片地址' }) + updateImageFile(filePath) + .then((uploadRes) => { + const imageUrl = uploadRes.data + if (!imageUrl) { + reject(new Error('上传成功但未返回图片地址')) + return + } + resolve(imageUrl) + }) + .catch((err) => { + console.error('上传失败', err) + uni.showToast({ title: '上传失败', icon: 'none' }) + reject(err) + }) + }, + fail: (err) => { + console.error('选择图片失败', err) + uni.showToast({ title: '选择图片失败', icon: 'none' }) + reject(err) + } + }) + }) +} \ No newline at end of file diff --git a/src/pages/webview/index.vue b/src/pages/webview/index.vue index 0c919ae..d97aa16 100644 --- a/src/pages/webview/index.vue +++ b/src/pages/webview/index.vue @@ -1,11 +1,12 @@