import { Platform } from 'react-native'; import { Unimp } from './NativeUniMP'; import type { IAppVersionInfoProps, ICapsuleBtnStyleProps, IConfigurationProps, InitializeProps, UniMPEventData, } from './types'; /** * 初始化小程序SDK * @param params 初始化参数 * @param capsuleBtnStyle 胶囊按钮样式 */ export function initialize( params: InitializeProps, capsuleBtnStyle?: ICapsuleBtnStyleProps ): Promise { return Unimp.initialize(params, capsuleBtnStyle); } /** * 校验小程序SDK是否已初始化(Android) */ export function isInitialize(): Promise { // 只支持Android if (Platform.OS === 'android') { return Unimp.isInitialize(); } return Promise.resolve(true); } /** * 获取小程序运行路径 * @param appid 小程序appid */ export function getAppBasePath(appid?: string): Promise { if (Platform.OS === 'android') { return Unimp.getAppBasePath(); } else { if (!appid) { return Promise.reject({ message: 'appid不能为空' }); } return Unimp.getAppBasePath(appid); } } /** * 将wgt包中的资源文件释放到uni小程序运行时路径下 * @param appid 小程序appid * @param wgtPath 小程序应用资源包路径 仅支持SD路径 不支持assets * @param password 资源包解压密码(猜的) */ export function releaseWgtToRunPath( appid: string, wgtPath?: string | null, password?: string ): Promise { return Unimp.releaseWgtToRunPath(appid, wgtPath, password); } /** * 检查当前appid小程序是否已释放wgt资源 * 可用来检查当前appid资源是否存在 * @param appid 小程序appid */ export function isExistsApp(appid: string): Promise { return Unimp.isExistsApp(appid); } /** * 读取导入到工程中的wgt应用资源 * @param appid 小程序appid */ export function getWgtPath(appid: string): Promise { return Unimp.getWgtPath(appid); } /** * 启动小程序 * @param appid uni小程序应用id * @param configuration uni小程序应用配置 */ export async function openUniMP( appid: string, configuration?: IConfigurationProps ): Promise { try { if (Platform.OS === 'android') { return Unimp.openUniMP(appid, configuration); } else { const isExists = await isExistsApp(appid); if (!isExists) { const wgtPath = await Unimp.getResourceFilePath(appid); if (!wgtPath) { return Promise.reject({ message: `未找到 ${appid} 的资源路径` }); } await releaseWgtToRunPath(appid, wgtPath); } return Unimp.openUniMP(appid, configuration); } } catch (error) { return Promise.reject(error); } } /** * 手动关闭小程序 * @param appid 小程序ID */ export async function closeUniMP(appid: string): Promise { return Unimp.closeUniMP(appid); } /** * 当前小程序显示到前台/退到后台 * @param appid 小程序ID * @param show 显示或隐藏 */ export async function showOrHideUniMP( appid: string, show: boolean ): Promise { return Unimp.showOrHideUniMP(appid, show); } /** * 宿主主动触发事件到正在运行的小程序 * @param appid 小程序ID * @param eventName 显示或隐藏 * @param data 传参 */ export async function sendUniMPEvent( appid: string, eventName: string, data: Record ): Promise { return Unimp.sendUniMPEvent(appid, eventName, data); } /** * 设置监听小程序发送给宿主的事件 */ export function setOnUniMPEventCallBack(): void { Unimp.setOnUniMPEventCallBack(); } /** * 将宿主的处理结果单次回调给小程序 * @param callbackId onEventReceive 事件中携带的回调标识 * @param responseData 回调数据 */ export function invokeUniMPEventCallback( callbackId: string, responseData: UniMPEventData ): Promise { return Unimp.invokeUniMPEventCallback(callbackId, responseData); } /** * 获取uni小程序版本信息 * @param appid uni小程序应用id */ export function getAppVersionInfo( appid: string ): Promise { return Unimp.getAppVersionInfo(appid); } /** * 获取运行时uni小程序的当前页面url 可用于页面直达等操作的地址 * @param appid 当前运行的小程序应用id */ export function getCurrentPageUrl(appid: string): Promise { return Unimp.getCurrentPageUrl(appid); }