Parcourir la source

完成公众号跳转小程序的功能

jevian ma(马作伟_沃航科技) il y a 2 mois
Parent
commit
edfc7ff3bb
2 fichiers modifiés avec 53 ajouts et 8 suppressions
  1. 4 1
      index.html
  2. 49 7
      src/views/ProjectInfo.vue

+ 4 - 1
index.html

@@ -9,10 +9,13 @@
       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>
+      window.wx = wx // 将wx挂载到window对象上
+    </script>
   </head>
   <body>
     <div id="app"></div>
-    <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
     <script type="module" src="/src/main.ts"></script>
   </body>
 </html>

+ 49 - 7
src/views/ProjectInfo.vue

@@ -27,6 +27,8 @@
       </van-swipe-cell>
     </div>
     <div class="add-btn">
+      <div style="text-align: center;">{{project_status}}</div>
+      <div v-html="wxtag"></div>
       <van-button type="primary" block @click="handleAddCommond">添加任务</van-button>
     </div>
 
@@ -35,12 +37,6 @@
         <van-cell title="编辑工程名称" clickable @click="handleEditName" />
         <van-cell title="编译文件" @click="getXMLFile" clickable />
         <van-cell title="设备烧录" @click="burnToDevice" clickable />
-        <wx-open-launch-weapp username="小程序的原始ID(gh_开头)" path="pages/index/index?param1=xxx&param2=xxx">
-          <template>
-            <!-- 自定义跳转按钮样式 -->
-            <button style="padding: 10px; background: #07c160; color: white">点击跳转小程序</button>
-          </template>
-        </wx-open-launch-weapp>
         <van-cell title="删除工程" @click="delProject" clickable />
       </van-cell-group>
     </van-popup>
@@ -80,11 +76,43 @@ const router = useRouter()
 const { projectList } = storeToRefs(useGlobalStore())
 const crtProject = ref<ProjectType>()
 
+const project_status = ref('未编译')
+const wxtag = ref('')
+
+let wx = window.wx
+console.log('wx', wx)
+
+fetch('https://plceditor.worldflying.cn/api/build/getjsconfig', {
+  method: 'POST',
+  body: JSON.stringify({url: location.origin + location.pathname + location.search})
+}).then((res) => res.json()).then((res) => {
+  wx.config(res.data)
+  wx.ready(function () {
+    console.log('微信初始化成功')
+  })
+  wx.error(function(res:any){
+    console.log('微信初始化失败', res)
+  })
+})
+
 watch(
   () => route,
   () => {
     crtProject.value = projectList.value.find((item) => item.id === route.params.id)
     // commandList.value = crtProject.value!.commandList
+    let imgobjStr = localStorage.getItem('imgobj' + crtProject.value!.id)
+    if (imgobjStr) {
+      try {
+        let imgobj = JSON.parse(imgobjStr)
+        let now = new Date().getTime() // 当前时间戳
+        if (now < imgobj.deadline) {
+          project_status.value = '已编译'
+          wxtag.value = '<wx-open-launch-weapp appid="wx4c5a777c71f2981c" path="pages/index/rj45cfgbybt/plc/index?imgurl=' + imgobj.url + '"><template><button>烧录设备</button></template></wx-open-launch-weapp>'
+        }
+      } catch (e) {
+        console.log(e)
+      }
+    }
   },
   {
     deep: true,
@@ -1365,7 +1393,7 @@ const getXMLFile = () => {
       </instances>
     </project>`
 
-  console.log(xml)
+  // console.log(xml)
   // 生成文件下载
   // const blob = new Blob([xml], { type: 'text/plain;charset=utf-8' })
   // const formdata = new FormData()
@@ -1385,6 +1413,19 @@ const getXMLFile = () => {
     .then((res) => res.json())
     .then((res) => {
       console.log(res)
+      if (res.errcode === 3000) {
+        console.log(res.errmsg)
+      } else if (res.errcode === 0) {
+        localStorage.setItem('imgobj' + crtProject.value!.id, JSON.stringify({
+          url: res.url,
+          deadline: new Date().getTime() + 30*60*1000
+        }))
+        project_status.value = '已编译'
+        let w = '<wx-open-launch-weapp appid="wx4c5a777c71f2981c" path="pages/index/rj45cfgbybt/plc/index?imgurl=' + res.url + '"><template><button>烧录设备</button></template></wx-open-launch-weapp>'
+        console.log(w)
+        wxtag.value = w
+        console.log('本地存储成功')
+      }
     })
 
   // const link = document.createElement('a')
@@ -1408,6 +1449,7 @@ const delProject = () => {
       .then(() => {
         // on confirm
         console.log(projectList.value)
+        localStorage.removeItem('imgobj' + crtProject.value!.id)
         projectList.value = projectList.value.filter((item) => crtProject.value?.id !== item.id)
         router.replace({ name: 'Automate' })
       })