本帖最后由 3xxx 于 2018-4-15 20:29 编辑
用golang语言开发的文档管理界面:永久开源,免费使用。 中文界面的ONLYOFFICE进行word在线协作效果: powerpoint在线协作的效果: excel表格的在线协作效果: 大家按照附件中的步骤部署一个在局域网,就可以所有人享受协作文档的乐趣了。 5、二次开发EngineerCMS (1)运行前要替换EngineerCMS\views\onlyoffice\onlyoffice.tpl中的3处IP,如果engineercms和docker安装在同一个电脑上,那么三个IP相同,见5.1节。 (2)访问前要按照第6节中的方法设置宿主机(安装docker的电脑)的端口转发。 下文的开发仅供兴趣参考。 5.1 协作编辑页面——汉化界面 EngineerCMS\views\onlyoffice\onlyoffice.tpl - <!DOCTYPE html>
- <html style="height: 100%;">
- <head>
- <title>fei-OnlyOffice</title>
- </head>
- <body style="height: 100%; margin: 0;">
- <div id="placeholder" style="height: 100%"></div>
- <script type="text/javascript" src="http://192.168.99.100:9000/web-apps/apps/api/documents/api.js"></script>
- <script type="text/javascript">
- // alert({{.Doc.FileName}});
- window.docEditor = new DocsAPI.DocEditor("placeholder",
- {
- "document": {
- "fileType": "{{.fileType}}",
- "key": "{{.Key}}",//"Khirz6zTPdfd7"
- "title": "{{.Doc.FileName}}",
- "url": "http://192.168.99.1/attachment/onlyoffice/{{.Doc.FileName}}"
- },
- "documentType": "{{.documentType}}",
- "editorConfig": {
- "callbackUrl": "http://192.168.99.1/url-to-callback?id={{.Doc.Id}}",
- "user": {
- "id": "{{.Uid}}",
- "name": "{{.Uname}}"
- },
- "lang": "zh-CN",//"en-US",
- },
- "height": "100%",
- "width": "100%"
- });
- </script>
- </body>
- </html>
复制代码5.2 协作页面的保存和回调 - package controllers
- import (
- "encoding/json"
- "engineercms/models"
- "github.com/astaxie/beego"
- "io/ioutil"
- "net/http"
- "net/url"
- "os"
- "path"
- "regexp"
- "strconv"
- "strings"
- "time"
- )
- type OnlyController struct {
- beego.Controller
- }
- type Callback struct {
- Key string `json:"key"`
- Status int `json:"status"`
- Url string `json:"url"`
- Changesurl string `json:"changesurl"`
- History history `json:"history"`
- Users []string `json:"users"`
- Actions []action `json:"actions"`
- Lastsave string `json:"lastsave"`
- Notmodified bool `json:"notmodified"`
- }
- type action struct {
- Type int
- userid string
- }
- type history struct {
- changes []change
- serverVersion string
- }
- type change struct {
- created string
- User User1
- }
- type User1 struct {
- id string
- name string
- }
- //协作页面的显示
- func (c *OnlyController) OnlyOffice() {
- id := c.Ctx.Input.Param(":id")
- //pid转成64为
- idNum, err := strconv.ParseInt(id, 10, 64)
- if err != nil {
- beego.Error(err)
- }
- //根据附件id取得附件的prodid,路径
- onlyattachment, err := models.GetOnlyAttachbyId(idNum)
- if err != nil {
- beego.Error(err)
- }
- c.Data["Doc"] = onlyattachment
- c.Data["Key"] = strconv.FormatInt(onlyattachment.Updated.UnixNano(), 10)
- var uname string
- v := c.GetSession("uname")
- if v != nil {
- uname = v.(string)
- c.Data["Uname"] = v.(string)
- user, err := models.GetUserByUsername(uname)
- if err != nil {
- beego.Error(err)
- }
- c.Data["Uid"] = user.Id
- // useridstring = strconv.FormatInt(user.Id, 10)
- } else {
- c.Data["Uname"] = c.Ctx.Input.IP()
- c.Data["Uid"] = c.Ctx.Input.IP()
- }
- if path.Ext(onlyattachment.FileName) == ".docx" || path.Ext(onlyattachment.FileName) == ".DOCX" {
- c.Data["fileType"] = "docx"
- c.Data["documentType"] = "text"
- } else if path.Ext(onlyattachment.FileName) == ".XLSX" || path.Ext(onlyattachment.FileName) == ".xlsx" {
- c.Data["fileType"] = "xlsx"
- c.Data["documentType"] = "spreadsheet"
- } else if path.Ext(onlyattachment.FileName) == ".pptx" || path.Ext(onlyattachment.FileName) == ".PPTX" {
- c.Data["fileType"] = "pptx"
- c.Data["documentType"] = "presentation"
- } else if path.Ext(onlyattachment.FileName) == ".doc" || path.Ext(onlyattachment.FileName) == ".DOC" {
- c.Data["fileType"] = "doc"
- c.Data["documentType"] = "text"
- } else if path.Ext(onlyattachment.FileName) == ".txt" || path.Ext(onlyattachment.FileName) == ".TXT" {
- c.Data["fileType"] = "txt"
- c.Data["documentType"] = "text"
- } else if path.Ext(onlyattachment.FileName) == ".XLS" || path.Ext(onlyattachment.FileName) == ".xls" {
- c.Data["fileType"] = "xls"
- c.Data["documentType"] = "spreadsheet"
- } else if path.Ext(onlyattachment.FileName) == ".csv" || path.Ext(onlyattachment.FileName) == ".CSV" {
- c.Data["fileType"] = "csv"
- c.Data["documentType"] = "spreadsheet"
- } else if path.Ext(onlyattachment.FileName) == ".ppt" || path.Ext(onlyattachment.FileName) == ".PPT" {
- c.Data["fileType"] = "ppt"
- c.Data["documentType"] = "presentation"
- }
- u := c.Ctx.Input.UserAgent()
- matched, err := regexp.MatchString("AppleWebKit.*Mobile.*", u)
- if err != nil {
- beego.Error(err)
- }
- if matched == true {
- // beego.Info("移动端~")
- c.TplName = "onlyoffice/onlyoffice.tpl"
- } else {
- // beego.Info("电脑端!")
- c.TplName = "onlyoffice/onlyoffice.tpl"
- }
- }
- //协作页面的保存和回调
- //关闭浏览器标签后获取最新文档保存到文件夹
- func (c *OnlyController) UrltoCallback() {
- // pk1 := c.Ctx.Input.RequestBody
- id := c.Input().Get("id")
- //pid转成64为
- idNum, err := strconv.ParseInt(id, 10, 64)
- if err != nil {
- beego.Error(err)
- }
- //根据附件id取得附件的prodid,路径
- onlyattachment, err := models.GetOnlyAttachbyId(idNum)
- if err != nil {
- beego.Error(err)
- }
- var callback Callback
- json.Unmarshal(c.Ctx.Input.RequestBody, &callback)
- if callback.Status == 1 || callback.Status == 4 {
- c.Data["json"] = map[string]interface{}{"error": 0}
- c.ServeJSON()
- } else if callback.Status == 2 {
- resp, err := http.Get(callback.Url)
- if err != nil {
- beego.Error(err)
- }
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- beego.Error(err)
- }
- defer resp.Body.Close()
- if err != nil {
- beego.Error(err)
- }
- f, err := os.OpenFile(".\\attachment\\onlyoffice\"+onlyattachment.FileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, os.ModePerm)
- if err != nil {
- beego.Error(err)
- }
- defer f.Close()
- _, err = f.Write(body)
- // _, err = f.WriteString(str)
- // _, err = io.Copy(body, f)
- if err != nil {
- beego.Error(err)
- } else {
- err = models.UpdateOnlyAttachment(idNum)
- if err != nil {
- beego.Error(err)
- }
- }
- c.Data["json"] = map[string]interface{}{"error": 0}
- c.ServeJSON()
- }
- }
复制代码
|