-
Notifications
You must be signed in to change notification settings - Fork 0
/
article.go
311 lines (285 loc) · 7.56 KB
/
article.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package admin
import (
"fmt"
"github.com/astaxie/beego/orm"
"github.com/nfnt/resize"
"github.com/royburns/foodarts/models"
"image/jpeg"
"io"
"os"
"strconv"
"strings"
"time"
)
type ArticleController struct {
baseController
}
//添加
func (this *ArticleController) Add() {
this.Data["posttime"] = this.getTime().Format("2006-01-02 15:04:05")
this.display()
}
//管理
func (this *ArticleController) List() {
var (
page int64
pagesize int64 = 10
status int64
offset int64
list []*models.Post
post models.Post
searchtype string
keyword string
)
searchtype = this.GetString("searchtype")
keyword = this.GetString("keyword")
status, _ = this.GetInt64("status")
if page, _ = this.GetInt64("page"); page < 1 {
page = 1
}
offset = (page - 1) * pagesize
query := post.Query().Filter("status", status)
if keyword != "" {
switch searchtype {
case "title":
query = query.Filter("title__icontains", keyword)
case "author":
query = query.Filter("author__icontains", keyword)
case "tag":
query = query.Filter("tags__icontains", keyword)
}
}
count, _ := query.Count()
if count > 0 {
query.OrderBy("-istop", "-posttime").Limit(pagesize, offset).All(&list)
}
this.Data["searchtype"] = searchtype
this.Data["keyword"] = keyword
this.Data["count_1"], _ = post.Query().Filter("status", 1).Count()
this.Data["count_2"], _ = post.Query().Filter("status", 2).Count()
this.Data["status"] = status
this.Data["list"] = list
this.Data["pagebar"] = models.NewPager(page, count, pagesize, fmt.Sprintf("/admin/article/list?status=%d&searchtype=%s&keyword=%s&page=%s", status, searchtype, keyword, "%d")).ToString()
this.display()
}
//编辑
func (this *ArticleController) Edit() {
id, _ := this.GetInt64("id")
post := models.Post{Id: id}
if post.Read() != nil {
this.Abort("404")
}
post.Tags = strings.Trim(post.Tags, ",")
this.Data["post"] = post
this.Data["posttime"] = post.Posttime.Format("2006-01-02 15:04:05")
this.display()
}
//保存
func (this *ArticleController) Save() {
var (
id int64 = 0
title string = strings.TrimSpace(this.GetString("title"))
content string = this.GetString("content")
tags string = strings.TrimSpace(this.GetString("tags"))
urlname string = strings.TrimSpace(this.GetString("urlname"))
color string = strings.TrimSpace(this.GetString("color"))
cover string = strings.TrimSpace(this.GetString("cover"))
timestr string = strings.TrimSpace(this.GetString("posttime"))
status int64 = 0
istop int8 = 0
urltype int8 = 0
post models.Post
)
if title == "" {
this.showmsg("标题不能为空!")
}
id, _ = this.GetInt64("id")
status, _ = this.GetInt64("status")
if this.GetString("istop") == "1" {
istop = 1
}
if this.GetString("urltype") == "1" {
urltype = 1
}
if status != 1 && status != 2 {
status = 0
}
if cover == "" {
cover = "/static/upload/defaultcover.png"
}
addtags := make([]string, 0)
//标签过滤
if tags != "" {
tagarr := strings.Split(tags, ",")
for _, v := range tagarr {
if tag := strings.TrimSpace(v); tag != "" {
exists := false
for _, vv := range addtags {
if vv == tag {
exists = true
break
}
}
if !exists {
addtags = append(addtags, tag)
}
}
}
}
if id < 1 {
post.Userid = this.userid
post.Author = this.username
post.Posttime = this.getTime()
post.Updated = this.getTime()
post.Insert()
models.Cache.Delete("latestblog")
} else {
post.Id = id
if post.Read() != nil {
goto RD
}
if post.Tags != "" {
var tagobj models.Tag
var tagpostobj models.TagPost
oldtags := strings.Split(strings.Trim(post.Tags, ","), ",")
//标签统计-1
tagobj.Query().Filter("name__in", oldtags).Update(orm.Params{"count": orm.ColValue(orm.Col_Minus, 1)})
//删掉tag_post表的记录
tagpostobj.Query().Filter("postid", post.Id).Delete()
}
}
if len(addtags) > 0 {
for _, v := range addtags {
tag := models.Tag{Name: v}
if tag.Read("Name") == orm.ErrNoRows {
tag.Count = 1
tag.Insert()
} else {
tag.Count += 1
tag.Update("Count")
}
tp := models.TagPost{Tagid: tag.Id, Postid: post.Id, Poststatus: int8(status), Posttime: this.getTime()}
tp.Insert()
}
post.Tags = "," + strings.Join(addtags, ",") + ","
}
if posttime, err := time.Parse("2006-01-02 15:04:05", timestr); err == nil {
post.Posttime = posttime
} else {
post.Posttime, _ = time.Parse("2006-01-02 15:04:05", post.Posttime.Format("2006-01-02 15:04:05"))
}
post.Status = int8(status)
post.Title = title
post.Color = color
post.Istop = istop
post.Cover = cover
post.Content = content
post.Urlname = urlname
post.Urltype = urltype
post.Updated = this.getTime()
post.Update("tags", "status", "title", "color", "cover", "istop", "content", "urlname", "urltype", "updated", "posttime")
RD:
this.Redirect("/admin/article/list", 302)
}
//删除
func (this *ArticleController) Delete() {
id, _ := this.GetInt64("id")
post := models.Post{Id: id}
if post.Read() == nil {
post.Delete()
}
models.Cache.Delete("latestblog")
this.Redirect("/admin/article/list", 302)
}
//批处理
func (this *ArticleController) Batch() {
ids := this.GetStrings("ids[]")
op := this.GetString("op")
idarr := make([]int64, 0)
for _, v := range ids {
if id, _ := strconv.Atoi(v); id > 0 {
idarr = append(idarr, int64(id))
}
}
var post models.Post
switch op {
case "topub": //移到已发布
post.Query().Filter("id__in", idarr).Update(orm.Params{"status": 0})
case "todrafts": //移到草稿箱
post.Query().Filter("id__in", idarr).Update(orm.Params{"status": 1})
case "totrash": //移到回收站
post.Query().Filter("id__in", idarr).Update(orm.Params{"status": 2})
case "delete": //批量删除
for _, id := range idarr {
obj := models.Post{Id: id}
if obj.Read() == nil {
obj.Delete()
}
}
}
this.Redirect(this.Ctx.Request.Referer(), 302)
}
//上传文件(用于文章图片上传,文章封面,说说封面)
func (this *ArticleController) Upload() {
file, header, err := this.GetFile("upfile")
utype := this.GetString("type")
if utype == "" {
utype = "1"
}
index, _ := strconv.Atoi(utype)
ext := strings.ToLower(header.Filename[strings.LastIndex(header.Filename, "."):])
out := make(map[string]string)
out["url"] = ""
out["fileType"] = ext
out["original"] = header.Filename
out["state"] = "SUCCESS"
if err != nil {
out["state"] = err.Error()
} else {
savepath := pathArr[index] + time.Now().Format("20060102")
if err = os.MkdirAll(savepath, os.ModePerm); err != nil {
out["state"] = err.Error()
} else {
filename := fmt.Sprintf("%s/%d%s", savepath, time.Now().UnixNano(), ext)
if this.GetString("type") == "2" {
w, _ := strconv.Atoi(this.GetString("w"))
h, _ := strconv.Atoi(this.GetString("h"))
err = createSmallPic(file, filename, w, h)
if err != nil {
out["state"] = err.Error()
}
} else {
if err = this.SaveToFile("upfile", filename); err != nil {
out["state"] = err.Error()
}
}
out["url"] = filename[1:]
}
}
this.Data["json"] = out
this.ServeJson()
}
func createSmallPic(file io.Reader, fileSmall string, w, h int) error {
// decode jpeg into image.Image
img, err := jpeg.Decode(file)
if err != nil {
return err
}
b := img.Bounds()
if w > b.Dx() {
w = b.Dx()
}
if h > b.Dy() {
h = b.Dy()
}
// resize to width 1000 using Lanczos resampling
// and preserve aspect ratio
m := resize.Resize(uint(w), uint(h), img, resize.Lanczos3)
out, err := os.Create(fileSmall)
if err != nil {
return err
}
defer out.Close()
// write new image to file
return jpeg.Encode(out, m, nil)
}