Parcourir la source

笔记菜单后台渲染

layui
mail_yanpeng@163.com il y a 6 ans
Parent
révision
58b58aa201
  1. 2
      controllers/BlogController.go
  2. 32
      controllers/NoteController.go
  3. 24
      controllers/UserController.go
  4. 7
      models/Result.go
  5. 1
      routers/NoteRouter.go
  6. 7
      service/NoteService.go
  7. 1
      views/login.html
  8. 45
      views/note.html

2
controllers/BlogController.go

@ -22,7 +22,7 @@ func (this *BlogController) Save() {
if err == nil {
this.Data["json"] = blog
} else {
this.Data["json"] = models.ReurnError("保存失败")
this.Data["json"] = models.ReurnError(500,"保存失败")
}
this.ServeJSON()
}

32
controllers/NoteController.go

@ -23,7 +23,30 @@ func (this *NoteController) Save() {
if err == nil {
this.Data["json"] = note
} else {
this.Data["json"] = models.ReurnError("保存失败")
this.Data["json"] = models.ReurnError(500,"保存失败")
}
this.ServeJSON()
}
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)
note := &models.Note{Id:id}
err1 := service.GetNote(note)
if err1 != nil {
fmt.Print(err1)
this.Data["json"] = models.ReurnError(500,"保存失败")
}
if uid != note.UserId {
this.Data["json"] = models.ReurnError(403,"保存失败")
}
note.NoteHtml = noteHtml
err := service.SaveNote(note)
if err == nil {
this.Data["json"] = note
} else {
this.Data["json"] = models.ReurnError(500,"保存失败")
}
this.ServeJSON()
}
@ -37,7 +60,7 @@ func (this *NoteController) SaveNoteColl() {
if err == nil {
this.Data["json"] = note
} else {
this.Data["json"] = models.ReurnError("保存失败")
this.Data["json"] = models.ReurnError(500,"保存失败")
}
this.ServeJSON()
}
@ -60,11 +83,11 @@ func (this *NoteController) Note() {
this.Redirect("/login", 302)
}
noteColls, err := service.GetNoteColl(uid.(int64))
if err != nil {
if err == nil {
if len(noteColls) > 0 {
for i := 0; i < len(noteColls); i++ {
notes, err1 := service.GetNoteByPid(noteColls[i].Id)
if err1 != nil {
if err1 == nil {
noteColls[i].Notes = notes
}
}
@ -73,6 +96,7 @@ func (this *NoteController) Note() {
noteColls = make([]*models.NoteColl, 0)
}
fmt.Println(noteColls)
fmt.Println(len(noteColls))
this.Data["NoteColls"] = noteColls
this.TplName = "note.html"
}

24
controllers/UserController.go

@ -37,19 +37,19 @@ func (this *UserController) Login() {
username := this.GetString("username")
userpwd := this.GetString("userpwd")
if username == "" {
this.Data["json"] = models.ReurnError("用户名为空")
this.Data["json"] = models.ReurnError(1,"用户名为空")
this.ServeJSON()
}
if len(username) < 4 {
this.Data["json"] = models.ReurnError("用户名最低4位")
this.Data["json"] = models.ReurnError(1,"用户名最低4位")
this.ServeJSON()
}
if userpwd == "" {
this.Data["json"] = models.ReurnError("密码为空")
this.Data["json"] = models.ReurnError(1,"密码为空")
this.ServeJSON()
}
if len(userpwd) < 6 {
this.Data["json"] = models.ReurnError("密码最低6位")
this.Data["json"] = models.ReurnError(1,"密码最低6位")
this.ServeJSON()
}
user, error := service.FindByUserName(username)
@ -62,10 +62,10 @@ func (this *UserController) Login() {
this.SetSession("userid", user.Id)
fmt.Println(this.CruSession)
} else {
this.Data["json"] = models.ReurnError("用户名或密码错误")
this.Data["json"] = models.ReurnError(1,"用户名或密码错误")
}
} else {
this.Data["json"] = models.ReurnError("用户名不存在")
this.Data["json"] = models.ReurnError(1,"用户名不存在")
}
this.ServeJSON()
}
@ -76,24 +76,24 @@ func (this *UserController) Regist() {
username = strings.Replace(username, " ", "", -1)
userpwd = strings.Replace(userpwd, " ", "", -1)
if username == "" {
this.Data["json"] = models.ReurnError("用户名为空")
this.Data["json"] = models.ReurnError(1,"用户名为空")
this.ServeJSON()
}
if len(username) < 4 {
this.Data["json"] = models.ReurnError("用户名最低4位")
this.Data["json"] = models.ReurnError(1,"用户名最低4位")
this.ServeJSON()
}
if userpwd == "" {
this.Data["json"] = models.ReurnError("密码为空")
this.Data["json"] = models.ReurnError(1,"密码为空")
this.ServeJSON()
}
if len(userpwd) < 6 {
this.Data["json"] = models.ReurnError("密码最低6位")
this.Data["json"] = models.ReurnError(1,"密码最低6位")
this.ServeJSON()
}
user, _ := service.FindByUserName(username)
if user != nil {
this.Data["json"] = models.ReurnError("用户已经存在")
this.Data["json"] = models.ReurnError(1,"用户已经存在")
this.ServeJSON()
}
h := md5.New()
@ -107,7 +107,7 @@ func (this *UserController) Regist() {
if err == nil {
this.Data["json"] = models.ReurnSuccess("")
} else {
this.Data["json"] = models.ReurnError("注册失败")
this.Data["json"] = models.ReurnError(1,"注册失败")
}
this.ServeJSON()
}

7
models/Result.go

@ -5,8 +5,11 @@ type Error struct {
Msg string
}
func ReurnError(msg string) *Error {
return &Error{Status: byte(1), Msg: msg}
func ReurnError(status int,msg string) *Error {
if msg == "" {
msg = "error"
}
return &Error{Status: byte(status), Msg: msg}
}
type Success struct {

1
routers/NoteRouter.go

@ -10,4 +10,5 @@ func init() {
beego.Router("/note", &controllers.NoteController{}, "get:Note")
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")
}

7
service/NoteService.go

@ -27,10 +27,9 @@ func GetNoteByPid(pid int64) ([]*models.Note, error) {
o := orm.NewOrm()
qs := o.QueryTable(models.Note{})
_, err := qs.Filter("Pid", pid).All(&notes)
return notes,err
return notes, err
}
func SaveNoteColl(note *models.NoteColl) error {
o := orm.NewOrm()
id, err := o.Insert(note)
@ -45,5 +44,5 @@ func GetNoteColl(uid int64) ([]*models.NoteColl, error) {
o := orm.NewOrm()
qs := o.QueryTable(models.NoteColl{})
_, err := qs.Filter("UserId", uid).All(&notes)
return notes,err
}
return notes, err
}

1
views/login.html

@ -53,6 +53,7 @@
layer.msg(data.Msg, {icon: 5});
}else{
layer.msg("登录成功", {icon: 6});
window.location.href = "/"
}
}, 'json')
})

