更新:登录功能
This commit is contained in:
43
uni_modules/wot-design-uni/components/common/interceptor.ts
Normal file
43
uni_modules/wot-design-uni/components/common/interceptor.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { isPromise } from './util'
|
||||
|
||||
function noop() {}
|
||||
|
||||
export type Interceptor = (...args: any[]) => Promise<boolean> | boolean | undefined | void
|
||||
|
||||
export function callInterceptor(
|
||||
interceptor: Interceptor | undefined,
|
||||
{
|
||||
args = [],
|
||||
done,
|
||||
canceled,
|
||||
error
|
||||
}: {
|
||||
args?: unknown[]
|
||||
done: () => void
|
||||
canceled?: () => void
|
||||
error?: () => void
|
||||
}
|
||||
) {
|
||||
if (interceptor) {
|
||||
// eslint-disable-next-line prefer-spread
|
||||
const returnVal = interceptor.apply(null, args)
|
||||
|
||||
if (isPromise(returnVal)) {
|
||||
returnVal
|
||||
.then((value) => {
|
||||
if (value) {
|
||||
done()
|
||||
} else if (canceled) {
|
||||
canceled()
|
||||
}
|
||||
})
|
||||
.catch(error || noop)
|
||||
} else if (returnVal) {
|
||||
done()
|
||||
} else if (canceled) {
|
||||
canceled()
|
||||
}
|
||||
} else {
|
||||
done()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user