Commit 64d7469d authored by wykolor's avatar wykolor

feat: 通信

parent 135ef5fe
import { createApp } from "vue"; import { createApp } from "vue";
import App from "./App.vue"; import App from "./App.vue";
import router from "./router"; import router from "./router";
import store from "./store"; import store from "./store";
import "./assets/style/normalize.css"; import "./assets/style/normalize.css";
import "amfe-flexible"; import "amfe-flexible";
import nativeBridge from './util/nativeBridge' import nativeBridge from './util/nativeBridge'
createApp(App) createApp(App)
.use(store) .use(store)
.use(router) .use(router)
.use(nativeBridge) .use(nativeBridge)
.mount("#app"); .mount("#app");
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router"; import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import Bonus from "../views/bonus/index.vue"; import Bonus from "../views/bonus/index.vue";
const routes: Array<RouteRecordRaw> = [ const routes: Array<RouteRecordRaw> = [
{ {
path: "/", path: "/",
name: "Bonus", name: "Bonus",
component: Bonus component: Bonus
}, },
{ {
path: "/bonusDetail", path: "/bonusDetail",
name: "bonusDetail", name: "bonusDetail",
component: () => import("../views/bonus/bonus-detail/index.vue") component: () => import("../views/bonus/bonus-detail/index.vue")
}, },
{ {
path: "/withdraw", path: "/withdraw",
name: "withdraw", name: "withdraw",
component: () => import("../views/bonus/withdraw/index.vue") component: () => import("../views/bonus/withdraw/index.vue")
}, },
{ {
path: "/test", path: "/test",
name: "test", name: "test",
component: () => import("../views/test/index.vue") component: () => import("../views/test/index.vue")
} }
]; ];
const router = createRouter({ const router = createRouter({
history: createWebHashHistory(), history: createWebHashHistory(),
routes routes
}); });
export default router; export default router;
/* eslint-disable @typescript-eslint/camelcase */ /* eslint-disable @typescript-eslint/camelcase */
import { App } from "vue"; import { App } from "vue";
import mitt from "./mitt"; import mitt from "./mitt";
const isiOS = () => { const isiOS = () => {
const u = navigator.userAgent; const u = navigator.userAgent;
const iOs = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); const iOs = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
return iOs; return iOs;
}; };
const isAndroid = () => { const isAndroid = () => {
const u = navigator.userAgent; const u = navigator.userAgent;
const android = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1; const android = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1;
return android; return android;
}; };
const postMessage = data => { const postMessage = data => {
if (isAndroid()) { if (isAndroid()) {
window.ReactNativeWebView && window.ReactNativeWebView &&
window.ReactNativeWebView.onMessage && window.ReactNativeWebView.onMessage &&
window.ReactNativeWebView.onMessage(JSON.stringify(data)); window.ReactNativeWebView.onMessage(JSON.stringify(data));
} }
window.ReactNativeWebView && window.ReactNativeWebView &&
window.ReactNativeWebView.postMessage && window.ReactNativeWebView.postMessage &&
window.ReactNativeWebView.postMessage(JSON.stringify(data)); window.ReactNativeWebView.postMessage(JSON.stringify(data));
}; };
if (isiOS() || isAndroid()) { if (isiOS() || isAndroid()) {
// const debugLog = console.log // const debugLog = console.log
// eslint-disable-next-line func-names // eslint-disable-next-line func-names
// console.log = function (...args) { // console.log = function (...args) {
// postMessage({ // postMessage({
// type: 'log', // type: 'log',
// data: args // data: args
// }) // })
// debugLog(...args) // debugLog(...args)
// } // }
} }
function install(Vue: App) { function install(Vue: App) {
const GEEK_NATVIE_NAME = "GEEK_NATVIE_NAME"; const GEEK_NATVIE_NAME = "GEEK_NATVIE_NAME";
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
const event_bus = mitt(); const event_bus = mitt();
// eslint-disable-next-line func-names // eslint-disable-next-line func-names
window.$geek_excuteJS = function (args) { window.$geek_excuteJS = function (args) {
try { try {
const params = JSON.parse(args); const params = JSON.parse(args);
event_bus.emit(GEEK_NATVIE_NAME, params); event_bus.emit(GEEK_NATVIE_NAME, params);
} catch (error) { } catch (error) {
event_bus.emit(GEEK_NATVIE_NAME, args); event_bus.emit(GEEK_NATVIE_NAME, args);
} }
}; };
Vue.mixin({ Vue.mixin({
methods: { methods: {
listenNativeMessage(arg) { listenNativeMessage(arg) {
const { method, data } = arg; const { method, data } = arg;
if (method) { if (method) {
this[method] && this[method](data); this[method] && this[method](data);
} }
}, },
$nativeTitle(title) { $nativeTitle(title) {
const message = { const message = {
type: "title", type: "title",
data: title data: title
}; };
postMessage(message); postMessage(message);
}, },
$shareImg(base64Img, timeline = false) { $shareImg(base64Img, timeline = false) {
const message = { const message = {
type: timeline ? "shareTimeline" : "share", type: timeline ? "shareTimeline" : "share",
data: { data: {
type: "img", type: "img",
img: base64Img img: base64Img
} }
}; };
postMessage(message); postMessage(message);
}, },
$shareText(txt, timeline = false) { $shareText(txt, timeline = false) {
const message = { const message = {
type: timeline ? "shareTimeline" : "share", type: timeline ? "shareTimeline" : "share",
data: { data: {
type: "text", type: "text",
description: txt description: txt
} }
}; };
postMessage(message); postMessage(message);
}, },
$shareNews({ title, url, description }, timeline = false) { $shareNews({ title, url, description }, timeline = false) {
const message = { const message = {
type: timeline ? "shareTimeline" : "share", type: timeline ? "shareTimeline" : "share",
data: { data: {
type: "news", type: "news",
description, description,
webpageUrl: url, webpageUrl: url,
title title
} }
}; };
postMessage(message); postMessage(message);
}, },
$playVieo(url) { $playVieo(url) {
const message = { const message = {
type: "video", type: "video",
data: { data: {
url url
} }
}; };
postMessage(message); postMessage(message);
}, },
$postMessage(message) { $postMessage(message) {
postMessage(message); postMessage(message);
}, },
$exitWebView(data) { $exitWebView(data) {
this.$postMessage({ this.$postMessage({
type: "exitWebview", type: "exitWebview",
data data
}); });
}, },
$shareReport(params) { $shareReport(params) {
this.$postMessage({ this.$postMessage({
type: "reportShare", type: "reportShare",
params params
}); });
} }
}, },
created() { created() {
event_bus.on(GEEK_NATVIE_NAME, this.listenNativeMessage); event_bus.on(GEEK_NATVIE_NAME, this.listenNativeMessage);
}, },
beforeUnmount() { beforeUnmount() {
event_bus.off(GEEK_NATVIE_NAME, this.listenNativeMessage); event_bus.off(GEEK_NATVIE_NAME, this.listenNativeMessage);
} }
}); });
} }
export default install; export default install;
/* eslint-disable @typescript-eslint/camelcase */ /* eslint-disable @typescript-eslint/camelcase */
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
import { onBeforeUnmount, onMounted, getCurrentInstance, ComponentInternalInstance } from "vue"; import { onBeforeUnmount, onMounted, getCurrentInstance, ComponentInternalInstance } from "vue";
import mitt from "./mitt"; import mitt from "./mitt";
const eventBus = mitt(); const eventBus = mitt();
const GEEK_NATVIE_NAME = "GEEK_NATVIE_NAME"; const GEEK_NATVIE_NAME = "GEEK_NATVIE_NAME";
interface NativeInvokeFuncParams { interface NativeInvokeFuncParams {
method: string; method: string;
data: any; data: any;
} }
const isAndroid = () => { const isAndroid = () => {
const u = navigator.userAgent; const u = navigator.userAgent;
const android = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1; const android = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1;
return android; return android;
}; };
window.$geek_excuteJS = function(args) { window.$geek_excuteJS = function(args) {
try { try {
const params = JSON.parse(args); const params = JSON.parse(args);
eventBus.emit<NativeInvokeFuncParams>(GEEK_NATVIE_NAME, params); eventBus.emit<NativeInvokeFuncParams>(GEEK_NATVIE_NAME, params);
} catch (error) { } catch (error) {
eventBus.emit<NativeInvokeFuncParams>(GEEK_NATVIE_NAME, args); eventBus.emit<NativeInvokeFuncParams>(GEEK_NATVIE_NAME, args);
} }
}; };
const sendNativeMessage = (data: { type: string; data: any }) => { const sendNativeMessage = (data: { type: string; data: any }) => {
if (isAndroid()) { if (isAndroid()) {
window.ReactNativeWebView && window.ReactNativeWebView &&
window.ReactNativeWebView.onMessage && window.ReactNativeWebView.onMessage &&
window.ReactNativeWebView.onMessage(JSON.stringify(data)); window.ReactNativeWebView.onMessage(JSON.stringify(data));
} }
window.ReactNativeWebView && window.ReactNativeWebView &&
window.ReactNativeWebView.postMessage && window.ReactNativeWebView.postMessage &&
window.ReactNativeWebView.postMessage(JSON.stringify(data)); window.ReactNativeWebView.postMessage(JSON.stringify(data));
}; };
function useNativeBridage( function useNativeBridage(
eventType: string, eventType: string,
listener: (this: ComponentInternalInstance | null, ...arg: any[]) => any listener: (this: ComponentInternalInstance | null, ...arg: any[]) => any
): [typeof sendNativeMessage, typeof eventBus] { ): [typeof sendNativeMessage, typeof eventBus] {
const _listener = function(this: ComponentInternalInstance | null, arg?: NativeInvokeFuncParams) { const _listener = function(this: ComponentInternalInstance | null, arg?: NativeInvokeFuncParams) {
if (!arg) return; if (!arg) return;
if (eventType === arg.method) { if (eventType === arg.method) {
listener.apply(this, [arg.data]); listener.apply(this, [arg.data]);
} }
}; };
let fn: let fn:
| ((this: ComponentInternalInstance | null, arg?: NativeInvokeFuncParams) => void) | ((this: ComponentInternalInstance | null, arg?: NativeInvokeFuncParams) => void)
| null = null; | null = null;
onMounted(() => { onMounted(() => {
fn = _listener.bind(getCurrentInstance()); fn = _listener.bind(getCurrentInstance());
eventBus.on<NativeInvokeFuncParams>(GEEK_NATVIE_NAME, fn); eventBus.on<NativeInvokeFuncParams>(GEEK_NATVIE_NAME, fn);
}); });
onBeforeUnmount(() => { onBeforeUnmount(() => {
if (fn) { if (fn) {
eventBus.off<NativeInvokeFuncParams>(GEEK_NATVIE_NAME, fn); eventBus.off<NativeInvokeFuncParams>(GEEK_NATVIE_NAME, fn);
} }
}); });
return [sendNativeMessage, eventBus]; return [sendNativeMessage, eventBus];
} }
export { useNativeBridage }; export { useNativeBridage };
/* eslint-disable @typescript-eslint/no-var-requires */ /* eslint-disable @typescript-eslint/no-var-requires */
const tsImportPluginFactory = require("ts-import-plugin"); const tsImportPluginFactory = require("ts-import-plugin");
module.exports = { module.exports = {
devServer: { devServer: {
hot: true, hot: true,
open: true, open: true,
https: false, https: false,
proxy: { proxy: {
//配置跨域 //配置跨域
"/mock": { "/mock": {
target: "http://192.168.44.114:4100", target: "http://192.168.44.114:4100",
ws: true, ws: true,
changOrigin: true //允许跨域 changOrigin: true //允许跨域
}, },
"/api": { "/api": {
target: "http://192.168.44.114:4100", target: "http://192.168.44.114:4100",
ws: true, ws: true,
changOrigin: true //允许跨域 changOrigin: true //允许跨域
}, },
"/service": { "/service": {
target: "http://192.168.44.114:4100", target: "http://192.168.44.114:4100",
ws: true, ws: true,
changOrigin: true //允许跨域 changOrigin: true //允许跨域
} }
} }
}, },
chainWebpack: config => { chainWebpack: config => {
config.module config.module
.rule("ts") .rule("ts")
.use("ts-loader") .use("ts-loader")
.tap(options => { .tap(options => {
return Object.assign(options || {}, { return Object.assign(options || {}, {
transpileOnly: false, transpileOnly: false,
getCustomTransformers: () => ({ getCustomTransformers: () => ({
before: [ before: [
tsImportPluginFactory({ tsImportPluginFactory({
libraryDirectory: "es", libraryDirectory: "es",
libraryName: "vant", libraryName: "vant",
style: true style: true
}) })
] ]
}) })
}); });
}); });
}, },
css: { css: {
loaderOptions: { loaderOptions: {
postcss: { postcss: {
plugins: [ plugins: [
require("postcss-plugin-px2rem")({ require("postcss-plugin-px2rem")({
rootValue: 75, rootValue: 75,
propList: ["*"], propList: ["*"],
replace: true, replace: true,
mediaQuery: false, mediaQuery: false,
minPixelValue: 2, minPixelValue: 2,
exclude: /(node_module)/i exclude: /(node_module)/i
}) })
] ]
} }
} }
} }
}; };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment