github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/routers/MainHandler.go (about) 1 package routers 2 3 import ( 4 "fmt" 5 6 "github.com/insionng/makross" 7 "github.com/insionng/makross/cache" 8 9 "strconv" 10 11 "github.com/insionng/yougam/helper" 12 "github.com/insionng/yougam/models" 13 "github.com/insionng/yougam/modules/setting" 14 ) 15 16 func GetMainHandler(self *makross.Context) error { 17 18 var IsSignin bool 19 //var _usr_ = new(models.User) 20 if _, okay := self.Session.Get("SignedUser").(*models.User); okay { 21 //_usr_ = sUser 22 IsSignin = okay 23 } 24 25 cc := cache.Store(self) 26 27 self.Set("catpage", "home") 28 self.Set("messager", false) 29 30 if v := self.Args("version").String(); len(v) > 0 { 31 self.Response.Header().Set("Cache-Control", "no-cache") 32 } 33 34 keyword := self.Args("keyword").String() 35 category := self.Args("category").String() 36 node := self.Args("node").String() 37 username := self.Args("username").String() 38 cid := self.Args("cid").MustInt64() 39 nid := self.Args("nid").MustInt64() 40 tab := self.Args("tab").String() 41 page := self.Args("page").MustInt64() 42 ctype := self.Args("ctype").MustInt64() 43 limit := self.Args("limit").MustInt64() 44 if limit <= 0 { 45 limit = 25 46 } 47 48 url := "/topics" 49 if tab == "lastest" { 50 url = url + "/lastest/" 51 tab = "id" 52 self.Set("tab", "lastest") 53 } else if tab == "hotness" { 54 url = url + "/hotness/" 55 tab = "hotness" 56 self.Set("tab", "hotness") 57 } else if tab == "rising" { 58 url = url + "/rising/" 59 tab = "hotup" 60 self.Set("tab", "rising") 61 } else if tab == "scores" { 62 url = url + "/scores/" 63 tab = "hotscore" 64 self.Set("tab", "scores") 65 } else if tab == "votes" { 66 url = url + "/votes/" 67 tab = "hotvote" 68 self.Set("tab", "votes") 69 } else if tab == "controversial" { 70 url = url + "/controversial/" 71 tab = "reply_count" 72 self.Set("tab", "controversial") 73 } else if tab == "popular" { 74 url = url + "/popular/" 75 tab = "views" 76 self.Set("tab", "popular") 77 } else if tab == "cold" { 78 url = url + "/cold/" 79 tab = "cold" 80 self.Set("tab", "cold") 81 } else if tab == "favorites" { 82 url = url + "/favorites/" 83 tab = "favorite_count" 84 self.Set("tab", "favorites") 85 } else if tab == "lastest" { 86 url = url + "/lastest/" 87 tab = "id" 88 self.Set("tab", "lastest") 89 } else { //最后一个是默认选择 90 url = url + "/optimal/" 91 tab = "confidence" 92 self.Set("tab", "optimal") 93 } 94 95 self.Set("isdefault", false) 96 97 TplNames := "main" 98 99 switch { 100 case len(keyword) > 0: //搜索模式 101 { 102 //如果已经登录 103 if IsSignin { 104 limit = 30 105 } 106 107 if rc, err := models.SearchSubject(keyword, 0, 0, "id"); err == nil { 108 109 rcs := int64(len(*rc)) 110 pages, pageout, beginnum, endnum, offset := helper.Pages(rcs, page, limit) 111 112 if st, err := models.SearchSubject(keyword, int(offset), int(limit), "hotness"); err == nil { 113 114 self.Set("topics", *st) 115 116 } 117 118 if k := self.FormValue("keyword"); len(k) > 0 { 119 self.Set("search_keyword", k) 120 } else { 121 self.Set("search_keyword", keyword) 122 } 123 124 self.Set("pagesbar", helper.Pagesbar("/search/", keyword, rcs, pages, pageout, beginnum, endnum, 5)) 125 } else { 126 127 self.Flash.Error(fmt.Sprintf("SearchSubject errors:%v", err)) 128 return self.Redirect("/") 129 } 130 } 131 case len(category) > 0: //特定分类名下的话题 132 { 133 totalRecords, _ := models.GetSubjectsByCategory4Count(category, 0, 0, ctype) 134 if totalRecords > 0 { 135 pages, page, beginnum, endnum, offset := helper.Pages(totalRecords, page, limit) 136 137 if cat, e := models.GetCategoryByTitle(category); cat != nil && e == nil { 138 cat.Views = cat.Views + 1 139 cat.Hotup = cat.Hotup + 1 140 cat.Hotness = helper.Hotness(cat.Hotup, cat.Hotdown, cat.Created) 141 cat.Confidence = helper.Confidence(cat.Hotup, cat.Hotdown) 142 cat.Hotvote = helper.QhotVote(cat.Hotup, cat.Hotdown) 143 cat.Hotscore = helper.Score(cat.Hotup, cat.Hotdown) 144 models.PutCategory(cat.Id, cat) 145 } 146 147 if tps := models.GetSubjectsByCategory(category, int(offset), int(limit), ctype, tab); *tps != nil { 148 self.Set("topics", *tps) 149 url = "/category/" + category + url 150 } 151 152 self.Set("pagesbar", helper.Pagesbar(url, "", totalRecords, pages, page, beginnum, endnum, 5)) 153 } 154 } 155 case cid > 0: //特定分类ID下的话题 156 { 157 totalRecords, _ := models.GetSubjectsByCid4Count(cid, 0, 0, ctype) 158 if totalRecords > 0 { 159 pages, page, beginnum, endnum, offset := helper.Pages(totalRecords, page, limit) 160 161 if cat, e := models.GetCategory(cid); cat != nil && e == nil { 162 cat.Views = cat.Views + 1 163 cat.Hotup = cat.Hotup + 1 164 cat.Hotness = helper.Hotness(cat.Hotup, cat.Hotdown, cat.Created) 165 cat.Confidence = helper.Confidence(cat.Hotup, cat.Hotdown) 166 cat.Hotvote = helper.QhotVote(cat.Hotup, cat.Hotdown) 167 cat.Hotscore = helper.Score(cat.Hotup, cat.Hotdown) 168 models.PutCategory(cat.Id, cat) 169 } 170 171 if tps := models.GetSubjectsByCid(cid, int(offset), int(limit), ctype, tab); *tps != nil { 172 self.Set("topics", *tps) 173 url = "/category/" + strconv.FormatInt(cid, 10) + url 174 } 175 176 self.Set("pagesbar", helper.Pagesbar(url, "", totalRecords, pages, page, beginnum, endnum, 5)) 177 } 178 } 179 case len(node) > 0: //特定节点名下的话题 180 { 181 self.Set("CurNdTitle", node) 182 183 totalRecords, _ := models.GetSubjectsByNode4Count(node, 0, 0, ctype) 184 if totalRecords > 0 { 185 pages, page, beginnum, endnum, offset := helper.Pages(totalRecords, page, limit) 186 187 if nd, e := models.GetNodeByTitle(node); nd != nil && e == nil { 188 //self.Set["curnodeContent"] = nd.Content 189 nd.Views = nd.Views + 1 190 /* 191 nd.Hotup = nd.Hotup + 1 192 nd.Hotness = helper.Hotness(nd.Hotup, nd.Hotdown, nd.Created) 193 nd.Confidence = helper.Confidence(nd.Hotup, nd.Hotdown) 194 nd.Hotvote = helper.QhotVote(nd.Hotup, nd.Hotdown) 195 nd.Hotscore = helper.Score(nd.Hotup, nd.Hotdown) 196 */ 197 models.PutNode(nd.Id, nd) 198 self.Set("curnode", *nd) 199 200 } 201 202 if tps := models.GetSubjectsByNode(node, int(offset), int(limit), ctype, tab); *tps != nil { 203 self.Set("topics", *tps) 204 url = "/node/" + node + url 205 206 } 207 208 self.Set("pagesbar", helper.Pagesbar(url, "", totalRecords, pages, page, beginnum, endnum, 5)) 209 } 210 } 211 case nid > 0: //特定节点ID下的话题 212 { 213 if nd, e := models.GetNode(nid); nd != nil && e == nil { 214 self.Set("CurNdTitle", nd.Title) 215 216 totalRecords, _ := models.GetSubjectsByNid4Count(nid, 0, 0, ctype) 217 if totalRecords > 0 { 218 pages, page, beginnum, endnum, offset := helper.Pages(totalRecords, page, limit) 219 220 if nd, e := models.GetNode(nid); nd != nil && e == nil { 221 //self.Set["curnodeContent"] = nd.Content 222 nd.Views = nd.Views + 1 223 /* 224 nd.Hotup = nd.Hotup + 1 225 nd.Hotness = helper.Hotness(nd.Hotup, nd.Hotdown, nd.Created) 226 nd.Confidence = helper.Confidence(nd.Hotup, nd.Hotdown) 227 nd.Hotvote = helper.QhotVote(nd.Hotup, nd.Hotdown) 228 nd.Hotscore = helper.Score(nd.Hotup, nd.Hotdown) 229 */ 230 models.PutNode(nd.Id, nd) 231 self.Set("curnode", *nd) 232 233 } 234 235 if tps := models.GetSubjectsByNid(nid, int(offset), int(limit), ctype, tab); *tps != nil { 236 self.Set("topics", *tps) 237 url = "/node/" + strconv.FormatInt(nid, 10) + url 238 } 239 240 self.Set("pagesbar", helper.Pagesbar(url, "", totalRecords, pages, page, beginnum, endnum, 5)) 241 } 242 } 243 244 } 245 case len(username) > 0: //特定用户名下的话题 246 { 247 self.Set("CurUsrTitle", username) 248 249 totalRecords, _ := models.GetSubjectsByUser4Count(username, 0, 0, ctype) 250 if totalRecords > 0 { 251 pages, page, beginnum, eusrnum, offset := helper.Pages(totalRecords, page, limit) 252 253 //if usr, e := models.GetUserByUsername(username); usr != nil && e == nil { 254 //self.Set["curusernameContent"] = usr.Content 255 //usr.Views = usr.Views + 1 256 /* 257 usr.Hotup = usr.Hotup + 1 258 usr.Hotness = helper.Hotness(usr.Hotup, usr.Hotdown, usr.Created) 259 usr.Confidence = helper.Confidence(usr.Hotup, usr.Hotdown) 260 usr.Hotvote = helper.QhotVote(usr.Hotup, usr.Hotdown) 261 usr.Hotscore = helper.Score(usr.Hotup, usr.Hotdown) 262 */ 263 //models.PutUser(usr.Id, usr) 264 //self.Set["curusername"] = *usr 265 266 //} 267 268 if tps := models.GetSubjectsByUser(username, int(offset), int(limit), ctype, tab); *tps != nil { 269 self.Set("topics", *tps) 270 url = "/createdby/" + username + url 271 272 } 273 274 self.Set("pagesbar", helper.Pagesbar(url, "", totalRecords, pages, page, beginnum, eusrnum, 5)) 275 } 276 } 277 default: //默认显示首页话题列表数据 278 { 279 self.Set("isdefault", true) 280 totalRecords, err := models.GetTopicsByPid4Count(0, 0, 0, ctype) 281 if err != nil { 282 return err 283 } 284 if totalRecords <= 0 { 285 self.Set("topics", nil) 286 } else { 287 288 pages, page, beginnum, endnum, offset := helper.Pages(totalRecords, page, limit) 289 290 tps := new([]*models.Topic) 291 292 if tab == "hotup" { //计算一月内的趋势 若果没有数据则继续缩小时间范围 293 tps = models.GetTopicsByPidSinceCreated(0, int(offset), int(limit), ctype, tab, helper.ThisMonth()) 294 if totalRecords := int64(len(*tps)); totalRecords <= 0 { 295 tps = models.GetTopicsByPidSinceCreated(0, int(offset), int(limit), ctype, tab, helper.ThisWeek()) 296 if totalRecords := int64(len(*tps)); totalRecords <= 0 { 297 tps = models.GetTopicsByPidSinceCreated(0, int(offset), int(limit), ctype, tab, helper.ThisDate()) 298 if totalRecords := int64(len(*tps)); totalRecords <= 0 { 299 tps = models.GetTopicsByPidSinceCreated(0, int(offset), int(limit), ctype, tab, helper.ThisHour()) 300 if totalRecords := int64(len(*tps)); totalRecords > 0 { 301 goto rising 302 } 303 } else { 304 goto rising 305 } 306 } else { 307 goto rising 308 } 309 } else { 310 goto rising 311 } 312 313 rising: 314 { 315 pages, page, beginnum, endnum, _ := helper.Pages(totalRecords, page, limit) 316 self.Set("pagesbar", helper.Pagesbar(url, "", totalRecords, pages, page, beginnum, endnum, 5)) 317 } 318 } else { 319 320 tps = models.GetTopicsByPid(0, int(offset), int(limit), ctype, tab) 321 if totalRecords := int64(len(*tps)); totalRecords > 0 { 322 self.Set("pagesbar", helper.Pagesbar(url, "", totalRecords, pages, page, beginnum, endnum, 5)) 323 } 324 325 } 326 self.Set("topics", *tps) 327 328 } 329 } 330 } 331 332 //置顶话题 333 /* 334 if tpsBySort := models.GetTopicsByPid(0, 0, 0, 0, "sort"); len(*tpsBySort) > 0 { 335 self.Set["TopicsBySort"] = *tpsBySort 336 } 337 */ 338 if item := setting.Cache.Get("Main_TopicsBySort"); item != nil { 339 if !item.Expired() { 340 if tpsBySort, okay := item.Value().(*[]*models.Topic); okay { 341 self.Set("TopicsBySort", *tpsBySort) 342 } 343 } else { 344 if tpsBySort := models.GetTopicsByPid(0, 0, 0, 0, "sort"); len(*tpsBySort) > 0 { 345 cc.Set("Main_TopicsBySort", tpsBySort, 60*90) 346 self.Set("TopicsBySort", *tpsBySort) 347 } 348 } 349 } else { 350 if tpsBySort := models.GetTopicsByPid(0, 0, 0, 0, "sort"); len(*tpsBySort) > 0 { 351 cc.Set("Main_TopicsBySort", tpsBySort, 60*90) 352 self.Set("TopicsBySort", *tpsBySort) 353 } 354 } 355 356 if nds, err := models.NodesOfNavor(0, 0, "hotness"); nds != nil && err == nil { 357 self.Set("nodes", *nds) 358 } 359 360 if nds, err := models.GetNodesByCid(0, 0, 10, "confidence"); nds != nil && err == nil { 361 self.Set("nodes_sidebar_confidence_10", *nds) 362 } 363 364 /* 365 if tab == "hotness" { //当主列表显示最热话题的时候 右侧显示最新话题 366 if tps := models.GetTopicsByPid(0, 0, 10, 0, "created"); tps != nil { 367 self.Set["topics_sidebar_10"] = *tps 368 } 369 } else { 370 if tps := models.GetTopicsByPid(0, 0, 10, 0, "hotness"); tps != nil { 371 self.Set["topics_sidebar_10"] = *tps 372 } 373 } 374 */ 375 topics_sidebar_10_key, topics_sidebar_10_tab := "", "" 376 if tab == "hotness" { //当主列表显示最热话题的时候 右侧显示最新话题 377 topics_sidebar_10_tab = "created" 378 topics_sidebar_10_key = "topics_sidebar_10_" + "created" 379 } else { 380 topics_sidebar_10_tab = "hotness" 381 topics_sidebar_10_key = "topics_sidebar_10_" + "hotness" 382 } 383 384 var tps []*models.Topic 385 if err := cc.Get(topics_sidebar_10_key, &tps); err != nil { 386 if tps := models.GetTopicsByPid(0, 0, 10, 0, topics_sidebar_10_tab); tps != nil { 387 cc.Set(topics_sidebar_10_key, tps, 60*90) 388 self.Set("topics_sidebar_10", *tps) 389 } 390 391 } else { 392 393 self.Set("topics_sidebar_10", &tps) 394 395 } 396 397 /* 398 if rps := models.GetReplysByTid(0, 0, 0, 5, "confidence"); rps != nil { 399 self.Set["ConfidenceReplys"] = *rps 400 } 401 */ 402 var rps *[]*models.Reply 403 if err := cc.Get("Main_ConfidenceReplys", &rps); err != nil { 404 405 if rps := models.GetReplysByTid(0, 0, 0, 5, "confidence"); rps != nil { 406 cc.Set("Main_ConfidenceReplys", rps, 60*90) 407 self.Set("ConfidenceReplys", *rps) 408 } 409 410 } else { 411 412 self.Set("ConfidenceReplys", *rps) 413 414 } 415 416 /* 417 if rps := models.GetReplysByTid(0, 0, 0, 5, "created"); rps != nil { 418 self.Set["replys"] = *rps 419 } 420 */ 421 if err := cc.Get("Main_replys", &rps); err != nil { 422 if rps := models.GetReplysByTid(0, 0, 0, 5, "created"); rps != nil { 423 cc.Set("Main_replys", rps, 60*90) 424 self.Set("replys", *rps) 425 } 426 } else { 427 428 self.Set("replys", *rps) 429 430 } 431 432 return self.Render(TplNames) 433 434 }