Skip to content

Commit

Permalink
add tag export and import feature
Browse files Browse the repository at this point in the history
  • Loading branch information
eddycjy committed Jun 12, 2018
1 parent 8c92d44 commit 9aa3ebf
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 10 deletions.
2 changes: 2 additions & 0 deletions conf/app.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ ImageSavePath = upload/images/
ImageMaxSize = 5
ImageAllowExts = .jpg,.jpeg,.png

ExportSavePath = export/

LogSavePath = logs/
LogSaveName = log
LogFileExt = log
Expand Down
10 changes: 8 additions & 2 deletions models/tag.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package models

import "github.com/jinzhu/gorm"
import (
"github.com/jinzhu/gorm"
)

type Tag struct {
Model
Expand Down Expand Up @@ -40,7 +42,11 @@ func AddTag(name string, state int, createdBy string) error {

func GetTags(pageNum int, pageSize int, maps interface{}) ([]Tag, error) {
var tags []Tag
err := db.Where(maps).Offset(pageNum).Limit(pageSize).Find(&tags).Error
if pageSize > 0 && pageNum > 0{
db = db.Offset(pageNum).Limit(pageSize)
}

err := db.Where(maps).Find(&tags).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
Expand Down
18 changes: 10 additions & 8 deletions pkg/e/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ const (
ERROR_ADD_TAG_FAIL = 10006
ERROR_EDIT_TAG_FAIL = 10007
ERROR_DELETE_TAG_FAIL = 10008
ERROR_EXPORT_TAG_FAIL = 10009
ERROR_IMPORT_TAG_FAIL = 10010

ERROR_NOT_EXIST_ARTICLE = 10009
ERROR_CHECK_EXIST_ARTICLE_FAIL = 10010
ERROR_ADD_ARTICLE_FAIL = 10011
ERROR_DELETE_ARTICLE_FAIL = 10012
ERROR_EDIT_ARTICLE_FAIL = 10013
ERROR_COUNT_ARTICLE_FAIL = 10014
ERROR_GET_ARTICLES_FAIL = 10015
ERROR_GET_ARTICLE_FAIL = 10016
ERROR_NOT_EXIST_ARTICLE = 10011
ERROR_CHECK_EXIST_ARTICLE_FAIL = 10012
ERROR_ADD_ARTICLE_FAIL = 10013
ERROR_DELETE_ARTICLE_FAIL = 10014
ERROR_EDIT_ARTICLE_FAIL = 10015
ERROR_COUNT_ARTICLE_FAIL = 10016
ERROR_GET_ARTICLES_FAIL = 10017
ERROR_GET_ARTICLE_FAIL = 10018

ERROR_AUTH_CHECK_TOKEN_FAIL = 20001
ERROR_AUTH_CHECK_TOKEN_TIMEOUT = 20002
Expand Down
2 changes: 2 additions & 0 deletions pkg/e/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ var MsgFlags = map[int]string{
ERROR_ADD_TAG_FAIL: "新增标签失败",
ERROR_EDIT_TAG_FAIL: "修改标签失败",
ERROR_DELETE_TAG_FAIL: "删除标签失败",
ERROR_EXPORT_TAG_FAIL: "导出标签失败",
ERROR_IMPORT_TAG_FAIL: "导入标签失败",
ERROR_NOT_EXIST_ARTICLE: "该文章不存在",
ERROR_ADD_ARTICLE_FAIL: "新增文章失败",
ERROR_DELETE_ARTICLE_FAIL: "删除文章失败",
Expand Down
15 changes: 15 additions & 0 deletions pkg/export/excel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package export

import "github.com/EDDYCJY/go-gin-example/pkg/setting"

func GetExcelFullUrl(name string) string {
return setting.AppSetting.ImagePrefixUrl + "/" + GetExcelPath() + name
}

func GetExcelPath() string {
return setting.AppSetting.ExportSavePath
}

func GetExcelFullPath() string {
return setting.AppSetting.RuntimeRootPath + GetExcelPath()
}
2 changes: 2 additions & 0 deletions pkg/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type App struct {
ImageMaxSize int
ImageAllowExts []string

ExportSavePath string

LogSavePath string
LogSaveName string
LogFileExt string
Expand Down
5 changes: 5 additions & 0 deletions routers/api/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
"github.com/EDDYCJY/go-gin-example/pkg/upload"
)

// @Summary 上传图片
// @Produce json
// @Param image post file true "图片文件"
// @Success 200 {string} json "{"code":200,"data":{"image_save_url":"upload/images/96a.jpg", "image_url": "http://..."}"
// @Router /api/v1/tags/import [post]
func UploadImage(c *gin.Context) {
appG := app.Gin{C: c}
file, image, err := c.Request.FormFile("image")
Expand Down
59 changes: 59 additions & 0 deletions routers/api/v1/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/EDDYCJY/go-gin-example/pkg/setting"
"github.com/EDDYCJY/go-gin-example/pkg/util"
"github.com/EDDYCJY/go-gin-example/service/tag_service"
"github.com/EDDYCJY/go-gin-example/pkg/export"
"github.com/EDDYCJY/go-gin-example/pkg/logging"
)

// @Summary 获取多个文章标签
Expand Down Expand Up @@ -198,3 +200,60 @@ func DeleteTag(c *gin.Context) {

appG.Response(http.StatusOK, e.SUCCESS, nil)
}

// @Summary 导出文章标签
// @Produce json
// @Param name post string false "Name"
// @Param state post int false "State"
// @Success 200 {string} json "{"code":200,"data":{"export_save_url":"export/abc.xlsx", "export_url": "http://..."},"msg":"ok"}"
// @Router /api/v1/tags/export [post]
func ExportTag(c *gin.Context) {
appG := app.Gin{C: c}
name := c.PostForm("name")
state := -1
if arg := c.PostForm("state"); arg != "" {
state = com.StrTo(arg).MustInt()
}

tagService := tag_service.Tag{
Name: name,
State: state,
}

filename, err := tagService.Export()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_EXPORT_TAG_FAIL, nil)
return
}

appG.Response(http.StatusOK, e.SUCCESS, map[string]string{
"export_url": export.GetExcelFullUrl(filename),
"export_save_url": export.GetExcelPath() + filename,
})
}

// @Summary 导入文章标签
// @Produce json
// @Param file post file true "标签Excel文件"
// @Success 200 {string} json "{"code":200,"data":null,"msg":"ok"}"
// @Router /api/v1/tags/import [post]
func ImportTag(c *gin.Context) {
appG := app.Gin{C: c}

file, _, err := c.Request.FormFile("file")
if err != nil {
logging.Warn(err)
appG.Response(http.StatusOK, e.ERROR, nil)
return
}

tagService := tag_service.Tag{}
err = tagService.Import(file)
if err != nil {
logging.Warn(err)
appG.Response(http.StatusOK, e.ERROR_IMPORT_TAG_FAIL, nil)
return
}

appG.Response(http.StatusOK, e.SUCCESS, nil)
}
6 changes: 6 additions & 0 deletions routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/EDDYCJY/go-gin-example/pkg/upload"
"github.com/EDDYCJY/go-gin-example/routers/api"
"github.com/EDDYCJY/go-gin-example/routers/api/v1"
"github.com/EDDYCJY/go-gin-example/pkg/export"
)

func InitRouter() *gin.Engine {
Expand All @@ -24,6 +25,7 @@ func InitRouter() *gin.Engine {
r.Use(gin.Recovery())
gin.SetMode(setting.ServerSetting.RunMode)

r.StaticFS("/export", http.Dir(export.GetExcelFullPath()))
r.StaticFS("/upload/images", http.Dir(upload.GetImageFullPath()))

r.GET("/auth", api.GetAuth)
Expand All @@ -41,6 +43,10 @@ func InitRouter() *gin.Engine {
apiv1.PUT("/tags/:id", v1.EditTag)
//删除指定标签
apiv1.DELETE("/tags/:id", v1.DeleteTag)
//导出标签
r.POST("/tags/export", v1.ExportTag)
//导入标签
r.POST("/tags/import", v1.ImportTag)

//获取文章列表
apiv1.GET("/articles", v1.GetArticles)
Expand Down
78 changes: 78 additions & 0 deletions service/tag_service/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ package tag_service

import (
"encoding/json"
"strconv"
"time"

"github.com/tealeg/xlsx"

"github.com/EDDYCJY/go-gin-example/models"
"github.com/EDDYCJY/go-gin-example/pkg/export"
"github.com/EDDYCJY/go-gin-example/pkg/gredis"
"github.com/EDDYCJY/go-gin-example/pkg/logging"
"github.com/EDDYCJY/go-gin-example/service/cache_service"
"io"
"github.com/360EntSecGroup-Skylar/excelize"
)

type Tag struct {
Expand Down Expand Up @@ -82,6 +89,77 @@ func (t *Tag) GetAll() ([]models.Tag, error) {
return tags, nil
}

func (t *Tag) Export() (string, error) {
tags, err := t.GetAll()
if err != nil {
return "", err
}

file := xlsx.NewFile()
sheet, err := file.AddSheet("标签信息")
if err != nil {
return "", err
}

titles := []string{"ID", "名称", "创建人", "创建时间", "修改人", "修改时间"}
row := sheet.AddRow()

var cell *xlsx.Cell
for _, title := range titles {
cell = row.AddCell()
cell.Value = title
}

for _, v := range tags {
values := []string{
strconv.Itoa(v.ID),
v.Name,
v.CreatedBy,
strconv.Itoa(v.CreatedOn),
v.ModifiedBy,
strconv.Itoa(v.ModifiedOn),
}

row = sheet.AddRow()
for _, value := range values {
cell = row.AddCell()
cell.Value = value
}
}

time := strconv.Itoa(int(time.Now().Unix()))
filename := "tags-" + time + ".xlsx"

fullPath := export.GetExcelFullPath() + filename
err = file.Save(fullPath)
if err != nil {
return "", err
}

return filename, nil
}

func (t *Tag) Import(r io.Reader) error {
xlsx, err := excelize.OpenReader(r)
if err != nil {
return err
}

rows := xlsx.GetRows("标签信息")
for irow, row := range rows {
if irow > 0 {
var data []string
for _, cell := range row {
data = append(data, cell)
}

models.AddTag(data[1], 1, data[2])
}
}

return nil
}

func (t *Tag) getMaps() map[string]interface{} {
maps := make(map[string]interface{})
maps["deleted_on"] = 0
Expand Down

0 comments on commit 9aa3ebf

Please sign in to comment.