From c7f3fd5215399b37d0511b3bd555150ff1b13507 Mon Sep 17 00:00:00 2001
From: charles <981744753@qq.com>
Date: 星期一, 29 四月 2024 10:39:30 +0800
Subject: [PATCH] fix:回退原先版本

---
 src/stores/tasks.ts |  117 +++++++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 87 insertions(+), 30 deletions(-)

diff --git a/src/stores/tasks.ts b/src/stores/tasks.ts
index e115738..a1b3af8 100644
--- a/src/stores/tasks.ts
+++ b/src/stores/tasks.ts
@@ -1,14 +1,17 @@
 import { computed, ref } from 'vue'
 import { defineStore } from 'pinia'
 import type { Task, TasksGroupByChannel, TasksResponse } from '@/api/task'
-import type { TaskListParams } from '@/api'
-import { getTaskList } from '@/api'
+import type { TaskListParams, TaskInfoParams } from '@/api'
+import { getTaskList, getTaskInfo } from '@/api'
+import { isNumber } from 'lodash-es'
+import { getToken } from '@/common/utils/index'
 
 export interface ChannelMoreBtnStatus {
   /** true 浠诲姟鏈姞杞藉畬  false 鎵�鏈変换鍔″凡缁忓姞杞藉畬鎴�*/
   [channel: number]: boolean
 }
 
