Browse Source

添加编译条件

一只小菜杨 2 months ago
parent
commit
79822b720f
3 changed files with 52 additions and 31 deletions
  1. 10 7
      src/views/CommandInfo.vue
  2. 34 17
      src/views/ProjectInfo.vue
  3. 8 7
      vite.config.ts

+ 10 - 7
src/views/CommandInfo.vue

@@ -124,9 +124,9 @@
         </div>
       </div>
       <div class="empty" v-else>当前没有可执行任务</div>
-      <div class="footer" v-if="crtCommand!.stepList.length">
-        <div class="tip">如果条件不满足,将会从第一步开始重新执行</div>
-        <van-button type="primary" :disabled="!crtCommand!.stepList.length" @click="handleSaveCommand()"
+      <div class="footer" v-if="crtCommand!.stepList.length || isExistCnt">
+        <div class="tip">如果没有执行命令,则本条任务不会被编译;如果条件不满足,将会从第一步开始重新执行</div>
+        <van-button type="primary" :disabled="!crtCommand!.stepList.length && !isExistCnt" @click="handleSaveCommand()"
           >保存</van-button
         >
       </div>
@@ -183,14 +183,17 @@ const textMap: Record<string, string> = {
   delay: '延时',
   exec: '执行'
 }
+const isExistCnt = ref(false)
 
 watch(
   () => route,
   () => {
-    const project = projectList.value.find((item) => item.id === route.params.pid)
-    crtProject.value = project
-    if (project) {
-      const command = project.commandList!.find((item) => item.id === route.params.id) || []
+    crtProject.value= projectList.value.find((item) => item.id === route.params.pid)
+    if (crtProject.value) {
+      const command = crtProject.value.commandList!.find((item) => item.id === route.params.id)!
+      if (command.stepList.length) {
+        isExistCnt.value = true
+      }
       crtCommand.value = deepClone(command) as CommandType
       console.log(crtCommand.value)
     }

+ 34 - 17
src/views/ProjectInfo.vue

@@ -9,7 +9,7 @@
     </div>
 
     <div class="command-container">
-      <van-swipe-cell v-for="item in commandList" :key="item.id" :disabled="touchStatus">
+      <van-swipe-cell v-for="item in crtProject!.commandList" :key="item.id" :disabled="touchStatus">
         <van-cell
           clickable
           :border="false"
@@ -79,13 +79,12 @@ const router = useRouter()
 
 const { projectList } = storeToRefs(useGlobalStore())
 const crtProject = ref<ProjectType>()
-const commandList = ref<CommandType[]>([])
 
 watch(
   () => route,
   () => {
     crtProject.value = projectList.value.find((item) => item.id === route.params.id)
-    commandList.value = crtProject.value!.commandList
+    // commandList.value = crtProject.value!.commandList
   },
   {
     deep: true,
@@ -141,21 +140,22 @@ const handleCancel = async () => {
 }
 
 const handleDelete = (val: string) => {
-  console.log(val)
-  console.log(commandList.value)
   if (val) {
-    commandList.value = commandList.value.filter((item) => item.id !== val)
+    const project = projectList.value.find((item) => (item.id = crtProject.value!.id))
+    if (project) {
+      project.commandList = project.commandList.filter((item) => item.id !== val)
+    }
   }
 }
 
 const handleAddCommond = () => {
-  let name = `任务${commandList.value.length + 1}`
-  if (commandList.value.length) {
-    const beforeName = commandList.value[commandList.value.length - 1].name
+  let name = `任务${crtProject.value!.commandList.length + 1}`
+  if (crtProject.value!.commandList.length) {
+    const beforeName = crtProject.value!.commandList[crtProject.value!.commandList.length - 1].name
     const index = beforeName.split('任务')[1]
     name = `任务${+index + 1}`
   }
-  commandList.value.push({
+  crtProject.value!.commandList.push({
     id: '' + Date.now(),
     name,
     time: new Date().toLocaleString(),
@@ -169,6 +169,12 @@ const handleAddCommond = () => {
 // 文件编译
 const getXMLFile = () => {
   if (!crtProject.value) return
+  if (crtProject.value.commandList.length === 0) return showFailToast('请先添加任务')
+  if (crtProject.value.commandList.length === 1 && crtProject.value.commandList[0].stepList.length === 0) {
+    return showFailToast('任务内容为空')
+  }
+  const isExistExec = crtProject.value.commandList.some((item) => item.stepList.some((cItem) => cItem.type === 'exec' && cItem.list?.length !== 0))
+  if (!isExistExec) return showFailToast('请先添加执行任务')
   let xml = ''
   // 头部
   xml += `<?xml version="1.0" encoding="UTF-8"?>
@@ -244,6 +250,7 @@ const getXMLFile = () => {
                       </variable>`
   })
   crtProject.value.commandList.forEach((item, index) => {
+    if (!item.stepList.some(cItem => cItem.type === 'exec' && cItem.list?.length !== 0)) return
     instanceXml += `<pouInstance name="instance${index}" typeName="program${index}"/>`
     xml += `<pou name="program${index}" pouType="program">
               <interface>
@@ -1360,15 +1367,25 @@ const getXMLFile = () => {
 
   console.log(xml)
   // 生成文件下载
-  const blob = new Blob([xml], { type: 'text/plain;charset=utf-8' })
-  const formdata = new FormData()
-  formdata.append('file', blob, 'project.xml')
-  fetch('http://192.168.88.1:8080/api/upload', {
+  // const blob = new Blob([xml], { type: 'text/plain;charset=utf-8' })
+  // const formdata = new FormData()
+  // formdata.append('file', blob, 'project.xml')
+  const params = {
+    devicetype: 'PLC101',
+    xml
+  }
+  fetch('https://plceditor.worldflying.cn/api/build/buildcode', {
     method: 'POST',
-    body: formdata
-  }).then((res) => res.json()).then((res) => {
-    console.log(res)
+    body: JSON.stringify(params),
+    headers: {
+      'Content-Type': 'application/x-www-form-urlencoded',
+      'Content-Length': xml.length + ''
+    }
   })
+    .then((res) => res.json())
+    .then((res) => {
+      console.log(res)
+    })
 
   // const link = document.createElement('a')
   // link.href = URL.createObjectURL(blob)

+ 8 - 7
vite.config.ts

@@ -44,12 +44,13 @@ export default defineConfig({
     host: '0.0.0.0',
     cors: true,
     port: 8080,
-    proxy: {
-      '/api': {
-        target: 'http://smartelectric.worldflying.cn',
-        changeOrigin: true,
-        rewrite: (path) => path.replace(/^\/api/, '/api'),
-      }
-    }
+    // proxy: {
+    //   '/api': {
+    //     // target: 'http://smartelectric.worldflying.cn',
+    //     target: 'http://plceditor.worldflying.cn',
+    //     changeOrigin: true,
+    //     rewrite: (path) => path.replace(/^\/api/, '/api'),
+    //   }
+    // }
   }
 })