Skip to content

Commit 7734586

Browse files
committed
feat: 增加 rn-sdk api
1 parent 61fcad4 commit 7734586

File tree

1 file changed

+148
-2
lines changed

1 file changed

+148
-2
lines changed

index.js

Lines changed: 148 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,70 @@
1-
import { NativeModules } from 'react-native';
1+
import { NativeModules, NativeEventEmitter } from 'react-native';
2+
3+
4+
const handleCallbackDate = (params) => {
5+
console.log('callbackHandler called')
6+
let result = {}
7+
if(params instanceof Array) {
8+
result = JSON.parse(params[0])
9+
} else {
10+
console.error('参数错误')
11+
}
12+
13+
return result
14+
}
15+
16+
17+
const typeCheck = (data, type) => {
18+
const TYPE_MAP = {
19+
'[object String]': 'String',
20+
'[object Number]': 'Number',
21+
'[object Boolean]': 'Boolean',
22+
'[object Null]': 'Null',
23+
'[object Undefined]': 'Undefined',
24+
'[object Object]': 'Object'
25+
}
26+
if(!Object.values(TYPE_MAP).includes(type)) {
27+
console.error('error type')
28+
return
29+
}
30+
const result = TYPE_MAP[Object.prototype.toString.call(data)] === type
31+
if(!result) {
32+
console.error(`params should be ${type}`)
33+
}
34+
return result
35+
}
236

337
const { FINMopsdk } = NativeModules;
38+
39+
40+
const _handlePlatformMethodCall = async (data) => {
41+
const extensionAPIPrefix = 'extensionApi:'
42+
const webExtensionApiPrefix = 'webExtentionApi:'
43+
if(data.apiName.startsWith(extensionAPIPrefix)) {
44+
let apiName = data.apiName.substring(extensionAPIPrefix.length)
45+
const handler = MopSDK._extensionApis[apiName]
46+
if(handler !== null) {
47+
const res = await handler(data.params)
48+
FINMopsdk.eventReminderCallback(apiName, res, data.callbackId)
49+
}
50+
} else if(data.apiName.startsWith(webExtensionApiPrefix)) {
51+
let apiName = data.apiName.substring(webExtensionApiPrefix.length)
52+
const handler = MopSDK._webExtensionApis[apiName]
53+
if(handler !== null) {
54+
const res = await handler(data.params)
55+
FINMopsdk.eventReminderCallback(apiName, res, data.callbackId)
56+
}
57+
}
58+
}
59+
60+
const eventEmitter = new NativeEventEmitter(FINMopsdk);
61+
eventEmitter.addListener('EventReminder', (event) => {
62+
_handlePlatformMethodCall(event)
63+
})
64+
465
const MopSDK = {
66+
_extensionApis: {},
67+
_webExtensionApis: {},
568
initialize(params, callback) {
669
console.log(params);
770
console.log(callback);
@@ -28,6 +91,89 @@ const MopSDK = {
2891
},
2992
openApplet(appId, path, query, callback) {
3093
FINMopsdk.openApplet(appId, path, query, null, callback);
31-
}
94+
},
95+
// 关闭小程序
96+
closeApplet(appletId, animated) {
97+
FINMopsdk.closeApplet({ appletId, animated})
98+
},
99+
// 关闭当前打开的所有小程序
100+
closeAllApplets() {
101+
FINMopsdk.closeAllApplets()
102+
},
103+
// 二维码打开小程序
104+
qrcodeOpenApplet(qrcode) {
105+
FINMopsdk.qrcodeOpenApplet({ qrcode })
106+
},
107+
// 获取当前正在使用的小程序信息
108+
currentApplet() {
109+
return new Promise((resolve, reject) => {
110+
FINMopsdk.currentApplet({},(params) => {
111+
params = handleCallbackData(params)
112+
if(params.sucuess) {
113+
resolve(params.data)
114+
} else {
115+
reject(params)
116+
}
117+
})
118+
})
119+
},
120+
// 清除缓存的小程序
121+
clearApplets() {
122+
FINMopsdk.clearApplets()
123+
},
124+
// 通过二维码打开小程序
125+
qrcodeOpenApplet(qrcode) {
126+
FINMopsdk.qrcodeOpenApplet({qrcode})
127+
},
128+
// 注册小程序事件处理
129+
registerAppletHandler(handler) {
130+
this._extensionApis['forwardApplet'] = (params) => {
131+
handler.forwardApplet(params)
132+
}
133+
134+
this._extensionApis['getUserInfo'] = (params) => {
135+
return handler.getUserInfo()
136+
}
137+
138+
this._extensionApis["getCustomMenus"] = async (params) => {
139+
const res = await handler.getCustomMenus(params["appId"]);
140+
let list = res.map(item => {
141+
return { menuId, image, title, type}
142+
})
143+
return list
144+
};
145+
this._extensionApis["onCustomMenuClick"] = (params) => {
146+
return handler.onCustomMenuClick(
147+
params["appId"], params["path"], params["menuId"], params["appInfo"]);
148+
};
149+
this._extensionApis["appletDidOpen"] = (params) => {
150+
return handler.appletDidOpen(params["appId"]);
151+
};
152+
FINMopsdk.registerAppletHandler()
153+
},
154+
// 注册webview拓展api
155+
addWebExtentionApi(name, handler) {
156+
this._webExtensionApis[name] = handler
157+
FINMopsdk.addWebExtentionApi({ name })
158+
},
159+
// 原生调用webview中的js方法
160+
callJS(appId, eventName, nativeViewId, eventData) {
161+
// todo: 异步
162+
FINMopsdk.callJS({ appId, eventName, nativeViewId, eventData })
163+
},
164+
// 原生发送事件给小程序
165+
sendCustomEvent(appId, eventData) {
166+
// todo: 异步
167+
FINMopsdk.sendCustomEvent({ appId, eventData })
168+
},
169+
// 结束小程序
170+
finishRunningApplet(appletId, animated) {
171+
FINMopsdk.finishRunningApplet({ appletId, animated })
172+
},
173+
// 设置小程序切换动画 安卓
174+
setActivityTransitionAnim(anim) {
175+
FINMopsdk.setActivityTransitionAnim({ 'anim': anim.name})
176+
},
177+
32178
};
33179
export default MopSDK;

0 commit comments

Comments
 (0)