init: 毛孩子计划 小程序 + Go 后端 + 内嵌后台

- pets-fe: 微信原生小程序(首页/计划/记录/报告/社区/引导),
  服务端驱动、无假数据;弹层改用 scroll-view,打开时隐藏自定义 tabBar
- pets-be: Gin + GORM(MySQL, sundynix_ 前缀) + MinIO,统一响应/分页,
  微信 code2session 登录,provider-neutral AI(DeepSeek),go:embed React 后台
- 修复:分段选择类型不匹配(字符串 vs 数字)导致选不中

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Blizzard
2026-07-03 15:33:31 +08:00
commit 609f7d06cf
180 changed files with 15259 additions and 0 deletions
+34
View File
@@ -0,0 +1,34 @@
package errcode
// 业务错误码。0 成功,其余为业务/系统错误。
const (
Success = 0
ErrParams = 40000 // 参数错误
ErrUnauthized = 40100 // 未认证 / token 失效
ErrForbidden = 40300 // 无权限
ErrNotFound = 40400 // 资源不存在
ErrConflict = 40900 // 冲突(如重复)
ErrInternal = 50000 // 服务器内部错误
)
// Message 错误码默认文案
func Message(code int) string {
switch code {
case Success:
return "ok"
case ErrParams:
return "参数错误"
case ErrUnauthized:
return "未登录或登录已失效"
case ErrForbidden:
return "无权限"
case ErrNotFound:
return "资源不存在"
case ErrConflict:
return "资源冲突"
case ErrInternal:
return "服务器内部错误"
default:
return "未知错误"
}
}
+74
View File
@@ -0,0 +1,74 @@
package jwt
import (
"errors"
"time"
"github.com/golang-jwt/jwt/v5"
)
// Kind 区分令牌主体:小程序用户 / 后台管理员
type Kind string
const (
KindUser Kind = "user"
KindAdmin Kind = "admin"
)
// Claims 自定义声明,user 与 admin 复用同一结构,用 Kind 区分
type Claims struct {
ID uint `json:"id"`
Kind Kind `json:"kind"`
Name string `json:"name"`
Role string `json:"role,omitempty"`
jwt.RegisteredClaims
}
// Manager 签发/校验令牌
type Manager struct {
secret []byte
expireHours int
}
func NewManager(secret string, expireHours int) *Manager {
if expireHours <= 0 {
expireHours = 168
}
return &Manager{secret: []byte(secret), expireHours: expireHours}
}
// Generate 签发令牌
func (m *Manager) Generate(id uint, kind Kind, name, role string) (string, error) {
now := time.Now()
claims := Claims{
ID: id,
Kind: kind,
Name: name,
Role: role,
RegisteredClaims: jwt.RegisteredClaims{
IssuedAt: jwt.NewNumericDate(now),
ExpiresAt: jwt.NewNumericDate(now.Add(time.Duration(m.expireHours) * time.Hour)),
Issuer: "pets-be",
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString(m.secret)
}
// Parse 校验并解析令牌
func (m *Manager) Parse(tokenStr string) (*Claims, error) {
token, err := jwt.ParseWithClaims(tokenStr, &Claims{}, func(t *jwt.Token) (any, error) {
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, errors.New("unexpected signing method")
}
return m.secret, nil
})
if err != nil {
return nil, err
}
claims, ok := token.Claims.(*Claims)
if !ok || !token.Valid {
return nil, errors.New("invalid token")
}
return claims, nil
}
+70
View File
@@ -0,0 +1,70 @@
package response
// PageQuery 公共分页请求参数(列表接口统一内嵌复用)
// 用法:type XxxListReq struct { response.PageQuery; Keyword string `form:"keyword"` }
type PageQuery struct {
Page int `form:"page" json:"page"`
PageSize int `form:"page_size" json:"page_size"`
}
const (
defaultPage = 1
defaultPageSize = 10
maxPageSize = 100
)
// Normalize 修正非法/越界的分页参数,返回可安全使用的值
func (p *PageQuery) Normalize() {
if p.Page <= 0 {
p.Page = defaultPage
}
if p.PageSize <= 0 {
p.PageSize = defaultPageSize
}
if p.PageSize > maxPageSize {
p.PageSize = maxPageSize
}
}
// Offset 供 GORM .Offset() 使用
func (p PageQuery) Offset() int {
page, size := p.Page, p.PageSize
if page <= 0 {
page = defaultPage
}
if size <= 0 {
size = defaultPageSize
}
return (page - 1) * size
}
// Limit 供 GORM .Limit() 使用
func (p PageQuery) Limit() int {
if p.PageSize <= 0 {
return defaultPageSize
}
if p.PageSize > maxPageSize {
return maxPageSize
}
return p.PageSize
}
// PageResult 公共分页响应结构
type PageResult struct {
List any `json:"list"`
Total int64 `json:"total"`
Page int `json:"page"`
PageSize int `json:"page_size"`
}
// NewPage 组装分页响应;list 传空时序列化为 []
func NewPage(list any, total int64, p PageQuery) PageResult {
page, size := p.Page, p.PageSize
if page <= 0 {
page = defaultPage
}
if size <= 0 {
size = defaultPageSize
}
return PageResult{List: list, Total: total, Page: page, PageSize: size}
}
+60
View File
@@ -0,0 +1,60 @@
package response
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/sundynix/pets-be/pkg/errcode"
)
// Body 统一响应体:{ code, message, data }
type Body struct {
Code int `json:"code"`
Message string `json:"message"`
Data any `json:"data"`
}
// OK 成功响应(data 可为任意结构,含分页 PageResult)
func OK(c *gin.Context, data any) {
c.JSON(http.StatusOK, Body{
Code: errcode.Success,
Message: errcode.Message(errcode.Success),
Data: data,
})
}
// OKMsg 带自定义文案的成功响应
func OKMsg(c *gin.Context, message string, data any) {
c.JSON(http.StatusOK, Body{Code: errcode.Success, Message: message, Data: data})
}
// Fail 业务失败:HTTP 恒为 200,用 code 区分(前端统一按 code 判断)
func Fail(c *gin.Context, code int, message string) {
if message == "" {
message = errcode.Message(code)
}
c.JSON(http.StatusOK, Body{Code: code, Message: message, Data: nil})
}
// FailParams 参数错误快捷方法
func FailParams(c *gin.Context, message string) {
Fail(c, errcode.ErrParams, message)
}
// FailErr 内部错误快捷方法(携带 error 文案)
func FailErr(c *gin.Context, err error) {
msg := errcode.Message(errcode.ErrInternal)
if err != nil {
msg = err.Error()
}
Fail(c, errcode.ErrInternal, msg)
}
// Abort 在中间件中失败并中断(如鉴权失败)
func Abort(c *gin.Context, code int, message string) {
if message == "" {
message = errcode.Message(code)
}
c.AbortWithStatusJSON(http.StatusOK, Body{Code: code, Message: message, Data: nil})
}