81 lines
2.3 KiB
Go
81 lines
2.3 KiB
Go
// Package web 是给浏览器用的页面处理器。
|
||||
|
|
//
|
|||
|
|
// 和 handler/api 分开是有意的:页面出错要渲染错误页、要 CSRF 防护,
|
|||
|
|
// 接口出错要返回 JSON、要认证。混在一起迟早写错。
|
|||
|
|
//
|
|||
|
|
// 改动本文件前必读 admin/AGENTS.md。两条最容易踩:
|
|||
|
|
// - 本层**不写业务逻辑、不拼 SQL**,只做取参数 → 调 service → 渲染;
|
|||
|
|
// - 删除、导入这类破坏性操作用 POST,**不得用 GET**,
|
|||
|
|
// 浏览器和插件会预取 GET 链接。
|
|||
|
|
package web
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"database/sql"
|
|||
|
|
"net/http"
|
|||
|
|
|
|||
|
|
"github.com/gin-gonic/gin"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// Handler 持有各页面共用的依赖。
|
|||
|
|
type Handler struct {
|
|||
|
|
db *sql.DB
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Register 把四个模块的页面路由挂上去。
|
|||
|
|
//
|
|||
|
|
// 四个模块的页面结构完全一致(顶部工具条 / 中间带勾选的表格 / 底部状态条),
|
|||
|
|
// 这是有意的,见 docs/admin/05-ui-specification.md §2。
|
|||
|
|
func Register(r *gin.Engine, db *sql.DB) {
|
|||
|
|
h := &Handler{db: db}
|
|||
|
|
|
|||
|
|
// 打开根路径直接进第一个模块
|
|||
|
|
r.GET("/", func(c *gin.Context) {
|
|||
|
|
c.Redirect(http.StatusFound, "/shopee")
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
// 1. 蝦皮数据
|
|||
|
|
r.GET("/shopee", h.ShopeeList)
|
|||
|
|
r.POST("/shopee/import", h.ShopeeImport)
|
|||
|
|
r.POST("/shopee/save", h.ShopeeSave)
|
|||
|
|
r.POST("/shopee/delete", h.ShopeeDelete)
|
|||
|
|
r.POST("/shopee/collect", h.ShopeeCollect)
|
|||
|
|
|
|||
|
|
// 2. 顺运宝数据
|
|||
|
|
r.GET("/syb", h.SybList)
|
|||
|
|
r.POST("/syb/sync", h.SybSync)
|
|||
|
|
r.POST("/syb/match", h.SybMatch)
|
|||
|
|
r.POST("/syb/create-task", h.SybCreateTask)
|
|||
|
|
r.POST("/syb/delete", h.SybDelete)
|
|||
|
|
|
|||
|
|
// 3. 采购任务
|
|||
|
|
r.GET("/tasks", h.TaskList)
|
|||
|
|
r.POST("/tasks/delete", h.TaskDelete)
|
|||
|
|
|
|||
|
|
// 4. 客户端列表
|
|||
|
|
r.GET("/clients", h.ClientList)
|
|||
|
|
r.POST("/clients/delete", h.ClientDelete)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// page 组装每个页面都要的公共数据(导航高亮、标题)。
|
|||
|
|
func page(active, title string, extra gin.H) gin.H {
|
|||
|
|
data := gin.H{
|
|||
|
|
"Active": active,
|
|||
|
|
"Title": title,
|
|||
|
|
}
|
|||
|
|
for k, v := range extra {
|
|||
|
|
data[k] = v
|
|||
|
|
}
|
|||
|
|
return data
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// fail 渲染一个错误页。
|
|||
|
|
//
|
|||
|
|
// 错误信息要说清三件事:发生了什么、保住了什么、下一步做什么。
|
|||
|
|
// Go 的错误堆栈只写日志,不要贴到页面上,见 docs/admin/06-quality-security.md §5。
|
|||
|
|
func fail(c *gin.Context, status int, message string) {
|
|||
|
|
c.HTML(status, "partials/error", gin.H{
|
|||
|
|
"Title": "出错了",
|
|||
|
|
"Message": message,
|
|||
|
|
})
|
|||
|
|
}
|