45
views/note.html

@ -58,25 +58,16 @@
<nav id="cd-lateral-nav">
<ul class="cd-navigation">
{{range .NoteColls}}
<li class="item-has-children">
<a href="javascript:void(0)">Services</a>
<ul class="sub-menu">
<li><a href="javascript:void(0)" onclick="noteClick(1)">Brand</a></li>
<li><a href="javascript:void(0)">Web Apps</a></li>
<li><a href="javascript:void(0)">Mobile Apps</a></li>
</ul>
</li> <!-- item-has-children -->
<li class="item-has-children">
<a href="javascript:void(0)">Products</a>
<ul class="sub-menu">
<li><a href="javascript:void(0)">Product 1</a></li>
<li><a href="javascript:void(0)">Product 2</a></li>
<li><a href="javascript:void(0)">Product 3</a></li>
<li><a href="javascript:void(0)">Product 4</a></li>
<li><a href="javascript:void(0)">Product 5</a></li>
<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>
{{end}}
</ul>
</li>
{{end}}
</ul>
</nav>
@ -86,6 +77,7 @@
<script>
var E = window.wangEditor
var editor = new E('#editor')
var note = null
editor.customConfig.uploadImgServer = '/upload'
// 或者 var editor = new E( document.getElementById('editor') )
editor.create()
@ -99,13 +91,31 @@
currKey = e.keyCode || e.which || e.charCode;
if (currKey == 83 && (e.ctrlKey || e.metaKey)) {
layer.msg("ctrl+s")
saveNote()
return false;
}
}
function saveNote() {
$.post('/note/save',
function (data) {
if (!data.Status) {
editor.txt.html(data.NoteHtml);
note = data
}
}, 'json')
}
function noteClick(id) {
layer.msg(id)
$.get('/note/'+id,
function (data) {
if (!data.Status) {
editor.txt.html(data.NoteHtml);
note = data
}
}, 'json')
}
function addNoteCallback(obj) {
@ -149,7 +159,6 @@
title: val.trim()
},
function (data) {
debugger
if (!data.Status) {
parent.layer.msg("保存成功", {icon: 6});
var child = '<li class="item-has-children">\n' +

Chargement…
Annuler
Enregistrer