fix: perfect hooks

This commit is contained in:
chansee97 2024-06-04 20:43:09 +08:00
parent 648a0ba098
commit 448f3ba494
12 changed files with 76 additions and 66 deletions

View File

@ -1,12 +1,12 @@
/** 不同请求服务的环境配置 */ /** 不同请求服务的环境配置 */
export const serviceConfig: Record<ServiceEnvType, Record<string, string>> = { export const serviceConfig: Record<ServiceEnvType, Record<string, string>> = {
dev: { dev: {
url: 'https://mock.apifox.com/m1/4071143-0-default', url: 'https://mock.apifox.cn/m1/4071143-0-default',
}, },
test: { test: {
url: 'https://mock.apifox.com/m1/4071143-0-default', url: 'https://mock.apifox.cn/m1/4071143-0-default',
}, },
prod: { prod: {
url: 'https://mock.apifox.com/m1/4071143-0-default', url: 'https://mock.apifox.cn/m1/4071143-0-default',
}, },
} }

View File

@ -1,3 +1,4 @@
export * from './useBoolean' export * from './useBoolean'
export * from './useEcharts' export * from './useEcharts'
export * from './usePermission' export * from './usePermission'
export * from './refForm'

15
src/hooks/refForm.ts Normal file
View File

@ -0,0 +1,15 @@
export function refForm<T>(initValue: T) {
function jsonClone(value: T) {
return JSON.parse(JSON.stringify(value))
}
const target = ref<T>(initValue)
function setDefault() {
target.value = jsonClone(initValue)
}
return {
target,
setDefault,
}
}

View File

@ -66,73 +66,54 @@ echarts.use([
/** /**
* Echarts hooks函数 * Echarts hooks函数
* @param options -
* @description * @description
*/ */
export function useEcharts(options: Ref<ECOption>) { export function useEcharts(el: Ref<HTMLElement | null>, chartOptions: Ref<ECOption>) {
const appStore = useAppStore() const appStore = useAppStore()
const domRef = ref<HTMLElement>()
let chart: echarts.ECharts | null = null let chart: echarts.ECharts | null = null
const initialSize = { width: 0, height: 0 } const { width, height } = useElementSize(el)
const { width, height } = useElementSize(domRef, initialSize)
function canRender() { const isRendered = computed(() => Boolean(el && chart))
return initialSize.width > 0 && initialSize.height > 0
}
function isRendered() {
return Boolean(domRef.value && chart)
}
async function render() { async function render() {
const chartTheme = appStore.colorMode ? 'dark' : 'light' const chartTheme = appStore.colorMode ? 'dark' : 'light'
await nextTick() await nextTick()
if (domRef.value) { if (el) {
chart = echarts.init(domRef.value, chartTheme) chart = echarts.init(el.value, chartTheme)
update(options.value) update(chartOptions.value)
} }
} }
function update(updateOptions: ECOption) { function update(updateOptions: ECOption) {
if (isRendered()) if (isRendered.value)
chart!.setOption({ ...updateOptions, backgroundColor: 'transparent' }) chart!.setOption({ ...updateOptions, backgroundColor: 'transparent' })
} }
function resize() {
chart?.resize()
}
function destroy() { function destroy() {
chart?.dispose() chart?.dispose()
chart = null chart = null
} }
const sizeWatch = watch([width, height], async ([newWidth, newHeight]) => {
initialSize.width = newWidth watch([width, height], async ([newWidth, newHeight]) => {
initialSize.height = newHeight if (isRendered.value && newWidth && newHeight)
if (newWidth === 0 && newHeight === 0) { chart?.resize()
// 节点被删除 将chart置为空
chart = null
}
if (!canRender())
return
if (isRendered())
resize()
else await render()
}) })
const OptionWatch = watch(options, (newValue) => { watch(chartOptions, (newValue) => {
update(newValue) update(newValue)
}) })
onMounted(() => {
render()
})
onUnmounted(() => { onUnmounted(() => {
sizeWatch()
OptionWatch()
destroy() destroy()
}) })
return { return {
domRef, destroy,
update,
} }
} }

View File

@ -113,7 +113,9 @@ const lineOptions = ref<ECOption>({
data: [20, 71, 8, 50, 57, 32], data: [20, 71, 8, 50, 57, 32],
}], }],
}) as Ref<ECOption> }) as Ref<ECOption>
const { domRef: lineRef } = useEcharts(lineOptions)
const lineRef = ref<HTMLElement | null>(null)
useEcharts(lineRef, lineOptions)
</script> </script>
<template> <template>

View File

@ -90,7 +90,9 @@ const option = ref<ECOption>({
}, },
}], }],
}) as Ref<ECOption> }) as Ref<ECOption>
const { domRef: lineRef } = useEcharts(option)
const lineRef = ref<HTMLElement | null>(null)
useEcharts(lineRef, option)
</script> </script>
<template> <template>

View File

@ -50,7 +50,8 @@ const option = ref<ECOption>({
}, },
], ],
}) as Ref<ECOption> }) as Ref<ECOption>
const { domRef: lineRef } = useEcharts(option) const lineRef = ref<HTMLElement | null>(null)
useEcharts(lineRef, option)
</script> </script>
<template> <template>

View File

@ -99,7 +99,8 @@ const lineOptions = ref<ECOption>({
}, },
], ],
}) as Ref<ECOption> }) as Ref<ECOption>
const { domRef: lineRef } = useEcharts(lineOptions) const lineRef = ref<HTMLElement | null>(null)
useEcharts(lineRef, lineOptions)
</script> </script>
<template> <template>

View File

