Commit f0392d78 by amateur

添加原生接口桥接模块逻辑

1 parent 7b047071
export const NativeMethodNames = {
AppInfoApi: 'nativeAppInfo',
AppShare: 'nativeAppShare',
}
export const JsMethodNames = {
PayResult: 'payResult'
}
export const JsInterfaces = objVal2Array(JsMethodNames)
export const NativeApi = {
on: (jsMethodName, callback) => {
$api.on(jsinterface, callback)
},
call: (nativeMethodName, param, isBack) => {
return $api.call(nativeMethodName, param, isBack)
},
nativeAppInfo: (param) => {
return $api.call(NativeMethodNames.AppInfoApi, param, true)
},
nativeAppShare: (param) => {
return $api.call(NativeMethodNames.AppShare, param, true)
}
}
function objVal2Array(obj) {
const arrays = []
for (const key in obj) {
arrays.push(obj[key])
}
return arrays
}
\ No newline at end of file
const NativeInterface = {
import { JsInterfaces } from '@/api/NativeApis'
}
const JsInterface = {
}
class Interface {
constructor() {
constructor(interfaces) {
this.listeners = {}
this.init()
this.__init(interfaces)
}
on(methodName, callback) {
this.listeners[methodName] = callback
}
init() {
NativeInterface.forEach(methodName => {
this[methodName] = (data, jsMethodName, callback) => {
try {
if (jsMethodName) {
this.listeners[jsMethodName] = callback
}
const json = JSON.stringify(jsMethodName && { jsMethodName, data } || { data })
window.$NativeApi[methodName](json)
} catch (e) {
callback && callback()
console.log('-----无效,非内置调用', methodName)
call(nativeMethod, data, isback) {
if (isback) {
return this.__callNativeApiBack(nativeMethod, data)
}
return this.__callNativeApiBack(nativeMethod, data)
}
})
JsInterface.forEach(methodName => {
__init(interfaces = []) {
interfaces.forEach(methodName => {
this[methodName] = (data) => {
const objValue = data ? JSON.parse(data) : data
this.listeners[methodName](objValue)
this.listeners[methodName](data ? JSON.parse(data) : data)
}
})
});
}
__callNativeApiNotBack(nativeMethod, data) {
try {
if (that.__callNativeApi(nativeMethod, { data })) {
return Promise.reject(`无效调用:[${nativeMethod}]`)
}
return Promise.resolve()
} catch (e) {
return Promise.reject(`接口调用异常:[${nativeMethod}]`)
}
}
CallNativeFun(type, data) {
__callNativeApiBack(nativeMethod, data) {
const that = this
const jsMethodName = `js${Date.now()}`
return new Promise((resolve, reject) => {
try {
const json = JSON.stringify(data)
window.$NativeApi[type](json)
that[jsMethodName] = (data) => {
resolve(data ? JSON.parse(data) : data)
delete that[jsMethodName]
}
if (that.__callNativeApi(nativeMethod, { data, jsMethodName })) {
return reject(`无效调用:[${nativeMethod}]`)
}
} catch (e) {
console.log('-----无效,非内置调用', type)
return Promise.reject(`接口调用异常:[${nativeMethod}]`)
}
})
}
__callNativeApi(nativeMethod, data) {
if (window.$NativeApi || $NativeApi[nativeMethod]) {
window.$NativeApi[nativeMethod](JSON.stringify(data))
} else {
return true
}
}
}
window.$api = new Interface()
\ No newline at end of file
window.$api = new Interface(JsInterfaces)
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!