Commit 64d7469d authored by wykolor's avatar wykolor

feat: 通信

parent 135ef5fe
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");
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;
/* 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;
/* 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 };
/* 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
})
]
}
}
}
};
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