import { getCurrentPage, navigator } from '@dcloudio/uni-runtime'; import { HideNavigationBarLoading, HideNavigationBarLoadingOptions, HideNavigationBarLoadingSuccess, SetNavigationBarColor, SetNavigationBarColorOptions, SetNavigationBarColorSuccess, SetNavigationBarTitle, SetNavigationBarTitleOptions, SetNavigationBarTitleSuccess, ShowNavigationBarLoading, ShowNavigationBarLoadingOptions, ShowNavigationBarLoadingSuccess } from '../interface.uts'; import { API_SET_NAVIGATION_BAR_COLOR, API_SET_NAVIGATION_BAR_TITLE, SetNavigationBarColorProtocol, SetNavigationBarTitleProtocol } from '../protocol.uts'; export { SetNavigationBarColor, SetNavigationBarColorOptions, SetNavigationBarColorSuccess, SetNavigationBarTitle, SetNavigationBarTitleOptions, SetNavigationBarTitleSuccess, ShowNavigationBarLoading, ShowNavigationBarLoadingOptions, ShowNavigationBarLoadingSuccess, HideNavigationBarLoading, HideNavigationBarLoadingOptions, HideNavigationBarLoadingSuccess } interface TitleNView { titleText: string; autoBackButton?: boolean loading?: boolean } interface BackButton { color?: string } interface PlusWebviewWebviewTitleNViewStyles { backgroundColor?: string titleColor?: string, titleNView?: TitleNView backButton?: BackButton } interface Webview { getStyle: () => PlusWebviewWebviewTitleNViewStyles | null; setStyle: (style: PlusWebviewWebviewTitleNViewStyles) => void; }; interface $page { statusBarStyle: 'dark' | 'light' } interface Page { $getAppWebview: () => Webview | null; $page: $page } const getWebview = (page: Page): Webview | null => { const webview = page.$getAppWebview() return webview } export const setNavigationBarColor: SetNavigationBarColor = defineAsyncApi( API_SET_NAVIGATION_BAR_COLOR, ( args: SetNavigationBarColorOptions, executor: ApiExecutor ) => { const page = getCurrentPage() as Page if (!page) { return executor.reject(`getCurrentPages is empty`) } const webview = getWebview(page) if (webview) { const styles: PlusWebviewWebviewTitleNViewStyles = {} if (args.frontColor) { styles.titleColor = args.frontColor } if (args.backgroundColor) { styles.backgroundColor = args.backgroundColor } const statusBarStyle = args.frontColor === '#000000' ? 'dark' : 'light' try { navigator.setStatusBarStyle(statusBarStyle) } catch (error) { } // 用户调用api时同时改变当前页配置,这样在系统调用设置时,可以避免覆盖用户设置 page.$page.statusBarStyle = statusBarStyle const style = webview.getStyle() if (style && style.titleNView) { if (style.titleNView.autoBackButton) { styles.backButton = styles.backButton || {} styles.backButton.color = args.frontColor } webview.setStyle({ titleNView: styles as TitleNView, } as PlusWebviewWebviewTitleNViewStyles) } executor.resolve() } else { executor.reject() } }, SetNavigationBarColorProtocol ) as SetNavigationBarColor export const setNavigationBarTitle: SetNavigationBarTitle = defineAsyncApi( API_SET_NAVIGATION_BAR_TITLE, (args: SetNavigationBarTitleOptions, executor: ApiExecutor) => { const page = getCurrentPage() as Page if (!page) { return executor.reject(`getCurrentPages is empty`) } const webview = getWebview(page) if (webview) { const style = webview.getStyle() if (style && style.titleNView) { webview.setStyle({ titleNView: { titleText: args.title, } as TitleNView, } as PlusWebviewWebviewTitleNViewStyles) } executor.resolve() } else { executor.reject() } }, SetNavigationBarTitleProtocol ) as SetNavigationBarTitle export const showNavigationBarLoading: ShowNavigationBarLoading = defineAsyncApi( API_SET_NAVIGATION_BAR_TITLE, (args: ShowNavigationBarLoadingOptions, executor: ApiExecutor) => { const page = getCurrentPage() as Page if (!page) { return executor.reject(`getCurrentPages is empty`) } const webview = getWebview(page) if (webview) { const style = webview.getStyle() if (style && style.titleNView) { webview.setStyle({ titleNView: { loading: true, } as TitleNView, } as PlusWebviewWebviewTitleNViewStyles) } executor.resolve() } else { executor.reject() } } ) as ShowNavigationBarLoading export const hideNavigationBarLoading: HideNavigationBarLoading = defineAsyncApi( API_SET_NAVIGATION_BAR_TITLE, (args: HideNavigationBarLoadingOptions, executor: ApiExecutor) => { const page = getCurrentPage() as Page if (!page) { return executor.reject(`getCurrentPages is empty`) } const webview = getWebview(page) if (webview) { const style = webview.getStyle() if (style && style.titleNView) { webview.setStyle({ titleNView: { loading: false, } as TitleNView, } as PlusWebviewWebviewTitleNViewStyles) } executor.resolve() } else { executor.reject() } } ) as HideNavigationBarLoading