const toastOptions = { duration: 2000, mask: false }; const confirmOptions = { title: '提示', cancelText: '取消', confirmText: '确定', showCancel: true }; export function toast(message, opts = {}) { const options = { ...toastOptions, ...opts } return new Promise((resolve) => { uni.showToast({ title: message, icon: 'none', // mask: options.mask, duration: options.duration, }) if (options.duration > 0) { setTimeout(resolve, options.duration) } else { resolve() } }) } export function hideToast() { uni.hideToast() } export function loading(title = '加载中...') { uni.showLoading({ title, mask: true }) } export function hideLoading() { uni.hideLoading() } // 使用系统原生确认弹窗 export async function confirm(content, opt = {}) { const options = { ...confirmOptions, ...opt } return new Promise((resolve, reject) => { uni.showModal({ title: options.title, content, showCancel: options.showCancel, cancelText: options.cancelText, confirmText: options.confirmText, success: ({ confirm, cancel }) => { if (confirm) { resolve() } else if (cancel) { reject() } } }) }) } // 保存图片到相册 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) { // 用户拒绝授权,引导去设置 const [modalErr, modalRes] = await uni.showModal({ title: '提示', content: '需要您授权保存相册', confirmText: '去设置', cancelText: '取消' }) if (modalRes && modalRes.confirm) { await uni.openSetting() } return false } } // 保存图片 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('分享失败') } } } // 分享第三方小程序给微信好友 export function shareThirdPartyMiniProgram(options = {}) { const { title = '', appId = '', path = '', imageUrl = '', webUrl = '', miniProgramType = 0 } = options if (!appId) { console.error('分享第三方小程序需要提供 appId') toast('分享配置错误') return null } return { title, path, imageUrl, miniProgramType, // 0-正式版 1-开发版 2-体验版 webpageUrl: webUrl, // 兼容低版本的网页链接 success: () => { toast('分享成功') }, fail: (err) => { console.error('分享失败:', err) toast('分享失败') } } }