96 lines
2.2 KiB
JavaScript
96 lines
2.2 KiB
JavaScript
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) {
|
|
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('分享失败')
|
|
}
|
|
}
|
|
} |