Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mall-webview
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
frontend
mall-webview
Commits
e7253e60
Commit
e7253e60
authored
Mar 29, 2021
by
hewenhao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 调用原生方法
parent
ece7b02e
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
16 deletions
+60
-16
main.ts
src/main.ts
+7
-4
use-native-bridge.ts
src/util/use-native-bridge.ts
+53
-12
No files found.
src/main.ts
View file @
e7253e60
/* eslint-disable @typescript-eslint/no-var-requires */
import
{
createApp
}
from
"vue"
;
import
App
from
"./App.vue"
;
import
router
from
"./router"
;
import
store
from
"./store"
;
import
"./assets/style/normalize.css"
;
import
"amfe-flexible"
;
import
nativeBridge
from
'./util/nativeBridge'
import
VConsole
from
"vconsole"
;
new
VConsole
();
if
(
process
.
env
.
NODE_ENV
!==
"production"
)
{
const
VConsole
=
require
(
"vconsole"
);
new
VConsole
();
}
createApp
(
App
)
.
use
(
store
)
.
use
(
router
)
.
use
(
nativeBridge
)
.
mount
(
"#app"
);
src/util/use-native-bridge.ts
View file @
e7253e60
/* eslint-disable @typescript-eslint/no-use-before-define */
/* eslint-disable @typescript-eslint/camelcase */
/* eslint-disable @typescript-eslint/no-explicit-any */
import
{
Toast
}
from
"vant"
;
import
{
onBeforeUnmount
,
onMounted
,
getCurrentInstance
,
ComponentInternalInstance
}
from
"vue"
;
import
mitt
from
"./mitt"
;
const
eventBus
=
mitt
();
const
GEEK_NAT
VIE_NAME
=
"GEEK_NATVI
E_NAME"
;
const
GEEK_NAT
IVE_NAME
=
"GEEK_NATIV
E_NAME"
;
interface
NativeInvokeFuncParams
{
method
:
string
;
data
:
any
;
error
?:
string
|
null
;
}
const
isAndroid
=
()
=>
{
...
...
@@ -18,17 +19,16 @@ const isAndroid = () => {
return
android
;
};
window
.
$geek_excuteJS
=
function
(
args
)
{
console
.
log
(
args
);
window
.
$geek_excuteJS
=
function
(
args
)
{
try
{
const
params
=
JSON
.
parse
(
args
);
eventBus
.
emit
<
NativeInvokeFuncParams
>
(
GEEK_NAT
VI
E_NAME
,
params
);
eventBus
.
emit
<
NativeInvokeFuncParams
>
(
GEEK_NAT
IV
E_NAME
,
params
);
}
catch
(
error
)
{
eventBus
.
emit
<
NativeInvokeFuncParams
>
(
GEEK_NAT
VI
E_NAME
,
args
);
eventBus
.
emit
<
NativeInvokeFuncParams
>
(
GEEK_NAT
IV
E_NAME
,
args
);
}
};
const
sendNativeMessage
=
(
data
:
{
type
:
string
;
data
:
any
})
=>
{
const
sendNativeMessage
=
(
data
:
{
type
:
string
;
data
?
:
any
})
=>
{
if
(
isAndroid
())
{
window
.
ReactNativeWebView
&&
window
.
ReactNativeWebView
.
onMessage
&&
...
...
@@ -54,9 +54,7 @@ function useNativeBridge(
eventTypeOrEventMap
:
string
|
{
[
key
:
string
]:
ListenerType
},
listener
?:
ListenerType
):
[
typeof
sendNativeMessage
,
typeof
eventBus
]
{
const
_listener
=
function
(
this
:
ComponentInternalInstance
|
null
,
arg
?:
NativeInvokeFuncParams
)
{
console
.
log
(
arg
,
1111111
);
const
_listener
=
function
(
this
:
ComponentInternalInstance
|
null
,
arg
?:
NativeInvokeFuncParams
)
{
if
(
!
arg
)
return
;
const
{
method
,
data
}
=
arg
;
if
(
typeof
eventTypeOrEventMap
===
"string"
)
{
...
...
@@ -74,12 +72,12 @@ function useNativeBridge(
onMounted
(()
=>
{
fn
=
_listener
.
bind
(
getCurrentInstance
());
eventBus
.
on
<
NativeInvokeFuncParams
>
(
GEEK_NAT
VI
E_NAME
,
fn
);
eventBus
.
on
<
NativeInvokeFuncParams
>
(
GEEK_NAT
IV
E_NAME
,
fn
);
});
onBeforeUnmount
(()
=>
{
if
(
fn
)
{
eventBus
.
off
<
NativeInvokeFuncParams
>
(
GEEK_NAT
VI
E_NAME
,
fn
);
eventBus
.
off
<
NativeInvokeFuncParams
>
(
GEEK_NAT
IV
E_NAME
,
fn
);
}
});
...
...
@@ -87,3 +85,46 @@ function useNativeBridge(
}
export
{
useNativeBridge
};
const
INVOKE_NATIVE_METHOD_TIMEOUT
=
10000
;
function
invokeNativeMethod
<
T
=
any
,
P
=
any
>
(
arg
:
{
type
:
string
;
data
?:
P
}):
Promise
<
T
>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
const
callbackName
=
`
${
arg
.
type
}
_callback`
;
const
timer
=
setTimeout
(()
=>
{
reject
(
new
Error
(
`invoke
${
arg
.
type
}
timeout`
));
eventBus
.
off
(
GEEK_NATIVE_NAME
,
onCallback
);
},
INVOKE_NATIVE_METHOD_TIMEOUT
);
eventBus
.
on
(
GEEK_NATIVE_NAME
,
onCallback
);
function
onCallback
(
params
?:
NativeInvokeFuncParams
)
{
if
(
params
?.
method
===
callbackName
)
{
clearTimeout
(
timer
);
if
(
params
.
error
)
{
reject
(
new
Error
(
params
.
error
));
}
else
{
resolve
(
params
.
data
);
}
eventBus
.
off
(
GEEK_NATIVE_NAME
,
onCallback
);
}
}
sendNativeMessage
(
arg
);
});
}
/**
* 微信登录
*/
export
function
bindWechat
()
{
return
invokeNativeMethod
<
{
code
:
string
}
>
({
type
:
"bindWechat"
});
}
/**
* 打电话
*/
export
function
makecall
(
phone
:
string
)
{
return
invokeNativeMethod
<
null
,
string
>
({
type
:
"makecall"
,
data
:
phone
});
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
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 comment