代码重构:使用统一消息组件封装替代原始调用

This commit is contained in:
li-xiaochen 2025-07-13 21:45:05 +08:00
parent 717df6588a
commit fcdaa529cd
7 changed files with 111 additions and 99 deletions

View file

@ -89,7 +89,6 @@ provide('new_version', new_version)
<n-spin v-if="loading" class="container"> <n-spin v-if="loading" class="container">
<template #description>加载中</template> <template #description>加载中</template>
</n-spin> </n-spin>
<n-notification-provider v-else>
<n-tabs <n-tabs
type="card" type="card"
placement="left" placement="left"
@ -97,6 +96,7 @@ provide('new_version', new_version)
v-model:value="conf.page" v-model:value="conf.page"
@update:value="set_page" @update:value="set_page"
justify-content="center" justify-content="center"
v-else
> >
<n-tab-pane :disabled="running" name="init" tab="初始化"><init /></n-tab-pane> <n-tab-pane :disabled="running" name="init" tab="初始化"><init /></n-tab-pane>
<n-tab-pane :disabled="running" name="update" tab="更新代码"><update /></n-tab-pane> <n-tab-pane :disabled="running" name="update" tab="更新代码"><update /></n-tab-pane>
@ -117,7 +117,6 @@ provide('new_version', new_version)
</div> </div>
</template> </template>
</n-tabs> </n-tabs>
</n-notification-provider>
<n-global-style /> <n-global-style />
</n-config-provider> </n-config-provider>
</template> </template>

View file

@ -1,6 +1,7 @@
<script setup> <script setup>
import PlayIcon from '@vicons/ionicons5/Play' import PlayIcon from '@vicons/ionicons5/Play'
import { inject } from 'vue' import { inject } from 'vue'
import { notify } from '@/utils/naiveDiscrete.js'
const running = inject('running') const running = inject('running')
const log = inject('log') const log = inject('log')
@ -8,8 +9,6 @@ const steps = inject('steps')
const current_step = inject('current_step') const current_step = inject('current_step')
const current_state = inject('current_state') const current_state = inject('current_state')
const notification = useNotification()
async function start() { async function start() {
log.value = [] log.value = []
running.value = true running.value = true
@ -20,22 +19,14 @@ async function start() {
if ((await pywebview.api.run(cmd, step.cwd)) == 'failed') { if ((await pywebview.api.run(cmd, step.cwd)) == 'failed') {
current_state.value = 'error' current_state.value = 'error'
running.value = false running.value = false
notification['error']({ notify.error('命令运行失败')
content: '错误',
meta: '命令运行失败',
duration: 3000
})
return return
} }
} }
} }
current_state.value = 'finish' current_state.value = 'finish'
running.value = false running.value = false
notification['info']({ notify.info('命令运行完成')
content: '提示',
meta: '命令运行完成',
duration: 3000
})
} }
</script> </script>

View file

@ -1,26 +1,15 @@
<script setup> <script setup>
import { notify } from '@/utils/naiveDiscrete.js'
const running = inject('running') const running = inject('running')
const notification = useNotification()
async function rm_site_packages_and_python_scripts() { async function rm_site_packages_and_python_scripts() {
running.value = true running.value = true
notification['info']({ notify.info('开始移除site-packages和python/Script目录')
content: '提示',
meta: '开始移除site-packages和python/Script目录',
duration: 3000
})
const response = await pywebview.api.rm_site_packages() const response = await pywebview.api.rm_site_packages()
notification['info']({ notify.info(response)
content: '提示',
meta: response,
duration: 3000
})
const response2 = await pywebview.api.rm_python_scripts() const response2 = await pywebview.api.rm_python_scripts()
notification['info']({ notify.info(response2)
content: '提示',
meta: response2,
duration: 3000
})
running.value = false running.value = false
} }
</script> </script>

View file

@ -12,8 +12,7 @@ import {
Stop, Stop,
Search Search
} from '@vicons/ionicons5' } from '@vicons/ionicons5'
import { notify } from '@/utils/naiveDiscrete.js'
const notification = useNotification()
const config_store = useConfigStore() const config_store = useConfigStore()
const conf = config_store.config const conf = config_store.config
@ -90,17 +89,9 @@ async function handle_migrate(key) {
name: '默认实例', name: '默认实例',
path: response.data path: response.data
}) })
notification['success']({ notify.success(response.message)
content: '信息',
meta: response.message,
duration: 3000
})
} else { } else {
notification['error']({ notify.error(response.message)
content: '错误',
meta: response.message,
duration: 3000
})
} }
} else { } else {
const response = await pywebview.api.migrate_instances_config() const response = await pywebview.api.migrate_instances_config()
@ -110,17 +101,9 @@ async function handle_migrate(key) {
conf.instances = [...config_store.config.instances] conf.instances = [...config_store.config.instances]
} }
if (response.status) { if (response.status) {
notification['info']({ notify.info(response.message)
title: '信息',
content: response.message,
duration: 3000
})
} else { } else {
notification['error']({ notify.error(response.message)
title: '错误',
content: response.message,
duration: 3000
})
} }
} }
} }

