mirror of
https://github.com/ialley-workshop-open/uni-halo.git
synced 2026-11-13 22:11:42 +08:00
feat: 新增公告模块、数据加载hook与多项体验优化
- 新增公告全流程功能:首页公告轮播、公告列表页、公告详情页 - 新增统一数据加载状态hook useDataLoading与配套测试 - 重构配置加载逻辑,新增统一bootstrap静态配置拉取 - 新增维护模式、恋爱模块支持与验证码防刷功能 - 优化投票卡片、联系页、关于页UI样式 - 新增测试页面与全局玻璃卡片样式 - 修复markdown容器内边距与首页生命周期逻辑
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import { mount } from '@vue/test-utils'
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { defineComponent, h } from 'vue'
|
||||
import { useDataLoading } from './useDataLoading'
|
||||
|
||||
/**
|
||||
* 在 Vue 应用上下文中运行 composable。
|
||||
* composable 的 ref/computed 只能在 setup() 内使用,
|
||||
* withSetup 通过挂载一个临时组件来提供这个上下文。
|
||||
*/
|
||||
function withSetup<T>(composableFn: () => T): T {
|
||||
let result!: T
|
||||
const Comp = defineComponent({
|
||||
setup() {
|
||||
result = composableFn()
|
||||
return () => h('div')
|
||||
},
|
||||
})
|
||||
const wrapper = mount(Comp)
|
||||
wrapper.unmount()
|
||||
return result
|
||||
}
|
||||
|
||||
describe('useDataLoading', () => {
|
||||
it('初始状态:status=loading, data=undefined', () => {
|
||||
const fetcher = vi.fn().mockResolvedValue('data')
|
||||
const { status, data } = withSetup(() => useDataLoading(fetcher))
|
||||
|
||||
expect(status.value).toBe('loading')
|
||||
expect(data.value).toBeUndefined()
|
||||
})
|
||||
|
||||
it('run 成功:status 从 loading 流转到 success,data 更新为返回值并触发 onSuccess', async () => {
|
||||
const fetcher = vi.fn().mockResolvedValue('result')
|
||||
const onSuccess = vi.fn()
|
||||
const { status, data, run } = withSetup(() => useDataLoading(fetcher, { onSuccess }))
|
||||
|
||||
const promise = run()
|
||||
expect(status.value).toBe('loading')
|
||||
|
||||
await promise
|
||||
expect(status.value).toBe('success')
|
||||
expect(data.value).toBe('result')
|
||||
expect(onSuccess).toHaveBeenCalledWith('result')
|
||||
})
|
||||
|
||||
it('run 返回空数组:status 流转到 empty', async () => {
|
||||
const fetcher = vi.fn().mockResolvedValue([])
|
||||
const { status, run } = withSetup(() => useDataLoading(fetcher))
|
||||
|
||||
await run()
|
||||
expect(status.value).toBe('empty')
|
||||
})
|
||||
|
||||
it('isEmpty 自定义判空:返回 true 时 status 为 empty', async () => {
|
||||
const fetcher = vi.fn(async (): Promise<{ total: number, items: number[] }> => ({ total: 0, items: [1] }))
|
||||
const { status, run } = withSetup(() => useDataLoading(fetcher, { isEmpty: data => data.total === 0 }))
|
||||
|
||||
await run()
|
||||
expect(status.value).toBe('empty')
|
||||
})
|
||||
|
||||
it('run 失败:不向外抛出异常,status 流转到 error 并触发 onError', async () => {
|
||||
const err = new Error('network error')
|
||||
const onError = vi.fn()
|
||||
const fetcher = vi.fn().mockRejectedValue(err)
|
||||
const { status, data, run } = withSetup(() => useDataLoading(fetcher, { onError }))
|
||||
|
||||
await expect(run()).resolves.toBeUndefined()
|
||||
|
||||
expect(status.value).toBe('error')
|
||||
expect(data.value).toBeUndefined()
|
||||
expect(onError).toHaveBeenCalledWith(err)
|
||||
})
|
||||
|
||||
it('带参数 run:参数透传给 fetcher', async () => {
|
||||
const fetcher = vi.fn(async (name: string) => `hi ${name}`)
|
||||
const { data, run } = withSetup(() => useDataLoading(fetcher))
|
||||
|
||||
await run('world')
|
||||
expect(fetcher).toHaveBeenCalledWith('world')
|
||||
expect(data.value).toBe('hi world')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,69 @@
|
||||
import type { Ref } from 'vue'
|
||||
import { ref } from 'vue'
|
||||
|
||||
/** 数据加载四态(取值与旧 DataLoadingStatusEnum 一致,二者可互相兼容) */
|
||||
export type DataLoadingStatus = 'loading' | 'error' | 'empty' | 'success'
|
||||
|
||||
export interface IUseDataLoadingOptions<T> {
|
||||
/** 自定义判空;默认:数组看长度、对象看键数、空值视为空 */
|
||||
isEmpty?: (data: T) => boolean
|
||||
/** 成功回调(数据就绪) */
|
||||
onSuccess?: (data: T) => void
|
||||
/** 失败回调(run 已捕获异常,不会向外抛出) */
|
||||
onError?: (err: unknown) => void
|
||||
}
|
||||
|
||||
export interface IUseDataLoadingReturn<T, P extends unknown[]> {
|
||||
data: Ref<T | undefined>
|
||||
status: Ref<DataLoadingStatus>
|
||||
run: (...args: P) => Promise<T | undefined>
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据加载状态机
|
||||
* 页面只提供请求函数,loading/empty/success/error 状态由 hook 接管,
|
||||
* 取代「手工 ref + try/catch 里逐个搬运状态」的写法。
|
||||
*
|
||||
* @example
|
||||
* const { data, status, run } = useDataLoading(() => getMomentByName(name))
|
||||
* onLoad(() => run())
|
||||
* onPullDownRefresh(async () => { await run(); uni.stopPullDownRefresh() })
|
||||
*/
|
||||
export function useDataLoading<T, P extends unknown[] = []>(
|
||||
fetcher: (...args: P) => Promise<T>,
|
||||
options: IUseDataLoadingOptions<T> = {},
|
||||
): IUseDataLoadingReturn<T, P> {
|
||||
const { isEmpty, onSuccess, onError } = options
|
||||
|
||||
const data = ref<T | undefined>(undefined) as Ref<T | undefined>
|
||||
const status = ref<DataLoadingStatus>('loading')
|
||||
|
||||
function calcEmpty(value: T): boolean {
|
||||
if (isEmpty)
|
||||
return isEmpty(value)
|
||||
if (Array.isArray(value))
|
||||
return value.length === 0
|
||||
if (value !== null && typeof value === 'object')
|
||||
return Object.keys(value).length === 0
|
||||
return value === undefined || value === null || value === ''
|
||||
}
|
||||
|
||||
async function run(...args: P): Promise<T | undefined> {
|
||||
status.value = 'loading'
|
||||
try {
|
||||
const res = await fetcher(...args)
|
||||
data.value = res
|
||||
status.value = calcEmpty(res) ? 'empty' : 'success'
|
||||
onSuccess?.(res)
|
||||
return res
|
||||
}
|
||||
catch (err) {
|
||||
console.error('[useDataLoading] 请求失败', err)
|
||||
status.value = 'error'
|
||||
onError?.(err)
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
return { data, status, run }
|
||||
}
|
||||
@@ -1,18 +1,25 @@
|
||||
export enum DataLoadingStatusEnum {
|
||||
Loading = 'loading',
|
||||
Error = 'error',
|
||||
Empty = 'empty',
|
||||
Success = 'success',
|
||||
}
|
||||
import type { DataLoadingStatus } from './useDataLoading'
|
||||
|
||||
export function useDataLoadingStatus(status?:DataLoadingStatusEnum) {
|
||||
const loadingStatus = ref<DataLoadingStatusEnum>(status??DataLoadingStatusEnum.Loading)
|
||||
/**
|
||||
* @deprecated 请改用 useDataLoading:状态由 hook 内部流转,
|
||||
* 无需再在 try/catch 里手工调用 updateLoadingStatus 搬运状态。
|
||||
* 本文件保留仅为兼容存量页面(category/article-detail 等),后续逐步迁移。
|
||||
*/
|
||||
export const DataLoadingStatusEnum = {
|
||||
Loading: 'loading',
|
||||
Error: 'error',
|
||||
Empty: 'empty',
|
||||
Success: 'success',
|
||||
} as const
|
||||
|
||||
export function useDataLoadingStatus(status?: DataLoadingStatus) {
|
||||
const loadingStatus = ref<DataLoadingStatus>(status ?? DataLoadingStatusEnum.Loading)
|
||||
|
||||
function resetLoadingStatus() {
|
||||
loadingStatus.value = DataLoadingStatusEnum.Loading
|
||||
}
|
||||
|
||||
function updateLoadingStatus(newStatus: DataLoadingStatusEnum) {
|
||||
function updateLoadingStatus(newStatus: DataLoadingStatus) {
|
||||
loadingStatus.value = newStatus
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user