71 lines
1.7 KiB
TypeScript
71 lines
1.7 KiB
TypeScript
/**
|
||
* 桥接层统一入口
|
||
*
|
||
* 导出类型定义、环境检测器以及各环境提供者,
|
||
* 并提供 createBridge() 工厂函数自动选择合适的提供者实例。
|
||
*
|
||
* @module bridge
|
||
*/
|
||
|
||
// ── 类型定义 ──
|
||
export type {
|
||
IBridge,
|
||
GetLocationOptions,
|
||
LocationResult,
|
||
ScanType,
|
||
ScanCodeOptions,
|
||
ScanCodeResult,
|
||
OpenMapParams,
|
||
ImageSourceType,
|
||
ChooseImageOptions,
|
||
ChooseImageResult,
|
||
DeviceInfo,
|
||
} from './types'
|
||
|
||
// ── 环境检测 ──
|
||
export {
|
||
detectEnvironment,
|
||
isWechatMiniProgram,
|
||
isUniappWebView,
|
||
isBrowser,
|
||
} from './detector'
|
||
export type { Environment } from './detector'
|
||
|
||
// ── 提供者 ──
|
||
export { default as browserProvider } from './providers/browser'
|
||
|
||
// ── 工厂函数 ──
|
||
import type { IBridge } from './types'
|
||
import { detectEnvironment } from './detector'
|
||
import browserProvider from './providers/browser'
|
||
|
||
/**
|
||
* 根据当前运行环境自动创建对应的 Bridge 实例
|
||
*
|
||
* - 普通浏览器:返回 browserProvider(基于 Web API 降级实现)
|
||
* - 微信小程序 WebView:待实现
|
||
* - uni-app WebView:待实现
|
||
*
|
||
* @returns 符合 IBridge 接口的提供者实例
|
||
*/
|
||
export function createBridge(): IBridge {
|
||
const env = detectEnvironment()
|
||
|
||
switch (env) {
|
||
case 'wechat-mp':
|
||
// TODO: 引入 WechatProvider 后替换
|
||
throw new Error('微信小程序 WebView Bridge 尚未实现')
|
||
|
||
case 'uniapp-webview':
|
||
// TODO: 引入 UniappProvider 后替换
|
||
throw new Error('uni-app WebView Bridge 尚未实现')
|
||
|
||
case 'browser':
|
||
default:
|
||
return browserProvider
|
||
}
|
||
}
|
||
|
||
/** 当前环境的 Bridge 单例,页面直接使用 */
|
||
export const bridge: IBridge = createBridge()
|