feat(suffix): 支持根目录重命名、列宽保存、锚定值与文件发布
Some checks failed
Check / delivery-static (push) Has been cancelled
Check / api (push) Has been cancelled
Check / web (push) Has been cancelled

This commit is contained in:
lianjianwei2024
2026-10-31 09:17:28 +08:00
parent ac4b4a1c06
commit 5fdac9902d
18 changed files with 824 additions and 314 deletions

View File

@@ -175,8 +175,10 @@ func (o *Operations) Middleware() gin.HandlerFunc {
return
}
}
// Uploads have their own 64 MiB streaming bound; other JSON routes get a 64 KiB ceiling.
if !(path == "/api/v1/products/:id/releases" && c.Request.Method == "POST") {
// Uploads have their own streaming bounds (releases 64 MiB; suffix templates
// bound in their handler); other JSON routes get a 64 KiB ceiling.
if !(path == "/api/v1/products/:id/releases" && c.Request.Method == "POST") &&
!(path == "/api/v1/suffix-admin/suffixes/:id/files" && c.Request.Method == "POST") {
if c.Request.ContentLength > 64<<10 {
apiError(c, 413, "body_too_large", "Request body too large")
c.Abort()

View File

@@ -9,7 +9,7 @@ import (
"github.com/gin-gonic/gin"
)
const RequiredSchemaVersion = 17
const RequiredSchemaVersion = 21
func RegisterReadiness(r *gin.Engine, db *sql.DB) {
r.GET("/api/v1/health/ready", func(c *gin.Context) {

View File

@@ -7,6 +7,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"regexp"
@@ -42,6 +43,8 @@ func registerSuffixRoutes(secured *gin.RouterGroup, a *BusinessAPI) {
s.PATCH("/columns/:id", a.updateSuffixColumn)
s.DELETE("/columns/:id", a.deleteSuffixColumn)
s.PUT("/cells", a.saveSuffixCell)
s.GET("/col-widths", a.suffixColWidths)
s.PUT("/col-widths", a.saveSuffixColWidth)
s.GET("/overview", a.suffixOverview)
s.POST("/groups", a.createSuffixGroup)
s.PUT("/groups/:id", a.renameSuffixGroup)
@@ -54,6 +57,7 @@ func registerSuffixRoutes(secured *gin.RouterGroup, a *BusinessAPI) {
s.POST("/suffixes/:id/files", a.uploadSuffixFile)
s.DELETE("/files/:id", a.deleteSuffixFile)
s.GET("/files/:id/download", a.downloadSuffixFile)
s.POST("/files/:id/publish", a.publishSuffixFile)
}
func suffixTrimName(raw string, max int) (string, bool) {
@@ -61,8 +65,6 @@ func suffixTrimName(raw string, max int) (string, bool) {
return name, name != "" && utf8.RuneCountInString(name) <= max
}
const suffixRootNameValue = "按信息价的目录"
// ---- 目录树 ----
type suffixDirRow struct {
@@ -101,9 +103,6 @@ func (a *BusinessAPI) suffixTree(c *gin.Context) {
dirs[p].Children++
}
}
if len(dirs) > 0 {
dirs[0].Name = suffixRootNameValue
}
c.JSON(200, gin.H{"data": gin.H{"dirs": dirs}})
}
@@ -127,7 +126,7 @@ func (a *BusinessAPI) createSuffixDir(c *gin.Context) {
return
}
if in.ParentID == 0 {
apiError(c, 403, "forbidden", "只能建在「按信息价的目录」之内")
apiError(c, 403, "forbidden", "只能建在根目录之内")
return
}
if !a.dirExists(c, in.ParentID) {
@@ -158,10 +157,6 @@ func (a *BusinessAPI) dirIsRoot(c *gin.Context, id uint64) bool {
func (a *BusinessAPI) renameSuffixDir(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
if a.dirIsRoot(c, id) {
apiError(c, 403, "forbidden", "根目录名称固定,不能改名")
return
}
var in struct {
Name string `json:"name"`
}
@@ -250,15 +245,15 @@ func (a *BusinessAPI) suffixDirPage(c *gin.Context) {
colRows.Close()
type suffixLine struct {
ID uint64 `json:"id"`
GroupID uint64 `json:"group_id"`
GroupName string `json:"group_name"`
Name string `json:"name"`
Anchor bool `json:"anchor"`
ID uint64 `json:"id"`
GroupID uint64 `json:"group_id"`
GroupName string `json:"group_name"`
Name string `json:"name"`
AnchorValue string `json:"anchor_value"`
}
suffixes := []suffixLine{}
sfxRows, err := a.db.QueryContext(c,
"SELECT s.id,s.group_id,g.name,s.name, EXISTS(SELECT 1 FROM suffix_dir_anchors x WHERE x.dir_id=? AND x.suffix_id=s.id)"+
"SELECT s.id,s.group_id,g.name,s.name, COALESCE((SELECT x.value FROM suffix_dir_anchors x WHERE x.dir_id=? AND x.suffix_id=s.id),'')"+
" FROM suffixes s JOIN suffix_groups g ON g.id=s.group_id"+
" ORDER BY g.sort_order ASC,g.id ASC,s.sort_order ASC,s.id ASC", dirID)
if err != nil {
@@ -267,7 +262,7 @@ func (a *BusinessAPI) suffixDirPage(c *gin.Context) {
}
for sfxRows.Next() {
var r suffixLine
if err := sfxRows.Scan(&r.ID, &r.GroupID, &r.GroupName, &r.Name, &r.Anchor); err != nil {
if err := sfxRows.Scan(&r.ID, &r.GroupID, &r.GroupName, &r.Name, &r.AnchorValue); err != nil {
sfxRows.Close()
apiError(c, 500, "internal_error", "后缀加载失败")
return
@@ -422,30 +417,73 @@ func (a *BusinessAPI) reorderSuffixColumns(c *gin.Context) {
c.JSON(200, gin.H{"data": gin.H{"ok": true, "ordered": len(final)}})
}
// 列更新:name 与 width 至少传一个;重命名在同一目录内查重。
func (a *BusinessAPI) updateSuffixColumn(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
var in struct {
Width *int `json:"width"`
Name *string `json:"name"`
Width *int `json:"width"`
}
if c.ShouldBindJSON(&in) != nil || in.Width == nil {
apiError(c, 400, "invalid_request", "width 必填")
if c.ShouldBindJSON(&in) != nil || (in.Name == nil && in.Width == nil) {
apiError(c, 400, "invalid_request", "name 或 width 至少传一个")
return
}
w := *in.Width
if w < 0 || w > 2000 {
apiError(c, 400, "invalid_request", "列宽须为 0–2000(0=自适应)")
return
if in.Name != nil {
name, ok := suffixTrimName(*in.Name, 190)
if !ok {
apiError(c, 400, "invalid_request", "列名须为 1–190 字")
return
}
var dirID uint64
if err := a.db.QueryRowContext(c,
"SELECT dir_id FROM suffix_columns WHERE id=?", id).Scan(&dirID); err != nil {
apiError(c, 404, "not_found", "列不存在")
return
}
var dup int
if a.db.QueryRowContext(c,
"SELECT 1 FROM suffix_columns WHERE dir_id=? AND name=? AND id<>?", dirID, name, id).Scan(&dup) == nil {
apiError(c, 409, "duplicate_name", "这个目录里已有同名列")
return
}
if _, err := a.db.ExecContext(c, "UPDATE suffix_columns SET name=? WHERE id=?", name, id); err != nil {
apiError(c, 500, "internal_error", "列重命名失败")
return
}
}
res, err := a.db.ExecContext(c, "UPDATE suffix_columns SET width=? WHERE id=?", w, id)
if err != nil || rowsAffected(res) == 0 {
if in.Width != nil {
w := *in.Width
if w < 0 || w > 2000 {
apiError(c, 400, "invalid_request", "列宽须为 0–2000(0=自适应)")
return
}
if _, err := a.db.ExecContext(c, "UPDATE suffix_columns SET width=? WHERE id=?", w, id); err != nil {
apiError(c, 500, "internal_error", "列宽保存失败")
return
}
}
var out suffixColumnRow
if err := a.db.QueryRowContext(c,
"SELECT id,name,is_value,width FROM suffix_columns WHERE id=?", id).
Scan(&out.ID, &out.Name, &out.IsValue, &out.Width); err != nil {
apiError(c, 404, "not_found", "列不存在")
return
}
c.JSON(200, gin.H{"data": gin.H{"id": id, "width": w}})
c.JSON(200, gin.H{"data": out})
}
func (a *BusinessAPI) deleteSuffixColumn(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
var filled int
if err := a.db.QueryRowContext(c,
"SELECT COUNT(*) FROM suffix_cells WHERE column_id=? AND value<>''", id).Scan(&filled); err != nil {
apiError(c, 500, "internal_error", "删除失败")
return
}
if filled > 0 {
apiError(c, 409, "column_has_data", "该列已有数据,不能删除")
return
}
res, err := a.db.ExecContext(c, "DELETE FROM suffix_columns WHERE id=?", id)
if err != nil || rowsAffected(res) == 0 {
apiError(c, 404, "not_found", "列不存在")
@@ -494,20 +532,27 @@ func (a *BusinessAPI) saveSuffixCell(c *gin.Context) {
c.JSON(200, gin.H{"data": gin.H{"ok": true}})
}
// 锚定行存值:value 非空 upsert,空串删除(未锚定)。
func (a *BusinessAPI) setSuffixAnchor(c *gin.Context) {
dirID, _ := strconv.ParseUint(c.Param("id"), 10, 64)
var in struct {
SuffixID uint64 `json:"suffix_id"`
On bool `json:"on"`
Value string `json:"value"`
}
if c.ShouldBindJSON(&in) != nil {
apiError(c, 400, "invalid_request", "参数无效")
return
}
value := strings.TrimSpace(in.Value)
if utf8.RuneCountInString(value) > 190 {
apiError(c, 400, "invalid_request", "锚定值最多 190 字")
return
}
var exec error
if in.On {
if value != "" {
_, exec = a.db.ExecContext(c,
"INSERT IGNORE INTO suffix_dir_anchors(dir_id,suffix_id) VALUES(?,?)", dirID, in.SuffixID)
"INSERT INTO suffix_dir_anchors(dir_id,suffix_id,value) VALUES(?,?,?)"+
" ON DUPLICATE KEY UPDATE value=VALUES(value)", dirID, in.SuffixID, value)
} else {
_, exec = a.db.ExecContext(c,
"DELETE FROM suffix_dir_anchors WHERE dir_id=? AND suffix_id=?", dirID, in.SuffixID)
@@ -519,6 +564,79 @@ func (a *BusinessAPI) setSuffixAnchor(c *gin.Context) {
c.JSON(200, gin.H{"data": gin.H{"ok": true}})
}
// ---- 固定列列宽(序号/后缀/操作等,基本设置与目录工作区各一套,全部可自由设置) ----
// 默认值与前端 FIXED_COL_DEFAULTS 保持一致;0=自适应(导入文件为 0 时占满剩余宽度)。
var suffixColWidthDefaults = map[string]map[string]int{
"basic": {"idx": 50, "suffix": 50, "files": 0, "ops": 150},
"workspace": {"idx": 50, "suffix": 50, "ops": 80, "anchor": 300},
}
func (a *BusinessAPI) suffixColWidths(c *gin.Context) {
out := map[string]map[string]int{}
for scope, keys := range suffixColWidthDefaults {
out[scope] = map[string]int{}
for k, def := range keys {
out[scope][k] = def
}
}
rows, err := a.db.QueryContext(c, "SELECT scope,col_key,width FROM suffix_col_widths")
if err != nil {
apiError(c, 500, "internal_error", "列宽加载失败")
return
}
defer rows.Close()
for rows.Next() {
var scope, key string
var w int
if rows.Scan(&scope, &key, &w) == nil {
if keys, ok := out[scope]; ok {
if _, known := keys[key]; known && w >= 0 {
keys[key] = w
}
}
}
}
if err := rows.Err(); err != nil {
apiError(c, 500, "internal_error", "列宽加载失败")
return
}
c.JSON(200, gin.H{"data": gin.H{"widths": out}})
}
func (a *BusinessAPI) saveSuffixColWidth(c *gin.Context) {
var in struct {
Scope string `json:"scope"`
Key string `json:"key"`
Width *int `json:"width"`
}
if c.ShouldBindJSON(&in) != nil || in.Width == nil {
apiError(c, 400, "invalid_request", "参数无效")
return
}
keys, ok := suffixColWidthDefaults[in.Scope]
if !ok {
apiError(c, 400, "invalid_request", "scope 只能是 basic 或 workspace")
return
}
if _, known := keys[in.Key]; !known {
apiError(c, 400, "invalid_request", "未知的固定列 key")
return
}
w := *in.Width
if w < 0 || w > 2000 {
apiError(c, 400, "invalid_request", "列宽须为 0–2000(0=自适应)")
return
}
if _, err := a.db.ExecContext(c,
"INSERT INTO suffix_col_widths(scope,col_key,width) VALUES(?,?,?)"+
" ON DUPLICATE KEY UPDATE width=VALUES(width)", in.Scope, in.Key, w); err != nil {
apiError(c, 500, "internal_error", "列宽保存失败")
return
}
c.JSON(200, gin.H{"data": gin.H{"scope": in.Scope, "key": in.Key, "width": w}})
}
// ---- 基本设置:分组 + 后缀 + 模板文件 ----
type suffixFileRow struct {
@@ -526,6 +644,7 @@ type suffixFileRow struct {
FileName string `json:"file_name"`
FileSize int64 `json:"file_size"`
CreatedAt string `json:"created_at"`
Published bool `json:"published"`
}
func (a *BusinessAPI) suffixOverview(c *gin.Context) {
@@ -583,7 +702,7 @@ func (a *BusinessAPI) suffixOverview(c *gin.Context) {
return
}
fRows, err := a.db.QueryContext(c,
"SELECT id,suffix_id,file_name,file_size,created_at FROM suffix_files ORDER BY id ASC")
"SELECT id,suffix_id,file_name,file_size,created_at,published FROM suffix_files ORDER BY id ASC")
if err != nil {
apiError(c, 500, "internal_error", "文件加载失败")
return
@@ -592,7 +711,7 @@ func (a *BusinessAPI) suffixOverview(c *gin.Context) {
for fRows.Next() {
var f suffixFileRow
var sid uint64
if err := fRows.Scan(&f.ID, &sid, &f.FileName, &f.FileSize, &f.CreatedAt); err != nil {
if err := fRows.Scan(&f.ID, &sid, &f.FileName, &f.FileSize, &f.CreatedAt, &f.Published); err != nil {
apiError(c, 500, "internal_error", "文件加载失败")
return
}
@@ -904,12 +1023,13 @@ type suffixTemplateRootError struct{}
func (suffixTemplateRootError) Error() string { return "模板存储目录未配置" }
// 附件统一落盘在 PACKAGE_STORAGE_ROOT/attachments/<后缀id>/ 下(新软件,无旧目录兼容负担)。
func suffixTemplateRoot() (string, error) {
base := strings.TrimSpace(os.Getenv("PACKAGE_STORAGE_ROOT"))
if base == "" {
return "", suffixTemplateRootError{}
}
return filepath.Join(base, "suffix-templates"), nil
return filepath.Join(base, "attachments"), nil
}
func suffixUniqueName(dir string, name string) string {
@@ -928,6 +1048,8 @@ func suffixUniqueName(dir string, name string) string {
func (a *BusinessAPI) uploadSuffixFile(c *gin.Context) {
suffixID, _ := strconv.ParseUint(c.Param("id"), 10, 64)
// base64 上传体放宽到 128MB + 少量 JSON 包装开销(该路由已在全局 64KiB JSON 上限中豁免)。
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, suffixUploadMax+(64<<10))
var in struct {
FileName string `json:"file_name"`
Size int64 `json:"size"`
@@ -1009,7 +1131,7 @@ func (a *BusinessAPI) uploadSuffixFile(c *gin.Context) {
}})
}
// 只认模板根目录以内的路径,越界一律不删。
// 只认模板根目录以内的路径,越界一律不删;删完后顺手回收空的后缀目录。
func (a *BusinessAPI) removeDiskFiles(c *gin.Context, relPaths []string) {
root, err := suffixTemplateRoot()
if err != nil {
@@ -1019,11 +1141,34 @@ func (a *BusinessAPI) removeDiskFiles(c *gin.Context, relPaths []string) {
abs := filepath.Join(root, filepath.FromSlash(rel))
clean := filepath.Clean(abs)
if strings.HasPrefix(clean, filepath.Clean(root)+string(filepath.Separator)) {
_ = os.Remove(clean)
if os.Remove(clean) == nil {
_ = os.Remove(filepath.Dir(clean))
}
}
}
}
func (a *BusinessAPI) publishSuffixFile(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
var in struct {
On bool `json:"on"`
}
if c.ShouldBindJSON(&in) != nil {
apiError(c, 400, "invalid_request", "参数无效")
return
}
on := 0
if in.On {
on = 1
}
res, err := a.db.ExecContext(c, "UPDATE suffix_files SET published=? WHERE id=?", on, id)
if err != nil || rowsAffected(res) == 0 {
apiError(c, 404, "not_found", "文件不存在")
return
}
c.JSON(200, gin.H{"data": gin.H{"id": id, "published": in.On}})
}
func (a *BusinessAPI) deleteSuffixFile(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
var rel string

View File

@@ -11,6 +11,8 @@ import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
@@ -33,7 +35,7 @@ func TestSuffixAdminIntegration(t *testing.T) {
if err = db.Ping(); err != nil {
t.Fatal(err)
}
for _, table := range []string{"suffix_files", "suffix_dir_anchors", "suffix_cells", "suffix_columns", "suffixes", "suffix_groups", "suffix_dirs"} {
for _, table := range []string{"suffix_files", "suffix_dir_anchors", "suffix_cells", "suffix_columns", "suffixes", "suffix_groups", "suffix_dirs", "suffix_col_widths"} {
if _, err = db.Exec("DELETE FROM " + table); err != nil {
t.Fatalf("清理 %s: %v", table, err)
}
@@ -55,7 +57,8 @@ func TestSuffixAdminIntegration(t *testing.T) {
router := NewRouter()
RegisterBusiness(router, db)
t.Setenv("PACKAGE_STORAGE_ROOT", t.TempDir())
storageRoot := t.TempDir()
t.Setenv("PACKAGE_STORAGE_ROOT", storageRoot)
do := func(method, path, body string) *httptest.ResponseRecorder {
var reader *strings.Reader
if body == "" {
@@ -166,6 +169,16 @@ func TestSuffixAdminIntegration(t *testing.T) {
if w.Code != 403 {
t.Fatalf("删根 status=%d, want 403", w.Code)
}
// 根目录支持重命名
w = authed(http.MethodPut, fmt.Sprintf("/api/v1/suffix-admin/dirs/%d", rootID), `{"name":"建设项目"}`)
if w.Code != 200 {
t.Fatalf("改根名 status=%d body=%s", w.Code, w.Body.String())
}
w = authed(http.MethodGet, "/api/v1/suffix-admin/tree", "")
dirs = getJSON(w)["data"].(map[string]any)["dirs"].([]any)
if dirs[0].(map[string]any)["name"] != "建设项目" {
t.Fatalf("改根名后=%v", dirs[0].(map[string]any)["name"])
}
// 列(值设置勾选)+ 单元格 + 锚定
w = authed(http.MethodPost, fmt.Sprintf("/api/v1/suffix-admin/dirs/%d/columns", dirID), `{"name":"招标人","is_value":false}`)
@@ -235,9 +248,84 @@ func TestSuffixAdminIntegration(t *testing.T) {
t.Fatal("第二列应为值列")
}
// 锚定开与关
// 列重命名:成功、同目录重名 409、空名 400
w = authed(http.MethodPatch, fmt.Sprintf("/api/v1/suffix-admin/columns/%d", plainColID), `{"name":"招标单位"}`)
if w.Code != 200 {
t.Fatalf("重命名列 status=%d body=%s", w.Code, w.Body.String())
}
if name := getJSON(w)["data"].(map[string]any)["name"].(string); name != "招标单位" {
t.Fatalf("重命名后 name=%q, want 招标单位", name)
}
w = authed(http.MethodPatch, fmt.Sprintf("/api/v1/suffix-admin/columns/%d", plainColID), `{"name":"工料机类型"}`)
if w.Code != 409 {
t.Fatalf("重名列 status=%d, want 409", w.Code)
}
w = authed(http.MethodPatch, fmt.Sprintf("/api/v1/suffix-admin/columns/%d", plainColID), `{"name":" "}`)
if w.Code != 400 {
t.Fatalf("空名列 status=%d, want 400", w.Code)
}
// 列有值不可删除:普通列已存「普通文本」
w = authed(http.MethodDelete, fmt.Sprintf("/api/v1/suffix-admin/columns/%d", plainColID), "")
if w.Code != 409 {
t.Fatalf("删有值列 status=%d, want 409", w.Code)
}
// 空列可直接删除
w = authed(http.MethodDelete, fmt.Sprintf("/api/v1/suffix-admin/columns/%d", insertedID), "")
if w.Code != 200 {
t.Fatalf("删空列 status=%d body=%s", w.Code, w.Body.String())
}
// 清空单元格后即可删除
w = authed(http.MethodPut, "/api/v1/suffix-admin/cells",
fmt.Sprintf(`{"dir_id":%d,"suffix_id":%d,"column_id":%d,"value":""}`, dirID, suffixID, plainColID))
if w.Code != 200 {
t.Fatalf("清空单元格 status=%d body=%s", w.Code, w.Body.String())
}
w = authed(http.MethodDelete, fmt.Sprintf("/api/v1/suffix-admin/columns/%d", plainColID), "")
if w.Code != 200 {
t.Fatalf("清空后删列 status=%d body=%s", w.Code, w.Body.String())
}
// 固定列列宽:默认值(后缀 50)→ 保存 → 回读;非法参数拒绝
w = authed(http.MethodGet, "/api/v1/suffix-admin/col-widths", "")
if w.Code != 200 {
t.Fatalf("读列宽 status=%d body=%s", w.Code, w.Body.String())
}
widths := getJSON(w)["data"].(map[string]any)["widths"].(map[string]any)
if widths["basic"].(map[string]any)["suffix"].(float64) != 50 {
t.Fatalf("basic.suffix 默认应为 50,got %v", widths["basic"].(map[string]any)["suffix"])
}
if _, has := widths["basic"].(map[string]any)["files"]; !has {
t.Fatal("basic 固定列应含 files")
}
w = authed(http.MethodPut, "/api/v1/suffix-admin/col-widths", `{"scope":"workspace","key":"ops","width":66}`)
if w.Code != 200 {
t.Fatalf("存列宽 status=%d body=%s", w.Code, w.Body.String())
}
w = authed(http.MethodGet, "/api/v1/suffix-admin/col-widths", "")
widths = getJSON(w)["data"].(map[string]any)["widths"].(map[string]any)
if widths["workspace"].(map[string]any)["ops"].(float64) != 66 {
t.Fatalf("workspace.ops 应为 66,got %v", widths["workspace"].(map[string]any)["ops"])
}
if widths["workspace"].(map[string]any)["suffix"].(float64) != 50 {
t.Fatalf("未保存过的 workspace.suffix 应仍为默认 50,got %v", widths["workspace"].(map[string]any)["suffix"])
}
w = authed(http.MethodPut, "/api/v1/suffix-admin/col-widths", `{"scope":"nope","key":"ops","width":66}`)
if w.Code != 400 {
t.Fatalf("非法 scope status=%d, want 400", w.Code)
}
w = authed(http.MethodPut, "/api/v1/suffix-admin/col-widths", `{"scope":"basic","key":"files","width":9999}`)
if w.Code != 400 {
t.Fatalf("列宽越界 status=%d, want 400", w.Code)
}
w = authed(http.MethodPut, "/api/v1/suffix-admin/col-widths", `{"scope":"basic","key":"nope","width":10}`)
if w.Code != 400 {
t.Fatalf("未知列 key status=%d, want 400", w.Code)
}
// 锚定行存值:保存 → 回读;空值清除;超长拒绝
w = authed(http.MethodPost, fmt.Sprintf("/api/v1/suffix-admin/dirs/%d/anchor", dirID),
fmt.Sprintf(`{"suffix_id":%d,"on":true}`, suffixID))
fmt.Sprintf(`{"suffix_id":%d,"value":"3"}`, suffixID))
if w.Code != 200 {
t.Fatalf("锚定 status=%d", w.Code)
}
@@ -252,8 +340,25 @@ func TestSuffixAdminIntegration(t *testing.T) {
t.Fatal("找不到 cos4 行")
return nil
}()
if !cos4["anchor"].(bool) {
t.Fatal("锚定后 anchor 应为 true")
if cos4["anchor_value"].(string) != "3" {
t.Fatalf("锚定后 anchor_value=%q, want 3", cos4["anchor_value"])
}
w = authed(http.MethodPost, fmt.Sprintf("/api/v1/suffix-admin/dirs/%d/anchor", dirID),
fmt.Sprintf(`{"suffix_id":%d,"value":""}`, suffixID))
if w.Code != 200 {
t.Fatalf("清锚定 status=%d", w.Code)
}
w = authed(http.MethodGet, fmt.Sprintf("/api/v1/suffix-admin/dirs/%d/page", dirID), "")
sfx = getJSON(w)["data"].(map[string]any)["suffixes"].([]any)
for _, line := range sfx {
if line.(map[string]any)["name"] == "cos4" && line.(map[string]any)["anchor_value"].(string) != "" {
t.Fatal("清空后 anchor_value 应为空")
}
}
w = authed(http.MethodPost, fmt.Sprintf("/api/v1/suffix-admin/dirs/%d/anchor", dirID),
fmt.Sprintf(`{"suffix_id":%d,"value":"%s"}`, suffixID, strings.Repeat("长", 191)))
if w.Code != 400 {
t.Fatalf("超长锚定值 status=%d, want 400", w.Code)
}
// 模板上传(内容与哈希校验)→ 概览 → 删除
@@ -274,6 +379,11 @@ func TestSuffixAdminIntegration(t *testing.T) {
t.Fatalf("传模板 status=%d body=%s", w.Code, w.Body.String())
}
fileID := uint64(getJSON(w)["data"].(map[string]any)["id"].(float64))
// 附件必须落盘在 attachments/<后缀id>/ 下
storedName := getJSON(w)["data"].(map[string]any)["file_name"].(string)
if _, err := os.Stat(filepath.Join(storageRoot, "attachments", strconv.FormatUint(suffixID, 10), storedName)); err != nil {
t.Fatalf("附件未落在 attachments 目录: %v", err)
}
badSum := sha256.Sum256([]byte("tampered"))
w = authed(http.MethodPost, fmt.Sprintf("/api/v1/suffix-admin/suffixes/%d/files", suffixID),
fmt.Sprintf(`{"file_name":"坏.cos4","size":%d,"sha256":"%s","content_base64":"%s"}`,

View File

@@ -0,0 +1 @@
DROP TABLE suffix_col_widths;

View File

@@ -0,0 +1,9 @@
-- Suffix management fixed-column widths (basic settings + directory workspace tables).
-- scope: 'basic' | 'workspace'; col_key: idx/suffix/files/ops/anchor; width 0 = auto.
CREATE TABLE suffix_col_widths (
scope VARCHAR(20) NOT NULL,
col_key VARCHAR(32) NOT NULL,
width INT NOT NULL DEFAULT 0,
updated_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
PRIMARY KEY(scope,col_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

View File

@@ -0,0 +1,2 @@
-- 回滚:恢复根目录名称
UPDATE suffix_dirs SET name='按信息价的目录' WHERE parent_id IS NULL AND name='建设项目';

View File

@@ -0,0 +1,2 @@
-- 根目录改名:按信息价的目录 → 建设项目(目录现已支持重命名,此处仅迁移存量数据)
UPDATE suffix_dirs SET name='建设项目' WHERE parent_id IS NULL AND name='按信息价的目录';

View File

@@ -0,0 +1 @@
ALTER TABLE suffix_dir_anchors DROP COLUMN value;

View File

@@ -0,0 +1,2 @@
-- 锚定行从 1/— 开关改为存值:value 为锚定内容(如行号),'' = 未锚定。
ALTER TABLE suffix_dir_anchors ADD COLUMN value VARCHAR(190) NOT NULL DEFAULT '';

View File

@@ -0,0 +1 @@
ALTER TABLE suffix_files DROP COLUMN published;

View File

@@ -0,0 +1,2 @@
-- 骨架文件发布状态:0=未发布,1=已发布
ALTER TABLE suffix_files ADD COLUMN published TINYINT(1) NOT NULL DEFAULT 0;