refactor(user): 移除不必要的组件状态追踪代码

- 删除 isComponentMounted 状态变量及相关检查逻辑
- 简化异步函数中的组件挂载状态验证
- 保留必要的资源清理逻辑(定时器和Blob URL)
- 优化代码结构,提高可维护性

Vue 3 的响应式系统已经能够自动处理组件卸载时的状态更新,
无需手动追踪组件生命周期状态。
This commit is contained in:
Leo 2025-07-06 02:52:45 +08:00
parent 7254d8f9bd
commit 92ab0c3159

View File

@ -67,8 +67,6 @@ const selectedFile = ref<File | null>(null)
const updateSupport = ref(false) const updateSupport = ref(false)
const uploadProgress = ref(0) const uploadProgress = ref(0)
//
const isComponentMounted = ref(true)
const progressInterval = ref<NodeJS.Timeout | null>(null) const progressInterval = ref<NodeJS.Timeout | null>(null)
const createdBlobUrls = ref<string[]>([]) const createdBlobUrls = ref<string[]>([])
@ -113,6 +111,7 @@ const rules = {
{ type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }, { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' },
], ],
phone: [ phone: [
{ required: true, message: '请输入手机号', trigger: 'blur' },
{ pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号格式', trigger: 'blur' }, { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号格式', trigger: 'blur' },
], ],
password: [ password: [
@ -351,9 +350,6 @@ const columns: DataTableColumns<UserVo> = [
// //
async function getUserList() { async function getUserList() {
if (!isComponentMounted.value)
return
loading.value = true loading.value = true
try { try {
// //
@ -381,10 +377,6 @@ async function getUserList() {
const { isSuccess, data } = await fetchUserPage(params) const { isSuccess, data } = await fetchUserPage(params)
//
if (!isComponentMounted.value)
return
if (isSuccess && data) { if (isSuccess && data) {
tableData.value = data.records || [] tableData.value = data.records || []
pagination.value.itemCount = data.total || 0 pagination.value.itemCount = data.total || 0
@ -397,19 +389,15 @@ async function getUserList() {
} }
} }
catch (error) { catch (error) {
if (!isComponentMounted.value)
return
console.error('获取用户列表失败:', error) console.error('获取用户列表失败:', error)
coiMsgError('获取用户列表失败,请检查网络连接') coiMsgError('获取用户列表失败,请检查网络连接')
tableData.value = [] tableData.value = []
pagination.value.itemCount = 0 pagination.value.itemCount = 0
} }
finally { finally {
if (isComponentMounted.value) {
loading.value = false loading.value = false
} }
} }
}
// //
async function getRoleList() { async function getRoleList() {
@ -890,10 +878,6 @@ async function handleConfirmImport() {
const response = await importUserData(selectedFile.value, updateSupport.value) const response = await importUserData(selectedFile.value, updateSupport.value)
//
if (!isComponentMounted.value)
return
if (progressInterval.value) { if (progressInterval.value) {
clearInterval(progressInterval.value) clearInterval(progressInterval.value)
progressInterval.value = null progressInterval.value = null
@ -915,11 +899,9 @@ async function handleConfirmImport() {
} }
} }
catch (error) { catch (error) {
if (isComponentMounted.value) {
console.error('导入失败:', error) console.error('导入失败:', error)
coiMsgError('导入失败,请检查文件格式或联系管理员') coiMsgError('导入失败,请检查文件格式或联系管理员')
} }
}
finally { finally {
// //
if (progressInterval.value) { if (progressInterval.value) {
@ -927,12 +909,10 @@ async function handleConfirmImport() {
progressInterval.value = null progressInterval.value = null
} }
if (isComponentMounted.value) {
importLoading.value = false importLoading.value = false
uploadProgress.value = 0 uploadProgress.value = 0
} }
} }
}
// //
function handleCancelImport() { function handleCancelImport() {
@ -1105,9 +1085,6 @@ onMounted(() => {
// //
onBeforeUnmount(() => { onBeforeUnmount(() => {
//
isComponentMounted.value = false
// //
if (progressInterval.value) { if (progressInterval.value) {
clearInterval(progressInterval.value) clearInterval(progressInterval.value)