From dcc84cf449d65d672fb400ebe67f2483dd82a2b2 Mon Sep 17 00:00:00 2001 From: wangdongbo <949818794@qq.com> Date: Mon, 2 Feb 2026 08:53:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/share-actions.vue | 89 ++ pages/message/message.vue | 1393 ++++++++++----------- pages/work/team/invite/invite-patient.vue | 83 +- utils/share-usage-example.md | 234 ++++ utils/share.js | 169 +++ utils/widget.js | 43 + 6 files changed, 1311 insertions(+), 700 deletions(-) create mode 100644 components/share-actions.vue create mode 100644 utils/share-usage-example.md create mode 100644 utils/share.js diff --git a/components/share-actions.vue b/components/share-actions.vue new file mode 100644 index 0000000..2306b1a --- /dev/null +++ b/components/share-actions.vue @@ -0,0 +1,89 @@ + + + + + diff --git a/pages/message/message.vue b/pages/message/message.vue index ae2c985..654726d 100644 --- a/pages/message/message.vue +++ b/pages/message/message.vue @@ -1,697 +1,696 @@ - - - - - + + + + + \ No newline at end of file diff --git a/pages/work/team/invite/invite-patient.vue b/pages/work/team/invite/invite-patient.vue index 9016ff1..4221424 100644 --- a/pages/work/team/invite/invite-patient.vue +++ b/pages/work/team/invite/invite-patient.vue @@ -42,8 +42,8 @@ 进入团队首页,即可发起线上咨询、建档授权等服务 - 保存图片 - 分享微信 + 保存图片 + @@ -56,7 +56,7 @@ import { onLoad } from "@dcloudio/uni-app"; import useAccountStore from "@/store/account.js"; import useGuard from '@/hooks/useGuard'; import api from "@/utils/api.js"; -import { toast } from "@/utils/widget"; +import { toast, saveImageToPhotosAlbum, shareToWeChat } from "@/utils/widget"; import emptyData from "@/components/empty-data.vue"; import renamePopup from "./rename-popup.vue"; @@ -69,6 +69,7 @@ const current = ref(0); const list = ref([]); const visible = ref(false); const teamId = ref('') +const qrcodes = ref(null); const indicator = computed(() => ({ prev: current.value > 0, next: current.value < list.value.length - 1 @@ -113,6 +114,65 @@ async function change(name) { } } +// 保存二维码图片 +async function saveImage() { + if (!team.value || !team.value.qrcode) { + toast('暂无二维码'); + return; + } + + try { + const qrcodeComponent = qrcodes.value[current.value]; + if (!qrcodeComponent) { + toast('二维码未加载完成'); + return; + } + + // 获取二维码临时文件路径 + const tempFilePath = qrcodeComponent.toTempFilePath(); + if (tempFilePath) { + await saveImageToPhotosAlbum(tempFilePath); + } else { + toast('获取二维码失败'); + } + } catch (err) { + console.error('保存图片失败:', err); + toast('保存失败'); + } +} + +// 分享配置 +function onShareAppMessage() { + if (!team.value) { + return shareToWeChat({ + title: '邀请患者加入团队', + path: '/pages/work/team/invite/invite-patient' + }); + } + + return shareToWeChat({ + title: `邀请您加入${team.value.name}`, + path: `/pages/work/team/invite/invite-patient?teamId=${team.value.teamId}`, + imageUrl: team.value.qrcode || '' + }); +} + +// 分享到朋友圈 +function onShareTimeline() { + if (!team.value) { + return { + title: '邀请患者加入团队', + path: '/pages/work/team/invite/invite-patient' + }; + } + + return { + title: `邀请您加入${team.value.name}`, + query: `teamId=${team.value.teamId}`, + imageUrl: team.value.qrcode || '' + }; +} + onLoad(opts => { teamId.value = opts.teamId || ''; }) @@ -121,6 +181,12 @@ useShow(() => { getTeams() }) +// 导出分享方法供页面使用 +defineExpose({ + onShareAppMessage, + onShareTimeline +}) + \ No newline at end of file diff --git a/utils/share-usage-example.md b/utils/share-usage-example.md new file mode 100644 index 0000000..b4b8606 --- /dev/null +++ b/utils/share-usage-example.md @@ -0,0 +1,234 @@ +# 微信小程序分享功能使用指南 + +## 功能说明 + +提供了完整的微信小程序分享功能,包括: +- 分享给好友 +- 分享到朋友圈 +- 保存图片到相册 + +## 使用方法 + +### 1. 基础分享(在页面中) + +```vue + + + +``` + +### 2. 使用分享组件 + +```vue + + + +``` + +### 3. 保存二维码图片 + +```vue + + + +``` + +### 4. 动态分享内容 + +```vue + +``` + +## 配置说明 + +### 1. 启用分享到朋友圈 + +在 `pages.json` 中配置页面: + +```json +{ + "path": "pages/index/index", + "style": { + "navigationBarTitleText": "首页", + "enableShareTimeline": true + } +} +``` + +### 2. 全局分享配置 + +在 `App.vue` 中配置全局分享: + +```vue + +``` + +## API 说明 + +### createShareMessage(options) + +创建分享给好友的配置 + +**参数:** +- `title` (string): 分享标题 +- `path` (string): 分享路径 +- `imageUrl` (string): 分享图片URL + +**返回:** 分享配置对象 + +### createShareTimeline(options) + +创建分享到朋友圈的配置 + +**参数:** +- `title` (string): 分享标题 +- `query` (string): 分享路径参数 +- `imageUrl` (string): 分享图片URL + +**返回:** 分享配置对象 + +### saveImageToAlbum(filePath) + +保存图片到相册 + +**参数:** +- `filePath` (string): 图片路径(本地临时路径或网络路径) + +**返回:** Promise + +## 注意事项 + +1. 分享图片建议尺寸:5:4,推荐 500x400px +2. 分享路径必须是已注册的页面路径 +3. 保存图片需要用户授权相册权限 +4. 分享到朋友圈需要在页面配置中启用 +5. 网络图片会自动下载后保存到相册 diff --git a/utils/share.js b/utils/share.js new file mode 100644 index 0000000..2119bc0 --- /dev/null +++ b/utils/share.js @@ -0,0 +1,169 @@ +/** + * 微信小程序分享工具 + */ + +import { toast } from './widget' + +/** + * 创建分享到好友的配置 + * @param {Object} options 分享配置 + * @param {string} options.title 分享标题 + * @param {string} options.path 分享路径 + * @param {string} options.imageUrl 分享图片URL + * @returns {Object} 分享配置对象 + */ +export function createShareMessage(options = {}) { + const { title = '', path = '', imageUrl = '' } = options + + return { + title, + path, + imageUrl, + success: () => { + toast('分享成功') + }, + fail: (err) => { + console.error('分享失败:', err) + toast('分享失败') + } + } +} + +/** + * 创建分享到朋友圈的配置 + * @param {Object} options 分享配置 + * @param {string} options.title 分享标题 + * @param {string} options.query 分享路径参数 + * @param {string} options.imageUrl 分享图片URL + * @returns {Object} 分享配置对象 + */ +export function createShareTimeline(options = {}) { + const { title = '', query = '', imageUrl = '' } = options + + return { + title, + query, + imageUrl + } +} + +/** + * 在页面中启用分享功能 + * 使用方法:在页面的 setup 中调用 + * + * @example + * import { enableShare } from '@/utils/share' + * + * // 在 setup 中 + * enableShare({ + * message: { + * title: '分享标题', + * path: '/pages/index/index', + * imageUrl: 'https://example.com/image.jpg' + * }, + * timeline: { + * title: '朋友圈标题', + * query: 'id=123', + * imageUrl: 'https://example.com/image.jpg' + * } + * }) + */ +export function enableShare(config = {}) { + const { message, timeline } = config + + // 分享给好友 + if (message) { + uni.$on('onShareAppMessage', () => { + return createShareMessage(message) + }) + } + + // 分享到朋友圈 + if (timeline) { + uni.$on('onShareTimeline', () => { + return createShareTimeline(timeline) + }) + } +} + +/** + * 保存图片到相册 + * @param {string} filePath 图片路径(本地临时路径或网络路径) + */ +export async function saveImageToAlbum(filePath) { + try { + // 如果是网络图片,先下载 + let localPath = filePath + if (filePath.startsWith('http')) { + const res = await uni.downloadFile({ url: filePath }) + if (res[0]) { + throw new Error('下载图片失败') + } + localPath = res[1].tempFilePath + } + + // 检查授权 + const authRes = await uni.getSetting() + if (!authRes[1].authSetting['scope.writePhotosAlbum']) { + // 请求授权 + try { + await uni.authorize({ scope: 'scope.writePhotosAlbum' }) + } catch (err) { + // 用户拒绝授权,引导去设置 + const [modalErr, modalRes] = await uni.showModal({ + title: '提示', + content: '需要您授权保存相册', + confirmText: '去设置', + cancelText: '取消' + }) + + if (modalRes && modalRes.confirm) { + await uni.openSetting() + } + return false + } + } + + // 保存图片 + const [saveErr] = await uni.saveImageToPhotosAlbum({ filePath: localPath }) + if (saveErr) { + throw saveErr + } + + await toast('保存成功') + return true + } catch (err) { + console.error('保存图片失败:', err) + await toast('保存失败') + return false + } +} + +/** + * 生成带参数的小程序码 + * 需要后端接口支持 + * @param {Object} options + * @param {string} options.scene 场景值 + * @param {string} options.page 页面路径 + * @returns {Promise} 返回小程序码图片URL + */ +export async function generateMiniCode(options = {}) { + // 这里需要调用后端接口生成小程序码 + // 示例代码,需要根据实际后端接口调整 + try { + const res = await uni.request({ + url: '/api/wechat/generateMiniCode', + method: 'POST', + data: options + }) + + if (res[0] || !res[1].data.success) { + throw new Error('生成小程序码失败') + } + + return res[1].data.data.url + } catch (err) { + console.error('生成小程序码失败:', err) + throw err + } +} diff --git a/utils/widget.js b/utils/widget.js index 495028e..8c88f8a 100644 --- a/utils/widget.js +++ b/utils/widget.js @@ -50,4 +50,47 @@ export async function confirm(content, opt = {}) { } }) }) +} + +// 保存图片到相册 +export async function saveImageToPhotosAlbum(filePath) { + try { + // 检查授权 + const authRes = await uni.getSetting() + if (!authRes[1].authSetting['scope.writePhotosAlbum']) { + // 请求授权 + try { + await uni.authorize({ scope: 'scope.writePhotosAlbum' }) + } catch (err) { + await confirm('需要您授权保存相册', { title: '提示', showCancel: false }) + await uni.openSetting() + return + } + } + + // 保存图片 + await uni.saveImageToPhotosAlbum({ filePath }) + await toast('保存成功') + } catch (err) { + console.error('保存图片失败:', err) + await toast('保存失败') + } +} + +// 分享到微信 +export function shareToWeChat(options = {}) { + const { title = '', path = '', imageUrl = '' } = options + + return { + title, + path, + imageUrl, + success: () => { + toast('分享成功') + }, + fail: (err) => { + console.error('分享失败:', err) + toast('分享失败') + } + } } \ No newline at end of file