Files
EmailBill/Web/src/views/SettingView.vue

481 lines
12 KiB
Vue
Raw Normal View History

2026-02-02 16:59:24 +08:00
<template>
2025-12-27 21:15:26 +08:00
<div class="page-container-flex">
2026-01-16 11:15:44 +08:00
<van-nav-bar
title="设置"
placeholder
/>
2025-12-27 21:15:26 +08:00
<div class="scroll-content">
2026-01-16 11:15:44 +08:00
<div
class="detail-header"
style="padding-bottom: 5px"
>
2025-12-29 15:20:32 +08:00
<p>账单</p>
2025-12-27 21:15:26 +08:00
</div>
<van-cell-group inset>
2026-01-16 11:15:44 +08:00
<van-cell
title="从支付宝导入"
is-link
@click="handleImportClick('Alipay')"
/>
<van-cell
title="从微信导入"
is-link
@click="handleImportClick('WeChat')"
/>
<van-cell
title="周期记录"
is-link
@click="handlePeriodicRecord"
/>
2025-12-27 21:15:26 +08:00
</van-cell-group>
2025-12-25 11:20:56 +08:00
2025-12-27 21:15:26 +08:00
<!-- 隐藏的文件选择器 -->
2026-01-16 11:15:44 +08:00
<input
ref="fileInputRef"
type="file"
accept=".csv,.xlsx,.xls"
style="display: none"
@change="handleFileChange"
>
<div
class="detail-header"
style="padding-bottom: 5px"
>
2025-12-29 15:20:32 +08:00
<p>分类</p>
2025-12-27 21:15:26 +08:00
</div>
<van-cell-group inset>
2026-01-16 11:15:44 +08:00
<van-cell
title="待确认分类"
is-link
@click="handleUnconfirmedClassification"
/>
<van-cell
title="编辑分类"
is-link
@click="handleEditClassification"
/>
<van-cell
title="批量分类"
is-link
@click="handleBatchClassification"
/>
<van-cell
title="智能分类"
is-link
@click="handleSmartClassification"
/>
2025-12-31 11:10:10 +08:00
<!-- <van-cell title="自然语言分类" is-link @click="handleNaturalLanguageClassification" /> -->
2025-12-27 21:15:26 +08:00
</van-cell-group>
2026-01-16 11:15:44 +08:00
<div
class="detail-header"
style="padding-bottom: 5px"
>
<p>通知</p>
</div>
<van-cell-group inset>
<van-cell title="开启消息通知">
<template #right-icon>
2026-01-16 11:15:44 +08:00
<van-switch
v-model="notificationEnabled"
size="24"
:loading="notificationLoading"
@change="handleNotificationToggle"
/>
</template>
</van-cell>
2026-01-16 11:15:44 +08:00
<van-cell
v-if="notificationEnabled"
title="测试通知"
is-link
@click="handleTestNotification"
/>
</van-cell-group>
2026-01-16 11:15:44 +08:00
<div
class="detail-header"
style="padding-bottom: 5px"
>
2025-12-29 16:45:51 +08:00
<p>开发者</p>
</div>
<van-cell-group inset>
2026-01-16 11:15:44 +08:00
<van-cell
title="查看日志"
is-link
@click="handleLogView"
/>
<van-cell
title="清除缓存"
is-link
@click="handleReloadFromNetwork"
/>
<van-cell
title="定时任务"
is-link
@click="handleScheduledTasks"
/>
2026-02-02 16:59:24 +08:00
<van-cell
title="切换版本"
is-link
:value="versionStore.currentVersion.toUpperCase()"
@click="handleVersionSwitch"
/>
2026-01-02 19:21:47 +08:00
</van-cell-group>
2025-12-29 15:20:32 +08:00
2026-01-16 11:15:44 +08:00
<div
class="detail-header"
style="padding-bottom: 5px"
>
2025-12-27 21:15:26 +08:00
<p>账户</p>
</div>
<van-cell-group inset>
2026-01-16 11:15:44 +08:00
<van-cell
title="退出登录"
is-link
@click="handleLogout"
/>
2025-12-27 21:15:26 +08:00
</van-cell-group>
2026-01-02 19:17:21 +08:00
<!-- 底部安全距离 -->
2026-01-16 11:15:44 +08:00
<div style="height: calc(80px + env(safe-area-inset-bottom, 0px))" />
2025-12-25 13:27:23 +08:00
</div>
2025-12-25 11:20:56 +08:00
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
2025-12-25 13:27:23 +08:00
import { useRouter } from 'vue-router'
2026-02-02 16:59:24 +08:00
import { showLoadingToast, showSuccessToast, showToast, closeToast, showConfirmDialog, showDialog } from 'vant'
2025-12-25 11:20:56 +08:00
import { uploadBillFile } from '@/api/billImport'
2025-12-25 13:27:23 +08:00
import { useAuthStore } from '@/stores/auth'
2026-02-02 16:59:24 +08:00
import { useVersionStore } from '@/stores/version'
import { getVapidPublicKey, subscribe, testNotification } from '@/api/notification'
import { updateServiceWorker } from '@/registerServiceWorker'
2025-12-25 11:20:56 +08:00
2025-12-25 13:27:23 +08:00
const router = useRouter()
const authStore = useAuthStore()
2026-02-02 16:59:24 +08:00
const versionStore = useVersionStore()
2025-12-25 11:20:56 +08:00
const fileInputRef = ref(null)
const currentType = ref('')
const notificationEnabled = ref(false)
const notificationLoading = ref(false)
onMounted(async () => {
if ('serviceWorker' in navigator) {
const registration = await navigator.serviceWorker.ready
const subscription = await registration.pushManager.getSubscription()
notificationEnabled.value = !!subscription
}
})
2026-01-16 11:15:44 +08:00
function urlBase64ToUint8Array (base64String) {
const padding = '='.repeat((4 - (base64String.length % 4)) % 4)
const base64 = (base64String + padding).replace(/-/g, '+').replace(/_/g, '/')
const rawData = window.atob(base64)
const outputArray = new Uint8Array(rawData.length)
for (let i = 0; i < rawData.length; ++i) {
2026-01-16 11:15:44 +08:00
outputArray[i] = rawData.charCodeAt(i)
}
2026-01-16 11:15:44 +08:00
return outputArray
}
const handleNotificationToggle = async (checked) => {
if (!('serviceWorker' in navigator)) {
showToast('您的浏览器不支持推送通知')
notificationEnabled.value = false
return
}
notificationLoading.value = true
try {
const registration = await navigator.serviceWorker.ready
if (checked) {
// 开启通知
const permission = await Notification.requestPermission()
if (permission !== 'granted') {
showToast('请允许通知权限')
notificationEnabled.value = false
return
}
2026-01-16 11:15:44 +08:00
const { success, data, message } = await getVapidPublicKey()
2026-01-02 18:51:28 +08:00
if (!success) {
throw new Error(message || '获取 VAPID 公钥失败')
}
2026-01-16 11:15:44 +08:00
2026-01-02 18:51:28 +08:00
const convertedVapidKey = urlBase64ToUint8Array(data)
const subscription = await registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: convertedVapidKey
})
const subJson = subscription.toJSON()
await subscribe({
endpoint: subJson.endpoint,
p256DH: subJson.keys.p256dh,
auth: subJson.keys.auth
})
2026-01-16 11:15:44 +08:00
showSuccessToast('开启成功')
} else {
// 关闭通知
const subscription = await registration.pushManager.getSubscription()
if (subscription) {
await subscription.unsubscribe()
}
showSuccessToast('已关闭')
}
} catch (error) {
console.error(error)
showToast('操作失败: ' + (error.message || '未知错误'))
notificationEnabled.value = !checked // 回滚状态
} finally {
notificationLoading.value = false
}
}
const handleTestNotification = async () => {
try {
await testNotification('这是一条测试消息')
showSuccessToast('发送成功,请查看通知栏')
} catch (error) {
console.error(error)
showToast('发送失败')
}
}
2025-12-25 11:20:56 +08:00
/**
* 处理导入按钮点击
*/
const handleImportClick = (type) => {
currentType.value = type
// 触发文件选择
fileInputRef.value?.click()
}
2025-12-29 15:20:32 +08:00
const handlePeriodicRecord = () => {
router.push({ name: 'periodic-record' })
}
2025-12-25 11:20:56 +08:00
/**
* 处理文件选择
*/
const handleFileChange = async (event) => {
const file = event.target.files?.[0]
if (!file) {
return
}
// 验证文件类型
2026-01-16 11:15:44 +08:00
const validTypes = [
'text/csv',
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
]
2025-12-25 11:20:56 +08:00
if (!validTypes.includes(file.type)) {
showToast('请选择 CSV 或 Excel 文件')
return
}
// 验证文件大小(限制为 10MB
const maxSize = 10 * 1024 * 1024
if (file.size > maxSize) {
showToast('文件大小不能超过 10MB')
return
}
try {
// 显示加载提示
showLoadingToast({
message: '上传中...',
forbidClick: true,
duration: 0
})
// 上传文件
const typeName = currentType.value === 'Alipay' ? '支付宝' : '微信'
const { success, message } = await uploadBillFile(file, currentType.value)
if (!success) {
showToast(message || `${typeName}账单导入失败`)
return
}
showSuccessToast(message || `${typeName}账单导入成功`)
} catch (error) {
console.error('上传失败:', error)
showToast('上传失败: ' + (error.message || '未知错误'))
2026-01-16 11:15:44 +08:00
} finally {
2025-12-25 11:20:56 +08:00
closeToast()
// 清空文件输入,允许重复选择同一文件
event.target.value = ''
}
}
2025-12-25 13:27:23 +08:00
2025-12-26 15:21:31 +08:00
const handleEditClassification = () => {
router.push({ name: 'classification-edit' })
}
const handleBatchClassification = () => {
router.push({ name: 'classification-batch' })
}
const handleSmartClassification = () => {
router.push({ name: 'smart-classification' })
}
2025-12-31 12:46:03 +08:00
// const handleNaturalLanguageClassification = () => {
// router.push({ name: 'classification-nlp' })
// }
2025-12-26 15:21:31 +08:00
2025-12-25 13:27:23 +08:00
/**
* 处理退出登录
*/
const handleLogout = async () => {
try {
await showConfirmDialog({
title: '提示',
2026-01-16 11:15:44 +08:00
message: '确定要退出登录吗?'
2025-12-25 13:27:23 +08:00
})
2026-01-16 11:15:44 +08:00
2025-12-25 13:27:23 +08:00
authStore.logout()
showSuccessToast('已退出登录')
router.push({ name: 'login' })
} catch (error) {
console.error('取消退出登录:', error)
showToast('已取消退出登录')
}
}
2025-12-29 16:45:51 +08:00
/**
* 处理查看日志
*/
const handleLogView = () => {
router.push({ name: 'log' })
}
2026-01-02 19:21:47 +08:00
const handleUnconfirmedClassification = () => {
router.push({ name: 'unconfirmed-classification' })
}
const handleReloadFromNetwork = async () => {
2026-01-02 19:21:47 +08:00
try {
await showConfirmDialog({
title: '提示',
2026-01-16 11:15:44 +08:00
message: '确定要刷新网络吗?此操作不可撤销。'
2026-01-02 19:21:47 +08:00
})
// PWA程序强制页面更新到最新版本
if ('serviceWorker' in navigator) {
2026-01-04 18:51:55 +08:00
await updateServiceWorker()
showSuccessToast('正在更新,请稍候...')
// 延迟刷新页面以加载新版本
setTimeout(() => {
window.location.reload()
}, 1500)
2026-01-02 19:21:47 +08:00
} else {
showToast('当前环境不支持此操作')
2026-01-02 19:21:47 +08:00
return
}
} catch (error) {
console.error('取消刷新网络:', error)
showToast('已取消刷新网络')
2026-01-02 19:21:47 +08:00
}
}
const handleScheduledTasks = () => {
router.push({ name: 'scheduled-tasks' })
}
2026-02-02 16:59:24 +08:00
/**
* 处理版本切换
*/
const handleVersionSwitch = async () => {
try {
await showDialog({
title: '选择版本',
message: '请选择要使用的版本',
showCancelButton: true,
confirmButtonText: 'V2',
cancelButtonText: 'V1'
}).then(() => {
// 选择 V2
versionStore.setVersion('v2')
showSuccessToast('已切换到 V2')
// 尝试跳转到当前路由的 V2 版本
redirectToVersionRoute()
}).catch(() => {
// 选择 V1
versionStore.setVersion('v1')
showSuccessToast('已切换到 V1')
// 尝试跳转到当前路由的 V1 版本
redirectToVersionRoute()
})
} catch (error) {
console.error('版本切换失败:', error)
}
}
/**
* 根据当前版本重定向路由
*/
const redirectToVersionRoute = () => {
const currentRoute = router.currentRoute.value
const currentRouteName = currentRoute.name
if (versionStore.isV2()) {
// 尝试跳转到 V2 路由
const v2RouteName = `${currentRouteName}-v2`
const v2Route = router.getRoutes().find(route => route.name === v2RouteName)
if (v2Route) {
router.push({ name: v2RouteName })
}
// 如果没有 V2 路由,保持当前路由
} else {
// V1 版本:如果当前在 V2 路由,跳转到 V1
if (currentRouteName && currentRouteName.toString().endsWith('-v2')) {
const v1RouteName = currentRouteName.toString().replace(/-v2$/, '')
const v1Route = router.getRoutes().find(route => route.name === v1RouteName)
if (v1Route) {
router.push({ name: v1RouteName })
}
}
}
}
2025-12-25 11:20:56 +08:00
</script>
<style scoped>
/* 页面背景色 */
:deep(body) {
2026-01-13 17:00:44 +08:00
background-color: var(--van-background);
2025-12-25 11:20:56 +08:00
}
/* 增加卡片对比度 */
:deep(.van-cell-group--inset) {
2026-01-13 17:00:44 +08:00
background-color: var(--van-background-2);
2025-12-25 11:20:56 +08:00
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}
.detail-header {
padding: 16px 16px 5px 16px;
2025-12-29 15:20:32 +08:00
margin-bottom: 5px;
2025-12-25 11:20:56 +08:00
}
.detail-header p {
margin: 0;
font-size: 14px;
2026-01-13 17:00:44 +08:00
color: var(--van-text-color-2);
2025-12-25 11:20:56 +08:00
font-weight: normal;
}
2025-12-26 18:03:52 +08:00
/* 设置页面容器背景色 */
:deep(.van-nav-bar) {
background: transparent !important;
}
2026-01-16 11:15:44 +08:00
</style>