refactor
This commit is contained in:
70
src/App.vue
70
src/App.vue
@@ -1,24 +1,23 @@
|
||||
<script setup lang="ts">
|
||||
import {onMounted, watch} from "vue";
|
||||
import {BaseState, useBaseStore} from "@/stores/base.ts";
|
||||
import {useRuntimeStore} from "@/stores/runtime.ts";
|
||||
import {useSettingStore} from "@/stores/setting.ts";
|
||||
import useTheme from "@/hooks/theme.ts";
|
||||
import {loadJsLib, shakeCommonDict} from "@/utils";
|
||||
import {get, set} from 'idb-keyval'
|
||||
import { onMounted, watch } from 'vue'
|
||||
import { BaseState, useBaseStore } from '@/stores/base.ts'
|
||||
import { useRuntimeStore } from '@/stores/runtime.ts'
|
||||
import { useSettingStore } from '@/stores/setting.ts'
|
||||
import useTheme from '@/hooks/theme.ts'
|
||||
import { loadJsLib, shakeCommonDict } from '@/utils'
|
||||
import { get, set } from 'idb-keyval'
|
||||
|
||||
import {useRoute} from "vue-router";
|
||||
import {DictId} from "@/types/types.ts";
|
||||
import {APP_VERSION, AppEnv, LOCAL_FILE_KEY, Origin, SAVE_DICT_KEY, SAVE_SETTING_KEY} from "@/config/env.ts";
|
||||
import {syncSetting} from "@/apis";
|
||||
import {useUserStore} from "@/stores/user.ts";
|
||||
import MigrateDialog from "@/components/MigrateDialog.vue";
|
||||
import { useRoute } from 'vue-router'
|
||||
import { APP_VERSION, AppEnv, DictId, LOCAL_FILE_KEY, Origin, SAVE_DICT_KEY, SAVE_SETTING_KEY } from '@/config/env.ts'
|
||||
import { syncSetting } from '@/apis'
|
||||
import { useUserStore } from '@/stores/user.ts'
|
||||
import MigrateDialog from '@/components/MigrateDialog.vue'
|
||||
|
||||
const store = useBaseStore()
|
||||
const runtimeStore = useRuntimeStore()
|
||||
const settingStore = useSettingStore()
|
||||
const userStore = useUserStore()
|
||||
const {setTheme} = useTheme()
|
||||
const { setTheme } = useTheme()
|
||||
|
||||
let lastAudioFileIdList = []
|
||||
let isInitializing = true // 标记是否正在初始化
|
||||
@@ -26,22 +25,24 @@ watch(store.$state, (n: BaseState) => {
|
||||
// 如果正在初始化,不保存数据,避免覆盖
|
||||
if (isInitializing) return
|
||||
let data = shakeCommonDict(n)
|
||||
set(SAVE_DICT_KEY.key, JSON.stringify({val: data, version: SAVE_DICT_KEY.version}))
|
||||
set(SAVE_DICT_KEY.key, JSON.stringify({ val: data, version: SAVE_DICT_KEY.version }))
|
||||
|
||||
//筛选自定义和收藏
|
||||
let bookList = data.article.bookList.filter(v => v.custom || [DictId.articleCollect].includes(v.id))
|
||||
let audioFileIdList = []
|
||||
bookList.forEach(v => {
|
||||
//筛选 audioFileId 字体有值的
|
||||
v.articles.filter(s => !s.audioSrc && s.audioFileId).forEach(a => {
|
||||
//所有 id 存起来,下次直接判断字符串是否相等,因为这个watch会频繁调用
|
||||
audioFileIdList.push(a.audioFileId)
|
||||
})
|
||||
v.articles
|
||||
.filter(s => !s.audioSrc && s.audioFileId)
|
||||
.forEach(a => {
|
||||
//所有 id 存起来,下次直接判断字符串是否相等,因为这个watch会频繁调用
|
||||
audioFileIdList.push(a.audioFileId)
|
||||
})
|
||||
})
|
||||
if (audioFileIdList.toString() !== lastAudioFileIdList.toString()) {
|
||||
let result = []
|
||||
//删除未使用到的文件
|
||||
get(LOCAL_FILE_KEY).then((fileList: Array<{ id: string, file: Blob }>) => {
|
||||
get(LOCAL_FILE_KEY).then((fileList: Array<{ id: string; file: Blob }>) => {
|
||||
if (fileList && fileList.length > 0) {
|
||||
audioFileIdList.forEach(a => {
|
||||
let item = fileList.find(b => b.id === a)
|
||||
@@ -54,13 +55,17 @@ watch(store.$state, (n: BaseState) => {
|
||||
}
|
||||
})
|
||||
|
||||
watch(() => settingStore.$state, (n) => {
|
||||
if (isInitializing) return
|
||||
set(SAVE_SETTING_KEY.key, JSON.stringify({val: n, version: SAVE_SETTING_KEY.version}))
|
||||
if (AppEnv.CAN_REQUEST) {
|
||||
syncSetting(null, settingStore.$state)
|
||||
}
|
||||
}, {deep: true})
|
||||
watch(
|
||||
() => settingStore.$state,
|
||||
n => {
|
||||
if (isInitializing) return
|
||||
set(SAVE_SETTING_KEY.key, JSON.stringify({ val: n, version: SAVE_SETTING_KEY.version }))
|
||||
if (AppEnv.CAN_REQUEST) {
|
||||
syncSetting(null, settingStore.$state)
|
||||
}
|
||||
},
|
||||
{ deep: true }
|
||||
)
|
||||
|
||||
async function init() {
|
||||
isInitializing = true // 开始初始化
|
||||
@@ -76,10 +81,10 @@ async function init() {
|
||||
set(APP_VERSION.key, APP_VERSION.version)
|
||||
} else {
|
||||
get(APP_VERSION.key).then(r => {
|
||||
runtimeStore.isNew = r ? (APP_VERSION.version > Number(r)) : true
|
||||
runtimeStore.isNew = r ? APP_VERSION.version > Number(r) : true
|
||||
})
|
||||
}
|
||||
window.umami?.track('host', {host: window.location.host})
|
||||
window.umami?.track('host', { host: window.location.host })
|
||||
}
|
||||
|
||||
onMounted(init)
|
||||
@@ -88,7 +93,7 @@ onMounted(init)
|
||||
let showTransfer = $ref(false)
|
||||
onMounted(() => {
|
||||
if (new URLSearchParams(window.location.search).get('from_old_site') === '1' && location.origin === Origin) {
|
||||
if (localStorage.getItem('__migrated_from_2study_top__')) return;
|
||||
if (localStorage.getItem('__migrated_from_2study_top__')) return
|
||||
setTimeout(() => {
|
||||
showTransfer = true
|
||||
}, 1000)
|
||||
@@ -127,8 +132,5 @@ onMounted(() => {
|
||||
<!-- </transition>-->
|
||||
<!-- </router-view>-->
|
||||
<router-view></router-view>
|
||||
<MigrateDialog
|
||||
v-model="showTransfer"
|
||||
@ok="init"
|
||||
/>
|
||||
<MigrateDialog v-model="showTransfer" @ok="init" />
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user