123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- import { inject, onMounted, onUnmounted } from "vue";
- import { useRoute, useRouter } from "vue-router";
- import { LockBill, UnLockBill } from "@/api/modules/common";
- /**
- * @description 路由器跳转封装
- * */
- export const usePageRouter = () => {
- const route = useRoute();
- const router = useRouter();
- const tabRemove: Function = inject("tabRemove") as Function;
- /**
- * @description 页面打开, 兼容:如果有pageid参数就postMessage给K3,由K3打开
- * showType
- * ModalInCurrentForm: 只在当前单据中模态显示,可以操作其它单据(这情况可以内部实现,不需要post给K3)
- * MainNewTabPage: 主界面新页签(默认)
- * @param path 路由地址
- * @param title K3显示的标题
- */
- const pageOpen = (path: string, title?: string) => {
- let pageid = window.location.href.split("pageid=")[1];
- if (pageid) {
- // let poststr = JSON.stringify({
- // pageid: pageid,
- // putdata: {
- // url: path,
- // title: getRouteTitle(path, title),
- // showType: "MainNewTabPage"
- // }
- // });
- // let _window = window as any;
- // if (_window?.chrome?.webview) {
- // // 客户端
- // console.log("_window.chrome.webview.postMessage ", poststr, ";_window ", _window);
- // _window.chrome.webview.postMessage(poststr);
- // } else if (window.top) {
- // // 浏览器
- // console.log("window.top.postMessage ", poststr, ";window ", window);
- // window.top.postMessage(poststr, "*");
- // } else if (window.parent) {
- // // 一般不使用
- // console.log("window.parent.postMessage ", poststr, ";window ", window);
- // window.parent.postMessage(poststr, "*");
- // } else {
- // // 一般不使用
- // console.log("window.postMessage ", poststr, ";window ", window);
- // window.postMessage(poststr, "*");
- // }
- } else {
- router.push(path);
- }
- };
- const pageLockOpen = async (path: string, keyword: string, billid: number, billcode: string) => {
- await LockBill({ keyword, billid, billcode });
- router.push(path);
- };
- const pageLockRefresh = async (path: string, keyword: string, billid: number, billcode: string) => {
- await LockBill({ keyword, billid, billcode });
- router.replace(path);
- };
- const pageUnLockRefresh = async (path: string, keyword: string, billid: number, billcode: string) => {
- await UnLockBill({ keyword, billid, billcode });
- router.replace(path);
- };
- /**
- * @description 页面关闭, 兼容:如果有pageid参数就postMessage给K3,由K3关闭
- * */
- const pageClose = () => {
- let pageid = window.location.href.split("pageid=")[1];
- if (pageid) {
- // let poststr = JSON.stringify({
- // pageid: pageid,
- // putdata: {
- // url: "close"
- // }
- // });
- // let _window = window as any;
- // if (_window?.chrome?.webview) {
- // _window.chrome.webview.postMessage(poststr);
- // } else {
- // window.top?.postMessage(poststr, "*");
- // }
- } else {
- tabRemove(route.fullPath);
- }
- };
- /**
- * @description 页面刷新, 兼容:如果有pageid参数就postMessage给K3,由K3刷新
- * showType
- * InCurrentForm: 在当前单据中刷新
- * MainNewTabPage: 主界面新页签(默认)
- * @param path 路由地址
- * @param title K3显示的标题
- */
- const pageRefresh = (path: string, title?: string) => {
- let pageid = window.location.href.split("pageid=")[1];
- if (pageid) {
- // let poststr = JSON.stringify({
- // pageid: pageid,
- // putdata: {
- // url: path,
- // title: getRouteTitle(path, title),
- // showType: "InCurrentForm"
- // }
- // });
- // let _window = window as any;
- // if (_window?.chrome?.webview) {
- // _window.chrome.webview.postMessage(poststr);
- // } else {
- // window.top?.postMessage(poststr, "*");
- // }
- } else {
- router.replace(path);
- }
- };
- /**
- * @description 获取路由标题
- * @param path 路由地址
- * @param title 标题
- */
- const getRouteTitle = (path: string, title?: string) => {
- if (title) return title;
- const basePath = path.split("?")[0];
- try {
- const resolved = router.resolve(basePath);
- if (resolved.matched.length === 0) return "";
- // 获取最深层级的路由记录(支持嵌套路由)
- const matchedRoute = resolved.matched[resolved.matched.length - 1];
- return matchedRoute.meta?.title || "";
- } catch (e) {
- console.error("路由解析失败:", e);
- return "";
- }
- };
- /**
- * @description 自动监听message事件
- * */
- const useAutoMessageHandler = (handler: (e: MessageEvent) => void) => {
- onMounted(() => {
- addMessageHandler(handler);
- });
- onUnmounted(() => {
- removeMessageHandler(handler);
- });
- };
- /**
- * @description 手动监听message事件
- * */
- const addMessageHandler = (handler: (e: MessageEvent) => void) => {
- let _window = window as any;
- if (_window?.chrome?.webview) {
- _window.chrome.webview.addEventListener("message", handler);
- } else {
- window.addEventListener("message", handler);
- }
- };
- /**
- * @description 手动移除message事件
- * */
- const removeMessageHandler = (handler: (e: MessageEvent) => void) => {
- let _window = window as any;
- if (_window?.chrome?.webview) {
- _window.chrome.webview.removeEventListener("message", handler);
- } else {
- window.removeEventListener("message", handler);
- }
- };
- return {
- pageOpen,
- pageClose,
- pageRefresh,
- pageLockOpen,
- pageLockRefresh,
- pageUnLockRefresh,
- useAutoMessageHandler
- };
- };
|