/** * Sample React Native App * https://github.com/facebook/react-native * * @format * @flow strict-local */ import React, {useState, useEffect} from 'react'; import type {Node} from 'react'; import BackgroundTimer from 'react-native-background-timer'; const appId = '6260bf62e680b10001b1911f'; import MopSDK from 'react-native-mopsdk'; import { SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, useColorScheme, Button, View, Platform, NativeModules, Alert, NativeEventEmitter, } from 'react-native'; import ScanScreen from './scanner.js'; let isInited = false; const openApplet = () => { MopSDK.openApplet({appId}); }; const getCurrentApplet = callback => { let time = 0; BackgroundTimer.runBackgroundTimer(() => { if (time === 0) { console.log('getCurrentApplet call'); MopSDK.currentApplet().then(res => { console.log('currentApplet', res); callback(JSON.stringify(res)); }); time++; } }, 10000); }; const closeApplet = () => { let time = 0; BackgroundTimer.runBackgroundTimer(() => { if (time === 0) { console.log('closeApplet call'); MopSDK.closeApplet(appId, true); time++; } }, 10000); }; const closeAllApplets = () => { let time = 0; BackgroundTimer.runBackgroundTimer(() => { if (time === 0) { Alert.alert('closeAllApplets call'); MopSDK.closeAllApplets(); time++; } }, 10000); }; const qrcodeOpenApplet = qrcode => { console.warn('当前扫码', qrcode); console.log('qrcodeOpenApplet call'); MopSDK.qrcodeOpenApplet(qrcode); }; const clearApplets = () => { console.log('clearApplets call'); MopSDK.clearApplets(); }; const registerAppletHandler = () => { const handler = { forwardApplet(params) { console.log('forwardApplet call', params); return []; }, getUserInfo(params) { console.log('getUserInfo call', params); return { name: 'jimmy', nickName: 'jimmy 123', avatarUrl: 'https://img9.doubanio.com/view/subject/s/public/s34197964.jpg', }; }, getCustomMenus(params) { console.log('getCustomMenus call'); let list = [ { menuId: 'menuid1', image: 'image', title: 'title', type: 'type', foo: 'foo', }, { menuId: 'menuid2', image: 'image', title: 'title', type: 'type', foo: 'foo', }, { menuId: 'menuid2', image: 'image', title: 'title', type: 'type', foo: 'foo', }, { menuId: 'menuid4', image: 'image', title: 'title', type: 'type', foo: 'foo', }, ]; return list; }, onCustomMenuClick(params) { console.log('onCustomMenuClick', params); }, appletDidOpen(params) { console.log('appletDidOpen', params); return params; }, }; MopSDK.registerAppletHandler(handler); }; const addWebExtentionApi = () => { console.log('addWebExtentionApi call'); const rnWebCustomAPI = params => { console.warn('webview 自定义api rnWebCustomAPI call', params); return { errMsg: 'rnWebCustomAPI:ok', data: 'customAPI', }; }; MopSDK.addWebExtentionApi('rnWebCustomAPI', rnWebCustomAPI); }; const registerExtensionApi = () => { console.log('registerExtensionApi call'); const rnCustomAPI = params => { console.warn('自定义 api rn rnCustomAPI call', params); return { errMsg: 'rnCustomAPI:ok', data: 'webCustomAPI', }; }; MopSDK.registerExtensionApi('rnCustomAPI', rnCustomAPI); }; const callJS = () => { if (Platform.OS !== 'android') { let time = 0; BackgroundTimer.runBackgroundTimer(() => { if (time === 0) { console.warn('ios calljs 执行'); MopSDK.callJS(appId, 'app2jsFunction', { data: 100, }) .then(res => { Alert.alert('calljs 调用成功'); console.warn('calljs 调用成功', res); }) .catch(res => { Alert.alert('calljs 失败'); console.warn('calljs 调用失败', res); }); time++; } }, 10000); } else { MopSDK.callJS(appId, 'app2jsFunction', { data: 100, }) .then(res => { console.warn('calljs 调用成功', res); }) .catch(res => { console.warn('calljs 调用失败', res); }); } }; const sendCustomEvent = () => { console.warn('sendCustomEvent call'); MopSDK.sendCustomEvent(appId, { evenatName: 'hello-world', foo: 'test', }); }; const finishRunningApplet = () => { Alert.alert('finishRunningApplet'); console.warn('结束运行的小程序'); MopSDK.finishRunningApplet(appId, true); }; const setActivityTransitionAnim = () => { if (Platform.OS !== 'android') { console.warn('仅安卓支持'); return; } console.log('setActivityTransitionAnim call'); MopSDK.setActivityTransitionAnim('SlideFromBottomToTopAnim'); }; const App: () => Node = () => { const [isShowScaner, setIsShowScaner] = useState(false); const [qrcode, setQrcode] = useState(''); const [appInfo, setAppInfo] = useState(''); const isDarkMode = useColorScheme() === 'dark'; useEffect(() => { if (!isInited) { const eventEmitter = new NativeEventEmitter(NativeModules.FINMopSDK); MopSDK.initialize({ appkey: 'Ev7QHvml1UcW98Y1GaLfRz34ReffbDESaTXbCoFyKhEm0a3gam0elOOOdZ6Twpa3HkBzlvOwJ2cyhOrMVWuuGw==', secret: '16f2d2700453ae51', apiServer: 'https://api.finclip.com', apiPrefix: '/api/v1/mop/', nativeEventEmitter: eventEmitter, finMopSDK: NativeModules.FINMopSDK, }) .then(res => { isInited = true; Alert.alert('初始化成功'); }) .catch(error => { Alert.alert('初始化失败'); }); } }); const handleSetIsShowScaner = status => { setIsShowScaner(status); }; const handleQRCodeResult = str => { setQrcode(str); qrcodeOpenApplet(str); }; return ( React Native SDK Demo 打开小程序