@ -54,7 +54,8 @@ const pieOptions = ref<ECOption>({
}, },
], ],
}) as Ref<ECOption> }) as Ref<ECOption>
const { domRef: pieRef } = useEcharts(pieOptions) const pieRef = ref<HTMLElement | null>(null)
useEcharts(pieRef, pieOptions)
// 线 // 线
const lineOptions = ref<ECOption>({ const lineOptions = ref<ECOption>({
@ -245,7 +246,8 @@ const lineOptions = ref<ECOption>({
}, },
], ],
}) as Ref<ECOption> }) as Ref<ECOption>
const { domRef: lineRef } = useEcharts(lineOptions) const lineRef = ref<HTMLElement | null>(null)
useEcharts(lineRef, lineOptions)
// //
const barOptions = ref<ECOption>({ const barOptions = ref<ECOption>({
@ -385,7 +387,9 @@ const barOptions = ref<ECOption>({
}, },
], ],
}) as Ref<ECOption> }) as Ref<ECOption>
const { domRef: barRef } = useEcharts(barOptions) const barRef = ref<HTMLElement | null>(null)
useEcharts(barRef, barOptions)
// //
const radarOptions = ref<ECOption>({ const radarOptions = ref<ECOption>({
title: { title: {
@ -477,7 +481,8 @@ const radarOptions = ref<ECOption>({
}, },
], ],
}) as Ref<ECOption> }) as Ref<ECOption>
const { domRef: radarRef } = useEcharts(radarOptions) const radarRef = ref<HTMLElement | null>(null)
useEcharts(radarRef, radarOptions)
</script> </script>
<template> <template>

View File

@ -1,5 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import { useBoolean } from '@/hooks' import { refForm, useBoolean } from '@/hooks'
import { fetchRoleList } from '@/service' import { fetchRoleList } from '@/service'
interface Props { interface Props {
@ -19,15 +19,14 @@ const { bool: modalVisible, setTrue: showModal, setFalse: hiddenModal } = useBoo
const { bool: submitLoading, setTrue: startLoading, setFalse: endLoading } = useBoolean(false) const { bool: submitLoading, setTrue: startLoading, setFalse: endLoading } = useBoolean(false)
const formModel = ref() const { target: formModel, setDefault } = refForm<Entity.User>({
const defaultFormModal: Entity.User = {
userName: '', userName: '',
gender: undefined, gender: undefined,
email: '', email: '',
tel: '', tel: '',
role: [], role: [],
status: 1, status: 1,
} })
type ModalType = 'add' | 'view' | 'edit' type ModalType = 'add' | 'view' | 'edit'
const modalType = shallowRef<ModalType>('add') const modalType = shallowRef<ModalType>('add')
@ -47,15 +46,17 @@ async function openModal(type: ModalType = 'add', data: any) {
getRoleList() getRoleList()
const handlers = { const handlers = {
async add() { async add() {
formModel.value = { ...defaultFormModal } setDefault()
}, },
async view() { async view() {
if (data) if (!data)
formModel.value = { ...data } return
formModel.value = { ...data }
}, },
async edit() { async edit() {
if (data) if (!data)
formModel.value = { ...data } return
formModel.value = { ...data }
}, },
} }
await handlers[type]() await handlers[type]()

View File

@ -162,7 +162,7 @@ const treeData = ref([
<template> <template>
<n-flex> <n-flex>
<n-card class="w-60"> <n-card class="w-70">
<n-tree <n-tree
block-line block-line
:data="treeData" :data="treeData"

View File

@ -3,7 +3,7 @@ import type {
FormItemRule, FormItemRule,
} from 'naive-ui' } from 'naive-ui'
import HelpInfo from '@/components/common/HelpInfo.vue' import HelpInfo from '@/components/common/HelpInfo.vue'
import { useBoolean } from '@/hooks' import { refForm, useBoolean } from '@/hooks'
import { Regex } from '@/constants' import { Regex } from '@/constants'
import { fetchRoleList } from '@/service' import { fetchRoleList } from '@/service'
@ -24,7 +24,7 @@ const emit = defineEmits<{
const { bool: modalVisible, setTrue: showModal, setFalse: hiddenModal } = useBoolean(false) const { bool: modalVisible, setTrue: showModal, setFalse: hiddenModal } = useBoolean(false)
const { bool: submitLoading, setTrue: startLoading, setFalse: endLoading } = useBoolean(false) const { bool: submitLoading, setTrue: startLoading, setFalse: endLoading } = useBoolean(false)
const defaultFormModal: AppRoute.RowRoute = { const { target: formModel, setDefault } = refForm<AppRoute.RowRoute>({
'name': '', 'name': '',
'path': '', 'path': '',
'id': -1, 'id': -1,
@ -41,8 +41,7 @@ const defaultFormModal: AppRoute.RowRoute = {
'meta.withoutTab': true, 'meta.withoutTab': true,
'meta.pinTab': false, 'meta.pinTab': false,
'meta.menuType': 'page', 'meta.menuType': 'page',
} })
const formModel = ref()
type ModalType = 'add' | 'view' | 'edit' type ModalType = 'add' | 'view' | 'edit'
const modalType = shallowRef<ModalType>('add') const modalType = shallowRef<ModalType>('add')
@ -62,15 +61,17 @@ async function openModal(type: ModalType = 'add', data: AppRoute.RowRoute) {
showModal() showModal()
const handlers = { const handlers = {
async add() { async add() {
formModel.value = { ...defaultFormModal } setDefault()
}, },
async view() { async view() {
if (data) if (!data)
formModel.value = { ...data } return
formModel.value = { ...data }
}, },
async edit() { async edit() {
if (data) if (!data)
formModel.value = { ...data } return
formModel.value = { ...data }
}, },
} }
await handlers[type]() await handlers[type]()