mirror of
https://gitee.com/likeadmin/likeadmin_java.git
synced 2026-05-31 06:02:57 +08:00
新版底层提交
This commit is contained in:
69
admin/src/hooks/useDictOptions.ts
Normal file
69
admin/src/hooks/useDictOptions.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { dictDataAll } from '@/api/setting/dict'
|
||||
import { reactive, toRaw } from 'vue'
|
||||
|
||||
interface Options {
|
||||
[propName: string]: {
|
||||
api: PromiseFun
|
||||
params?: Record<string, any>
|
||||
transformData?(data: any): any
|
||||
}
|
||||
}
|
||||
|
||||
// {
|
||||
// dict: {
|
||||
// api: dictData,
|
||||
// params: { name: 'user' },
|
||||
// transformData(data: any) {
|
||||
// return data.list
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
export function useDictOptions<T = any>(options: Options) {
|
||||
const optionsData: any = reactive({})
|
||||
const optionsKey = Object.keys(options)
|
||||
const apiLists = optionsKey.map((key) => {
|
||||
const value = options[key]
|
||||
optionsData[key] = []
|
||||
return () => value.api(toRaw(value.params) || {})
|
||||
})
|
||||
|
||||
const refresh = async () => {
|
||||
const res = await Promise.allSettled<Promise<any>>(apiLists.map((api) => api()))
|
||||
res.forEach((item, index) => {
|
||||
const key = optionsKey[index]
|
||||
if (item.status == 'fulfilled') {
|
||||
const { transformData } = options[key]
|
||||
const data = transformData ? transformData(item.value) : item.value
|
||||
optionsData[key] = data
|
||||
}
|
||||
})
|
||||
}
|
||||
refresh()
|
||||
return {
|
||||
optionsData: optionsData as T,
|
||||
refresh
|
||||
}
|
||||
}
|
||||
|
||||
// useDictOptions<{
|
||||
// dict: any[]
|
||||
// }>({
|
||||
// dict: dictData
|
||||
// })
|
||||
|
||||
export function useDictData<T = any>(dict: string[]) {
|
||||
const options: Options = {}
|
||||
for (const type of dict) {
|
||||
options[type] = {
|
||||
api: dictDataAll,
|
||||
params: {
|
||||
dictType: type
|
||||
}
|
||||
}
|
||||
}
|
||||
const { optionsData } = useDictOptions<T>(options)
|
||||
return {
|
||||
dictData: optionsData
|
||||
}
|
||||
}
|
||||
21
admin/src/hooks/useLockFn.ts
Normal file
21
admin/src/hooks/useLockFn.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { ref } from 'vue'
|
||||
|
||||
export function useLockFn(fn: (...args: any[]) => Promise<any>) {
|
||||
const isLock = ref(false)
|
||||
const lockFn = async (...args: any[]) => {
|
||||
if (isLock.value) return
|
||||
isLock.value = true
|
||||
try {
|
||||
const res = await fn(...args)
|
||||
isLock.value = false
|
||||
return res
|
||||
} catch (e) {
|
||||
isLock.value = false
|
||||
throw e
|
||||
}
|
||||
}
|
||||
return {
|
||||
isLock,
|
||||
lockFn
|
||||
}
|
||||
}
|
||||
62
admin/src/hooks/usePaging.ts
Normal file
62
admin/src/hooks/usePaging.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { reactive, toRaw } from 'vue'
|
||||
|
||||
// 分页钩子函数
|
||||
interface Options {
|
||||
page?: number
|
||||
size?: number
|
||||
fetchFun: (_arg: any) => Promise<any>
|
||||
params?: Record<any, any>
|
||||
firstLoading?: boolean
|
||||
}
|
||||
|
||||
export function usePaging(options: Options) {
|
||||
const { page = 1, size = 15, fetchFun, params = {}, firstLoading = false } = options
|
||||
// 记录分页初始参数
|
||||
const paramsInit: Record<any, any> = Object.assign({}, toRaw(params))
|
||||
// 分页数据
|
||||
const pager = reactive({
|
||||
page,
|
||||
size,
|
||||
loading: firstLoading,
|
||||
count: 0,
|
||||
lists: [] as any[]
|
||||
})
|
||||
// 请求分页接口
|
||||
const getLists = () => {
|
||||
pager.loading = true
|
||||
return fetchFun({
|
||||
pageNo: pager.page,
|
||||
pageSize: pager.size,
|
||||
...params
|
||||
})
|
||||
.then((res: any) => {
|
||||
pager.count = res?.count
|
||||
pager.lists = res?.lists
|
||||
return Promise.resolve(res)
|
||||
})
|
||||
.catch((err: any) => {
|
||||
return Promise.reject(err)
|
||||
})
|
||||
.finally(() => {
|
||||
pager.loading = false
|
||||
})
|
||||
}
|
||||
// 重置为第一页
|
||||
const resetPage = () => {
|
||||
pager.page = 1
|
||||
getLists()
|
||||
}
|
||||
// 重置参数
|
||||
const resetParams = () => {
|
||||
Object.keys(paramsInit).forEach((item) => {
|
||||
params[item] = paramsInit[item]
|
||||
})
|
||||
getLists()
|
||||
}
|
||||
return {
|
||||
pager,
|
||||
getLists,
|
||||
resetParams,
|
||||
resetPage
|
||||
}
|
||||
}
|
||||
17
admin/src/hooks/useWatchRoute.ts
Normal file
17
admin/src/hooks/useWatchRoute.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import type { RouteLocationNormalizedLoaded } from 'vue-router'
|
||||
|
||||
export function useWatchRoute(callback: (route: RouteLocationNormalizedLoaded) => void) {
|
||||
const route = useRoute()
|
||||
watch(
|
||||
route,
|
||||
() => {
|
||||
callback(route)
|
||||
},
|
||||
{
|
||||
immediate: true
|
||||
}
|
||||
)
|
||||
return {
|
||||
route
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user