UNPKG

imobile_for_reactnative

Version:

iMobile for ReactNative,是SuperMap iMobile推出的一款基于React-Native框架的移动应用开发工具。基于该开发工具,用户可以使用JavaScript开发语言,开发出在Android和IOS操作系统下运行的原生移动GIS应用,入门门槛低,一次开发,处处运行。

364 lines (319 loc) 8.78 kB
import { EmitterSubscription, NativeEventEmitter, NativeModules } from "react-native" const FileTools = NativeModules.FileTools const nativeEvt = new NativeEventEmitter(FileTools) const MESSAGE_IMPORTEXTERNALDATA = 'com.supermap.RN.Mapcontrol.message_importexternaldata' const MESSAGE_SHARERESULT = 'com.supermap.RN.Mapcontrol.message_shareresult' export interface FileInfo { /** 文件或文件夹名 */ name: string type: 'directory' | 'file' } export interface FileFilter { name?: string extension?: string type?: 'file' | 'Directory' exclued?: string } export interface FiltedData { /** 不含根目录的路径 */ path: string name: string mtime: string isDirectory: boolean isTemplate?: boolean /** pxp文件中的3维场景类型 */ Type?: number } /** * 初始化文件目录 * @param username 用户名 */ async function initImageData(): Promise<boolean> { try { return FileTools.initImageData() } catch (error) { return false } } /** 获取本地存储的绝对路径 */ async function getHomeDirectory(): Promise<string> { return FileTools.getHomeDirectory() } /** 获取本地存储的绝对路径 */ async function appendingHomeDirectory(path?: string): Promise<string> { try { return await FileTools.getHomeDirectory() + (path !== undefined ? path : '') } catch (error) { return '' } } /** * 创建目录,目录存在时跳过 * @param path 绝对路径 */ async function createDirectory(path: string): Promise<boolean> { try { return FileTools.createDirectory(path) } catch (error) { return false } } /** * 创建文件 * @param path 文件的绝对路径 */ async function createFile(path: string): Promise<boolean> { return FileTools.createFile(path) } /** * 获取指定路径下的文件及文件夹 * @throws 可能抛异常 */ async function getDirectoryContent(path: string): Promise<Array<FileInfo>> { try { return FileTools.getDirectoryContent(path) } catch (error) { return [] } } /** * 获取图片/视频 url的绝对路径 * @param {*} contentUrl */ async function getContentAbsolutePath(contentUrl: string): Promise<string> { try { return await FileTools.getContentAbsolutePath(contentUrl) } catch (error) { return '' } } /** * 复制 assets 或 bundle 内的文件 * @param fileName asset里的文件名 * @param path 目标路径 */ async function copyAssetFile(fileName: string, path: string): Promise<boolean> { try { return FileTools.copyAssetFile(fileName, path) } catch (error) { return false } } /** * 复制文件,相同文件不覆盖 * @param override 是否覆盖相同文件,默认false * @throws */ async function copyFile(sourcePath: string, targetPath: string, override = false): Promise<boolean> { try { return FileTools.copyFile(sourcePath, targetPath, override) } catch (error) { return false } } async function copyFiles(fromPaths: string[] , toPath: string, override = false): Promise<string[]> { if (!fromPaths || fromPaths.length === 0 || !toPath) return [] return FileTools.copyFiles(fromPaths, toPath, override) } /** * 拷贝文件夹,并根据后缀不同拷贝到不同的文件夹下,过滤两级目录 * @param from 待拷贝文件路径 * @param to 将要拷贝到的文件路径 * @param filterFileSuffix 过滤文件后缀 * @param filterFileDicName 过滤的文件存放的文件夹名称 * @param otherFileDicName 其他文件存放的文件夹名称 * @param override 是否唯一,如果唯一需要覆盖文件,如果不唯一则加后缀_1、_2、... * @return true/false */ async function copyFilefilter(fromPaths: string, toPath: string, filter: { filterFileSuffix: string, filterFileDicName: string, otherFileDicName: string }, override = false): Promise<boolean> { if (!fromPaths || fromPaths.length === 0 || !toPath) return false return FileTools.copyFilefilter(fromPaths, toPath, filter, override) } async function copydir(fromPath: string, toPath: string): Promise<boolean> { if (!fromPath || !toPath) return false return FileTools.copydir(fromPath, toPath) } /** * 判断文件是否存在 */ async function fileIsExist(path: string): Promise<boolean> { try { return FileTools.fileIsExist(path) } catch (error) { return false } } /** * 判断文件是否存在在Home Directory中 * @param path * @returns {Promise} */ async function fileIsExistInHomeDirectory(path: string): Promise<boolean> { return await FileTools.fileIsExistInHomeDirectory(path) } /** * 压缩文件 */ async function zipFiles(pathArr: Array<string>, targetPath: string): Promise<boolean> { try { return FileTools.zipFiles(pathArr, targetPath) } catch (error) { return false } } /** * 压缩文件 * 传入的是需要压缩的文件目录 */ async function zipFile(path: string, targetPath: string): Promise<boolean> { try { return FileTools.zipFile(path, targetPath) } catch (error) { return false } } /** * 解压文件 * @throws 可能抛异常 */ async function unZipFile(zipPath: string, targetPath: string): Promise<boolean> { try { return FileTools.unZipFile(zipPath, targetPath) } catch (error) { return false } } /** * 删除文件或文件夹 */ async function deleteFile(path: string, suffix = ''): Promise<boolean> { try { return FileTools.deleteFile(path, suffix) } catch (error) { return false } } /** * 获取文件夹内容 * @param path * @returns {Promise} */ async function getPathList(path: string): Promise<{path: string, name: string, isDirectory: boolean}[]> { return await FileTools.getPathList(path) } /** * 根据过滤条件过滤文件夹内文件或文件夹 */ async function getPathListByFilter(path: string, filter: FileFilter): Promise<Array<FiltedData>> { try { return FileTools.getPathListByFilter(path, filter) } catch (error) { return [] } } /** * 遍历指定目录下指定类型文件 * @param path 文件目录 * @param extensions 类型 字符串形式,多个类型逗号隔开 如 "xml,shp" * @returns Array 返回值格式 [{name:'xxx',path:'xxx'}] */ async function getPathListByFilterDeep(path: string, extensions: string) { return await FileTools.getPathListByFilterDeep(path, extensions) } async function getMaps(path: string, filter?: FileFilter): Promise<Array<FiltedData>> { try { return FileTools.getMaps(path, filter) } catch (error) { return [] } } async function readFile(filePath: string): Promise<string> { return await FileTools.readFile(filePath) } async function writeFile(filePath: string, strJson: string): Promise<boolean> { return await FileTools.writeToFile(filePath, strJson) } /** * 判断是否是文件夹 * @param path */ async function isDirectory(path: string): Promise<boolean> { return await FileTools.isDirectory(path) } let listenerIMPORT: EmitterSubscription | null = null function addImportExternalData(handlers: {callback(e: boolean ): void}) { if ( handlers) { if (typeof handlers.callback === 'function') { listenerIMPORT?.remove() listenerIMPORT = nativeEvt.addListener(MESSAGE_IMPORTEXTERNALDATA, handlers.callback) } } } let listenerSHARERESULT: EmitterSubscription | null = null function getShareResult(handlers: {callback(e: boolean ): void}) { if (handlers) { if (typeof handlers.callback === 'function') { listenerSHARERESULT?.remove() listenerSHARERESULT = nativeEvt.addListener(MESSAGE_SHARERESULT, handlers.callback) } } } function getImportState(): Promise<boolean> { return FileTools.getImportState() } /** app启动时将跳转过来的uri所携带的数据复制到 Import 目录下 */ function handleUriData(): Promise<boolean> { return FileTools.handleUriData() } /** 加密 */ async function encode(data = ''): Promise<string> { try { if (!data) return '' return await FileTools.encode(data) } catch (e) { return '' } } /** 解密 */ async function decoder(data = ''): Promise<string> { try { if (!data) return '' return await FileTools.decoder(data) } catch (e) { return '' } } export default { getHomeDirectory, appendingHomeDirectory, createDirectory, createFile, getDirectoryContent, copyAssetFile, copyFile, fileIsExist, zipFiles, zipFile, unZipFile, deleteFile, getPathListByFilter, initImageData, getMaps, readFile, getContentAbsolutePath, copyFiles, copyFilefilter, copydir, fileIsExistInHomeDirectory, getPathList, getPathListByFilterDeep, writeFile, isDirectory, addImportExternalData, getShareResult, getImportState, handleUriData, encode, decoder, }