wangzhibo
10 天以前 220e4fab27b7f419b3e89e2f2d28ae789aac09d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import type {
  ParentHistoryDetail,
  ParentHistoryList,
  ParentLanguagePair,
  ParentLesson,
  IdiomCatalog,
  ParentScene,
  ParentDeviceStatus,
  ParentTask,
  ParentTaskList,
  ParentTaskSave,
} from './types'
import { http } from '@/http/request/alova'
 
const noCache = {
  ignoreAuth: false,
  toast: false,
}
 
export function getParentScenes() {
  return http.Get<ParentScene[]>('/user/scenes', {
    meta: noCache,
    cacheFor: { expire: 0 },
  })
}
 
export function getParentIdiomCatalog() {
  return http.Get<IdiomCatalog>('/user/idiom-catalog', {
    meta: noCache,
    cacheFor: { expire: 0 },
  })
}
 
export function getParentLessons(scene: string) {
  return http.Get<ParentLesson[]>('/user/lessons', {
    params: { scene },
    meta: noCache,
    cacheFor: { expire: 0 },
  })
}
 
export function getParentLanguagePairs() {
  return http.Get<ParentLanguagePair[]>('/user/language-pairs', {
    meta: noCache,
    cacheFor: { expire: 0 },
  })
}
 
export function getParentDeviceStatus(macAddress: string, agentId: string) {
  return http.Get<ParentDeviceStatus>('/device/presence', {
    params: { macAddress, agentId },
    meta: noCache,
    cacheFor: { expire: 0 },
    timeout: 10000,
  })
}
 
export function getParentTasks(macAddress: string, agentId: string, runMode?: string) {
  return http.Get<ParentTaskList>('/user/parent-task', {
    params: {
      macAddress,
      agentId,
      ...(runMode ? { runMode } : {}),
    },
    meta: noCache,
    cacheFor: { expire: 0 },
  })
}
 
export function getParentTask(id: string) {
  return http.Get<ParentTask>(`/user/parent-task/${id}`, {
    meta: noCache,
    cacheFor: { expire: 0 },
  })
}
 
const mqttMeta = {
  ignoreAuth: false,
  toast: false,
  isExposeError: true,
}
 
export function createParentTask(data: ParentTaskSave) {
  return http.Post<ParentTask>('/user/parent-task', data, {
    timeout: 20000,
    meta: mqttMeta,
  })
}
 
export function updateParentTask(id: string, data: ParentTaskSave) {
  return http.Put<ParentTask>(`/user/parent-task/${id}`, data, {
    timeout: 20000,
    meta: mqttMeta,
    cacheFor: { expire: 0 },
  })
}
 
export function deleteParentTask(id: string) {
  return http.Delete<boolean>(`/user/parent-task/${id}`, {
    timeout: 20000,
    meta: mqttMeta,
  })
}
 
export function runParentTaskNow(id: string) {
  return http.Post<ParentTask>(`/user/parent-task/${id}/run`, {}, {
    timeout: 20000,
    meta: mqttMeta,
  })
}
 
export function stopParentTask(id: string) {
  return http.Post<ParentTask>(`/user/parent-task/${id}/stop`, {}, {
    timeout: 20000,
    meta: mqttMeta,
  })
}
 
export function getParentHistory(macAddress: string, agentId: string, limit = 20) {
  return http.Get<ParentHistoryList>('/user/parent-history', {
    params: { macAddress, agentId, limit },
    meta: noCache,
    cacheFor: { expire: 0 },
  })
}
 
export function getParentHistoryDetail(id: string, macAddress: string, agentId: string) {
  return http.Get<ParentHistoryDetail>(`/user/parent-history/${id}`, {
    params: { macAddress, agentId },
    meta: noCache,
    cacheFor: { expire: 0 },
  })
}
 
export function parentTaskError(error: unknown, fallback: string) {
  if (typeof error === 'string' && error.trim())
    return error.trim()
  const message = error instanceof Error ? error.message : ''
  const cut = message.includes(':') ? message.split(':').slice(1).join(':').trim() : message.trim()
  return cut || fallback
}