## 主要功能 - ✅ 用户认证模块 (登录/注册/JWT) - ✅ 照片管理模块 (上传/查询/分页/搜索) - ✅ 分类管理模块 (创建/查询/分页) - ✅ 用户管理模块 (用户列表/分页查询) - ✅ 健康检查接口 ## 技术实现 - 基于 go-zero v1.8.0 标准架构 - Handler → Logic → Model 三层架构 - SQLite/PostgreSQL 数据库支持 - JWT 认证机制 - bcrypt 密码加密 - 统一响应格式 - 自定义模型方法 (分页/搜索) ## API 接口 - POST /api/v1/auth/login - 用户登录 - POST /api/v1/auth/register - 用户注册 - GET /api/v1/health - 健康检查 - GET /api/v1/photos - 照片列表 - POST /api/v1/photos - 上传照片 - GET /api/v1/categories - 分类列表 - POST /api/v1/categories - 创建分类 - GET /api/v1/users - 用户列表 ## 配置完成 - 开发环境配置 (SQLite) - 生产环境支持 (PostgreSQL) - JWT 认证配置 - 文件上传配置 - Makefile 构建脚本 服务已验证可正常构建和启动。
123 lines
3.7 KiB
Go
Executable File
123 lines
3.7 KiB
Go
Executable File
// Code generated by goctl. DO NOT EDIT.
|
|
// versions:
|
|
// goctl version: 1.8.4
|
|
|
|
package model
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/zeromicro/go-zero/core/stores/builder"
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
|
"github.com/zeromicro/go-zero/core/stringx"
|
|
)
|
|
|
|
var (
|
|
userFieldNames = builder.RawFieldNames(&User{})
|
|
userRows = strings.Join(userFieldNames, ",")
|
|
userRowsExpectAutoSet = strings.Join(stringx.Remove(userFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
|
userRowsWithPlaceHolder = strings.Join(stringx.Remove(userFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
|
)
|
|
|
|
type (
|
|
userModel interface {
|
|
Insert(ctx context.Context, data *User) (sql.Result, error)
|
|
FindOne(ctx context.Context, id int64) (*User, error)
|
|
FindOneByEmail(ctx context.Context, email string) (*User, error)
|
|
FindOneByUsername(ctx context.Context, username string) (*User, error)
|
|
Update(ctx context.Context, data *User) error
|
|
Delete(ctx context.Context, id int64) error
|
|
}
|
|
|
|
defaultUserModel struct {
|
|
conn sqlx.SqlConn
|
|
table string
|
|
}
|
|
|
|
User struct {
|
|
Id int64 `db:"id"`
|
|
Username string `db:"username"` // 用户名
|
|
Email string `db:"email"` // 邮箱
|
|
Password string `db:"password"` // 密码
|
|
Avatar string `db:"avatar"` // 头像
|
|
Status int64 `db:"status"` // 状态 1:正常 0:禁用
|
|
CreatedAt time.Time `db:"created_at"`
|
|
UpdatedAt time.Time `db:"updated_at"`
|
|
}
|
|
)
|
|
|
|
func newUserModel(conn sqlx.SqlConn) *defaultUserModel {
|
|
return &defaultUserModel{
|
|
conn: conn,
|
|
table: "`user`",
|
|
}
|
|
}
|
|
|
|
func (m *defaultUserModel) Delete(ctx context.Context, id int64) error {
|
|
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
|
|
_, err := m.conn.ExecCtx(ctx, query, id)
|
|
return err
|
|
}
|
|
|
|
func (m *defaultUserModel) FindOne(ctx context.Context, id int64) (*User, error) {
|
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", userRows, m.table)
|
|
var resp User
|
|
err := m.conn.QueryRowCtx(ctx, &resp, query, id)
|
|
switch err {
|
|
case nil:
|
|
return &resp, nil
|
|
case sqlx.ErrNotFound:
|
|
return nil, ErrNotFound
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
func (m *defaultUserModel) FindOneByEmail(ctx context.Context, email string) (*User, error) {
|
|
var resp User
|
|
query := fmt.Sprintf("select %s from %s where `email` = ? limit 1", userRows, m.table)
|
|
err := m.conn.QueryRowCtx(ctx, &resp, query, email)
|
|
switch err {
|
|
case nil:
|
|
return &resp, nil
|
|
case sqlx.ErrNotFound:
|
|
return nil, ErrNotFound
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
func (m *defaultUserModel) FindOneByUsername(ctx context.Context, username string) (*User, error) {
|
|
var resp User
|
|
query := fmt.Sprintf("select %s from %s where `username` = ? limit 1", userRows, m.table)
|
|
err := m.conn.QueryRowCtx(ctx, &resp, query, username)
|
|
switch err {
|
|
case nil:
|
|
return &resp, nil
|
|
case sqlx.ErrNotFound:
|
|
return nil, ErrNotFound
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
func (m *defaultUserModel) Insert(ctx context.Context, data *User) (sql.Result, error) {
|
|
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?)", m.table, userRowsExpectAutoSet)
|
|
ret, err := m.conn.ExecCtx(ctx, query, data.Username, data.Email, data.Password, data.Avatar, data.Status)
|
|
return ret, err
|
|
}
|
|
|
|
func (m *defaultUserModel) Update(ctx context.Context, newData *User) error {
|
|
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, userRowsWithPlaceHolder)
|
|
_, err := m.conn.ExecCtx(ctx, query, newData.Username, newData.Email, newData.Password, newData.Avatar, newData.Status, newData.Id)
|
|
return err
|
|
}
|
|
|
|
func (m *defaultUserModel) tableName() string {
|
|
return m.table
|
|
}
|