Browse Source

添加判断是否再wx中打开

一只小菜杨 2 months ago
parent
commit
ed9435639f
5 changed files with 22 additions and 2 deletions
  1. 1 0
      global.d.ts
  2. 13 2
      index.html
  3. 4 0
      src/router/index.ts
  4. 2 0
      src/views/Automate.vue
  5. 2 0
      src/views/ProjectInfo.vue

+ 1 - 0
global.d.ts

@@ -1,4 +1,5 @@
 
 interface Window {
   wx: any
+  isInWx: boolean
 }

+ 13 - 2
index.html

@@ -9,9 +9,20 @@
       content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, viewport-fit=cover"
     />
     <title>PLC Editor</title>
-    <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
+    <!-- <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script> -->
     <script>
-      window.wx = wx // 将wx挂载到window对象上
+      const crtAgent = navigator.userAgent
+      const isInWx = false
+      if (crtAgent.includes('MicroMessenger')) {
+        window.isInWx = true
+        const scriptEl = document.createElement('script')
+        scriptEl.type = 'text/javascript'
+        scriptEl.src = 'https://res.wx.qq.com/open/js/jweixin-1.6.0.js'
+        document.head.appendChild(scriptEl)
+        scriptEl.onload = function () {
+          window.wx = wx // 将wx挂载到window对象上
+        }
+      }
     </script>
   </head>
   <body>

+ 4 - 0
src/router/index.ts

@@ -53,6 +53,10 @@ const getWxConfig = (url: string) => {
 }
 
 router.beforeEach((to, from, next) => {
+  if (!window.isInWx) {
+    next()
+    return
+  }
   const url = location.origin + location.pathname + location.search
   // console.log(url)
   const wxConfigStr = localStorage.getItem(url)

+ 2 - 0
src/views/Automate.vue

@@ -136,6 +136,7 @@ const handleCancel = async () => {
 
 const wx = window.wx
 const initWxEnv = () => {
+  if (!window.isInWx) return showFailToast('请在微信中打开')
   const url = location.origin + location.pathname + location.search
   const wxConfigStr = localStorage.getItem(url)
   if (!wxConfigStr) showFailToast('初始化失败')
@@ -152,6 +153,7 @@ const initWxEnv = () => {
 }
 initWxEnv()
 const importProject = () => {
+  if (!window.isInWx) return
   wx.scanQRCode({
     needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
     scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码,默认二者都有

+ 2 - 0
src/views/ProjectInfo.vue

@@ -114,6 +114,7 @@ let wx = window.wx
 //     })
 //   })
 const initWxEnv = () => {
+  if (!window.isInWx) return showFailToast('请在微信中打开')
   const url = location.origin + location.pathname + location.search
   const wxConfigStr = localStorage.getItem(url)
   if (!wxConfigStr) showFailToast('初始化失败')
@@ -1520,6 +1521,7 @@ const showSharePopup = ref(false)
 const shareImg = ref('')
 // const qrcodeCanvas = ref<HTMLCanvasElement>()
 const shareProject = async () => {
+  if (!window.isInWx) return
   showSharePopup.value = true
   await nextTick()
   qrcode.toDataURL(