Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
polysoft-nafmii
/
polysoft-h5
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit f0392d78
authored
May 07, 2021
by
amateur
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
添加原生接口桥接模块逻辑
1 parent
7b047071
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
85 additions
and
30 deletions
src/api/NativeApis.js
src/utils/Interface.js
src/api/NativeApis.js
0 → 100644
View file @
f0392d7
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
\ No newline at end of file
src/utils/Interface.js
View file @
f0392d7
const
NativeInterface
=
{
import
{
JsInterfaces
}
from
'@/api/NativeApis'
}
const
JsInterface
=
{
}
class
Interface
{
class
Interface
{
constructor
()
{
constructor
(
interfaces
)
{
this
.
listeners
=
{}
this
.
listeners
=
{}
this
.
init
(
)
this
.
__init
(
interfaces
)
}
}
on
(
methodName
,
callback
)
{
on
(
methodName
,
callback
)
{
this
.
listeners
[
methodName
]
=
callback
this
.
listeners
[
methodName
]
=
callback
}
}
init
()
{
call
(
nativeMethod
,
data
,
isback
)
{
NativeInterface
.
forEach
(
methodName
=>
{
if
(
isback
)
{
this
[
methodName
]
=
(
data
,
jsMethodName
,
callback
)
=>
{
return
this
.
__callNativeApiBack
(
nativeMethod
,
data
)
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
)
}
}
return
this
.
__callNativeApiBack
(
nativeMethod
,
data
)
}
}
})
JsInterface
.
forEach
(
methodName
=>
{
__init
(
interfaces
=
[])
{
interfaces
.
forEach
(
methodName
=>
{
this
[
methodName
]
=
(
data
)
=>
{
this
[
methodName
]
=
(
data
)
=>
{
const
objValue
=
data
?
JSON
.
parse
(
data
)
:
data
this
.
listeners
[
methodName
](
data
?
JSON
.
parse
(
data
)
:
data
)
this
.
listeners
[
methodName
](
objValue
)
}
}
})
})
;
}
}
__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
{
try
{
const
json
=
JSON
.
stringify
(
data
)
that
[
jsMethodName
]
=
(
data
)
=>
{
window
.
$NativeApi
[
type
](
json
)
resolve
(
data
?
JSON
.
parse
(
data
)
:
data
)
delete
that
[
jsMethodName
]
}
if
(
that
.
__callNativeApi
(
nativeMethod
,
{
data
,
jsMethodName
}))
{
return
reject
(
`无效调用:[
${
nativeMethod
}
]`
)
}
}
catch
(
e
)
{
}
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
\ No newline at end of file
window
.
$api
=
new
Interface
(
JsInterfaces
)
\ No newline at end of file
\ No newline at end of file
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment