170 lines
4.0 KiB
JavaScript
170 lines
4.0 KiB
JavaScript
/**
|
|
* 微信小程序分享工具
|
|
*/
|
|
|
|
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<string>} 返回小程序码图片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
|
|
}
|
|
}
|