forked from TruthHun/BookStack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathads.go
305 lines (289 loc) · 7.12 KB
/
ads.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
package models
import (
"fmt"
"math/rand"
"sync"
"time"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
)
// 广告位
type AdsPosition struct {
Id int
Title string
Identify string `orm:"index;size(32)"`
IsMobile bool `orm:"index"`
}
// 广告
type AdsCont struct {
Id int
Pid int `orm:"index"`
Title string
Code string `orm:"size(4096)"`
Start int
StartTime string `orm:"-"`
End int
EndTime string `orm:"-"`
Status bool
}
func NewAdsPosition() *AdsPosition {
return &AdsPosition{}
}
func NewAdsCont() *AdsCont {
return &AdsCont{}
}
const (
AdsPositionBeforeFriendLink = "global-before-friend-link"
AdsPositionGlobalFooter = "global-footer"
AdsPositionUnderLatestRecommend = "index-under-latest-recommend"
AdsPositionSearchRight = "search-right"
AdsPositionSearchTop = "search-top"
AdsPositionSearchBottom = "search-bottom"
AdsPositionUnderBookName = "intro-under-book-name"
AdsPositionBeforeMenu = "intro-before-menu"
AdsPositionBeforeRelatedBooks = "intro-before-related-books"
AdsPositionUnderExploreNav = "explore-under-nav"
AdsPositionBeforeExplorePagination = "explore-before-pagination"
AdsPositionUnderExplorePagination = "explore-under-pagination"
AdsPositionContentTop = "content-top"
AdsPositionContentBottom = "content-bottom"
)
var (
adsCache sync.Map // map[pid][]AdsCont
positionCache sync.Map // map[positionIdentify-isMobile]=pid
)
func InstallAdsPosition() {
positions := []AdsPosition{
{
IsMobile: false,
Title: "[全局]页面底部",
Identify: AdsPositionGlobalFooter,
},
{
IsMobile: false,
Title: "[友链]顶部",
Identify: AdsPositionBeforeFriendLink,
},
{
IsMobile: false,
Title: "[首页]最新推荐下方",
Identify: AdsPositionUnderLatestRecommend,
},
{
IsMobile: false,
Title: "[搜索页]搜索结果右侧",
Identify: AdsPositionSearchRight,
},
{
IsMobile: false,
Title: "[搜索页]搜索结果上方",
Identify: AdsPositionSearchTop,
},
{
IsMobile: false,
Title: "[搜索页]搜索结果下方",
Identify: AdsPositionSearchBottom,
},
{
IsMobile: false,
Title: "[书籍介绍页]书籍名称下方",
Identify: AdsPositionUnderBookName,
},
{
IsMobile: false,
Title: "[书籍介绍页]文档概述上方",
Identify: AdsPositionBeforeMenu,
},
{
IsMobile: false,
Title: "[书籍介绍页]相关书籍上方",
Identify: AdsPositionBeforeRelatedBooks,
},
{
IsMobile: false,
Title: "[内容阅读页]内容上方",
Identify: AdsPositionContentTop,
},
{
IsMobile: false,
Title: "[内容阅读页]内容下方",
Identify: AdsPositionContentBottom,
},
{
IsMobile: false,
Title: "[发现页]导航栏下方",
Identify: AdsPositionUnderExploreNav,
},
{
IsMobile: false,
Title: "[发现页]分页上方",
Identify: AdsPositionBeforeExplorePagination,
},
{
IsMobile: false,
Title: "[发现页]分页下方",
Identify: AdsPositionUnderExplorePagination,
},
{
IsMobile: true,
Title: "[全局]页面底部",
Identify: AdsPositionGlobalFooter,
},
{
IsMobile: true,
Title: "[友链]顶部",
Identify: AdsPositionBeforeFriendLink,
},
{
IsMobile: true,
Title: "[首页]最新推荐下方",
Identify: AdsPositionUnderLatestRecommend,
},
{
IsMobile: true,
Title: "[搜索页]搜索结果上方",
Identify: AdsPositionSearchTop,
},
{
IsMobile: true,
Title: "[搜索页]搜索结果下方",
Identify: AdsPositionSearchBottom,
},
{
IsMobile: true,
Title: "[书籍介绍页]书籍名称下方",
Identify: AdsPositionUnderBookName,
},
{
IsMobile: true,
Title: "[书籍介绍页]文档概述上方",
Identify: AdsPositionBeforeMenu,
},
{
IsMobile: true,
Title: "[书籍介绍页]相关书籍上方",
Identify: AdsPositionBeforeRelatedBooks,
},
{
IsMobile: true,
Title: "[发现页]导航栏下方",
Identify: AdsPositionUnderExploreNav,
},
{
IsMobile: true,
Title: "[发现页]分页上方",
Identify: AdsPositionBeforeExplorePagination,
},
{
IsMobile: true,
Title: "[发现页]分页下方",
Identify: AdsPositionUnderExplorePagination,
},
{
IsMobile: true,
Title: "[内容阅读页]内容上方",
Identify: AdsPositionContentTop,
},
{
IsMobile: true,
Title: "[内容阅读页]内容下方",
Identify: AdsPositionContentBottom,
},
}
o := orm.NewOrm()
for _, position := range positions {
table := &AdsPosition{}
o.QueryTable(table).Filter("is_mobile", position.IsMobile).Filter("identify", position.Identify).One(table)
if table.Id == 0 {
o.Insert(&position)
}
}
}
func initAdsCache() {
o := orm.NewOrm()
var pos []AdsPosition
o.QueryTable(&AdsPosition{}).All(&pos)
for _, item := range pos {
key := fmt.Sprintf("%v-%v", item.Identify, item.IsMobile)
positionCache.Store(key, item.Id)
}
UpdateAdsCache()
}
func UpdateAdsCache() {
var (
ads []AdsCont
cache sync.Map
data = make(map[int][]AdsCont)
)
now := time.Now().Unix()
orm.NewOrm().QueryTable(&AdsCont{}).Filter("status", 1).Filter("start__lt", now).Filter("end__gt", now).All(&ads)
for _, item := range ads {
data[item.Pid] = append(data[item.Pid], item)
}
if beego.AppConfig.String("runmode") == "dev" {
beego.Info(" =============== update ads cache =============== ")
}
for pid, arr := range data {
cache.Store(pid, arr)
}
adsCache = cache
}
func (m *AdsCont) GetPositions() []AdsPosition {
var positions []AdsPosition
orm.NewOrm().QueryTable(NewAdsPosition()).OrderBy("is_mobile").All(&positions)
return positions
}
func (m *AdsCont) Lists(isMobile bool, status ...bool) (ads []AdsCont) {
var (
positions []AdsPosition
pids []interface{}
tablePosition = NewAdsPosition()
tableAds = NewAdsCont()
)
o := orm.NewOrm()
o.QueryTable(tablePosition).Filter("is_mobile", isMobile).All(&positions)
for _, p := range positions {
pids = append(pids, p.Id)
}
q := o.QueryTable(tableAds).Filter("pid__in", pids...)
if len(status) > 0 {
q = q.Filter("status", status[0])
}
q.All(&ads)
layout := "2006-01-02"
for idx, ad := range ads {
ad.StartTime = time.Unix(int64(ad.Start), 0).Format(layout)
ad.EndTime = time.Unix(int64(ad.End), 0).Format(layout)
ads[idx] = ad
}
return
}
func GetAdsCode(positionIdentify string, isMobile bool) (code string) {
if beego.AppConfig.String("runmode") == "dev" {
beego.Debug("getAdsCode", positionIdentify, isMobile)
}
key := fmt.Sprintf("%v-%v", positionIdentify, isMobile)
pid, ok := positionCache.Load(key)
if !ok {
return
}
data, ok := adsCache.Load(pid.(int))
if !ok {
return
}
var ads []AdsCont
nowSec := int(time.Now().Unix())
for _, ad := range data.([]AdsCont) {
if ad.Start <= nowSec && ad.End >= nowSec {
ads = append(ads, ad)
}
}
lenAds := len(ads)
if lenAds == 0 {
return
} else if lenAds == 1 {
return ads[0].Code
}
rand.Seed(time.Now().UnixNano())
return ads[rand.Intn(lenAds)].Code
}