新增小程序后端代码,包括数据库、路由、服务层等。
小程序前后端打通,包括登录、注册、绑定设备、查询绑定信息,修改小朋友名称等功能。
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { View, Text, Switch, Slider, Image } from '@tarojs/components'
|
||||
import { useState, useEffect } from 'react'
|
||||
import Taro from '@tarojs/taro'
|
||||
import { getCurrentBinding } from '@/services/binding'
|
||||
import './index.scss'
|
||||
|
||||
interface DeviceData {
|
||||
@@ -20,35 +21,59 @@ export default function Device() {
|
||||
const [hasDevice, setHasDevice] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
// 检查登录状态
|
||||
const isLoggedIn = Taro.getStorageSync('isLoggedIn')
|
||||
if (!isLoggedIn) {
|
||||
console.log('[Device] Checking login status...')
|
||||
const token = Taro.getStorageSync('token')
|
||||
console.log('[Device] Token exists:', !!token)
|
||||
|
||||
if (!token) {
|
||||
console.log('[Device] No token, redirecting to login')
|
||||
Taro.redirectTo({ url: '/pages/login/index' })
|
||||
return
|
||||
}
|
||||
|
||||
// 检查设备绑定状态
|
||||
const deviceInfo = Taro.getStorageSync('deviceInfo')
|
||||
const hasDeviceFlag = Taro.getStorageSync('hasDevice')
|
||||
|
||||
setHasDevice(!!hasDeviceFlag)
|
||||
|
||||
if (deviceInfo && hasDeviceFlag) {
|
||||
setDevice({
|
||||
id: deviceInfo.id,
|
||||
name: deviceInfo.name || '小夏的伴伴',
|
||||
status: deviceInfo.status || 'online',
|
||||
battery: deviceInfo.battery || 82,
|
||||
daysLeft: 4,
|
||||
sleepEnabled: true,
|
||||
sleepTime: '每天 22:00 - 07:00',
|
||||
volume: 50
|
||||
})
|
||||
}
|
||||
|
||||
setIsLoading(false)
|
||||
loadDeviceInfo()
|
||||
}, [])
|
||||
|
||||
const loadDeviceInfo = async () => {
|
||||
try {
|
||||
const binding = await getCurrentBinding()
|
||||
console.log('[Device] Got binding:', binding)
|
||||
|
||||
if (binding) {
|
||||
setHasDevice(true)
|
||||
Taro.setStorageSync('hasDevice', '1')
|
||||
Taro.setStorageSync('deviceInfo', {
|
||||
id: binding.device_id,
|
||||
name: '伴伴设备',
|
||||
status: 'online',
|
||||
battery: 82
|
||||
})
|
||||
setDevice({
|
||||
id: binding.device_id,
|
||||
name: '伴伴设备',
|
||||
status: 'online',
|
||||
battery: 82,
|
||||
daysLeft: 4,
|
||||
sleepEnabled: true,
|
||||
sleepTime: '每天 22:00 - 07:00',
|
||||
volume: 50
|
||||
})
|
||||
} else {
|
||||
setHasDevice(false)
|
||||
Taro.removeStorageSync('hasDevice')
|
||||
Taro.removeStorageSync('deviceInfo')
|
||||
}
|
||||
} catch (err: any) {
|
||||
console.log('[Device] No binding or error:', err?.message || err)
|
||||
if (err?.status === 404) {
|
||||
setHasDevice(false)
|
||||
Taro.removeStorageSync('hasDevice')
|
||||
Taro.removeStorageSync('deviceInfo')
|
||||
}
|
||||
}
|
||||
setIsLoading(false)
|
||||
}
|
||||
|
||||
const handleSleepToggle = (e: any) => {
|
||||
if (!device) return
|
||||
setDevice({ ...device, sleepEnabled: e.detail.value })
|
||||
|
||||
Reference in New Issue
Block a user