sky/internal/transport/http/account_handlers.go
root 6ed55254a0 chore: initial import of sky project
Go + PostgreSQL + Valkey backend (cmd/internal/migrations),
uni-app mobile frontend, PC web (web/), admin console (admin/),
Android WebView container (android_project/).
Build artifacts, node_modules, local env and debug screenshots excluded.
2026-11-09 22:22:44 +08:00

68 lines
2.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package api
import (
"net/http"
"github.com/jackc/pgx/v5"
"github.com/yourorg/article-platform/internal/platform/auth"
"github.com/yourorg/article-platform/internal/platform/httpx"
"github.com/yourorg/article-platform/internal/platform/middleware"
)
// deleteAccount 注销账号。需要密码二次确认;
// 事务内软删除名下全部文章,再将账号置为软删除 + banned。
func (d *Deps) deleteAccount(w http.ResponseWriter, r *http.Request) {
var req struct {
Password string `json:"password"`
}
if err := httpx.Decode(r, &req); err != nil {
httpx.WriteError(w, err)
return
}
if req.Password == "" {
httpx.WriteError(w, httpx.InvalidParam("请输入密码确认注销"))
return
}
uid := middleware.UserID(r.Context())
hash, err := d.Users.GetPasswordHash(r.Context(), d.Pool, uid)
if err != nil {
httpx.WriteError(w, httpx.NotFound("账号不存在"))
return
}
if err := auth.VerifyPassword(hash, req.Password); err != nil {
httpx.WriteError(w, httpx.NewError(http.StatusUnauthorized, httpx.CodeUnauthorized, "密码错误,无法注销"))
return
}
err = d.Articles.WithTx(r.Context(), func(tx pgx.Tx) error {
// 软删除名下文章(进入回收站,后台仍可审计)
if _, err := tx.Exec(r.Context(),
`UPDATE articles SET deleted_at=now(), updated_at=now() WHERE owner_id=$1 AND deleted_at IS NULL`, uid); err != nil {
return err
}
// 账号软删除:unique 索引带 WHERE deleted_at IS NULL,注销后账号名可被复用
if _, err := tx.Exec(r.Context(),
`UPDATE users SET deleted_at=now(), status='banned', updated_at=now() WHERE id=$1`, uid); err != nil {
return err
}
// 清空关注关系与话题关注
if _, err := tx.Exec(r.Context(),
`DELETE FROM user_follows WHERE follower_id=$1 OR following_id=$1`, uid); err != nil {
return err
}
if _, err := tx.Exec(r.Context(),
`DELETE FROM user_topic_follows WHERE user_id=$1`, uid); err != nil {
return err
}
return nil
})
if err != nil {
httpx.WriteError(w, err)
return
}
httpx.WriteJSON(w, http.StatusOK, map[string]any{"ok": true, "message": "账号已注销"})
}