github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/routers/root/RReadTopic.go (about)

     1  package root
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/insionng/makross"
     6  	
     7  	"strconv"
     8  	"github.com/insionng/yougam/helper"
     9  	"github.com/insionng/yougam/models"
    10  )
    11  
    12  func GetRReadTopicHandler(self *makross.Context) error {
    13  	
    14  
    15  	TplNames := "root/create_topic"
    16  	self.Set("catpage", "RReadTopicHandler")
    17  
    18  	switch tid := self.Param("tid").MustInt64(); {
    19  	//单独模式
    20  	case tid > 0:
    21  		{
    22  			TplNames = "root/read_topic"
    23  			if tp, err := models.GetTopic(tid); tp != nil && err == nil {
    24  				self.Set("topic", *tp)
    25  
    26  				if nodes, err := models.GetNodes(0, 0, "id"); nodes != nil && err == nil {
    27  					self.Set("nodes", *nodes)
    28  				}
    29  
    30  			} else {
    31  				self.Flash.Error(fmt.Sprint(err))
    32  				return self.Render(TplNames)
    33  
    34  			}
    35  		}
    36  
    37  	//列表模式
    38  	case tid <= 0:
    39  		{
    40  			TplNames = "root/topic_table"
    41  			self.Set("catpage", "RReadTopicHandlerlist")
    42  
    43  			offset := self.Args("offset").MustInt64()
    44  			limit := self.Args("limit").MustInt64()
    45  			field := self.FormValue("field")
    46  
    47  			if limit == 0 {
    48  				limit = 1000 //默认限制显示最近1000条,需要显示全部请在提交请求的时候设置limit字段为-1
    49  			}
    50  
    51  			if field == "" {
    52  				field = "id"
    53  			}
    54  
    55  			if pid := self.Param("pid").MustInt64(); pid > 0 {
    56  
    57  				if tps := models.GetTopicsByPid(pid, int(offset), int(limit), 0, field); tps != nil && (len(*tps) > 0) {
    58  					self.Set("catpage", "RReadTopicHandler")
    59  					self.Set("topics", *tps)
    60  				}
    61  
    62  			} else {
    63  
    64  				if tps := models.GetSubjectsByNid(0, int(offset), int(limit), 0, field); tps != nil {
    65  					self.Set("topics", *tps)
    66  				}
    67  			}
    68  
    69  		}
    70  	}
    71  
    72  	return self.Render(TplNames)
    73  }
    74  
    75  func PostRReadTopicHandler(self *makross.Context) error {
    76  	
    77  
    78  	delrowids := self.FormValue("delrowids")
    79  
    80  	//删除话题
    81  	if len(delrowids) > 0 {
    82  
    83  		uid := int64(0)
    84  		role := int64(-1000)
    85  
    86  		delids := helper.Split(delrowids, ",")
    87  		for _, delid := range delids {
    88  			tid, _ := strconv.ParseInt(delid, 10, 0)
    89  
    90  			if tid > 0 {
    91  				if tp, err := models.GetTopic(tid); tp != nil && err == nil {
    92  					if tp.Pid == 0 {
    93  
    94  						if tps := models.GetTopicsByPid(tid, 0, 0, 0, "id"); tps != nil {
    95  							for _, g := range *tps {
    96  								models.DelTopic(g.Id, uid, role)
    97  
    98  							}
    99  
   100  							self.Flash.Success("删除 Topic id:" + delrowids + "成功!")
   101  
   102  						}
   103  					}
   104  				} else {
   105  
   106  					if e := models.DelTopic(tid, uid, role); e != nil {
   107  						self.Flash.Error(fmt.Sprintf("删除 Topic id:", strconv.FormatInt(tid, 10), "出现错误 ", e, "!"))
   108  
   109  					} else {
   110  						self.Flash.Success("删除 Topic id:" + strconv.FormatInt(tid, 10) + "成功!")
   111  
   112  					}
   113  				}
   114  			}
   115  		}
   116  
   117  	} else {
   118  		self.Flash.Error("非法 Topic id!")
   119  
   120  	}
   121  
   122  	return self.Redirect("/root/read/topic/")
   123  
   124  }