From 9d233861caec5b5ea0e1f0e139909ffaa10d3fdb Mon Sep 17 00:00:00 2001 From: Minho Date: Fri, 12 May 2017 19:14:29 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=86=99=E9=99=84=E4=BB=B6=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/app.conf.example | 20 +++ controllers/document.go | 60 +++++++- models/attachment.go | 68 +++++---- models/document.go | 1 + routers/router.go | 1 + static/css/markdown.css | 100 ++++++++++++- static/js/editor.js | 17 +++ static/js/html-editor.js | 7 +- static/wangEditor/plugins/attach-menu.js | 47 ++++++ views/document/html_edit_template.tpl | 174 +++++++++++++++++++++-- views/manager/setting.tpl | 19 +-- views/widgets/header.tpl | 38 +++-- 12 files changed, 489 insertions(+), 63 deletions(-) create mode 100644 static/wangEditor/plugins/attach-menu.js diff --git a/conf/app.conf.example b/conf/app.conf.example index b2c64af8..d7643149 100644 --- a/conf/app.conf.example +++ b/conf/app.conf.example @@ -72,3 +72,23 @@ cdnjs= cdncss= cdnimg= +################百度地图密钥################# +baidumapkey= + + + + + + + + + + + + + + + + + + diff --git a/controllers/document.go b/controllers/document.go index 0a592ca9..b2107115 100644 --- a/controllers/document.go +++ b/controllers/document.go @@ -229,7 +229,6 @@ func (c *DocumentController) Edit() { }else{ c.TplName = "document/" + bookResult.Editor + "_edit_template.tpl" } - beego.Info(bookResult) c.Data["Model"] = bookResult @@ -252,6 +251,8 @@ func (c *DocumentController) Edit() { c.Data["Result"] = template.JS("[]") } } + c.Data["BaiDuMapKey"] = beego.AppConfig.DefaultString("baidumapkey","") + } @@ -329,6 +330,7 @@ func (c *DocumentController) Upload() { identify := c.GetString("identify") doc_id,_ := c.GetInt("doc_id") + is_attach := true if identify == "" { c.JsonResult(6001,"参数错误") @@ -397,7 +399,7 @@ func (c *DocumentController) Upload() { } } - fileName := "attachment_" + strconv.FormatInt(time.Now().UnixNano(), 16) + fileName := "attach_" + strconv.FormatInt(time.Now().UnixNano(), 16) filePath := "uploads/" + time.Now().Format("200601") + "/" + fileName + ext @@ -417,12 +419,18 @@ func (c *DocumentController) Upload() { attachment.CreateAt = c.Member.MemberId attachment.FileExt = ext attachment.FilePath = filePath + attachment.DocumentId = doc_id + + if fileInfo, err := os.Stat(filePath); err == nil { + attachment.FileSize = float64(fileInfo.Size()) + } if doc_id > 0{ attachment.DocumentId = doc_id } if strings.EqualFold(ext,".jpg") || strings.EqualFold(ext,".jpeg") || strings.EqualFold(ext,"png") || strings.EqualFold(ext,"gif") { - attachment.HttpPath = c.BaseUrl() + "/" + filePath + attachment.HttpPath = "/" + filePath + is_attach = false } err = attachment.Insert(); @@ -433,7 +441,7 @@ func (c *DocumentController) Upload() { c.JsonResult(6006,"文件保存失败") } if attachment.HttpPath == "" { - attachment.HttpPath = c.BaseUrl() + beego.URLFor("DocumentController.DownloadAttachment",":key", identify, ":attach_id", attachment.AttachmentId) + attachment.HttpPath = beego.URLFor("DocumentController.DownloadAttachment",":key", identify, ":attach_id", attachment.AttachmentId) if err := attachment.Update();err != nil { beego.Error("SaveToFile => ",err) @@ -447,6 +455,8 @@ func (c *DocumentController) Upload() { "message" :"ok", "url" : attachment.HttpPath, "alt" : attachment.FileName, + "is_attach" : is_attach, + "attach" : attachment, } c.Data["json"] = result @@ -509,6 +519,44 @@ func (c *DocumentController) DownloadAttachment() { c.StopRun() } +//删除附件. +func (c *DocumentController) RemoveAttachment() { + c.Prepare() + attach_id ,_ := c.GetInt("attach_id") + + if attach_id <= 0 { + c.JsonResult(6001,"参数错误") + } + attach,err := models.NewAttachment().Find(attach_id) + + if err != nil { + beego.Error(err) + c.JsonResult(6002,"附件不存在") + } + document,err := models.NewDocument().Find(attach.DocumentId) + + if err != nil { + beego.Error(err) + c.JsonResult(6003,"文档不存在") + } + if c.Member.Role != conf.MemberSuperRole { + rel,err := models.NewRelationship().FindByBookIdAndMemberId(document.BookId,c.Member.MemberId) + if err != nil { + beego.Error(err) + c.JsonResult(6004,"权限不足") + } + if rel.RoleId == conf.BookObserver { + c.JsonResult(6004,"权限不足") + } + } + err = attach.Delete() + + if err != nil { + beego.Error(err) + c.JsonResult(6005,"删除失败") + } + c.JsonResult(0,"ok",attach) +} //删除文档. func (c *DocumentController) Delete() { c.Prepare() @@ -628,6 +676,10 @@ func (c *DocumentController) Content() { if err != nil { c.JsonResult(6003,"文档不存在") } + attach,err := models.NewAttachment().FindListByDocumentId(doc.DocumentId) + if err == nil { + doc.AttachList = attach + } c.JsonResult(0,"ok",doc) } diff --git a/models/attachment.go b/models/attachment.go index 69a6e568..7f962234 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -2,29 +2,32 @@ package models import ( "time" - "github.com/lifei6671/godoc/conf" + "github.com/astaxie/beego/orm" + "github.com/lifei6671/godoc/conf" + "os" + "github.com/astaxie/beego" ) // Attachment struct . type Attachment struct { - AttachmentId int `orm:"column(attachment_id);pk;auto;unique" json:"attachment_id"` - BookId int `orm:"column(book_id);type(int)" json:"book_id"` - DocumentId int `orm:"column(document_id);type(int);null" json:"doc_id"` - FileName string `orm:"column(file_name);size(255)" json:"file_name"` - FilePath string `orm:"column(file_path);size(2000)" json:"file_path"` - FileSize float64 `orm:"column(file_size);type(float)" json:"file_size"` - HttpPath string `orm:"column(http_path);size(2000)" json:"http_path"` - FileExt string `orm:"column(file_ext);size(50)" json:"file_ext"` - CreateTime time.Time `orm:"type(datetime);column(create_time);auto_now_add"` - CreateAt int `orm:"column(create_at);type(int)" json:"create_at"` + AttachmentId int `orm:"column(attachment_id);pk;auto;unique" json:"attachment_id"` + BookId int `orm:"column(book_id);type(int)" json:"book_id"` + DocumentId int `orm:"column(document_id);type(int);null" json:"doc_id"` + FileName string `orm:"column(file_name);size(255)" json:"file_name"` + FilePath string `orm:"column(file_path);size(2000)" json:"file_path"` + FileSize float64 `orm:"column(file_size);type(float)" json:"file_size"` + HttpPath string `orm:"column(http_path);size(2000)" json:"http_path"` + FileExt string `orm:"column(file_ext);size(50)" json:"file_ext"` + CreateTime time.Time `orm:"type(datetime);column(create_time);auto_now_add" json:"create_time"` + CreateAt int `orm:"column(create_at);type(int)" json:"create_at"` } - // TableName 获取对应数据库表名. func (m *Attachment) TableName() string { return "attachment" } + // TableEngine 获取数据使用的引擎. func (m *Attachment) TableEngine() string { return "INNODB" @@ -33,38 +36,51 @@ func (m *Attachment) TableNameWithPrefix() string { return conf.GetDatabasePrefix() + m.TableName() } -func NewAttachment() *Attachment { +func NewAttachment() *Attachment { return &Attachment{} } -func (m *Attachment) Insert() error { +func (m *Attachment) Insert() error { o := orm.NewOrm() - _,err := o.Insert(m) + _, err := o.Insert(m) return err } func (m *Attachment) Update() error { o := orm.NewOrm() - _,err := o.Update(m) + _, err := o.Update(m) return err } -func (m *Attachment) Find(id int) (*Attachment,error) { +func (m *Attachment) Delete() error { + o := orm.NewOrm() + + _,err := o.Delete(m) + + if err == nil { + if err1 := os.Remove(m.FilePath); err1 != nil { + beego.Error(err1) + } + } + + return err +} + +func (m *Attachment) Find(id int) (*Attachment, error) { if id <= 0 { - return m,ErrInvalidParameter + return m, ErrInvalidParameter } o := orm.NewOrm() - err := o.QueryTable(m.TableNameWithPrefix()).Filter("attachment_id",id).One(m) + err := o.QueryTable(m.TableNameWithPrefix()).Filter("attachment_id", id).One(m) - return m,err + return m, err } +func (m *Attachment) FindListByDocumentId(doc_id int) (attaches []*Attachment, err error) { + o := orm.NewOrm() - - - - - - + _,err = o.QueryTable(m.TableNameWithPrefix()).Filter("document_id",doc_id).OrderBy("-attachment_id").All(&attaches) + return +} diff --git a/models/document.go b/models/document.go index 392f9f37..271c2f5c 100644 --- a/models/document.go +++ b/models/document.go @@ -28,6 +28,7 @@ type Document struct { ModifyTime time.Time `orm:"column(modify_time);type(datetime);auto_now" json:"modify_time"` ModifyAt int `orm:"column(modify_at);type(int)" json:"-"` Version int64 `orm:"type(bigint);column(version)" json:"version"` + AttachList []*Attachment `orm:"-" json:"attach"` } diff --git a/routers/router.go b/routers/router.go index d306a849..588b0587 100644 --- a/routers/router.go +++ b/routers/router.go @@ -51,6 +51,7 @@ func init() { beego.Router("/book/setting/token", &controllers.BookController{},"post:CreateToken") beego.Router("/book/setting/delete", &controllers.BookController{},"post:Delete") + beego.Router("/api/attach/remove/", &controllers.DocumentController{},"post:RemoveAttachment") beego.Router("/api/:key/edit/?:id", &controllers.DocumentController{},"*:Edit") beego.Router("/api/upload",&controllers.DocumentController{},"post:Upload") beego.Router("/api/:key/create",&controllers.DocumentController{},"post:Create") diff --git a/static/css/markdown.css b/static/css/markdown.css index 6797e8cf..e8365547 100644 --- a/static/css/markdown.css +++ b/static/css/markdown.css @@ -195,8 +195,6 @@ body{ font-style: normal; } - - .manual-editor-status{ position: absolute; left: 0; @@ -208,4 +206,100 @@ body{ color: #555; background-color: #FAFAFA; z-index: 1000; -} \ No newline at end of file + line-height: 30px; +} +.manual-editor-status .item{ + display: inline-block; + margin-right: 10px; + margin-left: 10px; + cursor: pointer; +} +.attach-drop-panel{ + display: block; + position: relative; + width: 100%; + height: 100%; +} +.attach-drop-panel .webuploader-element-invisible{ + width: 500px; + height: 100px; + position: absolute; + top: 0; + +} +.attach-drop-panel .webuploader-pick{ + color: #ccc; + text-align: center; + margin: 20px 20px 15px!important; + padding: 5px!important; + font-size: 65px; + cursor: pointer; + border: 2px dotted #999; + display: block!important; + background: #ffffff; +} +.attach-drop-panel .webuploader-pick:hover{ + color: #333; + border-color: #333; +} +.attach-list{ + + background:#ffffff; + font-size: 12px; +} +.attach-list .attach-item{ + padding: 8px 10px; + line-height: 36px; + border: 1px solid #ddd; + border-bottom: none; + border-top-left-radius:3px; + border-top-right-radius:3px; +} +.attach-list .attach-item:last-child{ + border-bottom: 1px solid #ddd; + border-bottom-left-radius:3px; + border-bottom-right-radius:3px; +} +.attach-list .attach-item .progress{ + margin-top: 10px; + margin-bottom: 10px; + height: 10px; +} +.attach-list .attach-item .form-control{ + width: 60%; + float: left; +} + +.attach-list .attach-item .text{ + display: inline-block; + padding: 0 15px; +} +.attach-list .attach-item .close{ + float: right; + display: inline-block; + padding: 8px 0; + color: #586069; + background: #ffffff; + font-size: 16px; +} +.attach-list .attach-item .close:hover { + color: #333; +} + + + + + + + + + + + + + + + + + + diff --git a/static/js/editor.js b/static/js/editor.js index cf672c46..3a19fc9c 100644 --- a/static/js/editor.js +++ b/static/js/editor.js @@ -140,6 +140,23 @@ function pushDocumentCategory($node) { } window.documentCategory.push($node); } +/** + * 将数据重置到Vue列表中 + * @param $lists + */ +function pushVueLists($lists) { + for(var i in window.vueApp.lists){ + var item = window.vueApp.lists[i]; + window.vueApp.lists.$remove(item); + } + + for(var j in $lists){ + var item = $lists[j]; + window.vueApp.lists.push(item); + } + console.log(window.vueApp.lists) + $("#attachInfo").text(" " + window.vueApp.lists.length + " 个附件") +} //实现小提示 $("[data-toggle='tooltip']").hover(function () { diff --git a/static/js/html-editor.js b/static/js/html-editor.js index 939f643f..535da4c8 100644 --- a/static/js/html-editor.js +++ b/static/js/html-editor.js @@ -1,16 +1,17 @@ $(function () { - + wangEditor.config.mapAk = window.baiduMapKey; window.addDocumentModalFormHtml = $(this).find("form").html(); wangEditor.config.printLog = false; window.editor = new wangEditor('htmlEditor'); editor.config.uploadImgUrl = window.imageUploadURL; - editor.config.uploadImgFileName = "editormd-file-file"; + editor.config.uploadImgFileName = "editormd-image-file"; editor.config.uploadParams = { "editor" : "wangEditor" }; wangEditor.config.menus.splice(0,0,"|"); wangEditor.config.menus.splice(0,0,"save"); wangEditor.config.menus.splice(0,0,"release"); + wangEditor.config.menus.splice(29,0,"attach") //移除地图、背景色 editor.config.menus = $.map(wangEditor.config.menus, function(item, key) { @@ -81,6 +82,8 @@ $(function () { pushDocumentCategory(node); window.selectNode = node; + pushVueLists(res.data.attach); + }else{ layer.msg("文档加载失败"); } diff --git a/static/wangEditor/plugins/attach-menu.js b/static/wangEditor/plugins/attach-menu.js new file mode 100644 index 00000000..6be0fef8 --- /dev/null +++ b/static/wangEditor/plugins/attach-menu.js @@ -0,0 +1,47 @@ +(function () { + + // 获取 wangEditor 构造函数和 jquery + var E = window.wangEditor; + var $ = window.jQuery; + + // 用 createMenu 方法创建菜单 + E.createMenu(function (check) { + + // 定义菜单id,不要和其他菜单id重复。编辑器自带的所有菜单id,可通过『参数配置-自定义菜单』一节查看 + var menuId = 'attach'; + + // check将检查菜单配置(『参数配置-自定义菜单』一节描述)中是否该菜单id,如果没有,则忽略下面的代码。 + if (!check(menuId)) { + return; + } + + // this 指向 editor 对象自身 + var editor = this; + + // 创建 menu 对象 + var menu = new E.Menu({ + editor: editor, // 编辑器对象 + id: menuId, // 菜单id + title: '附件', // 菜单标题 + + // 正常状态和选中状态下的dom对象,样式需要自定义 + $domNormal: $(''), + $domSelected: $('') + }); + + + // 菜单正常状态下,点击将触发该事件 + menu.clickEvent = function (e) { + $("#uploadAttachModal").modal("show"); + }; + + + // 菜单选中状态下,点击将触发该事件 + menu.clickEventSelected = function (e) { + + }; + // 增加到editor对象中 + editor.menus[menuId] = menu; + }); + +})(); \ No newline at end of file diff --git a/views/document/html_edit_template.tpl b/views/document/html_edit_template.tpl index f8ffcab0..bd40b66b 100644 --- a/views/document/html_edit_template.tpl +++ b/views/document/html_edit_template.tpl @@ -6,6 +6,7 @@ 编辑文档 - Powered by MinDoc + - - - - - + + + + + + + @@ -36,7 +42,7 @@
- {{/*
+
@@ -78,7 +84,7 @@
- +
@@ -121,16 +127,168 @@
+ + + + + + + \ No newline at end of file diff --git a/views/manager/setting.tpl b/views/manager/setting.tpl index 724d2f88..8c072eca 100644 --- a/views/manager/setting.tpl +++ b/views/manager/setting.tpl @@ -8,8 +8,8 @@ 配置管理 - Powered by MinDoc - - + + @@ -77,10 +77,11 @@
-
- - -
+ +
+ + +
@@ -93,9 +94,9 @@ - - - + + +