/* { "NoteId": "550c0bee2ec82a2eb5000000", "NotebookId": "54a1676399c37b1c77000004", "UserId": "54a1676399c37b1c77000002", "Title": "asdfadsf--=", "Desc": "", "Tags": [ "" ], "Abstract": "", "Content": "", "IsMarkdown": false, "IsBlog": false, "IsTrash": false, "Usn": 8, "Files": [ { "FileId": "551975d599c37b970f000002", "LocalFileId": "", "Type": "", "Title": "", "HasBody": false, "IsAttach": false }, { "FileId": "551975de99c37b970f000003", "LocalFileId": "", "Type": "doc", "Title": "李铁-简历-ali-print-en.doc", "HasBody": false, "IsAttach": true }, { "FileId": "551975de99c37b970f000004", "LocalFileId": "", "Type": "doc", "Title": "李铁-简历-ali-print.doc", "HasBody": false, "IsAttach": true } ], "CreatedTime": "2015-03-20T20:00:52.463+08:00", "UpdatedTime": "2015-03-31T00:12:44.967+08:00", "PublicTime": "2015-03-20T20:00:52.463+08:00" } */ func (c ApiNote) GetNote(noteId string) revel.Result { if !bson.IsObjectIdHex(noteId) { re := info.NewApiRe() re.Msg = "noteIdInvalid" return c.RenderJson(re) } note := noteService.GetNote(noteId, c.getUserId()) if note.NoteId == "" { re := info.NewApiRe() re.Msg = "notExists" return c.RenderJson(re) } apiNotes := noteService.ToApiNotes([]info.Note{note}) return c.RenderJson(apiNotes[0]) }
// 注销 // [Ok] func (c ApiAuth) Logout() revel.Result { token := c.getToken() sessionService.Clear(token) re := info.NewApiRe() re.Ok = true return c.RenderJson(re) }
// 得到笔记本下的笔记 // [OK] func (c ApiNote) GetNotes(notebookId string) revel.Result { if notebookId != "" && !bson.IsObjectIdHex(notebookId) { re := info.NewApiRe() re.Msg = "notebookIdInvalid" return c.RenderJson(re) } _, notes := noteService.ListNotes(c.getUserId(), notebookId, false, c.GetPage(), pageSize, defaultSortField, false, false) return c.RenderJson(noteService.ToApiNotes(notes)) }
// 头像设置 // 参数file=文件 // 成功返回{Logo: url} 头像新url // [OK] func (c ApiUser) UpdateLogo() revel.Result { ok, msg, url := c.uploadImage() if ok { ok = userService.UpdateAvatar(c.getUserId(), url) return c.RenderJson(map[string]string{"Logo": url}) } else { re := info.NewApiRe() re.Msg = msg return c.RenderJson(re) } }
// 修改用户名 // [OK] func (c ApiUser) UpdateUsername(username string) revel.Result { re := info.NewApiRe() if c.GetUsername() == "demo" { re.Msg = "cannotUpdateDemo" return c.RenderJson(re) } if re.Ok, re.Msg = Vd("username", username); !re.Ok { return c.RenderJson(re) } re.Ok, re.Msg = userService.UpdateUsername(c.getUserId(), username) return c.RenderJson(re) }
// 修改密码 // [OK] func (c ApiUser) UpdatePwd(oldPwd, pwd string) revel.Result { re := info.NewApiRe() if c.GetUsername() == "demo" { re.Msg = "cannotUpdateDemo" return c.RenderJson(re) } if re.Ok, re.Msg = Vd("password", oldPwd); !re.Ok { return c.RenderJson(re) } if re.Ok, re.Msg = Vd("password", pwd); !re.Ok { return c.RenderJson(re) } re.Ok, re.Msg = userService.UpdatePwd(c.getUserId(), oldPwd, pwd) return c.RenderJson(re) }
// 获取用户信息 // [OK] func (c ApiUser) Info() revel.Result { re := info.NewApiRe() userInfo := c.getUserInfo() if userInfo.UserId == "" { return c.RenderJson(re) } apiUser := info.ApiUser{ UserId: userInfo.UserId.Hex(), Username: userInfo.Username, Email: userInfo.Email, Logo: userInfo.Logo, Verified: userInfo.Verified, } return c.RenderJson(apiUser) }
// 注册 // [Ok] // 成功后并不返回用户ID, 需要用户重新登录 func (c ApiAuth) Register(email, pwd string) revel.Result { re := info.NewApiRe() if !configService.IsOpenRegister() { re.Msg = "notOpenRegister" // 未开放注册 return c.RenderJson(re) } if re.Ok, re.Msg = Vd("email", email); !re.Ok { return c.RenderJson(re) } if re.Ok, re.Msg = Vd("password", pwd); !re.Ok { return c.RenderJson(re) } // 注册 re.Ok, re.Msg = authService.Register(email, pwd, "") return c.RenderJson(re) }
// 这里得到token, 若不是login, logout等公用操作, 必须验证是否已登录 func AuthInterceptor(c *revel.Controller) revel.Result { // 得到token /api/user/info?userId=xxx&token=xxxxx token := c.Params.Values.Get("token") noToken := false if token == "" { // 若无, 则取sessionId token = c.Session.Id() noToken = true } c.Session["_token"] = token // 全部变成首字大写 var controller = strings.Title(c.Name) var method = strings.Title(c.MethodName) // 验证是否已登录 // 通过sessionService判断该token下是否有userId, 并返回userId userId := sessionService.GetUserId(token) if noToken && userId == "" { // 从session中获取, api/file/getImage, api/file/getAttach, api/file/getAllAttach // 客户端 userId, _ = c.Session["UserId"] } c.Session["_userId"] = userId // 是否需要验证? if !needValidate(controller, method) { return nil } if userId != "" { return nil // 已登录 } // 没有登录, 返回错误的信息, 需要登录 re := info.NewApiRe() re.Msg = "NOTLOGIN" return c.RenderJson(re) }
// 0.2 新增 // 导出成PDF // test localhost:9000/api/note/exportPdf?noteId=554f07bf05fcd15fa9000000&token=562211dc99c37ba6a7000001 func (c ApiNote) ExportPdf(noteId string) revel.Result { re := info.NewApiRe() userId := c.getUserId() if noteId == "" { re.Msg = "noteNotExists" return c.RenderJson(re) } note := noteService.GetNoteById(noteId) if note.NoteId == "" { re.Msg = "noteNotExists" return c.RenderJson(re) } noteUserId := note.UserId.Hex() // 是否有权限 if noteUserId != userId { // 是否是有权限协作的 if !note.IsBlog && !shareService.HasReadPerm(noteUserId, userId, noteId) { re.Msg = "noteNotExists" return c.RenderJson(re) } } // path 判断是否需要重新生成之 guid := NewGuid() fileUrlPath := "files/" + Digest3(noteUserId) + "/" + noteUserId + "/" + Digest2(guid) + "/images/pdf" dir := revel.BasePath + "/" + fileUrlPath if !MkdirAll(dir) { re.Msg = "noDir" return c.RenderJson(re) } filename := guid + ".pdf" path := dir + "/" + filename appKey, _ := revel.Config.String("app.secretLeanote") if appKey == "" { appKey, _ = revel.Config.String("app.secret") } // 生成之 binPath := configService.GetGlobalStringConfig("exportPdfBinPath") // 默认路径 if binPath == "" { binPath = "/usr/local/bin/wkhtmltopdf" } url := configService.GetSiteUrl() + "/note/toPdf?noteId=" + noteId + "&appKey=" + appKey var cc string if note.IsMarkdown { cc = binPath + " --window-status done \"" + url + "\" \"" + path + "\"" // \"" + cookieDomain + "\" \"" + cookieName + "\" \"" + cookieValue + "\"" } else { cc = binPath + " \"" + url + "\" \"" + path + "\"" // \"" + cookieDomain + "\" \"" + cookieName + "\" \"" + cookieValue + "\"" } cmd := exec.Command("/bin/sh", "-c", cc) _, err := cmd.Output() if err != nil { re.Msg = "sysError" return c.RenderJson(re) } file, err := os.Open(path) if err != nil { re.Msg = "sysError" return c.RenderJson(re) } filenameReturn := note.Title filenameReturn = FixFilename(filenameReturn) if filenameReturn == "" { filenameReturn = "Untitled.pdf" } else { filenameReturn += ".pdf" } return c.RenderBinary(file, filenameReturn, revel.Attachment, time.Now()) // revel.Attachment }