UNPKG

imobile_for_reactnative

Version:

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

423 lines (377 loc) 12.3 kB
import { NativeModules, Platform, NativeEventEmitter, DeviceEventEmitter, EmitterSubscription } from 'react-native' import { EventConst } from '../../constains' import URL, {ONLINE_URL} from './url' import { request } from '../utils' import { QueryParamToStr } from './UrlUtils' const OnlineServiceNative = NativeModules.SOnlineService interface OnlineUploadFile { /** 上传文件路径 */ path: string, dataName: string, } /*获取ios原生层的回调*/ const callBackIOS = new NativeEventEmitter(OnlineServiceNative) export function init () { OnlineServiceNative.init() } export function login (userName: string, password: string) { if (userName === undefined || password === undefined) { return } return OnlineServiceNative.login(userName, password) } export function logout () { return OnlineServiceNative.logout() } /** * 获取OnlineService cookie * 仅支持android * @returns 返回OnlineService cookie */ export async function getAndroidSessionID (): Promise<string> { if (Platform.OS === 'android') { return OnlineServiceNative.getSessionID() } return '' } /** * 同步Android cookie * 仅支持android * @returns 返回是否同步成功 */ export async function syncAndroidCookie(): Promise<boolean> { if (Platform.OS === 'android') { return OnlineServiceNative.syncCookie('https://www.supermapol.com/') } return true } // /** // * 储存Image cache // * 仅支持android // * @param imageUrl image url // * @param saveFileName 存储image的名字 // * @returns 返回cache路径 // */ // export function _cacheImage (imageUrl: string, saveFileName: string) { // if (Platform.OS === 'android') { // return OnlineServiceNative.cacheImage(imageUrl, saveFileName) // } // return '不支持ios的缓存' // } /** * 移除所有cookies */ export function removeCookie (): void { OnlineServiceNative.removeCookie() } let uploadFileListener: EmitterSubscription, uploadingFileListener: EmitterSubscription, uploadFileFailListener: EmitterSubscription export function uploadFile (path: string, dataName: string, handler) { uploadFileListener && uploadFileListener.remove() uploadingFileListener && uploadingFileListener.remove() uploadFileFailListener && uploadFileFailListener.remove() if (handler) { if (typeof handler.onProgress === 'function') { uploadingFileListener = callBackIOS.addListener(EventConst.ONLINE_SERVICE_UPLOADING, function (obj) { handler.onProgress(obj.progress) }) } if (typeof handler.onResult === 'function') { uploadFileListener = callBackIOS.addListener(EventConst.ONLINE_SERVICE_UPLOADED, function (value) { handler.onResult(value) }) } if (typeof handler.onResult === 'function') { uploadFileFailListener = callBackIOS.addListener(EventConst.ONLINE_SERVICE_UPLOADFAILURE, function (value) { handler.onResult(value) }) } } return OnlineServiceNative.upload(path, dataName) } export function uploadFilebyType (path, dataName,dataType, handler) { uploadFileListener && uploadFileListener.remove() uploadingFileListener && uploadingFileListener.remove() if (handler) { if (typeof handler.onProgress === 'function') { uploadingFileListener = callBackIOS.addListener(EventConst.ONLINE_SERVICE_UPLOADING, function (obj) { handler.onProgress(obj.progress) }) } if (typeof handler.onResult === 'function') { uploadFileListener = callBackIOS.addListener(EventConst.ONLINE_SERVICE_UPLOADED, function (value) { handler.onResult(value) }) } } return OnlineServiceNative.uploadByType(path, dataName, dataType) } let downloadingListener let downloadedListener let downloadfailureListener export function downloadFileWithCallBack(path, dataName, handler) { downloadingListener && downloadingListener.remove() downloadedListener && downloadedListener.remove() downloadfailureListener && downloadfailureListener.remove() if (handler) { if (typeof handler.onProgress === 'function') { downloadingListener = callBackIOS.addListener(EventConst.ONLINE_SERVICE_DOWNLOADING, function (obj) { handler.onProgress(obj.progress); }) } if (typeof handler.onResult === 'function') { downloadedListener = callBackIOS.addListener(EventConst.ONLINE_SERVICE_DOWNLOADED, function (value) { handler.onResult(value); }) } if (typeof handler.onResult === 'function') { downloadfailureListener = callBackIOS.addListener(EventConst.ONLINE_SERVICE_DOWNLOADFAILURE, function (value) { handler.onResult(value); }) } } OnlineServiceNative.download(path, dataName) } export function downloadFile (path, onlineDataName) { OnlineServiceNative.download(path, onlineDataName) } export function cancelDownload () { if (Platform.OS === 'ios') { OnlineServiceNative.cancelDownload() } } export function downloadFileWithDataId (path, dataNameId) { OnlineServiceNative.downloadWithDataId(path, dataNameId) } export function downloadFileWithUrl (path, url, dataNameId) { OnlineServiceNative.downloadWithUrl(path, url, dataNameId) } export function getUserInfo () { return OnlineServiceNative.getUserInfo() } export function getUserInfoBy (name, type) { return OnlineServiceNative.getUserInfoBy(name, type) } export function loginWithPhoneNumber (phoneNumber, password) { if (phoneNumber === undefined || password === undefined) { return } return OnlineServiceNative.loginWithPhone(phoneNumber, password) } export function getDataList (currentPage, pageSize) { if (currentPage === undefined || pageSize === undefined) { return } return OnlineServiceNative.getDataList(currentPage, pageSize) } export async function getMyContentData(params) { try { let url = ONLINE_URL + URL.GetMyAccountData[0] + QueryParamToStr(params) let headers = {} let cookie = await getCookie() if (Platform.OS !== 'ios' && cookie) { headers.cookie = cookie } let result = await request(url, URL.GetMyAccountData[1], { headers: headers, body: {}, }) return result } catch (error) { } } export function getServiceList (currentPage, pageSize) { if (currentPage === undefined || pageSize === undefined) { return } return OnlineServiceNative.getServiceList(currentPage, pageSize) } export function registerWithEmail (email, nickname, password) { if (email === undefined || nickname === undefined || password === undefined) { return } return OnlineServiceNative.registerWithEmail(email, nickname, password) } export function registerWithPhone (phoneNumber, smsVerifyCode, nickname, password) { if (phoneNumber === undefined || smsVerifyCode === undefined || nickname === undefined || password === undefined) { return } return OnlineServiceNative.registerWithPhone(phoneNumber, smsVerifyCode, nickname, password) } export function sendSMSVerifyCode (phoneNumber) { if (phoneNumber === undefined) { return } return OnlineServiceNative.sendSMSVerifyCode(phoneNumber) } export function verifyCodeImage () { return OnlineServiceNative.verifyCodeImage() } export function retrievePassword (account, verifyCode, isPhoneAccount) { if (account === undefined || verifyCode === undefined || isPhoneAccount === undefined) { return } return OnlineServiceNative.retrievePassword(account, verifyCode, isPhoneAccount) } export function retrievePasswordSecond (firstResult) { if (firstResult === undefined) { return } return OnlineServiceNative.retrievePasswordSecond(firstResult) } export function retrievePasswordThrid (secondResult, safeCode) { if (secondResult === undefined || safeCode === undefined) { return } return OnlineServiceNative.retrievePasswordThrid(secondResult, safeCode) } export function retrievePasswordFourth (thridResult, newPassword) { if (thridResult === undefined || newPassword === undefined) { return } return OnlineServiceNative.retrievePasswordFourth(thridResult, newPassword) } export function deleteData (dataName) { if (dataName === undefined) { return } return OnlineServiceNative.deleteData(dataName) } export function deleteDataWithDataId (dataNameId) { if (dataNameId === undefined) { return } return OnlineServiceNative.deleteDataWithDataId(dataNameId) } export function deleteService (serviceName) { if (serviceName === undefined) { return } return OnlineServiceNative.deleteServiceWithServiceName(serviceName) } export function deleteServiceWithDataName (dataName) { if (dataName === undefined) { return } return OnlineServiceNative.deleteService(dataName) } export function deleteServiceWithServiceId (serviceId) { if (serviceId === undefined) { return } return OnlineServiceNative.deleteServiceWithServiceId(serviceId) } export function changeDataVisibility (dataName, isPublic) { if (dataName === undefined || isPublic === undefined) { return } return OnlineServiceNative.changeDataVisibility(dataName, isPublic) } export function changeDataVisibilityWithDataId (dataNameId, isPublic) { if (dataNameId === undefined || isPublic === undefined) { return } return OnlineServiceNative.changeDataVisibilityWithDataId(dataNameId, isPublic) } export function changeServiceVisibility (serviceName, isPublic) { if (serviceName === undefined || isPublic === undefined) { return } return OnlineServiceNative.changeServiceVisibility(serviceName, isPublic) } export function changeServiceVisibilityWithServiceId (serviceNameId, isPublic) { if (serviceNameId === undefined || isPublic === undefined) { return } return OnlineServiceNative.changeServiceVisibilityWithServiceId(serviceNameId, isPublic) } export function getAllUserDataList (currentPage) { if (currentPage === undefined) { return } return OnlineServiceNative.getAllUserDataList(currentPage) } export function getAllUserSymbolLibList (currentPage) { if (currentPage === undefined) { return } return OnlineServiceNative.getAllUserSymbolLibList(currentPage) } export function publishService (dataName) { if (dataName === undefined) { return } return OnlineServiceNative.publishService(dataName) } export function publishServiceWithDataId (dataId) { if (dataId === undefined) { return } return OnlineServiceNative.publishServiceWithDataId(dataId) } export function modifyPassword (oldPassword, newPassword) { return OnlineServiceNative.modifyPassword(oldPassword, newPassword) } export function modifyNickname (nickname) { return OnlineServiceNative.modifyNickname(nickname) } export function sendVerficationCode (phoneNumber) { return OnlineServiceNative.sendVerficationCode(phoneNumber) } export function bindPhoneNumber (phoneNumber, verifyCode) { return OnlineServiceNative.bindPhoneNumber(phoneNumber, verifyCode) } export function bindEmail (email) { return OnlineServiceNative.bindEmail(email) } export function getSuperMapKnown () { return OnlineServiceNative.getSuperMapKnown() } let reverseGeocodingListener export function reverseGeocoding (longitude, latitude, handler) { reverseGeocodingListener && reverseGeocodingListener.remove() if (typeof handler.onResult === 'function' && handler) { if (Platform.OS === 'ios') { reverseGeocodingListener = callBackIOS.addListener(EventConst.ONLINE_SERVICE_REVERSEGEOCODING, function (result) { handler.onResult(result) }) } else { reverseGeocodingListener = DeviceEventEmitter.addListener(EventConst.ONLINE_SERVICE_REVERSEGEOCODING, function (result) { handler.onResult(result) }) } } return OnlineServiceNative.reverseGeocoding(longitude, latitude) } export function loginWithParam (url, cookie, params) { if(Platform.OS === 'android') { if(!cookie) { cookie = '' } return OnlineServiceNative.loginWithParam(url, cookie, params) } else { return OnlineServiceNative.loginWithParam(url, params) } } export function getCookie () { return OnlineServiceNative.getCookie() } export function setOnlineServiceSite(site) { return OnlineServiceNative.setOnlineServiceSite(site) }