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
64d7469d
Commit
64d7469d
authored
Mar 26, 2021
by
wykolor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 通信
parent
135ef5fe
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
309 additions
and
309 deletions
+309
-309
main.ts
src/main.ts
+12
-12
index.ts
src/router/index.ts
+31
-31
nativeBridge.ts
src/util/nativeBridge.ts
+135
-135
use-native-bridage.ts
src/util/use-native-bridage.ts
+69
-69
vue.config.js
vue.config.js
+62
-62
No files found.
src/main.ts
View file @
64d7469d
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'
createApp
(
App
)
.
use
(
store
)
.
use
(
router
)
.
use
(
nativeBridge
)
.
mount
(
"#app"
);
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'
createApp
(
App
)
.
use
(
store
)
.
use
(
router
)
.
use
(
nativeBridge
)
.
mount
(
"#app"
);
src/router/index.ts
View file @
64d7469d
import
{
createRouter
,
createWebHashHistory
,
RouteRecordRaw
}
from
"vue-router"
;
import
Bonus
from
"../views/bonus/index.vue"
;
const
routes
:
Array
<
RouteRecordRaw
>
=
[
{
path
:
"/"
,
name
:
"Bonus"
,
component
:
Bonus
},
{
path
:
"/bonusDetail"
,
name
:
"bonusDetail"
,
component
:
()
=>
import
(
"../views/bonus/bonus-detail/index.vue"
)
},
{
path
:
"/withdraw"
,
name
:
"withdraw"
,
component
:
()
=>
import
(
"../views/bonus/withdraw/index.vue"
)
},
{
path
:
"/test"
,
name
:
"test"
,
component
:
()
=>
import
(
"../views/test/index.vue"
)
}
];
const
router
=
createRouter
({
history
:
createWebHashHistory
(),
routes
});
export
default
router
;
import
{
createRouter
,
createWebHashHistory
,
RouteRecordRaw
}
from
"vue-router"
;
import
Bonus
from
"../views/bonus/index.vue"
;
const
routes
:
Array
<
RouteRecordRaw
>
=
[
{
path
:
"/"
,
name
:
"Bonus"
,
component
:
Bonus
},
{
path
:
"/bonusDetail"
,
name
:
"bonusDetail"
,
component
:
()
=>
import
(
"../views/bonus/bonus-detail/index.vue"
)
},
{
path
:
"/withdraw"
,
name
:
"withdraw"
,
component
:
()
=>
import
(
"../views/bonus/withdraw/index.vue"
)
},
{
path
:
"/test"
,
name
:
"test"
,
component
:
()
=>
import
(
"../views/test/index.vue"
)
}
];
const
router
=
createRouter
({
history
:
createWebHashHistory
(),
routes
});
export
default
router
;
src/util/nativeBridge.ts
View file @
64d7469d
/* eslint-disable @typescript-eslint/camelcase */
import
{
App
}
from
"vue"
;
import
mitt
from
"./mitt"
;
const
isiOS
=
()
=>
{
const
u
=
navigator
.
userAgent
;
const
iOs
=
!!
u
.
match
(
/
\(
i
[^
;
]
+;
(
U;
)?
CPU.+Mac OS X/
);
return
iOs
;
};
const
isAndroid
=
()
=>
{
const
u
=
navigator
.
userAgent
;
const
android
=
u
.
indexOf
(
"Android"
)
>
-
1
||
u
.
indexOf
(
"Adr"
)
>
-
1
;
return
android
;
};
const
postMessage
=
data
=>
{
if
(
isAndroid
())
{
window
.
ReactNativeWebView
&&
window
.
ReactNativeWebView
.
onMessage
&&
window
.
ReactNativeWebView
.
onMessage
(
JSON
.
stringify
(
data
));
}
window
.
ReactNativeWebView
&&
window
.
ReactNativeWebView
.
postMessage
&&
window
.
ReactNativeWebView
.
postMessage
(
JSON
.
stringify
(
data
));
};
if
(
isiOS
()
||
isAndroid
())
{
// const debugLog = console.log
// eslint-disable-next-line func-names
// console.log = function (...args) {
// postMessage({
// type: 'log',
// data: args
// })
// debugLog(...args)
// }
}
function
install
(
Vue
:
App
)
{
const
GEEK_NATVIE_NAME
=
"GEEK_NATVIE_NAME"
;
// eslint-disable-next-line camelcase
const
event_bus
=
mitt
();
// eslint-disable-next-line func-names
window
.
$geek_excuteJS
=
function
(
args
)
{
try
{
const
params
=
JSON
.
parse
(
args
);
event_bus
.
emit
(
GEEK_NATVIE_NAME
,
params
);
}
catch
(
error
)
{
event_bus
.
emit
(
GEEK_NATVIE_NAME
,
args
);
}
};
Vue
.
mixin
({
methods
:
{
listenNativeMessage
(
arg
)
{
const
{
method
,
data
}
=
arg
;
if
(
method
)
{
this
[
method
]
&&
this
[
method
](
data
);
}
},
$nativeTitle
(
title
)
{
const
message
=
{
type
:
"title"
,
data
:
title
};
postMessage
(
message
);
},
$shareImg
(
base64Img
,
timeline
=
false
)
{
const
message
=
{
type
:
timeline
?
"shareTimeline"
:
"share"
,
data
:
{
type
:
"img"
,
img
:
base64Img
}
};
postMessage
(
message
);
},
$shareText
(
txt
,
timeline
=
false
)
{
const
message
=
{
type
:
timeline
?
"shareTimeline"
:
"share"
,
data
:
{
type
:
"text"
,
description
:
txt
}
};
postMessage
(
message
);
},
$shareNews
({
title
,
url
,
description
},
timeline
=
false
)
{
const
message
=
{
type
:
timeline
?
"shareTimeline"
:
"share"
,
data
:
{
type
:
"news"
,
description
,
webpageUrl
:
url
,
title
}
};
postMessage
(
message
);
},
$playVieo
(
url
)
{
const
message
=
{
type
:
"video"
,
data
:
{
url
}
};
postMessage
(
message
);
},
$postMessage
(
message
)
{
postMessage
(
message
);
},
$exitWebView
(
data
)
{
this
.
$postMessage
({
type
:
"exitWebview"
,
data
});
},
$shareReport
(
params
)
{
this
.
$postMessage
({
type
:
"reportShare"
,
params
});
}
},
created
()
{
event_bus
.
on
(
GEEK_NATVIE_NAME
,
this
.
listenNativeMessage
);
},
beforeUnmount
()
{
event_bus
.
off
(
GEEK_NATVIE_NAME
,
this
.
listenNativeMessage
);
}
});
}
export
default
install
;
/* eslint-disable @typescript-eslint/camelcase */
import
{
App
}
from
"vue"
;
import
mitt
from
"./mitt"
;
const
isiOS
=
()
=>
{
const
u
=
navigator
.
userAgent
;
const
iOs
=
!!
u
.
match
(
/
\(
i
[^
;
]
+;
(
U;
)?
CPU.+Mac OS X/
);
return
iOs
;
};
const
isAndroid
=
()
=>
{
const
u
=
navigator
.
userAgent
;
const
android
=
u
.
indexOf
(
"Android"
)
>
-
1
||
u
.
indexOf
(
"Adr"
)
>
-
1
;
return
android
;
};
const
postMessage
=
data
=>
{
if
(
isAndroid
())
{
window
.
ReactNativeWebView
&&
window
.
ReactNativeWebView
.
onMessage
&&
window
.
ReactNativeWebView
.
onMessage
(
JSON
.
stringify
(
data
));
}
window
.
ReactNativeWebView
&&
window
.
ReactNativeWebView
.
postMessage
&&
window
.
ReactNativeWebView
.
postMessage
(
JSON
.
stringify
(
data
));
};
if
(
isiOS
()
||
isAndroid
())
{
// const debugLog = console.log
// eslint-disable-next-line func-names
// console.log = function (...args) {
// postMessage({
// type: 'log',
// data: args
// })
// debugLog(...args)
// }
}
function
install
(
Vue
:
App
)
{
const
GEEK_NATVIE_NAME
=
"GEEK_NATVIE_NAME"
;
// eslint-disable-next-line camelcase
const
event_bus
=
mitt
();
// eslint-disable-next-line func-names
window
.
$geek_excuteJS
=
function
(
args
)
{
try
{
const
params
=
JSON
.
parse
(
args
);
event_bus
.
emit
(
GEEK_NATVIE_NAME
,
params
);
}
catch
(
error
)
{
event_bus
.
emit
(
GEEK_NATVIE_NAME
,
args
);
}
};
Vue
.
mixin
({
methods
:
{
listenNativeMessage
(
arg
)
{
const
{
method
,
data
}
=
arg
;
if
(
method
)
{
this
[
method
]
&&
this
[
method
](
data
);
}
},
$nativeTitle
(
title
)
{
const
message
=
{
type
:
"title"
,
data
:
title
};
postMessage
(
message
);
},
$shareImg
(
base64Img
,
timeline
=
false
)
{
const
message
=
{
type
:
timeline
?
"shareTimeline"
:
"share"
,
data
:
{
type
:
"img"
,
img
:
base64Img
}
};
postMessage
(
message
);
},
$shareText
(
txt
,
timeline
=
false
)
{
const
message
=
{
type
:
timeline
?
"shareTimeline"
:
"share"
,
data
:
{
type
:
"text"
,
description
:
txt
}
};
postMessage
(
message
);
},
$shareNews
({
title
,
url
,
description
},
timeline
=
false
)
{
const
message
=
{
type
:
timeline
?
"shareTimeline"
:
"share"
,
data
:
{
type
:
"news"
,
description
,
webpageUrl
:
url
,
title
}
};
postMessage
(
message
);
},
$playVieo
(
url
)
{
const
message
=
{
type
:
"video"
,
data
:
{
url
}
};
postMessage
(
message
);
},
$postMessage
(
message
)
{
postMessage
(
message
);
},
$exitWebView
(
data
)
{
this
.
$postMessage
({
type
:
"exitWebview"
,
data
});
},
$shareReport
(
params
)
{
this
.
$postMessage
({
type
:
"reportShare"
,
params
});
}
},
created
()
{
event_bus
.
on
(
GEEK_NATVIE_NAME
,
this
.
listenNativeMessage
);
},
beforeUnmount
()
{
event_bus
.
off
(
GEEK_NATVIE_NAME
,
this
.
listenNativeMessage
);
}
});
}
export
default
install
;
src/util/use-native-bridage.ts
View file @
64d7469d
/* 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
};
/* 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
};
vue.config.js
View file @
64d7469d
/* eslint-disable @typescript-eslint/no-var-requires */
const
tsImportPluginFactory
=
require
(
"ts-import-plugin"
);
module
.
exports
=
{
devServer
:
{
hot
:
true
,
open
:
true
,
https
:
false
,
proxy
:
{
//配置跨域
"/mock"
:
{
target
:
"http://192.168.44.114:4100"
,
ws
:
true
,
changOrigin
:
true
//允许跨域
},
"/api"
:
{
target
:
"http://192.168.44.114:4100"
,
ws
:
true
,
changOrigin
:
true
//允许跨域
},
"/service"
:
{
target
:
"http://192.168.44.114:4100"
,
ws
:
true
,
changOrigin
:
true
//允许跨域
}
}
},
chainWebpack
:
config
=>
{
config
.
module
.
rule
(
"ts"
)
.
use
(
"ts-loader"
)
.
tap
(
options
=>
{
return
Object
.
assign
(
options
||
{},
{
transpileOnly
:
false
,
getCustomTransformers
:
()
=>
({
before
:
[
tsImportPluginFactory
({
libraryDirectory
:
"es"
,
libraryName
:
"vant"
,
style
:
true
})
]
})
});
});
},
css
:
{
loaderOptions
:
{
postcss
:
{
plugins
:
[
require
(
"postcss-plugin-px2rem"
)({
rootValue
:
75
,
propList
:
[
"*"
],
replace
:
true
,
mediaQuery
:
false
,
minPixelValue
:
2
,
exclude
:
/
(
node_module
)
/i
})
]
}
}
}
};
/* eslint-disable @typescript-eslint/no-var-requires */
const
tsImportPluginFactory
=
require
(
"ts-import-plugin"
);
module
.
exports
=
{
devServer
:
{
hot
:
true
,
open
:
true
,
https
:
false
,
proxy
:
{
//配置跨域
"/mock"
:
{
target
:
"http://192.168.44.114:4100"
,
ws
:
true
,
changOrigin
:
true
//允许跨域
},
"/api"
:
{
target
:
"http://192.168.44.114:4100"
,
ws
:
true
,
changOrigin
:
true
//允许跨域
},
"/service"
:
{
target
:
"http://192.168.44.114:4100"
,
ws
:
true
,
changOrigin
:
true
//允许跨域
}
}
},
chainWebpack
:
config
=>
{
config
.
module
.
rule
(
"ts"
)
.
use
(
"ts-loader"
)
.
tap
(
options
=>
{
return
Object
.
assign
(
options
||
{},
{
transpileOnly
:
false
,
getCustomTransformers
:
()
=>
({
before
:
[
tsImportPluginFactory
({
libraryDirectory
:
"es"
,
libraryName
:
"vant"
,
style
:
true
})
]
})
});
});
},
css
:
{
loaderOptions
:
{
postcss
:
{
plugins
:
[
require
(
"postcss-plugin-px2rem"
)({
rootValue
:
75
,
propList
:
[
"*"
],
replace
:
true
,
mediaQuery
:
false
,
minPixelValue
:
2
,
exclude
:
/
(
node_module
)
/i
})
]
}
}
}
};
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