rollaward.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // 实时数据
  2. package controllers
  3. import (
  4. "box-gm/models"
  5. "encoding/json"
  6. "fmt"
  7. "log"
  8. "github.com/astaxie/beego"
  9. )
  10. type RollAwardController struct {
  11. beego.Controller
  12. }
  13. func (c *RollAwardController) Get() {
  14. userinfo := c.GetSession("user")
  15. if userinfo == nil {
  16. c.Ctx.Redirect(302, "/accountlogin")
  17. return
  18. }
  19. c.Data["username"] = userinfo.(*LoginInfo).Username
  20. c.Data["token"] = models.GetToken()
  21. userPermission := models.GetPermission(userinfo.(*LoginInfo).Username)
  22. if (userPermission & (1 << uint(models.ModelSystem))) == 0 {
  23. c.Ctx.Redirect(302, "/accountlogin")
  24. return
  25. }
  26. c.Data["ItemList"] = models.GetItemInfoList()
  27. c.Data["infoList"] = models.GetRollAwardInfo()
  28. c.TplName = "rollaward.tpl"
  29. }
  30. func (c *RollAwardController) Post() {
  31. userinfo := c.GetSession("user")
  32. log.Printf("RollAwardController Post userinfo[%v]", userinfo)
  33. if userinfo == nil {
  34. c.Ctx.Redirect(302, "/accountlogin")
  35. return
  36. }
  37. items := c.GetString("items")
  38. var eItems []int
  39. err := json.Unmarshal([]byte(items), &eItems)
  40. if err != nil {
  41. log.Printf("RollAwardController items[%s] err[%v]", items, err)
  42. c.Data["json"] = &map[string]interface{}{"status": false, "info": err.Error()}
  43. c.ServeJSON()
  44. return
  45. }
  46. u := new(models.RollAwardInfo)
  47. u.Id = models.CreateRollAwardId()
  48. u.ItemIdList = eItems
  49. err = models.AddRollAwardInfo(u)
  50. if err != nil {
  51. c.Data["json"] = &map[string]interface{}{"status": false, "info": err.Error()}
  52. c.ServeJSON()
  53. return
  54. }
  55. c.Data["json"] = &map[string]interface{}{"status": true, "info": "新增roll房奖池方案成功"}
  56. c.ServeJSON()
  57. }
  58. func (c *RollAwardController) Put() {
  59. userinfo := c.GetSession("user")
  60. if userinfo == nil {
  61. c.Ctx.Redirect(302, "/accountlogin")
  62. return
  63. }
  64. id, _ := c.GetInt("id")
  65. log.Printf("RollAwardController Put id[%d]", id)
  66. hf := models.GetRollAwardInfoById(id)
  67. if hf == nil {
  68. c.Data["json"] = &map[string]interface{}{"status": false, "info": "修改失败:奖池方案不存在"}
  69. c.ServeJSON()
  70. return
  71. }
  72. items := c.GetString("items")
  73. var eItems []int
  74. err := json.Unmarshal([]byte(items), &eItems)
  75. if err != nil {
  76. log.Printf("RollAwardController items[%s] err[%v]", items, err)
  77. c.Data["json"] = &map[string]interface{}{"status": false, "info": err.Error()}
  78. c.ServeJSON()
  79. return
  80. }
  81. hf.ItemIdList = eItems
  82. err = models.UpdateRollAwardInfo(hf)
  83. if err != nil {
  84. c.Data["json"] = &map[string]interface{}{"status": false, "info": err}
  85. c.ServeJSON()
  86. return
  87. }
  88. c.Data["json"] = &map[string]interface{}{"status": true, "info": fmt.Sprintf("修改奖池方案[%d]成功", id)}
  89. c.ServeJSON()
  90. }
  91. func (c *RollAwardController) Delete() {
  92. userinfo := c.GetSession("user")
  93. if userinfo == nil {
  94. c.Ctx.Redirect(302, "/accountlogin")
  95. return
  96. }
  97. id, _ := c.GetInt("id")
  98. log.Printf("RollAwardController Post Delete id[%d]", id)
  99. hf := models.GetRollAwardInfoById(id)
  100. if hf == nil {
  101. c.Data["json"] = &map[string]interface{}{"status": false, "info": "修改失败:兑换码配置不存在"}
  102. c.ServeJSON()
  103. return
  104. }
  105. models.DelRollAwardInfoById(id)
  106. c.Data["json"] = &map[string]interface{}{"status": true, "info": "删除兑换码成功"}
  107. c.ServeJSON()
  108. }