+const token = getToken()
 export const useTasksStore = defineStore('tasks', () => {
   const channels = ref<TasksGroupByChannel>({})
 
@@ -17,26 +20,30 @@
   /**
    * 鑾峰彇浠诲姟鏁版嵁
    * @param type 1鏈畬鎴�2浠婂ぉ鏈畬鎴�3宸插畬鎴�
+   * @param init 鏄惁鏄娆¤幏鍙�,棣栨闇�瑕侀�変腑绗竴椤逛换鍔�
    */
-  function getChannels(type: 1 | 2 | 3) {
+  function getChannels(type: 1 | 2 | 3, init = false) {
     currentType.value = type
     const params: TaskListParams = {
       type,
       offset: 0,
-      limit: 3
+      limit: 3,
+      deviceID: localStorage.getItem('currentDeviceID') || ''
     }
+
     return getTaskList(params)
       .then((res) => {
         channels.value = res.data
-
-        // 棣栨鑾峰彇閫氶亾鏁版嵁鏃惰嚜鍔ㄩ�変腑绗竴涓换鍔�
-        if (!activeTask?.value) {
-          selectFirstTask(channels.value)
-        }
       })
       .catch((err) => {
         console.error(err)
         channels.value = []
+      })
+      .finally(() => {
+        if (init) {
+          // 棣栨鑾峰彇閫氶亾鏁版嵁鏃惰嚜鍔ㄩ�変腑绗竴涓换鍔�
+          selectFirstTask(channels.value)
+        }
       })
   }
 
@@ -44,12 +51,31 @@
     const firstNotEmptyChannel = Object.entries(channels).find((ele) => {
       const taskList = (ele[1] as TasksResponse)?.Tasks
 
-      return !!taskList.length
+      return !!taskList?.length
     })
 
-    if (firstNotEmptyChannel) {
+    if (firstNotEmptyChannel && (token !== null || token !== '' || token !== 'undefined')) {
       const channelNumber = +firstNotEmptyChannel[0]
-      activeTask.value = channels[channelNumber].Tasks[0]
+      // activeTask.value = channels[channelNumber].Tasks[0]
+      console.log(channels[channelNumber].Tasks[0].Procedure.ID, '1111')
+      const params: TaskInfoParams = {
+        deviceID: localStorage.getItem('currentDeviceID') || '',
+        procedureID: channels[channelNumber].Tasks[0].Procedure.ID
+      }
+      return getTaskInfo(params)
+        .then((res) => {
+          activeTask.value = res.data
+        })
+        .catch((err) => {
+          console.error(err)
+        })
+        .finally(() => {})
+    } else {
+      // 濡傛灉娌℃湁浠诲姟灏辨竻绌哄綋鍓嶉�変腑鐨勪换鍔�
+      activeTask.value = undefined
+      if (channels[0]) {
+        setActiveChannel(0)
+      }
     }
   }
 
@@ -58,8 +84,20 @@
    */
   function autoSelectTask(channel: number) {
     const currentChannelTaskList = channels.value[channel].Tasks
-    if (currentChannelTaskList?.length) {
-      activeTask.value = currentChannelTaskList[0]
+    if (currentChannelTaskList?.length && (token !== null || token !== '' || token !== 'undefined')) {
+      // activeTask.value = currentChannelTaskList[0].Procedure.ID
+      const params: TaskInfoParams = {
+        deviceID: localStorage.getItem('currentDeviceID') || '',
+        procedureID: currentChannelTaskList[0].Procedure.ID
+      }
+      return getTaskInfo(params)
+        .then((res) => {
+          activeTask.value = res.data
+        })
+        .catch((err) => {
+          console.error(err)
+        })
+        .finally(() => {})
     } else {
       const firstNotEmptyChannel = Object.entries(channels.value).find((ele) => {
         const taskList = (ele[1] as TasksResponse)?.Tasks
@@ -70,6 +108,7 @@
       if (firstNotEmptyChannel) {
         const channelNumber = +firstNotEmptyChannel[0]
         activeTask.value = channels.value[channelNumber].Tasks[0]
+        setActiveChannel(channel)
       }
     }
   }
@@ -78,9 +117,13 @@
    * 鍒锋柊鎵�鏈夋暟鎹�
    */
   function reloadChannel(channel: number) {
-    getChannels(currentType.value).then(() => {
-      autoSelectTask(channel)
-    })
+    if (token !== null || token !== '' || token !== 'undefined') {
+      return getChannels(currentType.value).then(() => {
+        autoSelectTask(channel)
+      })
+    } else {
+      return
+    }
   }
 
   function reloadAllData() {
@@ -93,18 +136,22 @@
       type: currentType.value,
       channel: channelNumber,
       offset: taskLength,
-      limit: 10
+      limit: 10,
+      deviceID: localStorage.getItem('currentDeviceID') || ''
     }
-    getTaskList(params)
-      .then((res) => {
-        const existTasks = channels.value![channelNumber].Tasks ?? []
-        channels.value[channelNumber] = res.data[channelNumber] ?? {}
-        channels.value[channelNumber].Tasks = channels.value[channelNumber].Tasks ?? []
-        channels.value[channelNumber].Tasks = [...existTasks, ...channels.value[channelNumber].Tasks]
-      })
-      .catch((err) => {
-        console.error(err)
-      })
+
+    if (token !== null || token !== '' || token !== 'undefined') {
+      getTaskList(params)
+        .then((res) => {
+          const existTasks = channels.value![channelNumber].Tasks ?? []
+          channels.value[channelNumber] = res.data[channelNumber] ?? {}
+          channels.value[channelNumber].Tasks = channels.value[channelNumber].Tasks ?? []
+          channels.value[channelNumber].Tasks = [...existTasks, ...channels.value[channelNumber].Tasks]
+        })
+        .catch((err) => {
+          console.error(err)
+        })
+    }
   }
 
   function foldChannelTasksBtn(channelNumber: number) {
@@ -123,8 +170,16 @@
 
   /** 褰撳墠楂樹寒鐨勪换鍔� */
   const activeTask = ref<Task>()
-  function setActiveTask(task: Task) {
+  function setActiveTask(task: Task | undefined) {
     activeTask.value = task
+    if (isNumber(task?.Channel)) {
+      setActiveChannel(task?.Channel as number)
+    }
+  }
+
+  const activeChannel = ref<number>(0)
+  function setActiveChannel(channelNumber: number) {
+    activeChannel.value = channelNumber
   }
 
   return {
@@ -136,6 +191,8 @@
     setActiveTask,
     moreChannelTasksBtn,
     foldChannelTasksBtn,
-    reloadAllData
+    reloadAllData,
+    activeChannel,
+    setActiveChannel
   }
 })

--
Gitblit v1.8.0