新增
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
import { reactive, toRaw } from 'vue'
|
||||
|
||||
import { getDictData } from '@/api/app'
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
// useDictData<{
|
||||
// dict: any[]
|
||||
// }>(['dict'])
|
||||
|
||||
export function useDictData<T = any>(dict: string) {
|
||||
const dictData: any = reactive({})
|
||||
const refresh = async () => {
|
||||
const data = await getDictData({
|
||||
type: dict
|
||||
})
|
||||
Object.assign(dictData, data)
|
||||
}
|
||||
refresh()
|
||||
|
||||
return {
|
||||
dictData: dictData as T,
|
||||
refresh
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import useTabsStore from '@/stores/modules/multipleTabs'
|
||||
import useSettingStore from '@/stores/modules/setting'
|
||||
|
||||
export default function useMultipleTabs() {
|
||||
const router = useRouter()
|
||||
const route = useRoute()
|
||||
const tabsStore = useTabsStore()
|
||||
const settingStore = useSettingStore()
|
||||
|
||||
const tabsLists = computed(() => {
|
||||
return tabsStore.getTabList
|
||||
})
|
||||
|
||||
const currentTab = computed(() => {
|
||||
return route.fullPath
|
||||
})
|
||||
|
||||
const addTab = () => {
|
||||
if (!settingStore.openMultipleTabs) return
|
||||
tabsStore.addTab(router)
|
||||
}
|
||||
|
||||
const removeTab = (fullPath?: any) => {
|
||||
if (!settingStore.openMultipleTabs) return
|
||||
fullPath = fullPath ?? route.fullPath
|
||||
tabsStore.removeTab(fullPath, router)
|
||||
}
|
||||
|
||||
const removeOtherTab = () => {
|
||||
if (!settingStore.openMultipleTabs) return
|
||||
tabsStore.removeOtherTab(route)
|
||||
}
|
||||
|
||||
const removeAllTab = () => {
|
||||
if (!settingStore.openMultipleTabs) return
|
||||
tabsStore.removeAllTab(router)
|
||||
}
|
||||
|
||||
return {
|
||||
tabsLists,
|
||||
currentTab,
|
||||
addTab,
|
||||
removeTab,
|
||||
removeOtherTab,
|
||||
removeAllTab
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import { reactive, toRaw } from 'vue'
|
||||
|
||||
// 分页钩子函数
|
||||
interface Options {
|
||||
page?: number
|
||||
size?: number
|
||||
fetchFun: (_arg: any) => Promise<any>
|
||||
params?: Record<any, any>
|
||||
fixedParams?: Record<any, any>
|
||||
firstLoading?: boolean
|
||||
}
|
||||
|
||||
export function usePaging(options: Options) {
|
||||
const {
|
||||
page = 1,
|
||||
size = 15,
|
||||
fetchFun,
|
||||
params = {},
|
||||
fixedParams = {},
|
||||
firstLoading = false
|
||||
} = options
|
||||
// 记录分页初始参数
|
||||
const paramsInit: Record<any, any> = Object.assign({}, toRaw(params))
|
||||
// 分页数据
|
||||
const pager = reactive({
|
||||
page,
|
||||
size,
|
||||
loading: firstLoading,
|
||||
count: 0,
|
||||
lists: [] as any[],
|
||||
extend: {} as Record<string, any>
|
||||
})
|
||||
// 请求分页接口
|
||||
const getLists = () => {
|
||||
pager.loading = true
|
||||
return fetchFun({
|
||||
page_no: pager.page,
|
||||
page_size: pager.size,
|
||||
...params,
|
||||
...fixedParams
|
||||
})
|
||||
.then((res: any) => {
|
||||
pager.count = res?.count
|
||||
pager.lists = res?.lists
|
||||
pager.extend = res?.extend
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -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