@roki-h5/create-roki-app 0.1.6 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/templates/h5/package.json +1 -1
- package/templates/h5/src/constants/device.ts +0 -10
- package/templates/h5/src/constants/deviceOnline.ts +44 -0
- package/templates/h5/src/stores/app.ts +19 -5
- package/templates/h5/src/utils/useGlobalSse.ts +8 -10
- package/templates/h5/src/views/home/index.vue +24 -3
package/package.json
CHANGED
|
@@ -3,12 +3,6 @@ export const DEVICE_PROPERTY_KEY = {
|
|
|
3
3
|
POW_STATE: 'powState',
|
|
4
4
|
} as const
|
|
5
5
|
|
|
6
|
-
/** 设备在线状态(info/state 接口 data[0].status) */
|
|
7
|
-
export const DEVICE_ONLINE_STATUS = {
|
|
8
|
-
OFFLINE: 0,
|
|
9
|
-
ONLINE: 1,
|
|
10
|
-
} as const
|
|
11
|
-
|
|
12
6
|
/** 设备开关机状态 powState(调料机等) */
|
|
13
7
|
export const DEVICE_POW_STATE = {
|
|
14
8
|
OFF: '0',
|
|
@@ -68,10 +62,6 @@ export function normalizePowState(value: unknown) {
|
|
|
68
62
|
return String(value)
|
|
69
63
|
}
|
|
70
64
|
|
|
71
|
-
export function isDeviceOnline(status: unknown) {
|
|
72
|
-
return Number(status) === DEVICE_ONLINE_STATUS.ONLINE
|
|
73
|
-
}
|
|
74
|
-
|
|
75
65
|
export function isDevicePowerOn(powState: unknown) {
|
|
76
66
|
const normalized = normalizePowState(powState)
|
|
77
67
|
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 设备在线状态
|
|
3
|
+
* POST /rest/iot/api/device/info/state → data[0].status
|
|
4
|
+
*/
|
|
5
|
+
export const DEVICE_ONLINE_STATUS = {
|
|
6
|
+
/** 离线 */
|
|
7
|
+
OFFLINE: 0,
|
|
8
|
+
/** 在线 */
|
|
9
|
+
ONLINE: 1,
|
|
10
|
+
} as const
|
|
11
|
+
|
|
12
|
+
export type DeviceOnlineStatusCode =
|
|
13
|
+
(typeof DEVICE_ONLINE_STATUS)[keyof typeof DEVICE_ONLINE_STATUS]
|
|
14
|
+
|
|
15
|
+
/** status 码 → 展示文案 */
|
|
16
|
+
export const DEVICE_ONLINE_STATUS_LABEL: Record<
|
|
17
|
+
DeviceOnlineStatusCode,
|
|
18
|
+
string
|
|
19
|
+
> = {
|
|
20
|
+
[DEVICE_ONLINE_STATUS.OFFLINE]: '离线',
|
|
21
|
+
[DEVICE_ONLINE_STATUS.ONLINE]: '在线',
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function parseDeviceOnlineStatus(raw: unknown) {
|
|
25
|
+
if (raw === undefined || raw === null || raw === '') return null
|
|
26
|
+
const n = Number(raw)
|
|
27
|
+
return Number.isNaN(n) ? null : n
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** status === 1 为在线 */
|
|
31
|
+
export function isDeviceOnline(status: unknown) {
|
|
32
|
+
return Number(status) === DEVICE_ONLINE_STATUS.ONLINE
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function getDeviceOnlineStatusLabel(status: unknown) {
|
|
36
|
+
const code = parseDeviceOnlineStatus(status)
|
|
37
|
+
if (code === DEVICE_ONLINE_STATUS.OFFLINE) {
|
|
38
|
+
return DEVICE_ONLINE_STATUS_LABEL[DEVICE_ONLINE_STATUS.OFFLINE]
|
|
39
|
+
}
|
|
40
|
+
if (code === DEVICE_ONLINE_STATUS.ONLINE) {
|
|
41
|
+
return DEVICE_ONLINE_STATUS_LABEL[DEVICE_ONLINE_STATUS.ONLINE]
|
|
42
|
+
}
|
|
43
|
+
return ''
|
|
44
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { ref } from 'vue'
|
|
1
|
+
import { computed, ref } from 'vue'
|
|
2
2
|
import { defineStore } from 'pinia'
|
|
3
3
|
import { getWelcomeMessage, type WelcomeMessageData } from '@/api/welcome'
|
|
4
|
+
import { isDeviceOnline } from '@/constants/deviceOnline'
|
|
4
5
|
import { SseService } from '@/utils/sse'
|
|
5
6
|
|
|
6
7
|
function formatRequestDate(date = new Date()) {
|
|
@@ -21,7 +22,6 @@ export interface AppStateUpdates {
|
|
|
21
22
|
userId?: string
|
|
22
23
|
machineOpenStatus?: boolean | null
|
|
23
24
|
machinePowerStateReady?: boolean
|
|
24
|
-
machineOnlineStatus?: boolean
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
export const useAppStore = defineStore('app', () => {
|
|
@@ -48,8 +48,17 @@ export const useAppStore = defineStore('app', () => {
|
|
|
48
48
|
const machineOpenStatus = ref<boolean | null>(null)
|
|
49
49
|
/** 开关机状态是否已从 shadow/SSE 同步过 */
|
|
50
50
|
const machinePowerStateReady = ref(false)
|
|
51
|
-
/**
|
|
52
|
-
|
|
51
|
+
/**
|
|
52
|
+
* 设备在线状态码:info/state → data[0].status
|
|
53
|
+
* 0 离线 / 1 在线;未拉取前为 null
|
|
54
|
+
*/
|
|
55
|
+
const deviceOnlineStatusCode = ref<number | null>(null)
|
|
56
|
+
/** 是否在线(未拉取前默认 true,避免首屏误显示离线) */
|
|
57
|
+
const machineOnlineStatus = computed(() => {
|
|
58
|
+
const status = deviceOnlineStatusCode.value
|
|
59
|
+
if (status === null || status === undefined) return true
|
|
60
|
+
return isDeviceOnline(status)
|
|
61
|
+
})
|
|
53
62
|
/** SSE 客户端 */
|
|
54
63
|
const sseClient = ref<SseService | null>(null)
|
|
55
64
|
|
|
@@ -61,6 +70,10 @@ export const useAppStore = defineStore('app', () => {
|
|
|
61
70
|
accessToken.value = token
|
|
62
71
|
}
|
|
63
72
|
|
|
73
|
+
function setDeviceOnlineStatus(status: number) {
|
|
74
|
+
deviceOnlineStatusCode.value = status
|
|
75
|
+
}
|
|
76
|
+
|
|
64
77
|
function initSSEClient(url: string) {
|
|
65
78
|
if (sseClient.value) {
|
|
66
79
|
console.log('[SSE] 已存在连接,不重复创建')
|
|
@@ -102,7 +115,6 @@ export const useAppStore = defineStore('app', () => {
|
|
|
102
115
|
userId,
|
|
103
116
|
machineOpenStatus,
|
|
104
117
|
machinePowerStateReady,
|
|
105
|
-
machineOnlineStatus,
|
|
106
118
|
} as const
|
|
107
119
|
|
|
108
120
|
Object.entries(updates).forEach(([key, value]) => {
|
|
@@ -128,10 +140,12 @@ export const useAppStore = defineStore('app', () => {
|
|
|
128
140
|
welcomeCopy,
|
|
129
141
|
machineOpenStatus,
|
|
130
142
|
machinePowerStateReady,
|
|
143
|
+
deviceOnlineStatusCode,
|
|
131
144
|
machineOnlineStatus,
|
|
132
145
|
sseClient,
|
|
133
146
|
setAppName,
|
|
134
147
|
setAccessToken,
|
|
148
|
+
setDeviceOnlineStatus,
|
|
135
149
|
initSSEClient,
|
|
136
150
|
resetWelcomeCopy,
|
|
137
151
|
loadWelcomeMessage,
|
|
@@ -2,7 +2,8 @@ import { nextTick, watch } from 'vue'
|
|
|
2
2
|
import { storeToRefs } from 'pinia'
|
|
3
3
|
import { useRouter } from 'vue-router'
|
|
4
4
|
import { getDeviceStatus } from '@/api/deviceInfo'
|
|
5
|
-
import {
|
|
5
|
+
import { isDevicePowerOn, resolvePowState } from '@/constants/device'
|
|
6
|
+
import { DEVICE_ONLINE_STATUS, isDeviceOnline } from '@/constants/deviceOnline'
|
|
6
7
|
import { normalDeviceApiBase } from '@config/env'
|
|
7
8
|
import { useAppStore } from '@/stores/app'
|
|
8
9
|
import { useMachineStateStore } from '@/stores/machineState'
|
|
@@ -59,9 +60,10 @@ export function useGlobalSse() {
|
|
|
59
60
|
|
|
60
61
|
getDeviceStatus([deviceId.value])
|
|
61
62
|
.then((statusRes) => {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
63
|
+
const status = statusRes.data?.[0]?.status
|
|
64
|
+
if (status != null) {
|
|
65
|
+
appStore.setDeviceOnlineStatus(status)
|
|
66
|
+
}
|
|
65
67
|
})
|
|
66
68
|
.catch((error) => {
|
|
67
69
|
console.error('[SSE] 刷新设备在线状态失败:', error)
|
|
@@ -90,9 +92,7 @@ export function useGlobalSse() {
|
|
|
90
92
|
const powState = resolvePowState(res.data)
|
|
91
93
|
|
|
92
94
|
if (res.messageType === 'OFFLINE') {
|
|
93
|
-
appStore.
|
|
94
|
-
machineOnlineStatus: false,
|
|
95
|
-
})
|
|
95
|
+
appStore.setDeviceOnlineStatus(DEVICE_ONLINE_STATUS.OFFLINE)
|
|
96
96
|
void nextTick(() => {
|
|
97
97
|
void router.replace('/')
|
|
98
98
|
})
|
|
@@ -100,9 +100,7 @@ export function useGlobalSse() {
|
|
|
100
100
|
}
|
|
101
101
|
|
|
102
102
|
if (res.messageType === 'ONLINE') {
|
|
103
|
-
appStore.
|
|
104
|
-
machineOnlineStatus: true,
|
|
105
|
-
})
|
|
103
|
+
appStore.setDeviceOnlineStatus(DEVICE_ONLINE_STATUS.ONLINE)
|
|
106
104
|
}
|
|
107
105
|
|
|
108
106
|
if (!machineOnlineStatus.value) {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
|
-
import { computed } from 'vue'
|
|
2
|
+
import { computed, onMounted } from 'vue'
|
|
3
3
|
import { storeToRefs } from 'pinia'
|
|
4
4
|
import WelcomeInfo from '@/components/WelcomeInfo.vue'
|
|
5
|
-
import { setDeviceFunctionProperty } from '@/api/deviceInfo'
|
|
5
|
+
import { getDeviceStatus, setDeviceFunctionProperty } from '@/api/deviceInfo'
|
|
6
6
|
import { nativeApi } from '@/api/nativeApi'
|
|
7
7
|
import {
|
|
8
8
|
DEVICE_POWER_FUNCTION,
|
|
@@ -18,7 +18,7 @@ import iconPower from '@/assets/images/home/icon-power.png'
|
|
|
18
18
|
|
|
19
19
|
const appStore = useAppStore()
|
|
20
20
|
const machineStateStore = useMachineStateStore()
|
|
21
|
-
const { deviceName, machineOnlineStatus } = storeToRefs(appStore)
|
|
21
|
+
const { deviceName, deviceId, machineOnlineStatus } = storeToRefs(appStore)
|
|
22
22
|
const { deviceData } = storeToRefs(machineStateStore)
|
|
23
23
|
|
|
24
24
|
const homeHeaderTitle = computed(
|
|
@@ -99,6 +99,27 @@ function onMoreIconClick() {
|
|
|
99
99
|
|
|
100
100
|
void nativeApi.skipDeviceMore('5.0')
|
|
101
101
|
}
|
|
102
|
+
|
|
103
|
+
async function loadDeviceOnlineStatus() {
|
|
104
|
+
const id = String(deviceId.value ?? '').trim()
|
|
105
|
+
if (!id) return
|
|
106
|
+
|
|
107
|
+
try {
|
|
108
|
+
const res = await getDeviceStatus([id])
|
|
109
|
+
const status = res?.data?.[0]?.status
|
|
110
|
+
if (status != null) {
|
|
111
|
+
appStore.setDeviceOnlineStatus(status)
|
|
112
|
+
}
|
|
113
|
+
} catch (e) {
|
|
114
|
+
if (import.meta.env.DEV) {
|
|
115
|
+
console.warn('[Home] loadDeviceOnlineStatus failed', e)
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
onMounted(() => {
|
|
121
|
+
void loadDeviceOnlineStatus()
|
|
122
|
+
})
|
|
102
123
|
</script>
|
|
103
124
|
|
|
104
125
|
<template>
|