/
setting.go
219 lines (173 loc) · 4.94 KB
/
setting.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
// Copyright 2016 The StudyGolang Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// http://studygolang.com
// Author:polaris polaris@studygolang.com
package logic
import (
"encoding/json"
"errors"
"net/url"
"strings"
"sander/db"
"sander/logger"
"sander/model"
"github.com/polaris1119/goutils"
"golang.org/x/net/context"
)
type SettingLogic struct{}
var DefaultSetting = SettingLogic{}
func (SettingLogic) Update(ctx context.Context, form url.Values) error {
name := form.Get("name")
if name != "" {
WebsiteSetting.Name = name
}
domain := form.Get("domain")
if domain != "" {
WebsiteSetting.Domain = domain
}
titleSuffix := form.Get("title_suffix")
if titleSuffix != "" {
WebsiteSetting.TitleSuffix = titleSuffix
}
favicon := form.Get("favicon")
if favicon != "" {
WebsiteSetting.Favicon = favicon
}
startYear := goutils.MustInt(form.Get("start_year"))
if startYear != 0 {
WebsiteSetting.StartYear = startYear
}
logo := form.Get("logo")
if logo != "" {
WebsiteSetting.Logo = logo
}
WebsiteSetting.BlogUrl = form.Get("blog_url")
slogan := form.Get("slogan")
if slogan != "" {
WebsiteSetting.Slogan = slogan
}
WebsiteSetting.Beian = form.Get("beian")
WebsiteSetting.ReadingMenu = form.Get("reading_menu")
if docNameSlice, ok := form["doc_name"]; ok {
docUrlSlice := form["doc_url"]
docMenus := make([]*model.DocMenu, len(docNameSlice))
for i, docName := range docNameSlice {
docMenus[i] = &model.DocMenu{
Name: docName,
Url: docUrlSlice[i],
}
}
docMenusBytes, err := json.Marshal(docMenus)
if err != nil {
logger.Error("marshal doc menu error:", err)
return err
}
WebsiteSetting.DocMenus = docMenus
WebsiteSetting.DocsMenu = string(docMenusBytes)
}
if indexTabSlice, ok := form["index_tab"]; ok {
indexNameSlice := form["index_name"]
indexDataSourceSlice := form["index_data_source"]
indexNavs := make([]*model.IndexNav, len(indexTabSlice))
for i, indexTab := range indexTabSlice {
indexNavs[i] = &model.IndexNav{
Tab: indexTab,
Name: indexNameSlice[i],
DataSource: indexDataSourceSlice[i],
}
// 原来的子 tab 得保留
oldIndexNav := GetCurIndexNav(indexTab)
if oldIndexNav != nil {
indexNavs[i].Children = oldIndexNav.Children
}
}
indexNavsBytes, err := json.Marshal(indexNavs)
if err != nil {
logger.Error("marshal index tab nav error:", err)
return err
}
WebsiteSetting.IndexNavs = indexNavs
WebsiteSetting.IndexNav = string(indexNavsBytes)
}
if navNameSlice, ok := form["nav_name"]; ok {
navUrlSlice := form["nav_url"]
footerNavs := make([]*model.FooterNav, len(navNameSlice))
for i, navName := range navNameSlice {
outerWeb := true
if strings.HasPrefix(navUrlSlice[i], "/") {
outerWeb = false
}
footerNavs[i] = &model.FooterNav{
Name: navName,
Url: navUrlSlice[i],
OuterSite: outerWeb,
}
}
footerNavsBytes, err := json.Marshal(footerNavs)
if err != nil {
logger.Error("marshal footer nav error:", err)
return err
}
WebsiteSetting.FooterNavs = footerNavs
WebsiteSetting.FooterNav = string(footerNavsBytes)
}
if frLogoImageSlice, ok := form["fr_logo_image"]; ok {
frLogoUrlSlice := form["fr_logo_url"]
frWidthSlice := form["fr_logo_width"]
frHeightSlice := form["fr_logo_height"]
friendLogos := make([]*model.FriendLogo, len(frLogoImageSlice))
for i, frLogoImage := range frLogoImageSlice {
friendLogos[i] = &model.FriendLogo{
Image: frLogoImage,
Url: frLogoUrlSlice[i],
Width: frWidthSlice[i],
Height: frHeightSlice[i],
}
}
friendLogosBytes, err := json.Marshal(friendLogos)
if err != nil {
logger.Error("marshal friend logo error:", err)
return err
}
WebsiteSetting.FriendLogos = friendLogos
WebsiteSetting.FriendsLogo = string(friendLogosBytes)
}
_, err := db.MasterDB.Update(WebsiteSetting)
if err != nil {
logger.Error("Update setting error:", err)
return err
}
return nil
}
func (SettingLogic) UpdateIndexTabChildren(ctx context.Context, form url.Values) error {
if _, ok := form["tab"]; !ok {
return errors.New("父 tab 没有指定")
}
for _, indexTab := range WebsiteSetting.IndexNavs {
if indexTab.Tab == form.Get("tab") {
if indexUriSlice, ok := form["index_uri"]; ok {
indexTab.Children = make([]*model.IndexNavChild, len(indexUriSlice))
indexNameSlice := form["index_name"]
for i, indexUri := range indexUriSlice {
indexTab.Children[i] = &model.IndexNavChild{
Uri: indexUri,
Name: indexNameSlice[i],
}
}
}
}
}
indexNavsBytes, err := json.Marshal(WebsiteSetting.IndexNavs)
if err != nil {
logger.Error("marshal index child tab nav error:", err)
return err
}
WebsiteSetting.IndexNav = string(indexNavsBytes)
_, err = db.MasterDB.Update(WebsiteSetting)
if err != nil {
logger.Error("Update index child tab error:", err)
return err
}
return nil
}