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 | 162 ++++++++++++++++++++++++++++++++++++++++++----------- 1 files changed, 127 insertions(+), 35 deletions(-) diff --git a/src/stores/tasks.ts b/src/stores/tasks.ts index 8f8599d..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,15 +20,18 @@ /** * 鑾峰彇浠诲姟鏁版嵁 * @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') || '' } - getTaskList(params) + + return getTaskList(params) .then((res) => { channels.value = res.data }) @@ -33,6 +39,95 @@ console.error(err) channels.value = [] }) + .finally(() => { + if (init) { + // 棣栨鑾峰彇閫氶亾鏁版嵁鏃惰嚜鍔ㄩ�変腑绗竴涓换鍔� + selectFirstTask(channels.value) + } + }) + } + + function selectFirstTask(channels: TasksGroupByChannel) { + const firstNotEmptyChannel = Object.entries(channels).find((ele) => { + const taskList = (ele[1] as TasksResponse)?.Tasks + + return !!taskList?.length + }) + + if (firstNotEmptyChannel && (token !== null || token !== '' || token !== 'undefined')) { + const channelNumber = +firstNotEmptyChannel[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) + } + } + } + + /** + * 鏁版嵁鍔犺浇瀹屾垚鍚庤嚜鍔ㄩ�変腑涓�涓换鍔� + */ + function autoSelectTask(channel: number) { + const currentChannelTaskList = channels.value[channel].Tasks + 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 + + return !!taskList.length + }) + + if (firstNotEmptyChannel) { + const channelNumber = +firstNotEmptyChannel[0] + activeTask.value = channels.value[channelNumber].Tasks[0] + setActiveChannel(channel) + } + } + } + + /** + * 鍒锋柊鎵�鏈夋暟鎹� + */ + function reloadChannel(channel: number) { + if (token !== null || token !== '' || token !== 'undefined') { + return getChannels(currentType.value).then(() => { + autoSelectTask(channel) + }) + } else { + return + } + } + + function reloadAllData() { + getChannels(currentType.value) } function moreChannelTasksBtn(channelNumber: number) { @@ -41,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) { @@ -71,36 +170,29 @@ /** 褰撳墠楂樹寒鐨勪换鍔� */ 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 requestParamsMap = ref<{ - [channel: number]: TaskListParams - }>({}) - function getParamsByChannel(channel: number) { - return ( - requestParamsMap.value[channel] ?? { - type: 1, - offset: 0, - limit: 3 - } - ) + const activeChannel = ref<number>(0) + function setActiveChannel(channelNumber: number) { + activeChannel.value = channelNumber } - function setParamsByChannel(channel: number, params: TaskListParams) { - requestParamsMap.value[channel] = params - } return { channels, getChannels, moreBtnStatus, activeTask, + reloadChannel, setActiveTask, - requestParamsMap, - getParamsByChannel, - setParamsByChannel, moreChannelTasksBtn, - foldChannelTasksBtn + foldChannelTasksBtn, + reloadAllData, + activeChannel, + setActiveChannel } }) -- Gitblit v1.8.0