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 3050d4ce
authored
May 07, 2021
by
fanx
Browse Files
Options
Browse Files
Tag
Download
Plain Diff
fixed confict
2 parents
d3b5bcc8
f28d5c82
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
88 additions
and
37 deletions
src/api/NativeApis.js
src/utils/Interface.js
src/api/NativeApis.js
0 → 100644
View file @
3050d4c
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
(
jsMethodName
,
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
src/utils/Interface.js
View file @
3050d4c
const
NativeInterface
=
[
'callNativePay'
,
'callNativeShare'
]
import
{
JsInterfaces
}
from
'@/api/NativeApis'
const
JsInterface
=
[
'payResult'
,
'shareResult'
]
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
)
}
})
})
;
}
CallNativeFun
(
type
,
data
)
{
__callNativeApiNotBack
(
nativeMethod
,
data
)
{
try
{
const
json
=
JSON
.
stringify
(
data
)
window
.
$NativeApi
[
type
](
json
)
if
(
that
.
__callNativeApi
(
nativeMethod
,
{
data
}))
{
return
Promise
.
reject
(
`无效调用:[
${
nativeMethod
}
]`
)
}
return
Promise
.
resolve
()
}
catch
(
e
)
{
console
.
log
(
'-----无效,非内置调用'
,
type
)
return
Promise
.
reject
(
`接口调用异常:[
${
nativeMethod
}
]`
)
}
}
__callNativeApiBack
(
nativeMethod
,
data
)
{
const
that
=
this
const
jsMethodName
=
`js
${
Date
.
now
()}
`
return
new
Promise
((
resolve
,
reject
)
=>
{
try
{
that
[
jsMethodName
]
=
(
data
)
=>
{
resolve
(
data
?
JSON
.
parse
(
data
)
:
data
)
delete
that
[
jsMethodName
]
}
if
(
that
.
__callNativeApi
(
nativeMethod
,
{
data
,
jsMethodName
}))
{
return
reject
(
`无效调用:[
${
nativeMethod
}
]`
)
}
}
catch
(
e
)
{
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
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