feat: suffix naming rules, extension consistency, column widths, compact layout

- suffix rows are named by the suffix at creation (prompt); names are
  immutable afterwards; duplicates auto-suffix -1/-2 (global)
- new-group dialog takes group name + first suffix name; backend creates
  group and its first workspace row in one transaction
- upload validation: file extension must match the row's suffix
  (whole-row consistency), enforced client- and server-side
- column sort dialog gains per-column width inputs (persisted via
  PATCH /columns/:id, table renders fixed widths, 0 = auto)
- compact layout: denser tables/bars, viewport-height shell with
  independent scrollbars, cleanup step in e2e flow
This commit is contained in:
lianjianwei2024
2026-10-30 16:17:17 +08:00
parent 4ebf0afc22
commit ac4b4a1c06
6 changed files with 300 additions and 119 deletions

View File

@@ -39,6 +39,7 @@ func registerSuffixRoutes(secured *gin.RouterGroup, a *BusinessAPI) {
s.POST("/dirs/:id/columns", a.createSuffixColumn)
s.POST("/dirs/:id/columns/order", a.reorderSuffixColumns)
s.POST("/dirs/:id/anchor", a.setSuffixAnchor)
s.PATCH("/columns/:id", a.updateSuffixColumn)
s.DELETE("/columns/:id", a.deleteSuffixColumn)
s.PUT("/cells", a.saveSuffixCell)
s.GET("/overview", a.suffixOverview)
@@ -221,6 +222,7 @@ type suffixColumnRow struct {
ID uint64 `json:"id"`
Name string `json:"name"`
IsValue bool `json:"is_value"`
Width int `json:"width"`
}
func (a *BusinessAPI) suffixDirPage(c *gin.Context) {
@@ -231,14 +233,14 @@ func (a *BusinessAPI) suffixDirPage(c *gin.Context) {
}
columns := []suffixColumnRow{}
colRows, err := a.db.QueryContext(c,
"SELECT id,name,is_value FROM suffix_columns WHERE dir_id=? ORDER BY sort_order ASC,id ASC", dirID)
"SELECT id,name,is_value,width FROM suffix_columns WHERE dir_id=? ORDER BY sort_order ASC,id ASC", dirID)
if err != nil {
apiError(c, 500, "internal_error", "列加载失败")
return
}
for colRows.Next() {
var col suffixColumnRow
if err := colRows.Scan(&col.ID, &col.Name, &col.IsValue); err != nil {
if err := colRows.Scan(&col.ID, &col.Name, &col.IsValue, &col.Width); err != nil {
colRows.Close()
apiError(c, 500, "internal_error", "列加载失败")
return
@@ -306,6 +308,7 @@ func (a *BusinessAPI) createSuffixColumn(c *gin.Context) {
var in struct {
Name string `json:"name"`
IsValue bool `json:"is_value"`
Width int `json:"width"`
AfterID uint64 `json:"after_id"`
}
if c.ShouldBindJSON(&in) != nil {
@@ -317,6 +320,10 @@ func (a *BusinessAPI) createSuffixColumn(c *gin.Context) {
apiError(c, 400, "invalid_request", "列名须为 1–190 字")
return
}
if in.Width < 0 || in.Width > 2000 {
apiError(c, 400, "invalid_request", "列宽须为 0–2000(0=自适应)")
return
}
var dup int
if a.db.QueryRowContext(c,
"SELECT 1 FROM suffix_columns WHERE dir_id=? AND name=?", dirID, name).Scan(&dup) == nil {
@@ -346,14 +353,14 @@ func (a *BusinessAPI) createSuffixColumn(c *gin.Context) {
sortAt = int(order.Int64)
}
res, err := a.db.ExecContext(c,
"INSERT INTO suffix_columns(dir_id,name,is_value,sort_order) VALUES(?,?,?,?)",
dirID, name, in.IsValue, sortAt+1)
"INSERT INTO suffix_columns(dir_id,name,is_value,sort_order,width) VALUES(?,?,?,?,?)",
dirID, name, in.IsValue, sortAt+1, in.Width)
if err != nil {
apiError(c, 500, "internal_error", "列创建失败")
return
}
id, _ := res.LastInsertId()
c.JSON(201, gin.H{"data": gin.H{"id": id, "name": name, "is_value": in.IsValue}})
c.JSON(201, gin.H{"data": gin.H{"id": id, "name": name, "is_value": in.IsValue, "width": in.Width}})
}
func (a *BusinessAPI) reorderSuffixColumns(c *gin.Context) {
@@ -415,6 +422,28 @@ func (a *BusinessAPI) reorderSuffixColumns(c *gin.Context) {
c.JSON(200, gin.H{"data": gin.H{"ok": true, "ordered": len(final)}})
}
func (a *BusinessAPI) updateSuffixColumn(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
var in struct {
Width *int `json:"width"`
}
if c.ShouldBindJSON(&in) != nil || in.Width == nil {
apiError(c, 400, "invalid_request", "width 必填")
return
}
w := *in.Width
if w < 0 || w > 2000 {
apiError(c, 400, "invalid_request", "列宽须为 0–2000(0=自适应)")
return
}
res, err := a.db.ExecContext(c, "UPDATE suffix_columns SET width=? WHERE id=?", w, id)
if err != nil || rowsAffected(res) == 0 {
apiError(c, 404, "not_found", "列不存在")
return
}
c.JSON(200, gin.H{"data": gin.H{"id": id, "width": w}})
}
func (a *BusinessAPI) deleteSuffixColumn(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
res, err := a.db.ExecContext(c, "DELETE FROM suffix_columns WHERE id=?", id)
@@ -590,7 +619,8 @@ type suffixOverview struct {
func (a *BusinessAPI) createSuffixGroup(c *gin.Context) {
var in struct {
Name string `json:"name"`
Name string `json:"name"`
FirstSuffixName string `json:"first_suffix_name"`
}
if c.ShouldBindJSON(&in) != nil {
apiError(c, 400, "invalid_request", "参数无效")
@@ -606,6 +636,19 @@ func (a *BusinessAPI) createSuffixGroup(c *gin.Context) {
apiError(c, 409, "duplicate_name", "已有同名分组")
return
}
// 首个后缀名可选:填了就随组自动建一行工作行(同名自动 -1)。
firstName, firstOK := suffixTrimName(in.FirstSuffixName, 64)
if firstOK {
final := firstName
for i := 1; ; i++ {
var dup2 int
if a.db.QueryRowContext(c, "SELECT 1 FROM suffixes WHERE name=?", final).Scan(&dup2) != nil {
break
}
final = firstName + "-" + strconv.Itoa(i)
}
firstName = final
}
var order sql.NullInt64
_ = a.db.QueryRowContext(c, "SELECT MAX(sort_order) FROM suffix_groups").Scan(&order)
tx, err := a.db.BeginTx(c, nil)
@@ -621,17 +664,22 @@ func (a *BusinessAPI) createSuffixGroup(c *gin.Context) {
return
}
gid, _ := gres.LastInsertId()
// 新建分组自动带一行工作行,按钮齐全,双击即可命名。
if _, err := tx.ExecContext(c,
"INSERT INTO suffixes(group_id,name,sort_order) VALUES(?,'',0)", gid); err != nil {
apiError(c, 500, "internal_error", "分组创建失败")
return
created := []suffixOverview{}
if firstOK {
sres, err := tx.ExecContext(c,
"INSERT INTO suffixes(group_id,name,sort_order) VALUES(?,?,0)", gid, firstName)
if err != nil {
apiError(c, 500, "internal_error", "分组创建失败")
return
}
sid, _ := sres.LastInsertId()
created = append(created, suffixOverview{ID: uint64(sid), Name: firstName})
}
if err := tx.Commit(); err != nil {
apiError(c, 500, "internal_error", "分组创建失败")
return
}
c.JSON(201, gin.H{"data": gin.H{"id": gid, "name": name, "suffixes": []suffixOverview{}}})
c.JSON(201, gin.H{"data": gin.H{"id": gid, "name": name, "suffixes": created}})
}
func (a *BusinessAPI) renameSuffixGroup(c *gin.Context) {
@@ -715,12 +763,28 @@ func (a *BusinessAPI) suffixFilesOnDisk(c *gin.Context, groupID uint64) []string
func (a *BusinessAPI) createSuffix(c *gin.Context) {
var in struct {
GroupID uint64 `json:"group_id"`
Name string `json:"name"`
AfterID uint64 `json:"after_id"`
}
if c.ShouldBindJSON(&in) != nil || in.GroupID == 0 {
apiError(c, 400, "invalid_request", "参数无效")
return
}
name, ok := suffixTrimName(in.Name, 64)
if !ok {
apiError(c, 400, "invalid_request", "后缀名须为 1–64 字")
return
}
// 名称按后缀命名且不可修改:全局查重,同名自动 -1、-2…
final := name
for i := 1; ; i++ {
var dup int
if a.db.QueryRowContext(c, "SELECT 1 FROM suffixes WHERE name=?", final).Scan(&dup) != nil {
break
}
final = name + "-" + strconv.Itoa(i)
}
name = final
var groupSort int
if err := a.db.QueryRowContext(c,
"SELECT sort_order FROM suffix_groups WHERE id=?", in.GroupID).Scan(&groupSort); err != nil {
@@ -756,7 +820,7 @@ func (a *BusinessAPI) createSuffix(c *gin.Context) {
}
res, err := tx.ExecContext(c,
"INSERT INTO suffixes(group_id,name,sort_order) VALUES(?,?,?)",
in.GroupID, "", sortAfter+1)
in.GroupID, name, sortAfter+1)
if err != nil {
apiError(c, 500, "internal_error", "后缀创建失败")
return
@@ -766,7 +830,7 @@ func (a *BusinessAPI) createSuffix(c *gin.Context) {
apiError(c, 500, "internal_error", "后缀创建失败")
return
}
c.JSON(201, gin.H{"data": gin.H{"id": id, "group_id": in.GroupID, "name": ""}})
c.JSON(201, gin.H{"data": gin.H{"id": id, "group_id": in.GroupID, "name": name}})
}
func (a *BusinessAPI) renameSuffix(c *gin.Context) {
@@ -894,6 +958,25 @@ func (a *BusinessAPI) uploadSuffixFile(c *gin.Context) {
apiError(c, 400, "invalid_request", "文件哈希校验失败,请重新上传")
return
}
// 扩展名必须与本行后缀一致(整行统一):行名去掉 -N 去重尾缀后即为预期扩展名。
var suffixName string
if err := a.db.QueryRowContext(c,
"SELECT name FROM suffixes WHERE id=?", suffixID).Scan(&suffixName); err != nil {
apiError(c, 404, "not_found", "后缀不存在")
return
}
base := suffixName
if i := strings.LastIndex(base, "-"); i > 0 {
if _, err := strconv.Atoi(base[i+1:]); err == nil {
base = base[:i]
}
}
fileExt := strings.ToLower(strings.TrimPrefix(filepath.Ext(fileName), "."))
if fileExt == "" || fileExt != strings.ToLower(base) {
apiError(c, 400, "extension_mismatch",
fmt.Sprintf("上传文件的扩展名必须与本行后缀一致:本行是「%s」,只能上传 .%s 文件", suffixName, strings.ToLower(base)))
return
}
root, rootErr := suffixTemplateRoot()
if rootErr != nil {
apiError(c, 500, "storage_unconfigured", "PACKAGE_STORAGE_ROOT 未配置,模板无法落盘")

View File

@@ -115,14 +115,34 @@ func TestSuffixAdminIntegration(t *testing.T) {
if w.Code != 409 {
t.Fatalf("重名分组 status=%d, want 409", w.Code)
}
w = authed(http.MethodPost, "/api/v1/suffix-admin/suffixes", fmt.Sprintf(`{"group_id":%d}`, groupID))
w = authed(http.MethodPost, "/api/v1/suffix-admin/suffixes", fmt.Sprintf(`{"group_id":%d,"name":"cos4"}`, groupID))
if w.Code != 201 {
t.Fatalf("建后缀 status=%d body=%s", w.Code, w.Body.String())
}
suffixID := uint64(getJSON(w)["data"].(map[string]any)["id"].(float64))
w = authed(http.MethodPut, fmt.Sprintf("/api/v1/suffix-admin/suffixes/%d", suffixID), `{"name":"cos4"}`)
if w.Code != 200 {
t.Fatalf("改后缀名 status=%d body=%s", w.Code, w.Body.String())
// 建组带首个后缀名 → 自动工作行
w = authed(http.MethodPost, "/api/v1/suffix-admin/groups", `{"name":"广西自治区","first_suffix_name":"xml"}`)
if w.Code != 201 {
t.Fatalf("建组带后缀 status=%d body=%s", w.Code, w.Body.String())
}
gx := uint64(getJSON(w)["data"].(map[string]any)["id"].(float64))
sx := getJSON(w)["data"].(map[string]any)["suffixes"].([]any)
if len(sx) != 1 || sx[0].(map[string]any)["name"] != "xml" {
t.Fatalf("首后缀自动行不符: %v", sx)
}
_ = gx
// 同名再建 → 自动 -1
w = authed(http.MethodPost, "/api/v1/suffix-admin/suffixes", fmt.Sprintf(`{"group_id":%d,"name":"cos4"}`, groupID))
if w.Code != 201 {
t.Fatalf("同名后缀 status=%d body=%s", w.Code, w.Body.String())
}
if got := getJSON(w)["data"].(map[string]any)["name"]; got != "cos4-1" {
t.Fatalf("同名自动 -1: got %v", got)
}
// 空名 → 400
w = authed(http.MethodPost, "/api/v1/suffix-admin/suffixes", fmt.Sprintf(`{"group_id":%d,"name":""}`, groupID))
if w.Code != 400 {
t.Fatalf("空名 status=%d, want 400", w.Code)
}
// 目录树:根 + 子目录 + 改名 + 删除保护
@@ -240,6 +260,13 @@ func TestSuffixAdminIntegration(t *testing.T) {
content := []byte("COS4-skeleton-demo")
sum := sha256.Sum256(content)
b64 := base64.StdEncoding.EncodeToString(content)
// 扩展名与本行后缀不符 → 400
w = authed(http.MethodPost, fmt.Sprintf("/api/v1/suffix-admin/suffixes/%d/files", suffixID),
fmt.Sprintf(`{"file_name":"骨架.xml","size":%d,"sha256":"%s","content_base64":"%s"}`,
len(content), hex.EncodeToString(sum[:]), b64))
if w.Code != 400 {
t.Fatalf("扩展名不符 status=%d, want 400", w.Code)
}
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"}`,
len(content), hex.EncodeToString(sum[:]), b64))