usePageRouter.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { inject, onMounted, onUnmounted } from "vue";
  2. import { useRoute, useRouter } from "vue-router";
  3. import { LockBill, UnLockBill } from "@/api/modules/common";
  4. /**
  5. * @description 路由器跳转封装
  6. * */
  7. export const usePageRouter = () => {
  8. const route = useRoute();
  9. const router = useRouter();
  10. const tabRemove: Function = inject("tabRemove") as Function;
  11. /**
  12. * @description 页面打开, 兼容:如果有pageid参数就postMessage给K3,由K3打开
  13. * showType
  14. * ModalInCurrentForm: 只在当前单据中模态显示,可以操作其它单据(这情况可以内部实现,不需要post给K3)
  15. * MainNewTabPage: 主界面新页签(默认)
  16. * @param path 路由地址
  17. * @param title K3显示的标题
  18. */
  19. const pageOpen = (path: string, title?: string) => {
  20. let pageid = window.location.href.split("pageid=")[1];
  21. if (pageid) {
  22. // let poststr = JSON.stringify({
  23. // pageid: pageid,
  24. // putdata: {
  25. // url: path,
  26. // title: getRouteTitle(path, title),
  27. // showType: "MainNewTabPage"
  28. // }
  29. // });
  30. // let _window = window as any;
  31. // if (_window?.chrome?.webview) {
  32. // // 客户端
  33. // console.log("_window.chrome.webview.postMessage ", poststr, ";_window ", _window);
  34. // _window.chrome.webview.postMessage(poststr);
  35. // } else if (window.top) {
  36. // // 浏览器
  37. // console.log("window.top.postMessage ", poststr, ";window ", window);
  38. // window.top.postMessage(poststr, "*");
  39. // } else if (window.parent) {
  40. // // 一般不使用
  41. // console.log("window.parent.postMessage ", poststr, ";window ", window);
  42. // window.parent.postMessage(poststr, "*");
  43. // } else {
  44. // // 一般不使用
  45. // console.log("window.postMessage ", poststr, ";window ", window);
  46. // window.postMessage(poststr, "*");
  47. // }
  48. } else {
  49. router.push(path);
  50. }
  51. };
  52. const pageLockOpen = async (path: string, keyword: string, billid: number, billcode: string) => {
  53. await LockBill({ keyword, billid, billcode });
  54. router.push(path);
  55. };
  56. const pageLockRefresh = async (path: string, keyword: string, billid: number, billcode: string) => {
  57. await LockBill({ keyword, billid, billcode });
  58. router.replace(path);
  59. };
  60. const pageUnLockRefresh = async (path: string, keyword: string, billid: number, billcode: string) => {
  61. await UnLockBill({ keyword, billid, billcode });
  62. router.replace(path);
  63. };
  64. /**
  65. * @description 页面关闭, 兼容:如果有pageid参数就postMessage给K3,由K3关闭
  66. * */
  67. const pageClose = () => {
  68. let pageid = window.location.href.split("pageid=")[1];
  69. if (pageid) {
  70. // let poststr = JSON.stringify({
  71. // pageid: pageid,
  72. // putdata: {
  73. // url: "close"
  74. // }
  75. // });
  76. // let _window = window as any;
  77. // if (_window?.chrome?.webview) {
  78. // _window.chrome.webview.postMessage(poststr);
  79. // } else {
  80. // window.top?.postMessage(poststr, "*");
  81. // }
  82. } else {
  83. tabRemove(route.fullPath);
  84. }
  85. };
  86. /**
  87. * @description 页面刷新, 兼容:如果有pageid参数就postMessage给K3,由K3刷新
  88. * showType
  89. * InCurrentForm: 在当前单据中刷新
  90. * MainNewTabPage: 主界面新页签(默认)
  91. * @param path 路由地址
  92. * @param title K3显示的标题
  93. */
  94. const pageRefresh = (path: string, title?: string) => {
  95. let pageid = window.location.href.split("pageid=")[1];
  96. if (pageid) {
  97. // let poststr = JSON.stringify({
  98. // pageid: pageid,
  99. // putdata: {
  100. // url: path,
  101. // title: getRouteTitle(path, title),
  102. // showType: "InCurrentForm"
  103. // }
  104. // });
  105. // let _window = window as any;
  106. // if (_window?.chrome?.webview) {
  107. // _window.chrome.webview.postMessage(poststr);
  108. // } else {
  109. // window.top?.postMessage(poststr, "*");
  110. // }
  111. } else {
  112. router.replace(path);
  113. }
  114. };
  115. /**
  116. * @description 获取路由标题
  117. * @param path 路由地址
  118. * @param title 标题
  119. */
  120. const getRouteTitle = (path: string, title?: string) => {
  121. if (title) return title;
  122. const basePath = path.split("?")[0];
  123. try {
  124. const resolved = router.resolve(basePath);
  125. if (resolved.matched.length === 0) return "";
  126. // 获取最深层级的路由记录(支持嵌套路由)
  127. const matchedRoute = resolved.matched[resolved.matched.length - 1];
  128. return matchedRoute.meta?.title || "";
  129. } catch (e) {
  130. console.error("路由解析失败:", e);
  131. return "";
  132. }
  133. };
  134. /**
  135. * @description 自动监听message事件
  136. * */
  137. const useAutoMessageHandler = (handler: (e: MessageEvent) => void) => {
  138. onMounted(() => {
  139. addMessageHandler(handler);
  140. });
  141. onUnmounted(() => {
  142. removeMessageHandler(handler);
  143. });
  144. };
  145. /**
  146. * @description 手动监听message事件
  147. * */
  148. const addMessageHandler = (handler: (e: MessageEvent) => void) => {
  149. let _window = window as any;
  150. if (_window?.chrome?.webview) {
  151. _window.chrome.webview.addEventListener("message", handler);
  152. } else {
  153. window.addEventListener("message", handler);
  154. }
  155. };
  156. /**
  157. * @description 手动移除message事件
  158. * */
  159. const removeMessageHandler = (handler: (e: MessageEvent) => void) => {
  160. let _window = window as any;
  161. if (_window?.chrome?.webview) {
  162. _window.chrome.webview.removeEventListener("message", handler);
  163. } else {
  164. window.removeEventListener("message", handler);
  165. }
  166. };
  167. return {
  168. pageOpen,
  169. pageClose,
  170. pageRefresh,
  171. pageLockOpen,
  172. pageLockRefresh,
  173. pageUnLockRefresh,
  174. useAutoMessageHandler
  175. };
  176. };