diff --git a/App.js b/App.js index a2a3d27..e880919 100644 --- a/App.js +++ b/App.js @@ -1,93 +1,348 @@ /** * Sample React Native App - * - * adapted from App.js generated by the following command: - * - * react-native init example - * * https://github.com/facebook/react-native + * + * @format + * @flow strict-local */ -import React, {Component} from 'react'; -import {Platform, StyleSheet, Text, View, Button} from 'react-native'; +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'; -const onPressOpenCanvasApplet = () => { - MopSDK.openApplet('5ea03fa563cb900001d73863', '', '', data => {}); +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 onPressOpenDemoApplet = () => { - MopSDK.openApplet('5ea0401463cb900001d73865', '', '', data => {}); + +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 onPressOpenProfileApplet = () => { - MopSDK.openApplet('5ea0412663cb900001d73867', '', '', data => {}); + +const closeApplet = () => { + let time = 0; + BackgroundTimer.runBackgroundTimer(() => { + if (time === 0) { + console.log('closeApplet call'); + MopSDK.closeApplet(appId, true); + time++; + } + }, 10000); }; -export default class App extends Component<{}> { - state = { - status: 'starting', - message: '--', + +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; + }, }; - componentDidMount() { - MopSDK.initialize( - { - appkey: '22LyZEib0gLTQdU3MUauASlb4KFRNRajt4RmY6UDSucA', - secret: 'c5cc7a8c14a2b04a', + 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: + 'Ev7QHvml1UcW98Y1GaLfRz34ReffbDESaTXbCoFyKhHm5K1hSIZ5WQtcH5E2Ow5V4P7ZoS9FWirImuaa/yIlLg==', + secret: '97f8e4dd3a0e7634', apiServer: 'https://api.finclip.com', - apiPrefix: '/api/v1/mop', - }, - data => { - console.log('message;', data); - const s = JSON.stringify(data); - this.setState({ - status: 'native callback received', - message: s, + apiPrefix: '/api/v1/mop/', + nativeEventEmitter: eventEmitter, + finMopSDK: NativeModules.FINMopSDK, + }) + .then(res => { + isInited = true; + Alert.alert('初始化成功'); + }) + .catch(error => { + Alert.alert('初始化失败'); }); - }, - ); - } - render() { - return ( - - ☆FINMopsdk example☆ - STATUS: {this.state.status} - ☆NATIVE CALLBACK MESSAGE☆ - {this.state.message} -