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
service.config.ts
src
hooks
views
dashboard
monitor/components
workbench/components
demo/echarts
setting
account
menu/components

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

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

15
src/hooks/refForm.ts Normal 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,
}
}

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

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

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

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

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

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

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

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

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