更新
This commit is contained in:
1 parent
012f8edf8b
commit
36ff960fbd
33 files changed
+1644
-344
No files matched your search
@@ -7,6 +7,8 @@ import (
|
||||
"server/models"
|
||||
"server/pkg/identity"
|
||||
"server/pkg/jwtutil"
|
||||
"server/services/gueststore"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -19,9 +21,9 @@ type AppNotebookController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// 只放行移动端与租户端令牌:/api/** 仅被 uniapp 调用,平台令牌不该能打移动端接口。
|
||||
// 放行移动端、租户端与游客令牌:游客调用时不进业务表,分流到 gueststore。
|
||||
func (c *AppNotebookController) appNotebookClaims() (*jwtutil.Claims, error) {
|
||||
return identity.Claims(c.Ctx.Request.Header.Get("Authorization"), identity.EndApp, identity.EndBackend)
|
||||
return identity.Claims(c.Ctx.Request.Header.Get("Authorization"), identity.EndApp, identity.EndBackend, identity.EndGuest)
|
||||
}
|
||||
|
||||
func (c *AppNotebookController) nbJsonErr(httpStatus, bizCode int, msg string) {
|
||||
@@ -45,6 +47,11 @@ func (c *AppNotebookController) GetList() {
|
||||
|
||||
keyword := strings.TrimSpace(c.GetString("keyword"))
|
||||
|
||||
if identity.IsGuest(claims) {
|
||||
c.guestList(claims, keyword)
|
||||
return
|
||||
}
|
||||
|
||||
qs := models.Orm.QueryTable(new(models.BackendNotebook)).
|
||||
Filter("is_deleted", 0).
|
||||
Filter("user_id", claims.UserID)
|
||||
@@ -109,6 +116,11 @@ func (c *AppNotebookController) GetDetail() {
|
||||
return
|
||||
}
|
||||
|
||||
if identity.IsGuest(claims) {
|
||||
c.guestDetail(claims, id)
|
||||
return
|
||||
}
|
||||
|
||||
var note models.BackendNotebook
|
||||
err = models.Orm.QueryTable(new(models.BackendNotebook)).
|
||||
Filter("id", id).
|
||||
@@ -164,6 +176,11 @@ func (c *AppNotebookController) Create() {
|
||||
title = "无标题"
|
||||
}
|
||||
|
||||
if identity.IsGuest(claims) {
|
||||
c.guestCreateItem(claims, title, payload.Content, payload.Pinned)
|
||||
return
|
||||
}
|
||||
|
||||
userID := uint64(claims.UserID)
|
||||
pinned := int8(0)
|
||||
if payload.Pinned {
|
||||
@@ -224,6 +241,11 @@ func (c *AppNotebookController) Update() {
|
||||
return
|
||||
}
|
||||
|
||||
if identity.IsGuest(claims) {
|
||||
c.guestUpdateItem(claims, id, payload.Title, payload.Content, payload.Pinned)
|
||||
return
|
||||
}
|
||||
|
||||
var note models.BackendNotebook
|
||||
err = models.Orm.QueryTable(new(models.BackendNotebook)).
|
||||
Filter("id", id).
|
||||
@@ -286,6 +308,19 @@ func (c *AppNotebookController) Delete() {
|
||||
return
|
||||
}
|
||||
|
||||
if identity.IsGuest(claims) {
|
||||
if err := gueststore.Delete(uint64(claims.UserID), guestModuleNotebook, id); err != nil {
|
||||
if err == gueststore.ErrNotFound {
|
||||
c.nbJsonErr(404, 404, "笔记不存在")
|
||||
return
|
||||
}
|
||||
c.nbJsonErr(500, 500, "删除失败")
|
||||
return
|
||||
}
|
||||
c.nbOk(nil)
|
||||
return
|
||||
}
|
||||
|
||||
var note models.BackendNotebook
|
||||
err = models.Orm.QueryTable(new(models.BackendNotebook)).
|
||||
Filter("id", id).
|
||||
@@ -327,6 +362,11 @@ func (c *AppNotebookController) TogglePin() {
|
||||
return
|
||||
}
|
||||
|
||||
if identity.IsGuest(claims) {
|
||||
c.guestTogglePin(claims, id)
|
||||
return
|
||||
}
|
||||
|
||||
var note models.BackendNotebook
|
||||
err = models.Orm.QueryTable(new(models.BackendNotebook)).
|
||||
Filter("id", id).
|
||||
@@ -361,3 +401,107 @@ func (c *AppNotebookController) TogglePin() {
|
||||
"pinned": newPinned == 1,
|
||||
})
|
||||
}
|
||||
|
||||
// ---------- 游客分支(免登录态):数据存 yz_guest_data,响应结构与登录态一致 ----------
|
||||
|
||||
// guestNoteFull 读取并补全一条游客笔记(ID/时间以存储行为准)。
|
||||
func guestNoteFull(r gueststore.Row) guestNoteItem {
|
||||
var it guestNoteItem
|
||||
if json.Unmarshal(r.Payload, &it) != nil {
|
||||
it = guestNoteItem{Title: "无标题"}
|
||||
}
|
||||
it.ID = r.ID
|
||||
it.CreatedAt = r.CreateTime.Format("2006-01-02 15:04:05")
|
||||
it.UpdatedAt = it.CreatedAt
|
||||
if r.UpdateTime != nil {
|
||||
it.UpdatedAt = r.UpdateTime.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
return it
|
||||
}
|
||||
|
||||
func (c *AppNotebookController) guestList(claims *jwtutil.Claims, keyword string) {
|
||||
rows, err := gueststore.List(uint64(claims.UserID), guestModuleNotebook)
|
||||
if err != nil {
|
||||
c.nbJsonErr(500, 500, "查询失败")
|
||||
return
|
||||
}
|
||||
items := make([]guestNoteItem, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
it := guestNoteFull(r)
|
||||
if keyword != "" && !strings.Contains(it.Title, keyword) {
|
||||
continue
|
||||
}
|
||||
items = append(items, it)
|
||||
}
|
||||
// 与登录态一致:置顶优先,再按更新时间倒序
|
||||
sort.SliceStable(items, func(i, j int) bool {
|
||||
if items[i].Pinned != items[j].Pinned {
|
||||
return items[i].Pinned
|
||||
}
|
||||
return items[i].UpdatedAt > items[j].UpdatedAt
|
||||
})
|
||||
c.nbOk(map[string]interface{}{"list": items, "total": len(items)})
|
||||
}
|
||||
|
||||
func (c *AppNotebookController) guestDetail(claims *jwtutil.Claims, id uint64) {
|
||||
r, err := gueststore.Get(uint64(claims.UserID), guestModuleNotebook, id)
|
||||
if err != nil {
|
||||
c.nbJsonErr(404, 404, "笔记不存在")
|
||||
return
|
||||
}
|
||||
c.nbOk(guestNoteFull(r))
|
||||
}
|
||||
|
||||
func (c *AppNotebookController) guestCreateItem(claims *jwtutil.Claims, title, content string, pinned bool) {
|
||||
now := time.Now().Format("2006-01-02 15:04:05")
|
||||
it := guestNoteItem{Title: title, Content: content, Pinned: pinned, CreatedAt: now, UpdatedAt: now}
|
||||
newID, err := gueststore.Insert(uint64(claims.UserID), guestModuleNotebook, it)
|
||||
if err != nil {
|
||||
c.nbJsonErr(500, 500, "创建失败")
|
||||
return
|
||||
}
|
||||
c.nbOk(map[string]interface{}{
|
||||
"id": newID, "title": title, "content": content, "pinned": pinned,
|
||||
})
|
||||
}
|
||||
|
||||
func (c *AppNotebookController) guestUpdateItem(claims *jwtutil.Claims, id uint64, title, content string, pinned *bool) {
|
||||
r, err := gueststore.Get(uint64(claims.UserID), guestModuleNotebook, id)
|
||||
if err != nil {
|
||||
c.nbJsonErr(404, 404, "笔记不存在")
|
||||
return
|
||||
}
|
||||
it := guestNoteFull(r)
|
||||
if t := strings.TrimSpace(title); t != "" {
|
||||
it.Title = t
|
||||
}
|
||||
it.Content = content
|
||||
if pinned != nil {
|
||||
it.Pinned = *pinned
|
||||
}
|
||||
it.UpdatedAt = time.Now().Format("2006-01-02 15:04:05")
|
||||
if err = gueststore.Update(uint64(claims.UserID), guestModuleNotebook, id, it); err != nil {
|
||||
c.nbJsonErr(500, 500, "更新失败")
|
||||
return
|
||||
}
|
||||
c.nbOk(map[string]interface{}{
|
||||
"id": it.ID, "title": it.Title, "content": it.Content, "pinned": it.Pinned,
|
||||
"created_at": it.CreatedAt, "updated_at": it.UpdatedAt,
|
||||
})
|
||||
}
|
||||
|
||||
func (c *AppNotebookController) guestTogglePin(claims *jwtutil.Claims, id uint64) {
|
||||
r, err := gueststore.Get(uint64(claims.UserID), guestModuleNotebook, id)
|
||||
if err != nil {
|
||||
c.nbJsonErr(404, 404, "笔记不存在")
|
||||
return
|
||||
}
|
||||
it := guestNoteFull(r)
|
||||
it.Pinned = !it.Pinned
|
||||
it.UpdatedAt = time.Now().Format("2006-01-02 15:04:05")
|
||||
if err = gueststore.Update(uint64(claims.UserID), guestModuleNotebook, id, it); err != nil {
|
||||
c.nbJsonErr(500, 500, "操作失败")
|
||||
return
|
||||
}
|
||||
c.nbOk(map[string]interface{}{"pinned": it.Pinned})
|
||||
}
|
||||
Reference in new issue
Block a user