Commit 135ef5fe authored by wykolor's avatar wykolor

Merge branch 'feature/2021_03_12_bonus' of dev.jiuaoedu.com:wykolor/GeekBonusH5…

Merge branch 'feature/2021_03_12_bonus' of dev.jiuaoedu.com:wykolor/GeekBonusH5 into feature/2021_03_12_bonus
parents 8aec21dd fa44f64f
......@@ -3,7 +3,6 @@ import App from "./App.vue";
import router from "./router";
import store from "./store";
import "./assets/style/normalize.css";
import "vant/lib/index.css";
import "amfe-flexible";
import nativeBridge from './util/nativeBridge'
createApp(App)
......
......@@ -15,6 +15,11 @@ const routes: Array<RouteRecordRaw> = [
path: "/withdraw",
name: "withdraw",
component: () => import("../views/bonus/withdraw/index.vue")
},
{
path: "/test",
name: "test",
component: () => import("../views/test/index.vue")
}
];
......
/* eslint-disable @typescript-eslint/camelcase */
import { App } from "vue";
import mitt from "./mitt";
......
/* 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 };
<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>
/* eslint-disable @typescript-eslint/no-var-requires */
// const tsImportPluginFactory = require('ts-import-plugin')
const tsImportPluginFactory = require("ts-import-plugin");
module.exports = {
devServer: {
hot: true,
......@@ -24,6 +24,25 @@ module.exports = {
}
}
},
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: {
......
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