Browse Source

可恶的笔记终于完了

layui
yirenyishi 6 years ago
parent
commit
c8eb57ba43
  1. 70
      controllers/NoteController.go
  2. BIN
      data/beeblog.db
  3. 1
      routers/NoteRouter.go
  4. 15
      service/NoteService.go
  5. 18
      views/iframe/note.html
  6. 91
      views/note.html

70
controllers/NoteController.go

@ -14,11 +14,13 @@ type NoteController struct {
func (this *NoteController) Save() {
pid, _ := this.GetInt64("pid")
fmt.Println("pid", pid)
title := this.GetString("title")
uid := this.GetSession("userid").(int64)
note := &models.Note{Title: title, Pid: pid, UserId: uid}
uid := this.GetSession("userid")
if uid == nil {
this.Data["json"] = models.ReurnError(401,"保存失败")
this.ServeJSON()
}
note := &models.Note{Title: title, Pid: pid, UserId: uid.(int64)}
err := service.SaveNote(note)
if err == nil {
this.Data["json"] = note
@ -31,20 +33,26 @@ func (this *NoteController) Edit() {
idStr := this.Ctx.Input.Param(":id")
noteHtml := this.GetString("noteHtml")
id, _ := strconv.ParseInt(idStr, 10, 64)
uid := this.GetSession("userid").(int64)
uid := this.GetSession("userid")
if uid == nil{
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
}
note := &models.Note{Id:id}
err1 := service.GetNote(note)
if err1 != nil {
fmt.Print(err1)
this.Data["json"] = models.ReurnError(500,"保存失败")
this.ServeJSON()
}
if uid != note.UserId {
this.Data["json"] = models.ReurnError(403,"保存失败")
this.Data["json"] = models.ReurnError(403,"")
this.ServeJSON()
}
note.NoteHtml = noteHtml
err := service.SaveNote(note)
err := service.EditNote(note)
if err == nil {
this.Data["json"] = note
this.Data["json"] = models.ReurnSuccess("")
} else {
this.Data["json"] = models.ReurnError(500,"保存失败")
}
@ -53,9 +61,12 @@ func (this *NoteController) Edit() {
func (this *NoteController) SaveNoteColl() {
title := this.GetString("title")
uid := this.GetSession("userid").(int64)
note := &models.NoteColl{Title: title, UserId: uid}
uid := this.GetSession("userid")
if uid == nil{
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
}
note := &models.NoteColl{Title: title, UserId: uid.(int64)}
err := service.SaveNoteColl(note)
if err == nil {
this.Data["json"] = note
@ -66,6 +77,11 @@ func (this *NoteController) SaveNoteColl() {
}
func (this *NoteController) Get() {
uid := this.GetSession("userid")
if uid == nil{
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
}
idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64)
note := &models.Note{Id: id}
@ -73,6 +89,36 @@ func (this *NoteController) Get() {
if err == nil {
this.Data["json"] = note
}
if note.UserId != uid.(int64) {
this.Data["json"] = models.ReurnError(403, "")
this.ServeJSON()
}
this.ServeJSON()
}
func (this *NoteController) Delete() {
uid := this.GetSession("userid")
if uid == nil{
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
}
idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64)
note := &models.Note{Id: id}
err := service.GetNote(note)
if err != nil {
this.Data["json"] = models.ReurnError(500,"")
this.ServeJSON()
}
if note.UserId != uid.(int64) {
this.Data["json"] = models.ReurnError(403, "")
this.ServeJSON()
}
err = service.DelNote(note)
if err != nil {
this.Data["json"] = models.ReurnError(500,"")
this.ServeJSON()
}
this.Data["json"] = models.ReurnSuccess("")
this.ServeJSON()
}
@ -95,8 +141,6 @@ func (this *NoteController) Note() {
} else {
noteColls = make([]*models.NoteColl, 0)
}
fmt.Println(noteColls)
fmt.Println(len(noteColls))
this.Data["NoteColls"] = noteColls
this.TplName = "note.html"
}

BIN
data/beeblog.db

Binary file not shown.

1
routers/NoteRouter.go

@ -11,4 +11,5 @@ func init() {
beego.Router("/notecoll/save", &controllers.NoteController{}, "post:SaveNoteColl")
beego.Router("/note/save", &controllers.NoteController{}, "post:Save")
beego.Router("/note/edit/:id([0-9]+)", &controllers.NoteController{}, "post:Edit")
beego.Router("/note/del/:id([0-9]+)", &controllers.NoteController{}, "post:Delete")
}

15
service/NoteService.go

@ -8,6 +8,15 @@ import (
type NoteService struct {
}
func EditNote(note *models.Note) error {
o := orm.NewOrm()
id, err := o.Update(note)
if err == nil {
note.Id = id
}
return err
}
func SaveNote(note *models.Note) error {
o := orm.NewOrm()
id, err := o.Insert(note)
@ -22,6 +31,12 @@ func GetNote(note *models.Note) error {
return o.Read(note)
}
func DelNote(note *models.Note) error {
o := orm.NewOrm()
_,err := o.Delete(note)
return err
}
func GetNoteByPid(pid int64) ([]*models.Note, error) {
var notes []*models.Note
o := orm.NewOrm()

18
views/iframe/note.html

@ -46,18 +46,6 @@
</body>
<script>
$(function () {
// if (parent.tobj.catory) {
// $("#catory").val(parent.tobj.catory)
// }
// if (parent.tobj.labels) {
// for (var i = 0; i < parent.tobj.labels.length; i++) {
// var child = "<label class='tag tag-primary'>" + parent.tobj.labels[i] + "<a name='remove'><i class=\"iconfont icon-close\"></i></a> "
// + "<input type='checkbox' class='simple-tag' name='simple-tag-1' value='" + parent.tobj.labels[i] + "' checked='checked'/></label>"
// $("#labels").append(child)
// }
// }
$("#saveBtn").click(function () {
var pid = $("#catory").val()
var title = $("#lable").val()
@ -66,12 +54,16 @@
title: title
},
function (data) {
debugger
if (!data.Status) {
parent.addNoteCallback(data)
parent.layer.msg("保存成功", {icon: 6});
var index = parent.layer.getFrameIndex(window.name);
parent.layer.close(index);
} else if (data.Status = 401) {
parent.location.href = "/login"
parent.layer.close(index);
} else {
layer.msg("服务器异常", {icon: 5});
}
}, 'json')
})

91
views/note.html

@ -48,8 +48,8 @@
<nav>
<a href="/" target="_blank">首页</a>
<a href="javascript:void(0)" id="newNote">新增笔记</a>
<a href="javascript:void(0)" id="newNoteColl">新增文件夹</a>
<a href="javascript:void(0)">删除笔记</a>
<a href="javascript:void(0)" id="newNoteColl">管理文件夹</a>
<a href="javascript:void(0)" id="delNote">删除笔记</a>
<a href="javascript:void(0)">个人中心</a>
<a href="javascript:void(0)">Shortcodes</a>
</nav>
@ -63,7 +63,9 @@
<a href="javascript:void(0)">{{.Title}}</a>
<ul class="sub-menu" value="{{.Id}}">
{{range .Notes}}
<li><a href="javascript:void(0)" onclick="noteClick({{.Id}})">{{.Id}}-{{.Title}}</a></li>
<li>
<a href="javascript:void(0)" onclick="noteClick({{.Id}})" value="{{.Id}}">{{.Id}}-{{.Title}}</a>
</li>
{{end}}
</ul>
</li>
@ -98,21 +100,47 @@
}
function saveNote() {
$.post('/note/save',
if (!note || !note.Id) {
layer.msg('请在右边菜单中选择笔记', function () {
});
return
}
var noteHtml = editor.txt.html()
if (editor.txt.text() && editor.txt.text().trim().length != 0) {
layer.msg('保存内容为空', function () {
});
return
}
$.post('/note/edit/' + note.Id, {
noteHtml: noteHtml
},
function (data) {
if (!data.Status) {
editor.txt.html(data.NoteHtml);
note = data
if (data.Status == 0) {
layer.msg("保存成功", {icon: 6});
} else if (data.Status == 401) {
window.location.href = "/login"
} else {
layer.msg("服务器异常", {icon: 5});
}
}, 'json')
}
function noteClick(id) {
$.get('/note/'+id,
$.get('/note/' + id,
function (data) {
if (!data.Status) {
editor.txt.html(data.NoteHtml);
if (data.NoteHtml && data.NoteHtml.trim().length != 0) {
editor.txt.html(data.NoteHtml);
} else if (note) {
editor.txt.html("");
}
note = data
} else if (data.Status == 401) {
window.location.href = "/login"
} else if (data.Status == 403) {
layer.msg("暂无权限", {icon: 5});
} else {
layer.msg("服务器异常", {icon: 5});
}
}, 'json')
@ -121,9 +149,12 @@
function addNoteCallback(obj) {
var subMenu = $(".sub-menu")
$.each(subMenu, function (index, el) {
if ($(this).attr("value") == obj.Pid) {
var child = '<li><a href="javascript:void(0)" ' + obj.Id + '>' + obj.Title + '</a></li>'
$(".sub-menu").append(child)
if ($(el).attr("value") == obj.Pid) {
var child = '<li><a href="javascript:void(0)" onclick="noteClick(' + obj.Id + ')" value="' + obj.Id + '" >' + obj.Title + '</a></li>'
$(el).append(child)
if (!note || !note.Id) {
note = obj
}
return
}
})
@ -147,6 +178,42 @@
content: '/iframe/note.html' //iframe的url
});
})
$("#delNote").click(function () {
if (!note || !note.Id) {
layer.msg('请在右边菜单中选择笔记', function () {
});
return
}
layer.confirm('删除确认', {
btn: ['确认', '手滑了'] //按钮
}, function () {
$.post('/note/del/' + note.Id,
function (data) {
debugger
if (data.Status == 0) {
var subMenu = $(".sub-menu li a")
$.each(subMenu, function (index, el) {
if ($(el).attr("value") == note.Id) {
$(el).remove()
return
}
})
note = null
layer.msg("删除成功", {icon: 6});
} else if (data.Status == 401) {
window.location.href = "/login"
} else if (data.Status == 403) {
layer.msg("暂无权限", {icon: 5});
} else {
layer.msg("服务器异常", {icon: 5});
}
}, 'json')
}, function () {
});
})
$("#newNoteColl").click(function () {
layer.prompt({title: '请输入文件夹名称'}, function (val, index) {
if (val.trim().length < 3) {

Loading…
Cancel
Save