From a7eaf6114aa283a7fba73d66bbb1cbd197fc06f7 Mon Sep 17 00:00:00 2001 From: lealife Date: Sat, 28 Nov 2015 14:16:10 +0800 Subject: [PATCH] =?UTF-8?q?urlTitle=20=E4=BC=98=E5=8C=96,=20=E5=87=8F?= =?UTF-8?q?=E5=B0=91=E7=94=9F=E6=88=90=E6=AC=A1=E6=95=B0,=20=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E4=BD=BF=E7=94=A8id=E4=BD=9C=E4=B8=BAurlTitle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/service/BlogService.go | 8 ++++---- app/service/NotebookService.go | 8 ++++++-- app/service/UpgradeService.go | 4 ++-- app/service/init.go | 21 +++++++++++++++++++-- 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/app/service/BlogService.go b/app/service/BlogService.go index a4bbe27..89eb979 100644 --- a/app/service/BlogService.go +++ b/app/service/BlogService.go @@ -919,7 +919,7 @@ func (this *BlogService) UpateCateUrlTitle(userId string, cateId, urlTitle strin "UrlTitle": "", }) */ - url = GetUrTitle(userId, urlTitle, "notebook") + url = GetUrTitle(userId, urlTitle, "notebook", cateId) ok = db.UpdateByIdAndUserIdMap(db.Notebooks, cateId, userId, bson.M{ "UrlTitle": url, }) @@ -935,7 +935,7 @@ func (this *BlogService) UpateBlogUrlTitle(userId string, noteId, urlTitle strin ok = db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, bson.M{ "UrlTitle": "", }) - url = GetUrTitle(userId, urlTitle, "note") + url = GetUrTitle(userId, urlTitle, "note", noteId) ok = db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, bson.M{ "UrlTitle": url, }) @@ -1036,7 +1036,7 @@ func (this *BlogService) UpdateSingleUrlTitle(userId, singleId, urlTitle string) "UrlTitle": "", }) */ - url = GetUrTitle(userId, urlTitle, "single") + url = GetUrTitle(userId, urlTitle, "single", singleId) ok = db.UpdateByIdAndUserIdMap(db.BlogSingles, singleId, userId, bson.M{ "UrlTitle": url, }) @@ -1070,7 +1070,7 @@ func (this *BlogService) AddOrUpdateSingle(userId, singleId, title, content stri UserId: bson.ObjectIdHex(userId), Title: title, Content: content, - UrlTitle: GetUrTitle(userId, title, "single"), + UrlTitle: GetUrTitle(userId, title, "single", singleId), CreatedTime: time.Now(), } page.UpdatedTime = page.CreatedTime diff --git a/app/service/NotebookService.go b/app/service/NotebookService.go index a437004..4974e36 100644 --- a/app/service/NotebookService.go +++ b/app/service/NotebookService.go @@ -156,9 +156,13 @@ func (this *NotebookService) GetNotebooksByNotebookIds(notebookIds []bson.Object } // 添加 -// [ok] func (this *NotebookService) AddNotebook(notebook info.Notebook) (bool, info.Notebook) { - notebook.UrlTitle = GetUrTitle(notebook.UserId.Hex(), notebook.Title, "notebook") + + if notebook.NotebookId == "" { + notebook.NotebookId = bson.NewObjectId() + } + + notebook.UrlTitle = GetUrTitle(notebook.UserId.Hex(), notebook.Title, "notebook", notebook.NotebookId.Hex()) notebook.Usn = userService.IncrUsn(notebook.UserId.Hex()) now := time.Now() notebook.CreatedTime = now diff --git a/app/service/UpgradeService.go b/app/service/UpgradeService.go index f7cfcc3..ddd713f 100644 --- a/app/service/UpgradeService.go +++ b/app/service/UpgradeService.go @@ -65,7 +65,7 @@ func (this *UpgradeService) UpgradeBetaToBeta2(userId string) (ok bool, msg stri data["RecommendTime"] = note.UpdatedTime Log("Time " + noteId) } - data["UrlTitle"] = GetUrTitle(note.UserId.Hex(), note.Title, "note") + data["UrlTitle"] = GetUrTitle(note.UserId.Hex(), note.Title, "note", noteId) db.UpdateByIdAndUserIdMap2(db.Notes, note.NoteId, note.UserId, data) Log(noteId) } @@ -77,7 +77,7 @@ func (this *UpgradeService) UpgradeBetaToBeta2(userId string) (ok bool, msg stri for _, notebook := range notebooks { notebookId := notebook.NotebookId.Hex() data := bson.M{} - data["UrlTitle"] = GetUrTitle(notebook.UserId.Hex(), notebook.Title, "notebook") + data["UrlTitle"] = GetUrTitle(notebook.UserId.Hex(), notebook.Title, "notebook", notebookId) db.UpdateByIdAndUserIdMap2(db.Notebooks, notebook.NotebookId, notebook.UserId, data) Log(notebookId) } diff --git a/app/service/init.go b/app/service/init.go index 63e8194..a8ed2c6 100644 --- a/app/service/init.go +++ b/app/service/init.go @@ -2,6 +2,7 @@ package service import ( "github.com/leanote/leanote/app/db" + . "github.com/leanote/leanote/app/lea" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" "net/url" @@ -150,12 +151,28 @@ func getUniqueUrlTitle(userId string, urlTitle string, types string, padding int return urlTitle2 } +// 截取id 24位变成12位 +// 先md5, 再取12位 +func subIdHalf(id string) string { + idMd5 := Md5(id) + return idMd5[:12] +} + // types == note,notebook,single -func GetUrTitle(userId string, title string, types string) string { +// id noteId, notebookId, singleId 当title没的时候才有用, 用它来替换 +func GetUrTitle(userId string, title string, types string, id string) string { urlTitle := strings.Trim(title, " ") if urlTitle == "" { - urlTitle = "Untitled-" + userId + if id == "" { + urlTitle = "Untitled-" + userId + } else { + urlTitle = subIdHalf(id) + } + // 不允许title是ObjectId + } else if bson.IsObjectIdHex(title) { + urlTitle = subIdHalf(id) } + urlTitle = fixUrlTitle(urlTitle) return getUniqueUrlTitle(userId, urlTitle, types, 1) }