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
fa44f64f
Commit
fa44f64f
authored
Mar 26, 2021
by
hewenhao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: use native bridage
parent
3572ca7b
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
101 additions
and
1 deletion
+101
-1
index.ts
src/router/index.ts
+5
-0
nativeBridge.ts
src/util/nativeBridge.ts
+2
-1
use-native-bridage.ts
src/util/use-native-bridage.ts
+69
-0
index.vue
src/views/test/index.vue
+25
-0
No files found.
src/router/index.ts
View file @
fa44f64f
...
@@ -15,6 +15,11 @@ const routes: Array<RouteRecordRaw> = [
...
@@ -15,6 +15,11 @@ const routes: Array<RouteRecordRaw> = [
path
:
"/withdraw"
,
path
:
"/withdraw"
,
name
:
"withdraw"
,
name
:
"withdraw"
,
component
:
()
=>
import
(
"../views/bonus/withdraw/index.vue"
)
component
:
()
=>
import
(
"../views/bonus/withdraw/index.vue"
)
},
{
path
:
"/test"
,
name
:
"test"
,
component
:
()
=>
import
(
"../views/test/index.vue"
)
}
}
];
];
...
...
src/util/nativeBridge.ts
View file @
fa44f64f
/* eslint-disable @typescript-eslint/camelcase */
import
{
App
}
from
"vue"
;
import
{
App
}
from
"vue"
;
import
mitt
from
"./mitt"
;
import
mitt
from
"./mitt"
;
...
@@ -125,7 +126,7 @@ function install(Vue: App) {
...
@@ -125,7 +126,7 @@ function install(Vue: App) {
created
()
{
created
()
{
event_bus
.
on
(
GEEK_NATVIE_NAME
,
this
.
listenNativeMessage
);
event_bus
.
on
(
GEEK_NATVIE_NAME
,
this
.
listenNativeMessage
);
},
},
before
Destroy
()
{
before
Unmount
()
{
event_bus
.
off
(
GEEK_NATVIE_NAME
,
this
.
listenNativeMessage
);
event_bus
.
off
(
GEEK_NATVIE_NAME
,
this
.
listenNativeMessage
);
}
}
});
});
...
...
src/util/use-native-bridage.ts
0 → 100644
View file @
fa44f64f
/* eslint-disable @typescript-eslint/camelcase */
/* eslint-disable @typescript-eslint/no-explicit-any */
import
{
onBeforeUnmount
,
onMounted
,
getCurrentInstance
,
ComponentInternalInstance
}
from
"vue"
;
import
mitt
from
"./mitt"
;
const
eventBus
=
mitt
();
const
GEEK_NATVIE_NAME
=
"GEEK_NATVIE_NAME"
;
interface
NativeInvokeFuncParams
{
method
:
string
;
data
:
any
;
}
const
isAndroid
=
()
=>
{
const
u
=
navigator
.
userAgent
;
const
android
=
u
.
indexOf
(
"Android"
)
>
-
1
||
u
.
indexOf
(
"Adr"
)
>
-
1
;
return
android
;
};
window
.
$geek_excuteJS
=
function
(
args
)
{
try
{
const
params
=
JSON
.
parse
(
args
);
eventBus
.
emit
<
NativeInvokeFuncParams
>
(
GEEK_NATVIE_NAME
,
params
);
}
catch
(
error
)
{
eventBus
.
emit
<
NativeInvokeFuncParams
>
(
GEEK_NATVIE_NAME
,
args
);
}
};
const
sendNativeMessage
=
(
data
:
{
type
:
string
;
data
:
any
})
=>
{
if
(
isAndroid
())
{
window
.
ReactNativeWebView
&&
window
.
ReactNativeWebView
.
onMessage
&&
window
.
ReactNativeWebView
.
onMessage
(
JSON
.
stringify
(
data
));
}
window
.
ReactNativeWebView
&&
window
.
ReactNativeWebView
.
postMessage
&&
window
.
ReactNativeWebView
.
postMessage
(
JSON
.
stringify
(
data
));
};
function
useNativeBridage
(
eventType
:
string
,
listener
:
(
this
:
ComponentInternalInstance
|
null
,
...
arg
:
any
[])
=>
any
):
[
typeof
sendNativeMessage
,
typeof
eventBus
]
{
const
_listener
=
function
(
this
:
ComponentInternalInstance
|
null
,
arg
?:
NativeInvokeFuncParams
)
{
if
(
!
arg
)
return
;
if
(
eventType
===
arg
.
method
)
{
listener
.
apply
(
this
,
[
arg
.
data
]);
}
};
let
fn
:
|
((
this
:
ComponentInternalInstance
|
null
,
arg
?:
NativeInvokeFuncParams
)
=>
void
)
|
null
=
null
;
onMounted
(()
=>
{
fn
=
_listener
.
bind
(
getCurrentInstance
());
eventBus
.
on
<
NativeInvokeFuncParams
>
(
GEEK_NATVIE_NAME
,
fn
);
});
onBeforeUnmount
(()
=>
{
if
(
fn
)
{
eventBus
.
off
<
NativeInvokeFuncParams
>
(
GEEK_NATVIE_NAME
,
fn
);
}
});
return
[
sendNativeMessage
,
eventBus
];
}
export
{
useNativeBridage
};
src/views/test/index.vue
0 → 100644
View file @
fa44f64f
<
template
>
<div>
{{
count
}}
</div>
<button
@
click=
"emitTest"
>
emit
</button>
</
template
>
<
script
lang=
"ts"
setup
>
import
{
ref
}
from
"vue"
;
import
{
useNativeBridage
}
from
"@/util/use-native-bridage"
;
const
[
sendNativeMessage
,
eventBus
]
=
useNativeBridage
(
"bindWechatChange"
,
function
(
arg
)
{
console
.
log
(
this
,
arg
);
});
const
count
=
ref
(
1000
);
const
emitTest
=
()
=>
{
eventBus
.
emit
(
"GEEK_NATVIE_NAME"
,
{
method
:
"bindWechatChange"
,
data
:
{
hhh
:
13213
}
});
count
.
value
=
count
.
value
+
1
;
};
</
script
>
<
style
></
style
>
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