server code 4/27

This commit is contained in:
袁智鸿
2026-04-27 10:35:03 +08:00
parent f0fa33cd67
commit 2ccbcd01c0
143 changed files with 5812 additions and 756 deletions
+1
View File
@@ -0,0 +1 @@
* linguist-language=GO
+278
View File
@@ -0,0 +1,278 @@
# StackChan Server
**StackChan Server** is the backend server for the open-source StackChan project. It provides RESTful APIs for device management, user authentication, post management, comment systems, and dance control functionalities.
---
## Features
- **Device Management**: Device binding, unbinding, and information updates
- **User Authentication**: Login, registration, and JWT-based token authentication
- **Post System**: Create, read, and manage posts with text and image support
- **Comment System**: Full CRUD operations for post comments
- **Dance Control**: Dance creation, management, and motion data handling
- **App Store Integration**: App management and distribution
- **AI Agent Integration**: XiaoZhi (小智) AI assistant integration with agent configuration
- **Persistent Storage**: MySQL database with ORM-based data access
---
## Table of Contents
- [Prerequisites](#prerequisites)
- [Database Setup](#database-setup)
- [Configuration](#configuration)
- [Installation](#installation)
- [Running the Server](#running-the-server)
- [API Documentation](#api-documentation)
- [Project Structure](#project-structure)
- [Contributing](#contributing)
- [License](#license)
---
## Prerequisites
- **Go**: The project is developed in Go. Install **Go 1.24+** from
the [official download page](https://golang.google.cn/dl/).
Verify installation:
```bash
go version
# Expected output: "go version go1.24.x ..." (or similar)
```
- **MySQL**: Version 8.0+ for database storage
- **Git**: For cloning the repository
---
## Database Setup
Before running the project, you need to create the MySQL database first:
```bash
# Execute the database initialization script
mysql -u your_username -p < check_list/create_mysql_database.sql
```
This will create the `stackChan` database and all required tables.
---
## Configuration
Update the `manifest/config/config.yaml` file with your actual configuration:
### 1. Database Connection
```yaml
database:
default:
link: "mysql:your_username:your_password@tcp(127.0.0.1:3306)/stackChan?charset=utf8mb4&collation=utf8mb4_0900_ai_ci"
```
### 2. JWT Configuration
For production use, generate your own secure JWT secret key.
**Generate a secure JWT secret key:**
Option 1: Using OpenSSL (recommended)
```bash
# Generate a 32-byte (256-bit) random secret key encoded in base64
openssl rand -base64 32
```
Option 2: Using Python
```bash
python3 -c "import secrets; import base64; print(base64.b64encode(secrets.token_bytes(32)).decode())"
```
Update the configuration in `manifest/config/config.yaml`:
```yaml
jwt:
secret: "your_generated_secret_key_here"
```
### 3. RSA Keys
For production use, generate your own RSA key pairs for encryption.
### 4. XiaoZhi Configuration
Set your XiaoZhi (小智) API secret key:
```yaml
xiaozhi:
secret_key: "your_xiaozhi_secret_key_here"
generate_license_token: "your_xiaozhi_generate_license_token_here"
```
---
## Installation
```bash
# Clone the repository
git clone https://github.com/m5stack/StackChan.git
cd StackChan/server
# Download dependencies
go mod download
```
---
## Running the Server
### Development Mode
```bash
go run main.go
```
### Build and Run
```bash
# Build for current platform
go build -o stackchan-server main.go
# Run
./stackchan-server # Linux/macOS
stackchan-server.exe # Windows
```
### Using Makefile
```bash
# Build
make build
# Run
make run
```
---
## API Documentation
The server provides RESTful APIs organized by module:
### Device APIs (`/api/device/*`)
- `POST /device/bind` - Bind a device to the current user
- `POST /device/unbind` - Unbind a device from the current user
- `PUT /device/update` - Update device information
- `GET /devices` - Get list of devices
### User APIs (`/api/user/*`)
- `POST /user/login` - User login
- `POST /user/registration` - User registration
- `GET /user` - Get user information
### Dance APIs (`/api/dance/*`)
- `POST /dance` - Create a new dance
- `GET /dance` - Get dance information
- `PUT /dance` - Update dance details
- `DELETE /dance` - Delete a dance
### Post APIs (`/api/post/*`)
- `GET /post/get` - Get post details
- Post listing and comment operations
### Admin APIs (`/api/admin/*`)
- App management operations
- User management
---
## Project Structure
```
stackChan/
├── api/ # API definitions and request/response structures
│ ├── admin/ # Admin module APIs
│ ├── appstore/ # App store module APIs
│ ├── dance/ # Dance module APIs
│ ├── device/ # Device module APIs
│ ├── friend/ # Friend module APIs
│ ├── post/ # Post module APIs
│ ├── user/ # User module APIs
│ └── xiaozhi/ # XiaoZhi AI module APIs
├── internal/ # Internal application code
│ ├── boot/ # Boot initialization
│ ├── cmd/ # Command entry
│ ├── consts/ # Constants definitions
│ ├── controller/ # API controllers
│ ├── dao/ # Data Access Objects
│ ├── logic/ # Business logic
│ ├── middleware/ # HTTP middlewares
│ ├── model/ # Data models
│ ├── packed/ # Packed assets
│ ├── service/ # Business services
│ └── xiaozhi/ # XiaoZhi integration
├── manifest/ # Deployment and configuration
│ ├── config/ # Configuration files
│ ├── deploy/ # Deployment scripts
│ └── docker/ # Docker configurations
├── utility/ # Utility functions (RSA, etc.)
├── web/ # Web frontend assets
├── main.go # Application entry
├── go.mod # Go module definition
├── go.sum # Go dependencies checksum
├── Makefile # Build scripts
└── README.MD # This file
```
---
## Architecture Overview
The project follows a layered architecture:
1. **API Layer**: Defines request/response structures and routes
2. **Controller Layer**: Handles HTTP requests and responses
3. **Service Layer**: Implements business logic
4. **DAO Layer**: Data access and database operations
5. **Model Layer**: Data structures and entities
The project uses the GoFrame framework for rapid development and robust infrastructure.
---
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
For major changes, please open an issue first to discuss what you would like to change.
### Development Guidelines
- Follow Go conventions and best practices
- Write clear, descriptive comments in English
- Add tests for new features
- Ensure all existing tests pass
- Update documentation as needed
---
## License
[SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD](LICENSE)
[SPDX-License-Identifier: MIT](LICENSE)
---
## Support
For questions and support, please open an issue in the GitHub repository or contact the M5Stack team.
---
## Acknowledgments
- [GoFrame](https://goframe.org/) - The Go framework used in this project
- [M5Stack](https://m5stack.com/) - For supporting open-source development
- All contributors who help improve this project
-44
View File
@@ -1,44 +0,0 @@
# StackChan Server
**StackChan Server** is the Server of the open-source StackChan project. It handles core functionalities such
as device interactions, post management, and comment systems, providing stable and efficient API support.
---
## Features
- App and StackChan communication and interaction
- Device post creation and management (text and image support, similar to a social feed)
- Comment CRUD (Create, Read, Update, Delete) operations
- Dance control and data management
- Persistent storage using a relational database
---
## Getting Started
### Prerequisites
- **Go**: The project is developed in Go. Install **Go 1.24+** from
the [official download page](https://golang.google.cn/dl/).
Verify installation:
```bash
go version
# Expected output: "go version go1.24.x ..." (or similar)
### Clone the Repository
```bash
git clone https://github.com/m5stack/StackChan # Replace with the actual repository URL
cd StackChan/server
# Download dependencies
go mod download
# build
go build -o StackChan main.go
# Start running
StackChan # Linux/macOS
StackChan.exe # Windows
+19
View File
@@ -0,0 +1,19 @@
// =================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// =================================================================================
package admin
import (
"context"
"stackChan/api/admin/v1"
)
type IAdminV1 interface {
AdminLogin(ctx context.Context, req *v1.AdminLoginReq) (res *v1.AdminLoginRes, err error)
AddApp(ctx context.Context, req *v1.AddAppReq) (res *v1.AddAppRes, err error)
GetAppList(ctx context.Context, req *v1.GetAppListReq) (res *v1.GetAppListRes, err error)
DeleteApp(ctx context.Context, req *v1.DeleteAppReq) (res *v1.DeleteAppRes, err error)
UpdateApp(ctx context.Context, req *v1.UpdateAppReq) (res *v1.UpdateAppRes, err error)
}
+18
View File
@@ -0,0 +1,18 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package v1
import "github.com/gogf/gf/v2/frame/g"
type AdminLoginReq struct {
g.Meta `path:"/login" method:"post" tags:"Info" summary:"admin login info"`
UserName string `json:"user_name" description:"Admin username"`
Password string `json:"pass_word" description:"Admin password"`
}
type AdminLoginRes struct {
Token string `json:"token"`
}
+46
View File
@@ -0,0 +1,46 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package v1
import (
"stackChan/internal/model"
"github.com/gogf/gf/v2/frame/g"
)
type AddAppReq struct {
g.Meta `path:"/app/add" method:"post" tags:"App" summary:"App add request"`
AppName string `json:"appName" orm:"app_name" v:"required" d:"" description:"App name (required)"`
AppIconUrl string `json:"appIconUrl" orm:"app_icon_url" d:"" description:"App icon URL (optional)"`
Description string `json:"description" orm:"description" d:"" description:"App description (optional)"`
FirmwareUrl string `json:"firmwareUrl" orm:"firmware_url" d:"" description:"Firmware / installation package download URL (optional)"`
}
type AddAppRes model.AppInfo
type GetAppListReq struct {
g.Meta `path:"/apps" method:"get" tags:"App" summary:"App List Get"`
}
type GetAppListRes []model.AppInfo
type DeleteAppReq struct {
g.Meta `path:"/app/delete" method:"delete" tags:"App" summary:"App delete"`
Id int64 `json:"id" orm:"id" description:"App ID"` // App ID
}
type DeleteAppRes struct{}
type UpdateAppReq struct {
g.Meta `path:"/app/update" method:"put" tags:"App" summary:"App put"`
Id int64 `json:"id" orm:"id" v:"required" description:"App ID (required)"`
AppName string `json:"appName" orm:"app_name" d:"" description:"App name (optional)"`
AppIconUrl string `json:"appIconUrl" orm:"app_icon_url" d:"" description:"App icon URL (optional)"`
Description string `json:"description" orm:"description" d:"" description:"App description (optional)"`
FirmwareUrl string `json:"firmwareUrl" orm:"firmware_url" d:"" description:"Firmware / installation package download URL (optional)"`
}
type UpdateAppRes model.AppInfo
@@ -2,10 +2,14 @@
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// =================================================================================
package entity
package appstore
// SqliteSequence is the golang structure for table sqlite_sequence.
type SqliteSequence struct {
Name string `json:"name" orm:"name" description:""` //
Seq string `json:"seq" orm:"seq" description:""` //
import (
"context"
"stackChan/api/appstore/v1"
)
type IAppstoreV1 interface {
GetAppList(ctx context.Context, req *v1.GetAppListReq) (res *v1.GetAppListRes, err error)
}
+18
View File
@@ -0,0 +1,18 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package v1
import (
"stackChan/internal/model"
"github.com/gogf/gf/v2/frame/g"
)
type GetAppListReq struct {
g.Meta `path:"/apps" method:"get" tags:"App" summary:"App List Get"`
}
type GetAppListRes []model.AppInfo
+11
View File
@@ -8,6 +8,7 @@ import (
"context"
"stackChan/api/dance/v1"
"stackChan/api/dance/v2"
)
type IDanceV1 interface {
@@ -15,4 +16,14 @@ type IDanceV1 interface {
Delete(ctx context.Context, req *v1.DeleteReq) (res *v1.DeleteRes, err error)
Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error)
GetList(ctx context.Context, req *v1.GetListReq) (res *v1.GetListRes, err error)
GetDanceInfo(ctx context.Context, req *v1.GetDanceInfoReq) (res *v1.GetDanceInfoRes, err error)
GetMusicList(ctx context.Context, req *v1.GetMusicListReq) (res *v1.GetMusicListRes, err error)
}
type IDanceV2 interface {
GetList(ctx context.Context, req *v2.GetListReq) (res *v2.GetListRes, err error)
Create(ctx context.Context, req *v2.CreateReq) (res *v2.CreateRes, err error)
Delete(ctx context.Context, req *v2.DeleteReq) (res *v2.DeleteRes, err error)
Update(ctx context.Context, req *v2.UpdateReq) (res *v2.UpdateRes, err error)
GetDanceInfo(ctx context.Context, req *v2.GetDanceInfoReq) (res *v2.GetDanceInfoRes, err error)
}
+25 -12
View File
@@ -6,40 +6,53 @@ SPDX-License-Identifier: MIT
package v1
import (
"encoding/json"
"stackChan/internal/model"
"github.com/gogf/gf/v2/frame/g"
)
type CreateReq struct {
g.Meta `path:"/dance" method:"post" tags:"Dance" summary:"Dance create request"`
Mac string `json:"mac" v:"required"`
Index int `json:"index" v:"required"`
List []model.DanceData `json:"list" v:"required"`
g.Meta `path:"/dance" method:"post" tags:"Dance" summary:"Dance create request"`
DanceData json.RawMessage `json:"danceData"` // Dance motion data
DanceName string `json:"danceName" v:"required"` // Dance name
MusicUrl string `json:"musicUrl"` // Dance background music URL
}
type CreateRes string
type DeleteReq struct {
g.Meta `path:"/dance" method:"delete" tags:"Dance" summary:"Dance delete request"`
Mac string `json:"mac" v:"required"`
Index int `json:"index" v:"required"`
Id int64 `json:"id" v:"required"`
}
type DeleteRes string
type UpdateReq struct {
g.Meta `path:"/dance" method:"put" tags:"Dance" summary:"Dance put request"`
Mac string `json:"mac" v:"required"`
Index int `json:"index" v:"required"`
Data []model.DanceData `json:"list" v:"required"`
g.Meta `path:"/dance" method:"put" tags:"Dance" summary:"Dance put request"`
Id int64 `json:"id" v:"required"`
DanceData json.RawMessage `json:"danceData"` // Dance motion data
DanceName string `json:"danceName"` // Dance name
MusicUrl string `json:"musicUrl"` // Dance background music URL
}
type UpdateRes string
type GetListReq struct {
g.Meta `path:"/dance" method:"get" tags:"Dance" summary:"Dance get request"`
Mac string `json:"mac" v:"required"`
}
type GetListRes map[string][]model.DanceData
type GetListRes []model.Dance
type GetDanceInfoReq struct {
g.Meta `path:"/danceData" method:"get" tags:"Dance get request"`
Id int64 `json:"id" v:"required"`
}
type GetDanceInfoRes model.Dance
type GetMusicListReq struct {
g.Meta `path:"/musicList" method:"get" tags:"Dance get request"`
}
type GetMusicListRes []string
+54
View File
@@ -0,0 +1,54 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package v2
import (
"encoding/json"
"stackChan/internal/model"
"github.com/gogf/gf/v2/frame/g"
)
type GetListReq struct {
g.Meta `path:"/dance" method:"get" tags:"Dance" summary:"Dance get request"`
Mac string `json:"mac" v:"required"` // mac address
}
type GetListRes []model.Dance
type CreateReq struct {
g.Meta `path:"/dance" method:"post" tags:"Dance" summary:"Dance create request"`
Mac string `json:"mac" v:"required"` // mac address
DanceData json.RawMessage `json:"danceData"` // Dance motion data
DanceName string `json:"danceName" v:"required"` // Dance name
MusicUrl string `json:"musicUrl"` // Dance background music URL
}
type CreateRes string
type DeleteReq struct {
g.Meta `path:"/dance" method:"delete" tags:"Dance" summary:"Dance delete request"`
Id int64 `json:"id" v:"required"`
}
type DeleteRes string
type UpdateReq struct {
g.Meta `path:"/dance" method:"put" tags:"Dance" summary:"Dance put request"`
Id int64 `json:"id" v:"required"`
DanceData json.RawMessage `json:"danceData"` // Dance motion data
DanceName string `json:"danceName"` // Dance name
MusicUrl string `json:"musicUrl"` // Dance background music URL
}
type UpdateRes string
type GetDanceInfoReq struct {
g.Meta `path:"/danceData" method:"get" tags:"Dance get request"`
Id int64 `json:"id" v:"required"`
}
type GetDanceInfoRes model.Dance
+9
View File
@@ -8,6 +8,7 @@ import (
"context"
"stackChan/api/device/v1"
"stackChan/api/device/v2"
)
type IDeviceV1 interface {
@@ -17,3 +18,11 @@ type IDeviceV1 interface {
GetDeviceInfo(ctx context.Context, req *v1.GetDeviceInfoReq) (res *v1.GetDeviceInfoRes, err error)
UpdateDeviceInfo(ctx context.Context, req *v1.UpdateDeviceInfoReq) (res *v1.UpdateDeviceInfoRes, err error)
}
type IDeviceV2 interface {
GetDevices(ctx context.Context, req *v2.GetDevicesReq) (res *v2.GetDevicesRes, err error)
BindDevice(ctx context.Context, req *v2.BindDeviceReq) (res *v2.BindDeviceRes, err error)
UnbindDevice(ctx context.Context, req *v2.UnbindDeviceReq) (res *v2.UnbindDeviceRes, err error)
UpdateDevice(ctx context.Context, req *v2.UpdateDeviceReq) (res *v2.UpdateDeviceRes, err error)
AgentRestoreDefault(ctx context.Context, req *v2.AgentRestoreDefaultReq) (res *v2.AgentRestoreDefaultRes, err error)
}
+2 -6
View File
@@ -14,7 +14,6 @@ import (
type CreateReq struct {
g.Meta `path:"/device" method:"post" tags:"Device" summary:"Device create request"`
Mac string `json:"mac" v:"required" description:"Mac address"`
Name string `json:"name,omitempty" description:"Device name"`
}
@@ -24,29 +23,26 @@ type CreateRes struct {
type UpdateReq struct {
g.Meta `path:"/device" method:"put" tags:"Device" summary:"Device update request"`
Mac string `json:"mac" v:"required" description:"Mac address"`
Name string `json:"name" description:"Device name"`
}
type UpdateRes struct{}
type GetRandomDeviceReq struct {
g.Meta `path:"/device/randomList" method:"get" tags:"Device" summary:"Device get Random"`
Mac string `json:"mac" v:"required" description:"Mac address"`
g.Meta `path:"/device/randomList" method:"get" tags:"Device" summary:"Device get Random"`
PageSize int `json:"pageSize" v:"required" d:"6" description:"Page size"`
}
type GetRandomDeviceRes []entity.Device
type GetDeviceInfoReq struct {
g.Meta `path:"/device/info" method:"get" tags:"Device" summary:"Device Info Get request"`
Mac string `json:"mac" v:"required" description:"Mac address"`
}
type GetDeviceInfoRes model.DeviceInfo
type UpdateDeviceInfoReq struct {
g.Meta `path:"/device/info" method:"put" tags:"Device" summary:"Device Info Put request"`
Mac string `json:"mac" v:"required" description:"Mac address"`
Name string `json:"name" description:"Device name"`
}
+49
View File
@@ -0,0 +1,49 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package v2
import (
"stackChan/internal/model"
"github.com/gogf/gf/v2/frame/g"
)
type GetDevicesReq struct {
g.Meta `path:"/devices" method:"get" tags:"Device" summary:"Devices Get request"`
}
type GetDevicesRes []model.DeviceInfo
type BindDeviceReq struct {
g.Meta `path:"/device/bind" method:"post" tags:"Device" summary:"Bind device to current user"`
Mac string `json:"mac" v:"required" dc:"Device MAC address"`
}
type BindDeviceRes bool
type UnbindDeviceReq struct {
g.Meta `path:"/device/unbind" method:"post" tags:"Device" summary:"Unbind device from current user"`
Mac string `json:"mac" v:"required" dc:"Device MAC address"`
}
type UnbindDeviceRes bool
type UpdateDeviceReq struct {
g.Meta `path:"/device/update" method:"put" tags:"Device" summary:"Update device name for current user's bound device"`
Mac string `json:"mac" v:"required" dc:"Device MAC address"`
Name string `json:"name" dc:"New device name"`
Longitude float64 `json:"longitude" dc:"Device longitude"`
Latitude float64 `json:"latitude" dc:"Device latitude"`
}
type UpdateDeviceRes bool
type AgentRestoreDefaultReq struct {
g.Meta `path:"/device/agent/restore" method:"post" tags:"Device" summary:"Restore Agent to default template settings"`
Mac string `json:"mac" v:"required" dc:"Device MAC address"`
}
type AgentRestoreDefaultRes bool
-1
View File
@@ -9,7 +9,6 @@ import "github.com/gogf/gf/v2/frame/g"
type AddReq struct {
g.Meta `path:"/friend" method:"post" tags:"Friend" summary:"Friend add request"`
Mac string `json:"mac" v:"required" description:"Mac address"`
FriendMac string `json:"friendMac" v:"required" description:"Friend Mac address"`
}
@@ -2,15 +2,15 @@
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// =================================================================================
package do
package pano
import (
"github.com/gogf/gf/v2/frame/g"
"context"
"stackChan/api/pano/v1"
)
// SqliteSequence is the golang structure of table sqlite_sequence for DAO operations like Where/Data.
type SqliteSequence struct {
g.Meta `orm:"table:sqlite_sequence, do:true"`
Name any //
Seq any //
type IPanoV1 interface {
AddPano(ctx context.Context, req *v1.AddPanoReq) (res *v1.AddPanoRes, err error)
GetPanoList(ctx context.Context, req *v1.GetPanoListReq) (res *v1.GetPanoListRes, err error)
}
+27
View File
@@ -0,0 +1,27 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package v1
import (
"stackChan/internal/model"
"github.com/gogf/gf/v2/frame/g"
)
type AddPanoReq struct {
g.Meta `path:"/pano" method:"post" tags:"Pano" summary:"Pano add request"`
Url string `json:"url" v:"required" description:"Pano image url"`
}
type AddPanoRes struct {
Id int64 `json:"id"`
}
type GetPanoListReq struct {
g.Meta `path:"/pano" method:"get" tags:"Pano" summary:"Pano list"`
}
type GetPanoListRes []model.Pano
+10 -13
View File
@@ -13,9 +13,8 @@ import (
type CreatePostReq struct {
g.Meta `path:"/post/add" method:"post" tags:"Post" summary:"Post create request"`
Mac string `json:"mac" v:"required" description:"Mac address"`
ContentText string `json:"content_text" v:"required" description:"Content text"`
ContentImage string `json:"content_image" v:"required" description:"Content image"`
ContentImage string `json:"content_image" description:"Content image"`
}
type CreatePostRes struct {
@@ -24,8 +23,8 @@ type CreatePostRes struct {
type GetPostReq struct {
g.Meta `path:"/post/get" method:"get" tags:"Post" summary:"Post get request"`
Page int `json:"page" v:"required#Page不能为空" description:"页码"`
PageSize int `json:"pageSize" v:"required#每页数量不能为空" description:"每页条数"`
Page int `json:"page" v:"required#Page cannot be empty" description:"Page number"`
PageSize int `json:"pageSize" v:"required#Page size cannot be empty" description:"Items per page"`
}
type GetPostRes []model.Post
@@ -39,9 +38,8 @@ type DeletePostRes string
type CreatePostCommentReq struct {
g.Meta `path:"/post/comment/create" method:"post" tags:"Post" summary:"Post create comment"`
Mac string `json:"mac" v:"required" description:"Mac address"`
PostId int64 `json:"postId" v:"required" summary:"Post comment id"`
Content string `json:"content" description:"评论内容"`
Content string `json:"content" description:"Comment content"`
}
type CreatePostCommentRes struct {
@@ -49,19 +47,18 @@ type CreatePostCommentRes struct {
}
type DeletePostCommentReq struct {
g.Meta `path:"/post/comment/delete" method:"post" tags:"Post" summary:"Post delete comment"`
Mac string `json:"mac" v:"required" description:"Mac address"`
Id int `json:"id" summary:"Post comment id"`
g.Meta `path:"/post/comment/delete" method:"delete" tags:"Post" summary:"Post delete comment"`
PostId int64 `json:"postId" v:"required" summary:"Post comment id"`
CommentId int `json:"commentId" v:"required" summary:"Post comment id"`
}
type DeletePostCommentRes struct{}
type GetPostCommentReq struct {
g.Meta `path:"/post/comment/get" method:"get" tags:"Post" summary:"Post get comment"`
PostId int64 `json:"postId" summary:"Post comment id"`
Mac string `json:"mac" v:"required" description:"Mac address"`
Page int `json:"page" summary:"Post comment page"`
PageSize int `json:"pageSize" summary:"Post comment page"`
PostId int64 `json:"postId" summary:"Post comment id"`
Page int `json:"page" summary:"Post comment page"`
PageSize int `json:"pageSize" summary:"Post comment page"`
}
type GetPostCommentRes struct {
@@ -0,0 +1,16 @@
// =================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// =================================================================================
package stackchandevice
import (
"context"
"stackChan/api/stackchandevice/v2"
)
type IStackchandeviceV2 interface {
GetDeviceUserInfo(ctx context.Context, req *v2.GetDeviceUserInfoReq) (res *v2.GetDeviceUserInfoRes, err error)
UnbindDevice(ctx context.Context, req *v2.UnbindDeviceReq) (res *v2.UnbindDeviceRes, err error)
}
+24
View File
@@ -0,0 +1,24 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package v2
import (
"stackChan/internal/model"
"github.com/gogf/gf/v2/frame/g"
)
type GetDeviceUserInfoReq struct {
g.Meta `path:"/device/user" method:"get" tags:"Device" summary:"Get device information for StackChan device"`
}
type GetDeviceUserInfoRes *model.User
type UnbindDeviceReq struct {
g.Meta `path:"/device/unbind" method:"post" tags:"Device" summary:"Unbind device from current user for StackChan device"`
}
type UnbindDeviceRes bool
+17
View File
@@ -0,0 +1,17 @@
// =================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// =================================================================================
package user
import (
"context"
"stackChan/api/user/v2"
)
type IUserV2 interface {
Login(ctx context.Context, req *v2.LoginReq) (res *v2.LoginRes, err error)
GetUserInfo(ctx context.Context, req *v2.GetUserInfoReq) (res *v2.GetUserInfoRes, err error)
Registration(ctx context.Context, req *v2.RegistrationReq) (res *v2.RegistrationRes, err error)
}
+37
View File
@@ -0,0 +1,37 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package v2
import (
"stackChan/internal/model"
"github.com/gogf/gf/v2/frame/g"
)
type LoginReq struct {
g.Meta `path:"user/login" method:"post" tags:"Info" summary:"user login info"`
Username string `json:"username" v:"required" description:"Account or email"`
Password string `json:"password" v:"required" description:"Password"`
}
type LoginRes struct {
Token string `json:"token"`
}
type GetUserInfoReq struct {
g.Meta `path:"user" method:"get" tags:"Info" summary:"user get info"`
}
type GetUserInfoRes model.User
type RegistrationReq struct {
g.Meta `path:"user/registration" method:"post" tags:"Info" summary:"user registration"`
UserName string `json:"username" v:"required" description:"Username"`
Email string `json:"email" v:"required" description:"Email address"`
Password string `json:"password" v:"required" description:"Password"`
}
type RegistrationRes *model.RegistrationResponse
+26
View File
@@ -0,0 +1,26 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package v1
import "github.com/gogf/gf/v2/frame/g"
type GetXiaoZhiTokenReq struct {
g.Meta `path:"/xiaozhi/token" method:"get" tags:"XiaoZhi" summary:"XiaoZhi token"`
}
type GetXiaoZhiTokenRes string
type RefreshTokenReq struct {
g.Meta `path:"/xiaozhi/token/refresh" method:"get" tags:"XiaoZhi" summary:"XiaoZhi token refresh"`
}
type RefreshTokenRes string
type GetXiaoZhiGenerateLicenseTokenReq struct {
g.Meta `path:"/xiaozhi/generateLicenseToken" method:"get" tags:"XiaoZhi" summary:"XiaoZhi generateLicenseToken"`
}
type GetXiaoZhiGenerateLicenseTokenRes string
+17
View File
@@ -0,0 +1,17 @@
// =================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// =================================================================================
package xiaozhi
import (
"context"
"stackChan/api/xiaozhi/v1"
)
type IXiaozhiV1 interface {
GetXiaoZhiToken(ctx context.Context, req *v1.GetXiaoZhiTokenReq) (res *v1.GetXiaoZhiTokenRes, err error)
RefreshToken(ctx context.Context, req *v1.RefreshTokenReq) (res *v1.RefreshTokenRes, err error)
GetXiaoZhiGenerateLicenseToken(ctx context.Context, req *v1.GetXiaoZhiGenerateLicenseTokenReq) (res *v1.GetXiaoZhiGenerateLicenseTokenRes, err error)
}
+115
View File
@@ -0,0 +1,115 @@
create table stackChan.app_store
(
id bigint auto_increment
primary key,
app_name varchar(128) not null comment 'App 名称',
app_icon_url varchar(512) null comment 'App 图标 URL',
description text null comment 'App 描述信息',
firmware_url varchar(512) null comment '固件 / 安装包下载地址',
create_at datetime default CURRENT_TIMESTAMP null comment '创建时间',
update_at datetime default CURRENT_TIMESTAMP null on update CURRENT_TIMESTAMP comment '更新时间',
is_deleted tinyint(1) default 0 not null comment '是否删除0 正常 1 删除'
)
comment 'App Store 应用列表表';
create table stackChan.device_dance
(
id bigint auto_increment
primary key,
mac varchar(17) not null comment '设备MAC地址',
dance_name varchar(64) null comment '舞蹈名称',
dance_data json not null comment 'MotionData',
music_url varchar(255) null comment '舞蹈背景音乐URL',
created_at datetime default CURRENT_TIMESTAMP null,
updated_at datetime default CURRENT_TIMESTAMP null on update CURRENT_TIMESTAMP
);
create table stackChan.device_friend
(
mac_a varchar(17) not null,
mac_b varchar(17) not null,
primary key (mac_a, mac_b)
);
create index fk_friend_mac_b
on stackChan.device_friend (mac_b);
create table stackChan.user
(
uid bigint not null comment '用户唯一UID远程平台主键'
primary key,
username varchar(64) not null comment '登录用户名',
userslug varchar(64) null comment '用户别名',
display_name varchar(64) null comment '用户显示名称',
icon_text char null comment '用户图标文字',
icon_bg_color varchar(16) null comment '图标背景色',
email_confirmed tinyint(1) default 0 null comment '邮箱是否验证 0- 1-',
join_date bigint null comment '注册时间戳(毫秒)',
last_online bigint null comment '最后在线时间戳(毫秒)',
user_status varchar(32) default 'online' null comment '用户在线状态',
create_at datetime default CURRENT_TIMESTAMP null comment '本地创建时间',
update_at datetime default CURRENT_TIMESTAMP null on update CURRENT_TIMESTAMP comment '本地更新时间',
is_deleted tinyint(1) default 0 not null comment '是否删除 0-正常 1-删除',
constraint idx_username
unique (username)
)
comment '用户信息表';
create table stackChan.device
(
mac varchar(17) not null
primary key,
name varchar(255) null,
uid bigint null comment '绑定的用户UID',
bind_time varchar(32) null comment '设备绑定时间',
constraint fk_device_user_uid
foreign key (uid) references stackChan.user (uid)
on update cascade on delete set null
);
create index idx_device_uid
on stackChan.device (uid);
create table stackChan.device_pano
(
id bigint auto_increment
primary key,
mac varchar(17) not null comment '设备MAC地址',
pano_url varchar(512) not null comment '全景图URL',
created_at datetime default CURRENT_TIMESTAMP null comment '创建时间',
updated_at datetime default CURRENT_TIMESTAMP null on update CURRENT_TIMESTAMP,
constraint fk_pano_mac
foreign key (mac) references stackChan.device (mac)
on delete cascade
);
create index idx_device_pano_mac
on stackChan.device_pano (mac);
create table stackChan.device_post
(
id bigint auto_increment
primary key,
mac varchar(17) not null comment '发帖设备MAC',
content_text text null,
content_image varchar(512) null comment '图片URL',
created_at datetime default CURRENT_TIMESTAMP null comment '发帖时间',
constraint fk_post_mac
foreign key (mac) references stackChan.device (mac)
);
create table stackChan.device_post_comment
(
id bigint auto_increment
primary key,
post_id bigint not null comment '帖子ID',
mac varchar(17) not null comment '评论设备MAC',
content text null,
created_at datetime default CURRENT_TIMESTAMP null comment '评论时间',
constraint fk_comment_mac
foreign key (mac) references stackChan.device (mac),
constraint fk_comment_post
foreign key (post_id) references stackChan.device_post (id)
on delete cascade
);
Binary file not shown.
+23 -24
View File
@@ -1,46 +1,45 @@
module stackChan
go 1.24.0
go 1.26.2
require (
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.9.7
github.com/gogf/gf/v2 v2.9.7
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.10.0
github.com/gogf/gf/v2 v2.10.0
github.com/golang-jwt/jwt/v5 v5.3.1
github.com/gorilla/websocket v1.5.3
)
require (
filippo.io/edwards25519 v1.2.0 // indirect
github.com/BurntSushi/toml v1.6.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/clbanning/mxj/v2 v2.7.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/clipperhouse/displaywidth v0.11.0 // indirect
github.com/clipperhouse/uax29/v2 v2.7.0 // indirect
github.com/emirpasic/gods/v2 v2.0.0-alpha // indirect
github.com/fatih/color v1.18.0 // indirect
github.com/fatih/color v1.19.0 // indirect
github.com/fsnotify/fsnotify v1.9.0 // indirect
github.com/glebarez/go-sqlite v1.21.2 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-sql-driver/mysql v1.9.3 // indirect
github.com/goccy/go-json v0.10.6 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grokify/html-strip-tags-go v0.1.0 // indirect
github.com/magiconair/properties v1.8.10 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/olekukonko/errors v1.1.0 // indirect
github.com/olekukonko/ll v0.0.9 // indirect
github.com/olekukonko/tablewriter v1.1.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/mattn/go-isatty v0.0.21 // indirect
github.com/mattn/go-runewidth v0.0.23 // indirect
github.com/olekukonko/cat v0.0.0-20250911104152-50322a0618f6 // indirect
github.com/olekukonko/errors v1.2.0 // indirect
github.com/olekukonko/ll v0.1.8 // indirect
github.com/olekukonko/tablewriter v1.1.4 // indirect
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
go.opentelemetry.io/otel v1.39.0 // indirect
go.opentelemetry.io/otel/metric v1.39.0 // indirect
go.opentelemetry.io/otel/sdk v1.39.0 // indirect
go.opentelemetry.io/otel/trace v1.39.0 // indirect
golang.org/x/net v0.40.0 // indirect
golang.org/x/sys v0.39.0 // indirect
golang.org/x/text v0.32.0 // indirect
go.opentelemetry.io/otel v1.43.0 // indirect
go.opentelemetry.io/otel/metric v1.43.0 // indirect
go.opentelemetry.io/otel/sdk v1.43.0 // indirect
go.opentelemetry.io/otel/trace v1.43.0 // indirect
golang.org/x/net v0.53.0 // indirect
golang.org/x/sys v0.43.0 // indirect
golang.org/x/text v0.36.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
modernc.org/libc v1.22.5 // indirect
modernc.org/mathutil v1.5.0 // indirect
modernc.org/memory v1.5.0 // indirect
modernc.org/sqlite v1.23.1 // indirect
)
-100
View File
@@ -1,100 +0,0 @@
github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk=
github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/clbanning/mxj/v2 v2.7.0 h1:WA/La7UGCanFe5NpHF0Q3DNtnCsVoxbPKuyBNHWRyME=
github.com/clbanning/mxj/v2 v2.7.0/go.mod h1:hNiWqW14h+kc+MdF9C6/YoRfjEJoR3ou6tn/Qo+ve2s=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/emirpasic/gods/v2 v2.0.0-alpha h1:dwFlh8pBg1VMOXWGipNMRt8v96dKAIvBehtCt6OtunU=
github.com/emirpasic/gods/v2 v2.0.0-alpha/go.mod h1:W0y4M2dtBB9U5z3YlghmpuUhiaZT2h6yoeE+C1sCp6A=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo=
github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.9.7 h1:mzs0MblNT0pOlUB00c/hTcAenQ5N/cB651wh9VCJitc=
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.9.7/go.mod h1:Z2MgGyag0fZQ2+9ykafD7tQhau9h5ie3Chg/GUzfy5E=
github.com/gogf/gf/v2 v2.9.7 h1:Vp3VGZ7drPs89tZslT6j6BKBTaw7Xs3DMGWx4MlVtMA=
github.com/gogf/gf/v2 v2.9.7/go.mod h1:Svl1N+E8G/QshU2DUbh/3J/AJauqCgUnxHurXWR4Qx0=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grokify/html-strip-tags-go v0.1.0 h1:03UrQLjAny8xci+R+qjCce/MYnpNXCtgzltlQbOBae4=
github.com/grokify/html-strip-tags-go v0.1.0/go.mod h1:ZdzgfHEzAfz9X6Xe5eBLVblWIxXfYSQ40S/VKrAOGpc=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/magiconair/properties v1.8.10 h1:s31yESBquKXCV9a/ScB3ESkOjUYYv+X0rg8SYxI99mE=
github.com/magiconair/properties v1.8.10/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/olekukonko/errors v1.1.0 h1:RNuGIh15QdDenh+hNvKrJkmxxjV4hcS50Db478Ou5sM=
github.com/olekukonko/errors v1.1.0/go.mod h1:ppzxA5jBKcO1vIpCXQ9ZqgDh8iwODz6OXIGKU8r5m4Y=
github.com/olekukonko/ll v0.0.9 h1:Y+1YqDfVkqMWuEQMclsF9HUR5+a82+dxJuL1HHSRpxI=
github.com/olekukonko/ll v0.0.9/go.mod h1:En+sEW0JNETl26+K8eZ6/W4UQ7CYSrrgg/EdIYT2H8g=
github.com/olekukonko/tablewriter v1.1.0 h1:N0LHrshF4T39KvI96fn6GT8HEjXRXYNDrDjKFDB7RIY=
github.com/olekukonko/tablewriter v1.1.0/go.mod h1:5c+EBPeSqvXnLLgkm9isDdzR3wjfBkHR9Nhfp3NWrzo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
go.opentelemetry.io/otel v1.39.0 h1:8yPrr/S0ND9QEfTfdP9V+SiwT4E0G7Y5MO7p85nis48=
go.opentelemetry.io/otel v1.39.0/go.mod h1:kLlFTywNWrFyEdH0oj2xK0bFYZtHRYUdv1NklR/tgc8=
go.opentelemetry.io/otel/metric v1.39.0 h1:d1UzonvEZriVfpNKEVmHXbdf909uGTOQjA0HF0Ls5Q0=
go.opentelemetry.io/otel/metric v1.39.0/go.mod h1:jrZSWL33sD7bBxg1xjrqyDjnuzTUB0x1nBERXd7Ftcs=
go.opentelemetry.io/otel/sdk v1.39.0 h1:nMLYcjVsvdui1B/4FRkwjzoRVsMK8uL/cj0OyhKzt18=
go.opentelemetry.io/otel/sdk v1.39.0/go.mod h1:vDojkC4/jsTJsE+kh+LXYQlbL8CgrEcwmt1ENZszdJE=
go.opentelemetry.io/otel/sdk/metric v1.39.0 h1:cXMVVFVgsIf2YL6QkRF4Urbr/aMInf+2WKg+sEJTtB8=
go.opentelemetry.io/otel/sdk/metric v1.39.0/go.mod h1:xq9HEVH7qeX69/JnwEfp6fVq5wosJsY1mt4lLfYdVew=
go.opentelemetry.io/otel/trace v1.39.0 h1:2d2vfpEDmCJ5zVYz7ijaJdOF59xLomrvj7bjt6/qCJI=
go.opentelemetry.io/otel/trace v1.39.0/go.mod h1:88w4/PnZSazkGzz/w84VHpQafiU4EtqqlVdxWy+rNOA=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU=
golang.org/x/text v0.32.0/go.mod h1:o/rUWzghvpD5TXrTIBuJU77MTaN0ljMWE47kxGJQ7jY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
modernc.org/libc v1.22.5 h1:91BNch/e5B0uPbJFgqbxXuOnxBQjlS//icfQEGmvyjE=
modernc.org/libc v1.22.5/go.mod h1:jj+Z7dTNX8fBScMVNRAYZ/jF91K8fdT2hYMThc3YjBY=
modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ=
modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
modernc.org/memory v1.5.0 h1:N+/8c5rE6EqugZwHii4IFsaJ7MUhoWX07J5tC/iI5Ds=
modernc.org/memory v1.5.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU=
modernc.org/sqlite v1.23.1 h1:nrSBg4aRQQwq59JpvGEQ15tNxoO5pX/kUjcRNwSAGQM=
modernc.org/sqlite v1.23.1/go.mod h1:OrDj17Mggn6MhE+iPbBNf7RGKODDE9NFT0f3EwDzJqk=
+1 -3
View File
@@ -1,9 +1,7 @@
#SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
#SPDX-License-Identifier: MIT
gfcli:
gen:
dao:
- link: "sqlite::@file(/stackChan.sqlite)"
- link: ""
descriptionTag: true
docker:
+95
View File
@@ -0,0 +1,95 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package boot
import (
"context"
"stackChan/internal/web_socket"
"sync/atomic"
"time"
"github.com/gogf/gf/v2/frame/g"
)
func InitCron() {
startPingTimer()
startCleanTimer()
}
var (
pingTimerStarted atomic.Bool
cleanTimerStarted atomic.Bool
)
// startPingTimer starts the heartbeat timer, with panic recovery and restart logic.
func startPingTimer() {
if !pingTimerStarted.CompareAndSwap(false, true) {
return
}
ctx, cancel := context.WithCancel(context.Background())
_ = cancel // for future use
go func() {
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
g.Log().Info(ctx, "The heartbeat sending timer has been activated")
for {
select {
case <-ctx.Done():
pingTimerStarted.Store(false)
return
case <-ticker.C:
func() {
defer func() {
if err := recover(); err != nil {
g.Log().Errorf(ctx, "Heartbeat sending task crash: %v, the timer is about to be restarted", err)
pingTimerStarted.Store(false)
go func() {
time.Sleep(time.Second)
startPingTimer()
}()
}
}()
web_socket.StartPingTime(ctx)
}()
}
}
}()
}
// startCleanTimer starts the connection cleaning timer, with panic recovery and restart logic.
func startCleanTimer() {
if !cleanTimerStarted.CompareAndSwap(false, true) {
return
}
ctx, cancel := context.WithCancel(context.Background())
_ = cancel // for future use
go func() {
ticker := time.NewTicker(15 * time.Second)
defer ticker.Stop()
g.Log().Info(ctx, "The connection cleaning timer has been started")
for {
select {
case <-ctx.Done():
cleanTimerStarted.Store(false)
return
case <-ticker.C:
func() {
defer func() {
if err := recover(); err != nil {
g.Log().Errorf(ctx, "Connection cleanup task crash: %v, about to restart the timer", err)
cleanTimerStarted.Store(false)
go func() {
time.Sleep(time.Second)
startCleanTimer()
}()
}
}()
web_socket.CheckExpiredLinks(ctx)
}()
}
}
}()
}
+33 -34
View File
@@ -7,23 +7,27 @@ package cmd
import (
"context"
"fmt"
"net"
"net/http"
"path/filepath"
"stackChan/internal/boot"
"stackChan/internal/controller/admin"
"stackChan/internal/controller/appstore"
"stackChan/internal/controller/dance"
"stackChan/internal/controller/device"
"stackChan/internal/controller/file"
"stackChan/internal/controller/friend"
"stackChan/internal/controller/pano"
"stackChan/internal/controller/post"
"stackChan/internal/controller/stackchandevice"
"stackChan/internal/controller/user"
"stackChan/internal/controller/xiaozhi"
"stackChan/internal/middleware"
"stackChan/internal/web_socket"
"time"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gcmd"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/os/gtimer"
)
var (
@@ -32,20 +36,16 @@ var (
Usage: "main",
Brief: "start http server",
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
PrintIPAddr()
//Start a scheduled task to send ping messages
gtimer.SetInterval(ctx, time.Second*5, func(ctx context.Context) {
web_socket.StartPingTime(ctx)
})
//Start a timer to clean up long-lived connections that have been inactive for a long time on the app.
gtimer.SetInterval(ctx, time.Second*15, func(ctx context.Context) {
web_socket.CheckExpiredLinks(ctx)
})
s := g.Server()
s.SetClientMaxBodySize(100 * 1024 * 1024)
s.Use(middleware.CORS)
s.BindHandler("/stackChan/ws", web_socket.Handler)
// heartBeat
boot.InitCron()
///Configuration file access
s.Group("/file", func(group *ghttp.RouterGroup) {
group.GET("/*filepath", func(r *ghttp.Request) {
@@ -65,28 +65,27 @@ var (
})
})
s.Group("/stackChan", func(group *ghttp.RouterGroup) {
group.Middleware(ghttp.MiddlewareHandlerResponse)
group.Bind(device.NewV1(), friend.NewV1(), dance.NewV1(), file.NewV1(), post.NewV1())
s.Group("/stackChan/v2", func(group *ghttp.RouterGroup) {
group.Middleware(middleware.V2TokenAuthMiddleware, ghttp.MiddlewareHandlerResponse)
group.Bind(user.NewV2(), dance.NewV2(), device.NewV2())
})
s.Group("/stackChan", func(group *ghttp.RouterGroup) {
group.Middleware(middleware.TokenAuthMiddleware, ghttp.MiddlewareHandlerResponse)
group.Bind(device.NewV1(), friend.NewV1(), dance.NewV1(), file.NewV1(), post.NewV1(), pano.NewV1(), appstore.NewV1(), xiaozhi.NewV1(), stackchandevice.NewV2())
})
s.Group("/admin/stackChan", func(group *ghttp.RouterGroup) {
group.Middleware(middleware.AdminTokenAuthMiddleware, ghttp.MiddlewareHandlerResponse)
group.Bind(admin.NewV1(), file.NewV1())
})
// Do not use SetServerRoot, globally only provide frontend entry via /web
//s.SetServerRoot("web/management")
s.SetPort(12800)
s.Run()
return nil
},
}
)
func PrintIPAddr() {
addrs, err := net.InterfaceAddrs()
if err == nil {
fmt.Println("Local IP addresses detected on this machine:")
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && ipnet.IP.To4() != nil {
fmt.Println(" -", ipnet.IP.String())
}
}
} else {
fmt.Println("Could not detect local IP addresses:", err)
}
fmt.Println("Please update the StackChan and iOS client access addresses to use one of the above local IPs as needed.")
}
@@ -0,0 +1,6 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package admin
@@ -0,0 +1,20 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package admin
import (
"stackChan/api/admin"
)
type ControllerV1 struct{}
func NewV1() admin.IAdminV1 {
return &ControllerV1{}
}
@@ -0,0 +1,37 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package admin
import (
"context"
"stackChan/api/admin/v1"
"stackChan/internal/dao"
"stackChan/internal/model"
"stackChan/internal/model/do"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
)
func (c *ControllerV1) AddApp(ctx context.Context, req *v1.AddAppReq) (res *v1.AddAppRes, err error) {
app := do.AppStore{
AppName: req.AppName,
AppIconUrl: req.AppIconUrl,
Description: req.Description,
FirmwareUrl: req.FirmwareUrl,
}
id, err := dao.AppStore.Ctx(ctx).Data(&app).InsertAndGetId()
if err != nil {
return nil, gerror.WrapCode(gcode.CodeInternalError, err, "Failed to insert app")
}
var appInfo model.AppInfo
err = dao.AppStore.Ctx(ctx).Where("id", id).Scan(&appInfo)
if err != nil {
return nil, gerror.WrapCode(gcode.CodeInternalError, err, "Failed to fetch inserted app")
}
res = (*v1.AddAppRes)(&appInfo)
return res, nil
}
@@ -0,0 +1,17 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package admin
import (
"context"
"stackChan/internal/service"
"stackChan/api/admin/v1"
)
func (c *ControllerV1) AdminLogin(ctx context.Context, req *v1.AdminLoginReq) (res *v1.AdminLoginRes, err error) {
return service.AdminLogin(ctx, req)
}
@@ -0,0 +1,28 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package admin
import (
"context"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"stackChan/api/admin/v1"
"stackChan/internal/dao"
"github.com/gogf/gf/v2/frame/g"
)
func (c *ControllerV1) DeleteApp(ctx context.Context, req *v1.DeleteAppReq) (res *v1.DeleteAppRes, err error) {
_, err = dao.AppStore.Ctx(ctx).Where("id", req.Id).Data(g.Map{"is_deleted": 1}).Update()
if err != nil {
return nil, gerror.WrapCode(gcode.CodeInternalError, err, "Failed to delete app")
}
res = &v1.DeleteAppRes{}
return res, nil
}
@@ -0,0 +1,22 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package admin
import (
"context"
"stackChan/internal/service"
"stackChan/api/admin/v1"
)
func (c *ControllerV1) GetAppList(ctx context.Context, req *v1.GetAppListReq) (res *v1.GetAppListRes, err error) {
apps, err := service.GetAppList(ctx)
if err != nil {
return nil, err
}
response := v1.GetAppListRes(apps)
return &response, nil
}
@@ -0,0 +1,50 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package admin
import (
"context"
"stackChan/internal/model"
"stackChan/api/admin/v1"
"stackChan/internal/dao"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
)
func (c *ControllerV1) UpdateApp(ctx context.Context, req *v1.UpdateAppReq) (res *v1.UpdateAppRes, err error) {
updateData := g.Map{}
if req.AppName != "" {
updateData["app_name"] = req.AppName
}
if req.AppIconUrl != "" {
updateData["app_icon_url"] = req.AppIconUrl
}
if req.Description != "" {
updateData["description"] = req.Description
}
if req.FirmwareUrl != "" {
updateData["firmware_url"] = req.FirmwareUrl
}
if len(updateData) > 0 {
_, err = dao.AppStore.Ctx(ctx).WherePri(req.Id).Data(updateData).Update()
if err != nil {
return nil, gerror.WrapCode(gcode.CodeInternalError, err, "Failed to update app")
}
}
var appInfo model.AppInfo
err = dao.AppStore.Ctx(ctx).WherePri(req.Id).Scan(&appInfo)
if err != nil {
return nil, gerror.WrapCode(gcode.CodeInternalError, err, "Failed to fetch updated app")
}
res = (*v1.UpdateAppRes)(&appInfo)
return res, nil
}
@@ -0,0 +1,10 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package appstore
@@ -0,0 +1,20 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package appstore
import (
"stackChan/api/appstore"
)
type ControllerV1 struct{}
func NewV1() appstore.IAppstoreV1 {
return &ControllerV1{}
}
@@ -0,0 +1,22 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package appstore
import (
"context"
"stackChan/internal/service"
"stackChan/api/appstore/v1"
)
func (c *ControllerV1) GetAppList(ctx context.Context, req *v1.GetAppListReq) (res *v1.GetAppListRes, err error) {
apps, err := service.GetAppList(ctx)
if err != nil {
return nil, err
}
response := v1.GetAppListRes(apps)
return &response, nil
}
@@ -17,3 +17,9 @@ type ControllerV1 struct{}
func NewV1() dance.IDanceV1 {
return &ControllerV1{}
}
type ControllerV2 struct{}
func NewV2() dance.IDanceV2 {
return &ControllerV2{}
}
@@ -7,60 +7,75 @@ package dance
import (
"context"
"encoding/json"
"stackChan/internal/dao"
"stackChan/internal/model"
"stackChan/internal/model/do"
"stackChan/api/dance/v1"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
)
func (c *ControllerV1) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
if req.Index < 0 {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "Index cannot be negative")
// 1. Get and validate MAC address (business required parameter)
mac := g.RequestFromCtx(ctx).GetCtxVar(model.Mac).String()
if mac == "" {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "MAC address cannot be empty")
}
device, err := dao.Device.Ctx(ctx).Where("mac=", req.Mac).One()
if err != nil {
return nil, err
// 2. Auto validate using struct v tag (DanceName required), manual secondary validation as fallback
if req.DanceName == "" {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "Dance name cannot be empty")
}
if device.IsEmpty() {
_, err = dao.Device.Ctx(ctx).Data(dao.Device.Columns().Mac, req.Mac).Insert()
if err != nil {
return nil, err
// 3. Validate dance data not empty (RawMessage need to check if empty/only null)
if len(req.DanceData) == 0 || string(req.DanceData) == "null" {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "Dance data cannot be empty or null")
}
// 4. Use transaction to ensure data consistency
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
// 4.1 Query device, create if not exists
device, err := dao.Device.Ctx(ctx).TX(tx).Where("mac=?", mac).One()
if err != nil && !gerror.HasCode(err, gcode.CodeNotFound) {
return gerror.NewCode(gcode.CodeInternalError, "Failed to query device: %v", err.Error())
}
}
dance, err := dao.DeviceDance.Ctx(ctx).Where("mac=?", req.Mac).Where("dance_index=?", req.Index).One()
if err != nil {
return nil, err
}
// Create device if not exists
if device.IsEmpty() {
_, err = dao.Device.Ctx(ctx).TX(tx).Data(dao.Device.Columns().Mac, mac).Insert()
if err != nil {
return gerror.NewCode(gcode.CodeInternalError, "Failed to create device: %v", err.Error())
}
}
danceListJSON, err := json.Marshal(req.List)
if err != nil {
return nil, err
}
// 4.2 Check if dance data with same MAC+DanceName exists (avoid duplicates)
exist, err := dao.DeviceDance.Ctx(ctx).TX(tx).
Where("mac=?", mac).
Where("dance_name=?", req.DanceName).
Exist()
if err != nil {
return gerror.NewCode(gcode.CodeInternalError, "Failed to check duplicate dance data: %v", err.Error())
}
if exist {
return gerror.NewCode(gcode.CodeBusinessValidationFailed, "Dance data with MAC %s and name '%s' already exists", mac, req.DanceName)
}
if dance.IsEmpty() {
_, err = dao.DeviceDance.Ctx(ctx).Data(do.DeviceDance{
Mac: req.Mac,
DanceIndex: req.Index,
DanceData: danceListJSON,
// 4.3 Insert dance data (use RawMessage directly, no need for secondary serialization)
_, err = dao.DeviceDance.Ctx(ctx).TX(tx).Data(do.DeviceDance{
Mac: mac,
DanceData: req.DanceData, // Use RawMessage directly, avoid duplicate marshal
DanceName: req.DanceName,
MusicUrl: req.MusicUrl, // Add background music URL field
}).Insert()
if err != nil {
return nil, err
}
} else {
_, err = dao.DeviceDance.Ctx(ctx).Where("mac=?", req.Mac).Where("dance_index=?", req.Index).Data(do.DeviceDance{
DanceData: danceListJSON,
}).Update()
if err != nil {
return nil, err
return gerror.NewCode(gcode.CodeInternalError, "Failed to insert dance data: %v", err.Error())
}
return nil
})
if err != nil {
return nil, err
}
response := v1.CreateRes("Dance data saved successfully")
return &response, nil
@@ -8,11 +8,20 @@ package dance
import (
"context"
"stackChan/internal/dao"
"stackChan/internal/model"
"stackChan/api/dance/v1"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
)
func (c *ControllerV1) Delete(ctx context.Context, req *v1.DeleteReq) (res *v1.DeleteRes, err error) {
_, err = dao.DeviceDance.Ctx(ctx).Where("mac=", req.Mac).Where("dance_index=", req.Index).Delete()
mac := g.RequestFromCtx(ctx).GetCtxVar(model.Mac).String()
if mac == "" {
return nil, gerror.NewCode(gcode.CodeInvalidParameter)
}
_, err = dao.DeviceDance.Ctx(ctx).Where("id=", req.Id).Delete()
return
}
@@ -0,0 +1,34 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package dance
import (
"context"
"stackChan/internal/dao"
"stackChan/internal/model"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"stackChan/api/dance/v1"
)
func (c *ControllerV1) GetDanceInfo(ctx context.Context, req *v1.GetDanceInfoReq) (res *v1.GetDanceInfoRes, err error) {
mac := g.RequestFromCtx(ctx).GetCtxVar(model.Mac).String()
if mac == "" {
return nil, gerror.NewCode(gcode.CodeInvalidParameter)
}
if req.Id == 0 {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "The dance ID cannot be left blank.")
}
var dance model.Dance
err = dao.DeviceDance.Ctx(ctx).Where("mac=?", mac).Where("id=?", req.Id).Scan(&dance)
if err != nil {
return nil, gerror.NewCode(gcode.CodeInternalError)
}
return new(v1.GetDanceInfoRes(dance)), nil
}
@@ -7,38 +7,52 @@ package dance
import (
"context"
"encoding/json"
"stackChan/internal/dao"
"stackChan/internal/model"
"stackChan/internal/model/do"
"stackChan/internal/model/entity"
"strconv"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"stackChan/api/dance/v1"
)
func (c *ControllerV1) GetList(ctx context.Context, req *v1.GetListReq) (res *v1.GetListRes, err error) {
danceMap := make(map[string][]model.DanceData)
var list []entity.DeviceDance
mac := g.RequestFromCtx(ctx).GetCtxVar(model.Mac).String()
if mac == "" {
return nil, gerror.NewCode(gcode.CodeInvalidParameter)
}
var danceList []model.Dance
err = dao.DeviceDance.Ctx(ctx).Where(do.DeviceDance{
Mac: req.Mac,
}).Scan(&list)
Mac: mac,
}).Scan(&danceList)
if err != nil {
return nil, err
}
if len(list) > 0 {
deviceDance := list[0]
var danceList []model.DanceData
err = json.Unmarshal([]byte(deviceDance.DanceData), &danceList)
if err != nil {
return nil, gerror.WrapCode(gcode.CodeInvalidParameter, err)
// Core modification: insert default data only when query result is empty
if len(danceList) == 0 {
// Insert single default dance data
defaultDance := do.DeviceDance{
Mac: mac,
MusicUrl: "file/music/stackchan_music.mp3",
DanceData: model.DefaultDanceData,
}
_, err = dao.DeviceDance.Ctx(ctx).Data(defaultDance).Insert()
if err != nil {
return nil, err
}
// Re-query list (one data exists now)
err = dao.DeviceDance.Ctx(ctx).Where(do.DeviceDance{
Mac: mac,
}).Scan(&danceList)
if err != nil {
return nil, err
}
key := strconv.Itoa(deviceDance.DanceIndex)
danceMap[key] = danceList
}
response := v1.GetListRes(danceMap)
return &response, nil
return new(v1.GetListRes(danceList)), nil
}
@@ -0,0 +1,17 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package dance
import (
"context"
"stackChan/api/dance/v1"
)
func (c *ControllerV1) GetMusicList(ctx context.Context, req *v1.GetMusicListReq) (res *v1.GetMusicListRes, err error) {
var list = make([]string, 1)
list = append(list, "file/music/stackchan_music.mp3")
return new(v1.GetMusicListRes(list)), nil
}
@@ -9,23 +9,42 @@ import (
"context"
"encoding/json"
"stackChan/internal/dao"
"stackChan/internal/model"
"stackChan/internal/model/do"
"stackChan/api/dance/v1"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
)
func (c *ControllerV1) Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error) {
response := v1.UpdateRes("")
danceJSON, err := json.Marshal(req.Data)
mac := g.RequestFromCtx(ctx).GetCtxVar(model.Mac).String()
if mac == "" {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "MAC address cannot be empty")
}
if req.Id == 0 { // Adjust based on actual type of req.Id (int/string)
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "Dance ID cannot be empty")
}
updateData := do.DeviceDance{}
if req.MusicUrl != "" {
updateData.MusicUrl = req.MusicUrl
}
if req.DanceName != "" {
updateData.DanceName = req.DanceName
}
if req.DanceData != nil {
danceJSON, err := json.Marshal(req.DanceData)
if err != nil {
// Wrap serialization error, add business prompt
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "Dance data serialization failed: %v")
}
updateData.DanceData = danceJSON
}
_, err = dao.DeviceDance.Ctx(ctx).Where("mac=?", mac).Where("id=?", req.Id).Data(updateData).Update()
if err != nil {
return nil, err
}
_, err = dao.DeviceDance.Ctx(ctx).Where("mac=?", req.Mac).Where("dance_index=?", req.Index).Data(do.DeviceDance{
DanceData: danceJSON,
}).Update()
if err != nil {
return nil, err
}
response = "Update successful"
return &response, nil
return new(v1.UpdateRes("Update successful")), nil
}
@@ -0,0 +1,66 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package dance
import (
"context"
"stackChan/internal/dao"
"stackChan/internal/model/do"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"stackChan/api/dance/v2"
)
func (c *ControllerV2) Create(ctx context.Context, req *v2.CreateReq) (res *v2.CreateRes, err error) {
mac := req.Mac
if req.DanceName == "" {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "Dance name cannot be empty")
}
if len(req.DanceData) == 0 || string(req.DanceData) == "null" {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "Dance data cannot be empty or null")
}
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
device, err := dao.Device.Ctx(ctx).TX(tx).Where("mac=?", mac).One()
if err != nil && !gerror.HasCode(err, gcode.CodeNotFound) {
return gerror.NewCode(gcode.CodeInternalError, "Failed to query device: %v", err.Error())
}
if device.IsEmpty() {
_, err = dao.Device.Ctx(ctx).TX(tx).Data(dao.Device.Columns().Mac, mac).Insert()
if err != nil {
return gerror.NewCode(gcode.CodeInternalError, "Failed to create device: %v", err.Error())
}
}
exist, err := dao.DeviceDance.Ctx(ctx).TX(tx).
Where("mac=?", mac).
Where("dance_name=?", req.DanceName).
Exist()
if err != nil {
return gerror.NewCode(gcode.CodeInternalError, "Failed to check duplicate dance data: %v", err.Error())
}
if exist {
return gerror.NewCode(gcode.CodeBusinessValidationFailed, "Dance data with MAC %s and name '%s' already exists", mac, req.DanceName)
}
_, err = dao.DeviceDance.Ctx(ctx).TX(tx).Data(do.DeviceDance{
Mac: mac,
DanceData: req.DanceData,
DanceName: req.DanceName,
MusicUrl: req.MusicUrl,
}).Insert()
if err != nil {
return gerror.NewCode(gcode.CodeInternalError, "Failed to insert dance data: %v", err.Error())
}
return nil
})
if err != nil {
return nil, err
}
return new(v2.CreateRes("Dance data saved successfully")), nil
}
@@ -0,0 +1,18 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package dance
import (
"context"
"stackChan/internal/dao"
"stackChan/api/dance/v2"
)
func (c *ControllerV2) Delete(ctx context.Context, req *v2.DeleteReq) (res *v2.DeleteRes, err error) {
_, err = dao.DeviceDance.Ctx(ctx).Where("id=", req.Id).Delete()
return
}
@@ -0,0 +1,29 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package dance
import (
"context"
"stackChan/internal/dao"
"stackChan/internal/model"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"stackChan/api/dance/v2"
)
func (c *ControllerV2) GetDanceInfo(ctx context.Context, req *v2.GetDanceInfoReq) (res *v2.GetDanceInfoRes, err error) {
if req.Id == 0 {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "The dance ID cannot be left blank.")
}
var dance model.Dance
err = dao.DeviceDance.Ctx(ctx).Where("id=?", req.Id).Scan(&dance)
if err != nil {
return nil, gerror.NewCode(gcode.CodeInternalError)
}
return new(v2.GetDanceInfoRes(dance)), nil
}
@@ -0,0 +1,50 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package dance
import (
"context"
"stackChan/api/dance/v2"
"stackChan/internal/dao"
"stackChan/internal/model"
"stackChan/internal/model/do"
)
func (c *ControllerV2) GetList(ctx context.Context, req *v2.GetListReq) (res *v2.GetListRes, err error) {
mac := req.Mac
var danceList []model.Dance
err = dao.DeviceDance.Ctx(ctx).Where(do.DeviceDance{
Mac: mac,
}).Scan(&danceList)
if err != nil {
return nil, err
}
// Core modification: insert default data only when query result is empty
if len(danceList) == 0 {
// Insert single default dance data
defaultDance := do.DeviceDance{
DanceName: "StackChan Dance",
Mac: mac,
MusicUrl: "http://47.113.125.164:12800/file/music/stackchan_music.mp3",
DanceData: model.DefaultDanceData,
}
_, err = dao.DeviceDance.Ctx(ctx).Data(defaultDance).Insert()
if err != nil {
return nil, err
}
// Re-query list (one data exists now)
err = dao.DeviceDance.Ctx(ctx).Where(do.DeviceDance{
Mac: mac,
}).Scan(&danceList)
if err != nil {
return nil, err
}
}
return new(v2.GetListRes(danceList)), nil
}
@@ -0,0 +1,44 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package dance
import (
"context"
"encoding/json"
"stackChan/internal/dao"
"stackChan/internal/model/do"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"stackChan/api/dance/v2"
)
func (c *ControllerV2) Update(ctx context.Context, req *v2.UpdateReq) (res *v2.UpdateRes, err error) {
if req.Id == 0 { // Adjust based on actual type of req.Id (int/string)
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "Dance ID cannot be empty")
}
updateData := do.DeviceDance{}
if req.MusicUrl != "" {
updateData.MusicUrl = req.MusicUrl
}
if req.DanceName != "" {
updateData.DanceName = req.DanceName
}
if req.DanceData != nil {
danceJSON, err := json.Marshal(req.DanceData)
if err != nil {
// Wrap serialization error, add business prompt
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "Dance data serialization failed: %v")
}
updateData.DanceData = danceJSON
}
_, err = dao.DeviceDance.Ctx(ctx).Where("id=?", req.Id).Data(updateData).Update()
if err != nil {
return nil, err
}
return new(v2.UpdateRes("Update successful")), nil
}
@@ -18,3 +18,9 @@ type ControllerV1 struct{}
func NewV1() device.IDeviceV1 {
return &ControllerV1{}
}
type ControllerV2 struct{}
func NewV2() device.IDeviceV2 {
return &ControllerV2{}
}
@@ -9,12 +9,21 @@ import (
"context"
"stackChan/api/device/v1"
"stackChan/internal/dao"
"stackChan/internal/model"
"stackChan/internal/model/do"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
)
func (c *ControllerV1) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
mac := g.RequestFromCtx(ctx).GetCtxVar(model.Mac).String()
if mac == "" {
return nil, gerror.NewCode(gcode.CodeInvalidParameter)
}
insertId, err := dao.Device.Ctx(ctx).Data(do.Device{
Mac: req.Mac,
Mac: mac,
Name: req.Name,
}).InsertAndGetId()
if err != nil {
@@ -11,11 +11,19 @@ import (
"stackChan/internal/model"
"stackChan/api/device/v1"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
)
func (c *ControllerV1) GetDeviceInfo(ctx context.Context, req *v1.GetDeviceInfoReq) (res *v1.GetDeviceInfoRes, err error) {
mac := g.RequestFromCtx(ctx).GetCtxVar(model.Mac).String()
if mac == "" {
return nil, gerror.NewCode(gcode.CodeInvalidParameter)
}
var info model.DeviceInfo
err = dao.Device.Ctx(ctx).WherePri(req.Mac).Scan(&info)
err = dao.Device.Ctx(ctx).WherePri(mac).Scan(&info)
if err != nil {
return nil, err
}
@@ -9,14 +9,28 @@ import (
"context"
"stackChan/api/device/v1"
"stackChan/internal/dao"
"stackChan/internal/model"
"stackChan/internal/model/entity"
"stackChan/internal/web_socket"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
)
func (c *ControllerV1) GetRandomDevice(ctx context.Context, req *v1.GetRandomDeviceReq) (res *v1.GetRandomDeviceRes, err error) {
mac := g.RequestFromCtx(ctx).GetCtxVar(model.Mac).String()
if mac == "" {
return nil, gerror.NewCode(gcode.CodeInvalidParameter)
}
pageSize := req.PageSize
if pageSize <= 0 {
pageSize = 6
}
// Obtain the list of online StackChan mac addresses (excluding the current user) from the websocket layer.
macList := web_socket.GetRandomStackChanDevice(req.Mac, 6)
macList := web_socket.GetRandomStackChanDevice(mac, pageSize)
if len(macList) == 0 {
res = (*v1.GetRandomDeviceRes)(&[]entity.Device{})
@@ -8,14 +8,23 @@ package device
import (
"context"
"stackChan/internal/dao"
"stackChan/internal/model"
"stackChan/internal/model/do"
"stackChan/api/device/v1"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
)
func (c *ControllerV1) Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error) {
mac := g.RequestFromCtx(ctx).GetCtxVar(model.Mac).String()
if mac == "" {
return nil, gerror.NewCode(gcode.CodeInvalidParameter)
}
_, err = dao.Device.Ctx(ctx).Data(do.Device{
Name: req.Name,
}).WherePri(req.Mac).Update()
}).WherePri(mac).Update()
return
}
@@ -8,17 +8,26 @@ package device
import (
"context"
"stackChan/internal/dao"
"stackChan/internal/model"
"stackChan/internal/model/do"
"stackChan/api/device/v1"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
)
func (c *ControllerV1) UpdateDeviceInfo(ctx context.Context, req *v1.UpdateDeviceInfoReq) (res *v1.UpdateDeviceInfoRes, err error) {
mac := g.RequestFromCtx(ctx).GetCtxVar(model.Mac).String()
if mac == "" {
return nil, gerror.NewCode(gcode.CodeInvalidParameter)
}
doDevice := do.Device{}
if req.Name != "" {
doDevice.Name = req.Name
}
_, err = dao.Device.Ctx(ctx).Data(doDevice).WherePri(req.Mac).Update()
_, err = dao.Device.Ctx(ctx).Data(doDevice).WherePri(mac).Update()
if err != nil {
return nil, err
}
@@ -0,0 +1,26 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package device
import (
"context"
"stackChan/api/device/v2"
)
func (c *ControllerV2) AgentRestoreDefault(ctx context.Context, req *v2.AgentRestoreDefaultReq) (res *v2.AgentRestoreDefaultRes, err error) {
return new(v2.AgentRestoreDefaultRes(true)), err
//if req.Mac == "" {
// return nil, gerror.NewCode(gcode.CodeMissingParameter, "Device MAC address cannot be empty")
//}
//restoreResponse, err := service.RestoreDefaultAgent(req.Mac)
//if err != nil {
// return nil, err
//}
//if !restoreResponse {
// return nil, gerror.NewCode(gcode.CodeInternalError, "Failed to restore default configuration")
//}
//return new(v2.AgentRestoreDefaultRes(true)), nil
}
@@ -0,0 +1,41 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package device
import (
"context"
"stackChan/internal/dao"
"stackChan/internal/model"
"stackChan/internal/service"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
"stackChan/api/device/v2"
)
// BindDevice Device binding interface
func (c *ControllerV2) BindDevice(ctx context.Context, req *v2.BindDeviceReq) (res *v2.BindDeviceRes, err error) {
// 1. Get current logged-in user UID (from context)
_, err = service.CreateMacIfNotExists(ctx, req.Mac)
uid := g.RequestFromCtx(ctx).GetCtxVar(model.Uid).Int64()
if uid == 0 {
return nil, gerror.NewCode(gcode.CodeMissingParameter, "User UID cannot be empty")
}
if req.Mac == "" {
return nil, gerror.NewCode(gcode.CodeMissingParameter, "Device MAC address cannot be empty")
}
_, err = dao.Device.Ctx(ctx).
Where("mac = ?", req.Mac).
Data("uid", uid, "bind_time", gtime.Now().Format("Y-m-d H:i:s")).
Update()
if err != nil {
return nil, gerror.WrapCode(gcode.CodeDbOperationError, err, "Device binding failed")
}
return new(v2.BindDeviceRes(true)), nil
}
@@ -0,0 +1,31 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package device
import (
"context"
"stackChan/internal/dao"
"stackChan/internal/model"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"stackChan/api/device/v2"
)
func (c *ControllerV2) GetDevices(ctx context.Context, req *v2.GetDevicesReq) (res *v2.GetDevicesRes, err error) {
uid := g.RequestFromCtx(ctx).GetCtxVar(model.Uid).Int64()
if uid == 0 {
return nil, gerror.NewCode(gcode.CodeMissingParameter, "user UID is required")
}
devices := make([]model.DeviceInfo, 0)
err = dao.Device.Ctx(ctx).Where("uid=?", uid).Scan(&devices)
if err != nil {
return nil, err
}
return new(v2.GetDevicesRes(devices)), nil
}
@@ -0,0 +1,69 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package device
import (
"context"
"stackChan/internal/dao"
"stackChan/internal/model"
"stackChan/internal/service"
"stackChan/internal/xiaozhi"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"stackChan/api/device/v2"
)
// UnbindDevice Device unbinding interface
func (c *ControllerV2) UnbindDevice(ctx context.Context, req *v2.UnbindDeviceReq) (res *v2.UnbindDeviceRes, err error) {
_, err = service.CreateMacIfNotExists(ctx, req.Mac)
if err != nil {
return nil, gerror.WrapCode(gcode.CodeDbOperationError, err, "Failed to initialize device information")
}
uid := g.RequestFromCtx(ctx).GetCtxVar(model.Uid).Int64()
if uid == 0 {
return nil, gerror.NewCode(gcode.CodeMissingParameter, "User UID cannot be empty")
}
// 3. Validate MAC address parameter
if req.Mac == "" {
return nil, gerror.NewCode(gcode.CodeMissingParameter, "Device MAC address cannot be empty")
}
restoreResponse, err := service.RestoreDefaultAgent(req.Mac)
if err != nil {
return nil, err
}
if !restoreResponse {
return nil, gerror.NewCode(gcode.CodeInternalError, "Failed to restore default configuration")
}
// xiaozhi Unbind Device
unbindResponse, err := xiaozhi.UnbindDevice(&req.Mac)
if err != nil {
return nil, gerror.NewCode(gcode.CodeInternalError)
}
if !unbindResponse {
g.Log().Error(ctx, "xiaozhi Unbind Device failed:")
return nil, gerror.NewCode(gcode.CodeInternalError)
}
// 4. Perform unbind: set uid to 0/NULL (only the current user's own device can be unbound)
_, err = dao.Device.Ctx(ctx).
Where("mac = ?", req.Mac).
Where("uid = ?", uid).
Data("uid", nil, "bind_time", nil).
Update()
if err != nil {
return nil, gerror.WrapCode(gcode.CodeDbOperationError, err, "Device unbinding failed")
}
// 5. Return success response (consistent with bind interface format)
return new(v2.UnbindDeviceRes(true)), nil
}
@@ -0,0 +1,67 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package device
import (
"context"
"stackChan/internal/dao"
"stackChan/internal/model"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"stackChan/api/device/v2"
)
func (c *ControllerV2) UpdateDevice(ctx context.Context, req *v2.UpdateDeviceReq) (res *v2.UpdateDeviceRes, err error) {
// Get current logged-in user UID
uid := g.RequestFromCtx(ctx).GetCtxVar(model.Uid).Int64()
if uid == 0 {
return nil, gerror.NewCode(gcode.CodeMissingParameter, "User UID cannot be empty")
}
// check device exists and belongs to current user
count, err := dao.Device.Ctx(ctx).
Where("mac = ?", req.Mac).
Where("uid = ?", uid).
Count()
if err != nil {
return nil, gerror.WrapCode(gcode.CodeDbOperationError, err, "Failed to query device information")
}
if count == 0 {
return nil, gerror.NewCode(gcode.CodeNotFound, "device not found or not belong to current user")
}
// build update data
updateData := g.Map{}
if req.Name != "" {
updateData["name"] = req.Name
}
if req.Longitude != 0 {
updateData["longitude"] = req.Longitude
}
if req.Latitude != 0 {
updateData["latitude"] = req.Latitude
}
// no need to update
if len(updateData) == 0 {
return new(v2.UpdateDeviceRes(true)), nil
}
// update device information
_, err = dao.Device.Ctx(ctx).
Where("mac = ?", req.Mac).
Where("uid = ?", uid).
Data(updateData).
Update()
if err != nil {
return nil, gerror.WrapCode(gcode.CodeDbOperationError, err, "update device failed")
}
return new(v2.UpdateDeviceRes(true)), nil
}
@@ -8,18 +8,25 @@ package friend
import (
"context"
"stackChan/internal/dao"
"stackChan/internal/model"
"stackChan/internal/model/entity"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"stackChan/api/friend/v1"
)
func (c *ControllerV1) Add(ctx context.Context, req *v1.AddReq) (res *v1.AddRes, err error) {
if req.Mac == req.FriendMac {
mac := g.RequestFromCtx(ctx).GetCtxVar(model.Mac).String()
if mac == "" {
return nil, gerror.NewCode(gcode.CodeInvalidParameter)
}
if mac == req.FriendMac {
return nil, gerror.New("You cannot add yourself as a friend")
}
macA := req.Mac
macA := mac
macB := req.FriendMac
var friend entity.DeviceFriend
err = dao.DeviceFriend.Ctx(ctx).
+10
View File
@@ -0,0 +1,10 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package pano
@@ -0,0 +1,20 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package pano
import (
"stackChan/api/pano"
)
type ControllerV1 struct{}
func NewV1() pano.IPanoV1 {
return &ControllerV1{}
}
@@ -0,0 +1,45 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package pano
import (
"context"
"stackChan/internal/dao"
"stackChan/internal/model"
"stackChan/internal/model/entity"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"stackChan/api/pano/v1"
)
func (c *ControllerV1) AddPano(ctx context.Context, req *v1.AddPanoReq) (res *v1.AddPanoRes, err error) {
mac := g.RequestFromCtx(ctx).GetCtxVar(model.Mac).String()
if mac == "" {
return nil, gerror.NewCode(gcode.CodeInvalidParameter)
}
if req.Url == "" {
return nil, gerror.NewCode(gcode.CodeInvalidParameter)
}
Id, err := dao.DevicePano.Ctx(ctx).Data(entity.DevicePano{
Mac: mac,
PanoUrl: req.Url,
}).InsertAndGetId()
if err != nil {
return nil, gerror.NewCode(gcode.CodeInvalidParameter)
}
res = &v1.AddPanoRes{
Id: Id,
}
return res, nil
}
@@ -0,0 +1,41 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package pano
import (
"context"
"stackChan/internal/dao"
"stackChan/internal/model"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"stackChan/api/pano/v1"
)
func (c *ControllerV1) GetPanoList(ctx context.Context, req *v1.GetPanoListReq) (res *v1.GetPanoListRes, err error) {
mac := g.RequestFromCtx(ctx).GetCtxVar(model.Mac).String()
if mac == "" {
return nil, gerror.NewCode(gcode.CodeInvalidParameter)
}
var list []model.Pano
err = dao.DevicePano.Ctx(ctx).Where("mac = ?", mac).Scan(&list)
if err != nil {
return nil, gerror.NewCode(gcode.CodeInvalidParameter)
}
if list == nil {
list = make([]model.Pano, 0)
}
response := v1.GetPanoListRes(list)
return &response, nil
}
@@ -8,16 +8,22 @@ package post
import (
"context"
"stackChan/internal/dao"
"stackChan/internal/model"
"stackChan/internal/model/do"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"stackChan/api/post/v1"
)
func (c *ControllerV1) CreatePost(ctx context.Context, req *v1.CreatePostReq) (res *v1.CreatePostRes, err error) {
device, err := dao.Device.Ctx(ctx).Where("mac", req.Mac).One()
mac := g.RequestFromCtx(ctx).GetCtxVar(model.Mac).String()
if mac == "" {
return nil, gerror.NewCode(gcode.CodeInvalidParameter)
}
device, err := dao.Device.Ctx(ctx).Where("mac", mac).One()
if err != nil {
return nil, err
}
@@ -25,7 +31,7 @@ func (c *ControllerV1) CreatePost(ctx context.Context, req *v1.CreatePostReq) (r
return nil, gerror.NewCode(gcode.CodeInvalidRequest, "The device does not exist or the Mac address is incorrect")
}
insertId, err := dao.DevicePost.Ctx(ctx).Data(do.DevicePost{
Mac: req.Mac,
Mac: mac,
ContentText: req.ContentText,
ContentImage: req.ContentImage,
}).InsertAndGetId()
@@ -8,15 +8,24 @@ package post
import (
"context"
"stackChan/internal/dao"
"stackChan/internal/model"
"stackChan/internal/model/do"
"stackChan/api/post/v1"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
)
func (c *ControllerV1) CreatePostComment(ctx context.Context, req *v1.CreatePostCommentReq) (res *v1.CreatePostCommentRes, err error) {
mac := g.RequestFromCtx(ctx).GetCtxVar(model.Mac).String()
if mac == "" {
return nil, gerror.NewCode(gcode.CodeInvalidParameter)
}
id, err := dao.DevicePostComment.Ctx(ctx).Data(do.DevicePostComment{
PostId: req.PostId,
Mac: req.Mac,
Mac: mac,
Content: req.Content,
}).InsertAndGetId()
if err != nil {
@@ -12,11 +12,19 @@ import (
"stackChan/internal/model"
"stackChan/api/post/v1"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
)
func (c *ControllerV1) DeletePostComment(ctx context.Context, req *v1.DeletePostCommentReq) (res *v1.DeletePostCommentRes, err error) {
mac := g.RequestFromCtx(ctx).GetCtxVar(model.Mac).String()
if mac == "" {
return nil, gerror.NewCode(gcode.CodeInvalidParameter)
}
var postComment model.PostComment
err = dao.DevicePostComment.Ctx(ctx).Where("id=?", req.Id).Scan(&postComment)
err = dao.DevicePostComment.Ctx(ctx).Where("id=? AND post_id=? AND mac=?", req.CommentId, req.PostId, mac).Scan(&postComment)
if err != nil {
return nil, err
@@ -26,13 +34,13 @@ func (c *ControllerV1) DeletePostComment(ctx context.Context, req *v1.DeletePost
return nil, errors.New("post not found")
}
if postComment.Mac != req.Mac {
if postComment.Mac != mac {
return nil, errors.New("no authority to delete")
}
_, err = dao.DevicePostComment.
Ctx(ctx).
Where("id = ? AND mac = ?", req.Id, req.Mac).
Where("id=? AND post_id=?", req.CommentId, req.PostId).
Delete()
if err != nil {
return nil, err
@@ -26,7 +26,7 @@ func (c *ControllerV1) GetPostComment(ctx context.Context, req *v1.GetPostCommen
var list []*model.PostComment
db := dao.DevicePostComment.Ctx(ctx).As("dp").Where("mac = ? AND post_id = ?", req.Mac, req.PostId)
db := dao.DevicePostComment.Ctx(ctx).As("dp").Where("post_id = ?", req.PostId)
total, err := db.Count()
if err != nil {
@@ -0,0 +1,10 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package stackchandevice
@@ -0,0 +1,20 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package stackchandevice
import (
"stackChan/api/stackchandevice"
)
type ControllerV2 struct{}
func NewV2() stackchandevice.IStackchandeviceV2 {
return &ControllerV2{}
}
@@ -0,0 +1,62 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package stackchandevice
import (
"context"
"stackChan/internal/dao"
"stackChan/internal/model"
"stackChan/internal/model/entity"
"stackChan/internal/service"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"stackChan/api/stackchandevice/v2"
)
// GetDeviceUserInfo Get user information corresponding to the device
func (c *ControllerV2) GetDeviceUserInfo(ctx context.Context, req *v2.GetDeviceUserInfoReq) (res *v2.GetDeviceUserInfoRes, err error) {
// 1. Get MAC address from context
mac := g.RequestFromCtx(ctx).GetCtxVar(model.Mac).String()
if mac == "" {
return nil, gerror.NewCodef(gcode.CodeInvalidParameter, "Device MAC address is empty")
}
// 2. Ensure MAC record exists
_, err = service.CreateMacIfNotExists(ctx, mac)
if err != nil {
return nil, gerror.WrapCode(gcode.CodeInternalError, err, "create mac record failed")
}
// 3. Query device information based on MAC
var device entity.Device
err = dao.Device.Ctx(ctx).Where("mac", mac).Scan(&device)
if err != nil {
return nil, gerror.WrapCodef(gcode.CodeInternalError, err, "Failed to query device information")
}
// 4. Device not bound to user -> return null
if device.Uid == 0 {
return new(v2.GetDeviceUserInfoRes(nil)), nil
}
// 5. Query user information based on UID
var user model.User
err = dao.User.Ctx(ctx).Where("uid", device.Uid).Scan(&user)
if err != nil {
return nil, gerror.WrapCode(gcode.CodeInternalError, err, "query user info failed")
}
// 6. User does not exist -> return null
if user.Uid == 0 {
return new(v2.GetDeviceUserInfoRes(nil)), nil
}
// 7. Return username normally
return new(v2.GetDeviceUserInfoRes(&user)), nil
}
@@ -0,0 +1,60 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package stackchandevice
import (
"context"
"stackChan/internal/dao"
"stackChan/internal/model"
"stackChan/internal/service"
"stackChan/internal/xiaozhi"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"stackChan/api/stackchandevice/v2"
)
// UnbindDevice Unbind device from StackChan side
func (c *ControllerV2) UnbindDevice(ctx context.Context, req *v2.UnbindDeviceReq) (res *v2.UnbindDeviceRes, err error) {
mac := g.RequestFromCtx(ctx).GetCtxVar(model.Mac).String()
if mac == "" {
return nil, gerror.NewCode(gcode.CodeInvalidParameter)
}
_, err = service.CreateMacIfNotExists(ctx, mac)
if err != nil {
return nil, gerror.NewCode(gcode.CodeInternalError)
}
restoreResponse, err := service.RestoreDefaultAgent(mac)
if err != nil {
return nil, err
}
if !restoreResponse {
return nil, gerror.NewCode(gcode.CodeInternalError, "restore default agent failed")
}
/// xiaozhi Unbind Device
unbindResponse, err := xiaozhi.UnbindDevice(&mac)
if err != nil {
return nil, gerror.NewCode(gcode.CodeInternalError)
}
if !unbindResponse {
g.Log().Error(ctx, "xiaozhi Unbind Device failed")
return nil, gerror.NewCode(gcode.CodeInternalError)
}
/// update device table
_, err = dao.Device.Ctx(ctx).
Where("mac", mac).
Data("uid", nil, "bind_time", nil).
Update()
if err != nil {
return nil, gerror.NewCodef(gcode.CodeInternalError, "device unbind failed: %v", err)
}
return new(v2.UnbindDeviceRes(true)), nil
}
+10
View File
@@ -0,0 +1,10 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package user
@@ -0,0 +1,20 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package user
import (
"stackChan/api/user"
)
type ControllerV2 struct{}
func NewV2() user.IUserV2 {
return &ControllerV2{}
}
@@ -0,0 +1,34 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package user
import (
"context"
"stackChan/internal/dao"
"stackChan/internal/model"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"stackChan/api/user/v2"
)
func (c *ControllerV2) GetUserInfo(ctx context.Context, req *v2.GetUserInfoReq) (res *v2.GetUserInfoRes, err error) {
uid := g.RequestFromCtx(ctx).GetCtxVar(model.Uid).Int64()
if uid == 0 {
return nil, gerror.NewCode(gcode.CodeMissingParameter, "user UID is required")
}
var userInfo model.User
err = dao.User.Ctx(ctx).Where("uid=?", uid).Scan(&userInfo)
if err != nil {
return nil, gerror.WrapCode(gcode.CodeDbOperationError, err, "failed to query user information")
}
if userInfo.Uid == 0 {
return nil, gerror.NewCode(gcode.CodeNotFound, "user does not exist")
}
return new(v2.GetUserInfoRes(userInfo)), nil
}
@@ -0,0 +1,17 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package user
import (
"context"
"stackChan/internal/service"
"stackChan/api/user/v2"
)
func (c *ControllerV2) Login(ctx context.Context, req *v2.LoginReq) (res *v2.LoginRes, err error) {
return service.Login(ctx, req)
}
@@ -0,0 +1,17 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package user
import (
"context"
"stackChan/internal/service"
"stackChan/api/user/v2"
)
func (c *ControllerV2) Registration(ctx context.Context, req *v2.RegistrationReq) (res *v2.RegistrationRes, err error) {
return service.Registration(ctx, req)
}
@@ -0,0 +1,10 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package xiaozhi
@@ -0,0 +1,20 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package xiaozhi
import (
"stackChan/api/xiaozhi"
)
type ControllerV1 struct{}
func NewV1() xiaozhi.IXiaozhiV1 {
return &ControllerV1{}
}
@@ -0,0 +1,19 @@
package xiaozhi
import (
"context"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"stackChan/api/xiaozhi/v1"
)
func (c *ControllerV1) GetXiaoZhiGenerateLicenseToken(ctx context.Context, req *v1.GetXiaoZhiGenerateLicenseTokenReq) (res *v1.GetXiaoZhiGenerateLicenseTokenRes, err error) {
generateLicenseToken := g.Cfg().MustGet(ctx, "xiaozhi.generate_license_token").String()
if generateLicenseToken == "" {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "generate_license_token is empty")
}
return new(v1.GetXiaoZhiGenerateLicenseTokenRes(generateLicenseToken)), nil
}
@@ -0,0 +1,21 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package xiaozhi
import (
"context"
"stackChan/internal/xiaozhi"
"stackChan/api/xiaozhi/v1"
)
func (c *ControllerV1) GetXiaoZhiToken(ctx context.Context, req *v1.GetXiaoZhiTokenReq) (res *v1.GetXiaoZhiTokenRes, err error) {
token, err := xiaozhi.GetToken()
if err != nil {
return nil, err
}
return new(v1.GetXiaoZhiTokenRes(token)), nil
}
@@ -0,0 +1,21 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package xiaozhi
import (
"context"
"stackChan/internal/xiaozhi"
"stackChan/api/xiaozhi/v1"
)
func (c *ControllerV1) RefreshToken(ctx context.Context, req *v1.RefreshTokenReq) (res *v1.RefreshTokenRes, err error) {
token, err := xiaozhi.GetNewToken()
if err != nil {
return nil, err
}
return new(v1.RefreshTokenRes(token)), nil
}
+27
View File
@@ -0,0 +1,27 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
// =================================================================================
// This file is auto-generated by the GoFrame CLI tool. You may modify it as needed.
// =================================================================================
package dao
import (
"stackChan/internal/dao/internal"
)
// appStoreDao is the data access object for the table app_store.
// You can define custom methods on it to extend its functionality as needed.
type appStoreDao struct {
*internal.AppStoreDao
}
var (
// AppStore is a globally accessible object for table app_store operations.
AppStore = appStoreDao{internal.NewAppStoreDao()}
)
// Add your custom methods and functionality below.
+27
View File
@@ -0,0 +1,27 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
// =================================================================================
// This file is auto-generated by the GoFrame CLI tool. You may modify it as needed.
// =================================================================================
package dao
import (
"stackChan/internal/dao/internal"
)
// devicePanoDao is the data access object for the table device_pano.
// You can define custom methods on it to extend its functionality as needed.
type devicePanoDao struct {
*internal.DevicePanoDao
}
var (
// DevicePano is a globally accessible object for table device_pano operations.
DevicePano = devicePanoDao{internal.NewDevicePanoDao()}
)
// Add your custom methods and functionality below.
+98
View File
@@ -0,0 +1,98 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
// ==========================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// ==========================================================================
package internal
import (
"context"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
)
// AppStoreDao is the data access object for the table app_store.
type AppStoreDao struct {
table string // table is the underlying table name of the DAO.
group string // group is the database configuration group name of the current DAO.
columns AppStoreColumns // columns contains all the column names of Table for convenient usage.
handlers []gdb.ModelHandler // handlers for customized model modification.
}
// AppStoreColumns defines and stores column names for the table app_store.
type AppStoreColumns struct {
Id string //
AppName string // App name
AppIconUrl string // App icon URL
Description string // App description
FirmwareUrl string // Firmware / installation package download URL
CreateAt string // Creation time
UpdateAt string // Update time
IsDeleted string // Is deleted, 0 normal 1 deleted
}
// appStoreColumns holds the columns for the table app_store.
var appStoreColumns = AppStoreColumns{
Id: "id",
AppName: "app_name",
AppIconUrl: "app_icon_url",
Description: "description",
FirmwareUrl: "firmware_url",
CreateAt: "create_at",
UpdateAt: "update_at",
IsDeleted: "is_deleted",
}
// NewAppStoreDao creates and returns a new DAO object for table data access.
func NewAppStoreDao(handlers ...gdb.ModelHandler) *AppStoreDao {
return &AppStoreDao{
group: "default",
table: "app_store",
columns: appStoreColumns,
handlers: handlers,
}
}
// DB retrieves and returns the underlying raw database management object of the current DAO.
func (dao *AppStoreDao) DB() gdb.DB {
return g.DB(dao.group)
}
// Table returns the table name of the current DAO.
func (dao *AppStoreDao) Table() string {
return dao.table
}
// Columns returns all column names of the current DAO.
func (dao *AppStoreDao) Columns() AppStoreColumns {
return dao.columns
}
// Group returns the database configuration group name of the current DAO.
func (dao *AppStoreDao) Group() string {
return dao.group
}
// Ctx creates and returns a Model for the current DAO. It automatically sets the context for the current operation.
func (dao *AppStoreDao) Ctx(ctx context.Context) *gdb.Model {
model := dao.DB().Model(dao.table)
for _, handler := range dao.handlers {
model = handler(model)
}
return model.Safe().Ctx(ctx)
}
// Transaction wraps the transaction logic using function f.
// It rolls back the transaction and returns the error if function f returns a non-nil error.
// It commits the transaction and returns nil if function f returns nil.
//
// Note: Do not commit or roll back the transaction in function f,
// as it is automatically handled by this function.
func (dao *AppStoreDao) Transaction(ctx context.Context, f func(ctx context.Context, tx gdb.TX) error) (err error) {
return dao.Ctx(ctx).Transaction(ctx, f)
}
+13 -4
View File
@@ -1,3 +1,8 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
// ==========================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// ==========================================================================
@@ -21,14 +26,18 @@ type DeviceDao struct {
// DeviceColumns defines and stores column names for the table device.
type DeviceColumns struct {
Mac string //
Name string //
Mac string //
Name string //
Uid string // Bound user UID
BindTime string // Device binding time
}
// deviceColumns holds the columns for the table device.
var deviceColumns = DeviceColumns{
Mac: "mac",
Name: "name",
Mac: "mac",
Name: "name",
Uid: "uid",
BindTime: "bind_time",
}
// NewDeviceDao creates and returns a new DAO object for table data access.
+19 -12
View File
@@ -1,3 +1,8 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
// ==========================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// ==========================================================================
@@ -21,22 +26,24 @@ type DeviceDanceDao struct {
// DeviceDanceColumns defines and stores column names for the table device_dance.
type DeviceDanceColumns struct {
Id string //
Mac string // 设备MAC地址
DanceIndex string // 舞蹈编号,初始为1~3,可扩展
DanceData string // MotionData
CreatedAt string //
UpdatedAt string //
Id string //
Mac string // Device MAC address
DanceName string // Dance name
DanceData string // MotionData
MusicUrl string // Dance background music URL
CreatedAt string //
UpdatedAt string //
}
// deviceDanceColumns holds the columns for the table device_dance.
var deviceDanceColumns = DeviceDanceColumns{
Id: "id",
Mac: "mac",
DanceIndex: "dance_index",
DanceData: "dance_data",
CreatedAt: "created_at",
UpdatedAt: "updated_at",
Id: "id",
Mac: "mac",
DanceName: "dance_name",
DanceData: "dance_data",
MusicUrl: "music_url",
CreatedAt: "created_at",
UpdatedAt: "updated_at",
}
// NewDeviceDanceDao creates and returns a new DAO object for table data access.
@@ -1,3 +1,8 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
// ==========================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// ==========================================================================
@@ -0,0 +1,92 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
// ==========================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// ==========================================================================
package internal
import (
"context"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
)
// DevicePanoDao is the data access object for the table device_pano.
type DevicePanoDao struct {
table string // table is the underlying table name of the DAO.
group string // group is the database configuration group name of the current DAO.
columns DevicePanoColumns // columns contains all the column names of Table for convenient usage.
handlers []gdb.ModelHandler // handlers for customized model modification.
}
// DevicePanoColumns defines and stores column names for the table device_pano.
type DevicePanoColumns struct {
Id string //
Mac string // Device MAC address
PanoUrl string // Panorama URL
CreatedAt string // Creation time
UpdatedAt string //
}
// devicePanoColumns holds the columns for the table device_pano.
var devicePanoColumns = DevicePanoColumns{
Id: "id",
Mac: "mac",
PanoUrl: "pano_url",
CreatedAt: "created_at",
UpdatedAt: "updated_at",
}
// NewDevicePanoDao creates and returns a new DAO object for table data access.
func NewDevicePanoDao(handlers ...gdb.ModelHandler) *DevicePanoDao {
return &DevicePanoDao{
group: "default",
table: "device_pano",
columns: devicePanoColumns,
handlers: handlers,
}
}
// DB retrieves and returns the underlying raw database management object of the current DAO.
func (dao *DevicePanoDao) DB() gdb.DB {
return g.DB(dao.group)
}
// Table returns the table name of the current DAO.
func (dao *DevicePanoDao) Table() string {
return dao.table
}
// Columns returns all column names of the current DAO.
func (dao *DevicePanoDao) Columns() DevicePanoColumns {
return dao.columns
}
// Group returns the database configuration group name of the current DAO.
func (dao *DevicePanoDao) Group() string {
return dao.group
}
// Ctx creates and returns a Model for the current DAO. It automatically sets the context for the current operation.
func (dao *DevicePanoDao) Ctx(ctx context.Context) *gdb.Model {
model := dao.DB().Model(dao.table)
for _, handler := range dao.handlers {
model = handler(model)
}
return model.Safe().Ctx(ctx)
}
// Transaction wraps the transaction logic using function f.
// It rolls back the transaction and returns the error if function f returns a non-nil error.
// It commits the transaction and returns nil if function f returns nil.
//
// Note: Do not commit or roll back the transaction in function f,
// as it is automatically handled by this function.
func (dao *DevicePanoDao) Transaction(ctx context.Context, f func(ctx context.Context, tx gdb.TX) error) (err error) {
return dao.Ctx(ctx).Transaction(ctx, f)
}
+9 -4
View File
@@ -1,3 +1,8 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
// ==========================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// ==========================================================================
@@ -22,10 +27,10 @@ type DevicePostDao struct {
// DevicePostColumns defines and stores column names for the table device_post.
type DevicePostColumns struct {
Id string //
Mac string // 发帖设备MAC
ContentText string // 文本内容
ContentImage string // 图片URL
CreatedAt string // 发帖时间
Mac string // Post device MAC
ContentText string //
ContentImage string // Image URL
CreatedAt string // Post time
}
// devicePostColumns holds the columns for the table device_post.
@@ -1,3 +1,8 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
// ==========================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// ==========================================================================
@@ -22,10 +27,10 @@ type DevicePostCommentDao struct {
// DevicePostCommentColumns defines and stores column names for the table device_post_comment.
type DevicePostCommentColumns struct {
Id string //
PostId string // 帖子ID
Mac string // 评论设备MAC
Content string // 评论内容
CreatedAt string // 评论时间
PostId string // Post ID
Mac string // Comment device MAC
Content string //
CreatedAt string // Comment time
}
// devicePostCommentColumns holds the columns for the table device_post_comment.
@@ -1,3 +1,8 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
// ==========================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// ==========================================================================
+108
View File
@@ -0,0 +1,108 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
// ==========================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// ==========================================================================
package internal
import (
"context"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
)
// UserDao is the data access object for the table user.
type UserDao struct {
table string // table is the underlying table name of the DAO.
group string // group is the database configuration group name of the current DAO.
columns UserColumns // columns contains all the column names of Table for convenient usage.
handlers []gdb.ModelHandler // handlers for customized model modification.
}
// UserColumns defines and stores column names for the table user.
type UserColumns struct {
Uid string // User unique UID (remote platform primary key)
Username string // Login username
Userslug string // User alias
DisplayName string // User display name
IconText string // User icon text
IconBgColor string // Icon background color
EmailConfirmed string // Email verified, 0-no 1-yes
JoinDate string // Registration timestamp (milliseconds)
LastOnline string // Last online timestamp (milliseconds)
UserStatus string // User online status
CreateAt string // Local creation time
UpdateAt string // Local update time
IsDeleted string // Is deleted, 0-normal 1-deleted
}
// userColumns holds the columns for the table user.
var userColumns = UserColumns{
Uid: "uid",
Username: "username",
Userslug: "userslug",
DisplayName: "display_name",
IconText: "icon_text",
IconBgColor: "icon_bg_color",
EmailConfirmed: "email_confirmed",
JoinDate: "join_date",
LastOnline: "last_online",
UserStatus: "user_status",
CreateAt: "create_at",
UpdateAt: "update_at",
IsDeleted: "is_deleted",
}
// NewUserDao creates and returns a new DAO object for table data access.
func NewUserDao(handlers ...gdb.ModelHandler) *UserDao {
return &UserDao{
group: "default",
table: "user",
columns: userColumns,
handlers: handlers,
}
}
// DB retrieves and returns the underlying raw database management object of the current DAO.
func (dao *UserDao) DB() gdb.DB {
return g.DB(dao.group)
}
// Table returns the table name of the current DAO.
func (dao *UserDao) Table() string {
return dao.table
}
// Columns returns all column names of the current DAO.
func (dao *UserDao) Columns() UserColumns {
return dao.columns
}
// Group returns the database configuration group name of the current DAO.
func (dao *UserDao) Group() string {
return dao.group
}
// Ctx creates and returns a Model for the current DAO. It automatically sets the context for the current operation.
func (dao *UserDao) Ctx(ctx context.Context) *gdb.Model {
model := dao.DB().Model(dao.table)
for _, handler := range dao.handlers {
model = handler(model)
}
return model.Safe().Ctx(ctx)
}
// Transaction wraps the transaction logic using function f.
// It rolls back the transaction and returns the error if function f returns a non-nil error.
// It commits the transaction and returns nil if function f returns nil.
//
// Note: Do not commit or roll back the transaction in function f,
// as it is automatically handled by this function.
func (dao *UserDao) Transaction(ctx context.Context, f func(ctx context.Context, tx gdb.TX) error) (err error) {
return dao.Ctx(ctx).Transaction(ctx, f)
}
+27
View File
@@ -0,0 +1,27 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
// =================================================================================
// This file is auto-generated by the GoFrame CLI tool. You may modify it as needed.
// =================================================================================
package dao
import (
"stackChan/internal/dao/internal"
)
// userDao is the data access object for the table user.
// You can define custom methods on it to extend its functionality as needed.
type userDao struct {
*internal.UserDao
}
var (
// User is a globally accessible object for table user operations.
User = userDao{internal.NewUserDao()}
)
// Add your custom methods and functionality below.
+113
View File
@@ -0,0 +1,113 @@
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package middleware
import (
"stackChan/internal/model"
"stackChan/internal/service"
"stackChan/internal/web_socket"
"strings"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/golang-jwt/jwt/v5"
)
// TokenAuthMiddleware token
func TokenAuthMiddleware(r *ghttp.Request) {
mac, err := web_socket.GetMac(r)
if err != nil {
r.Middleware.Next()
return
}
if mac != "" {
r.SetCtxVar(model.Mac, mac)
}
r.Middleware.Next()
}
func V2TokenAuthMiddleware(r *ghttp.Request) {
if strings.HasPrefix(r.URL.Path, "/stackChan/v2/user/login") || strings.HasPrefix(r.URL.Path, "/stackChan/v2/user/registration") {
r.Middleware.Next()
return
}
tokenString := r.Header.Get("token")
if tokenString == "" {
r.Response.WriteJsonExit(gerror.NewCode(gcode.CodeNotAuthorized, "The token cannot be empty."))
}
tokenString = strings.TrimPrefix(tokenString, "Bearer ")
tokenString = strings.TrimSpace(tokenString)
if tokenString == "" {
r.Response.WriteJsonExit(gerror.NewCode(gcode.CodeNotAuthorized, "Invalid token format"))
}
jwtSecret := service.GetJwtSecret()
if jwtSecret == "" {
r.Response.WriteJsonExit(gerror.NewCode(gcode.CodeInternalError, "jwt The secret has not been configured."))
}
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, gerror.NewCodef(gcode.CodeNotAuthorized, "token signing algorithm error: %v, %v", token.Header["alg"])
}
return []byte(jwtSecret), nil
})
if err != nil || !token.Valid {
r.Response.WriteJsonExit(gerror.NewCode(gcode.CodeNotAuthorized, "The token is invalid or has expired."))
}
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
r.Response.WriteJsonExit(gerror.NewCode(gcode.CodeNotAuthorized, "Token payload format is incorrect"))
}
uid, ok := claims["id"].(float64)
if !ok {
r.Response.WriteJsonExit(gerror.NewCode(gcode.CodeNotAuthorized, "The user ID in the token is invalid."))
}
r.SetCtxVar(model.Uid, int64(uid))
r.Middleware.Next()
}
// CORS allow cross-origin
func CORS(r *ghttp.Request) {
r.Response.CORSDefault()
r.Middleware.Next()
}
// AdminTokenAuthMiddleware Admin token validation
func AdminTokenAuthMiddleware(r *ghttp.Request) {
if strings.HasPrefix(r.URL.Path, "/admin/stackChan/login") {
r.Middleware.Next()
return
}
tokenString := r.Header.Get("Authorization")
if tokenString == "" {
r.Response.WriteJsonExit(gerror.NewCode(gcode.CodeNotAuthorized, "Token missing"))
return
}
jwtSecret := service.GetJwtSecret()
if jwtSecret == "" {
r.Response.WriteJsonExit(gerror.NewCode(gcode.CodeInternalError, "JWT secret has not been configured."))
return
}
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
return []byte(jwtSecret), nil
})
if err != nil || !token.Valid {
r.Response.WriteJsonExit(gerror.NewCode(gcode.CodeNotAuthorized, "The token is invalid."))
return
}
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
if Username, ok := claims[model.Username].(string); ok {
if Username != "" {
r.SetCtxVar(model.Username, Username)
r.Middleware.Next()
return
}
}
}
r.Response.WriteJsonExit(gerror.NewCode(gcode.CodeNotAuthorized, "The username is missing in the token."))
}

Some files were not shown because too many files have changed in this diff Show More