View file

@ -2,8 +2,7 @@
import { Sync } from '@vicons/ionicons5' import { Sync } from '@vicons/ionicons5'
import { form_item_label_style } from '@/styles/styles.js' import { form_item_label_style } from '@/styles/styles.js'
import BaseMirrorOption from '@/components/BaseMirrorOption.vue' import BaseMirrorOption from '@/components/BaseMirrorOption.vue'
import { notify } from '@/utils/naiveDiscrete.js'
const notification = useNotification()
const update_able = inject('update_able') const update_able = inject('update_able')
const running = inject('running') const running = inject('running')
@ -19,11 +18,7 @@ async function update_self() {
const response = await pywebview.api.update_self( const response = await pywebview.api.update_self(
new_version.value['assets'][0]['browser_download_url'] new_version.value['assets'][0]['browser_download_url']
) )
notification['error']({ notify.error(response)
content: '错误',
meta: response,
duration: 3000
})
update_self_running.value = false update_self_running.value = false
running.value = false running.value = false
} }
@ -38,18 +33,10 @@ async function check_update() {
new_version.value = await pywebview.api.get_new_version() new_version.value = await pywebview.api.get_new_version()
if (new_version.value.tag_name > version.value) { if (new_version.value.tag_name > version.value) {
update_able.value = true update_able.value = true
notification['info']({ notify.info('有新版本可更新')
content: '提示',
meta: '有新版本可更新',
duration: 3000
})
} else { } else {
update_able.value = false update_able.value = false
notification['info']({ notify.info('当前已是最新版本')
content: '提示',
meta: '当前已是最新版本',
duration: 3000
})
} }
check_running.value = false check_running.value = false
running.value = false running.value = false

View file

@ -23,23 +23,23 @@ const steps = computed(() => [
const mirror_options = [ const mirror_options = [
{ {
label: 'PyPI', label: 'PyPI',
value: 'pypi', value: 'pypi'
}, },
{ {
label: '阿里云镜像源', label: '阿里云镜像源',
value: 'aliyun', value: 'aliyun'
}, },
{ {
label: '上海交通大学镜像源', label: '上海交通大学镜像源',
value: 'sjtu', value: 'sjtu'
}, },
{ {
label: '清华大学镜像源', label: '清华大学镜像源',
value: 'tuna', value: 'tuna'
}, },
{ {
label: '中国科学技术大学镜像源', label: '中国科学技术大学镜像源',
value: 'ustc', value: 'ustc'
} }
] ]
provide('steps', steps) provide('steps', steps)

View file

@ -0,0 +1,63 @@
import { createDiscreteApi, darkTheme, lightTheme } from 'naive-ui'
// 创建全局单例
const { message, dialog, notification, loadingBar } = createDiscreteApi(
['message', 'dialog', 'notification', 'loadingBar'],
{
configProviderProps: {
// 全局主题配置(示例使用系统主题)
theme: window.matchMedia('(prefers-color-scheme: dark)').matches ? darkTheme : lightTheme
}
}
)
// 创建统一的工厂函数
const createMessager =
(handler, defaults) =>
(content, options = {}) => {
const mergedOptions =
typeof content === 'string'
? { content, ...defaults, ...options }
: { ...defaults, ...content, ...options }
return handler(mergedOptions)
}
// 消息提示封装
export const toast = {
info: createMessager((opts) => message.info(opts.content, opts), { duration: 3000 }),
success: createMessager((opts) => message.success(opts.content, opts), { duration: 3000 }),
warning: createMessager((opts) => message.warning(opts.content, opts), { duration: 5000 }),
error: createMessager((opts) => message.error(opts.content, opts), { duration: 7000 })
}
// 通知封装
export const notify = {
info: createMessager(notification.info, { title: '提示', duration: 3000 }),
success: createMessager(notification.success, { title: '成功', duration: 3000 }),
warning: createMessager(notification.warning, { title: '警告', duration: 5000 }),
error: createMessager(notification.error, { title: '错误', duration: 7000 })
}
// 对话框封装
export const modal = {
info: (options) => dialog.info(options),
success: (options) => dialog.success(options),
warning: (options) => dialog.warning(options),
error: (options) => dialog.error(options),
confirm: (options) => dialog.confirm(options)
}
// 加载条控制器
export const loader = {
start: () => loadingBar.start(),
finish: () => loadingBar.finish(),
error: () => loadingBar.error(),
setProgress: (progress) => loadingBar.setProgress(progress)
}