exchangecode.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // 实时数据
  2. package controllers
  3. import (
  4. "box-gm/models"
  5. "box-gm/utils"
  6. "encoding/json"
  7. "fmt"
  8. "log"
  9. "github.com/astaxie/beego"
  10. )
  11. type ExchangeCodeController struct {
  12. beego.Controller
  13. }
  14. func (c *ExchangeCodeController) Get() {
  15. userinfo := c.GetSession("user")
  16. if userinfo == nil {
  17. c.Ctx.Redirect(302, "/accountlogin")
  18. return
  19. }
  20. c.Data["username"] = userinfo.(*LoginInfo).Username
  21. c.Data["token"] = models.GetToken()
  22. userPermission := models.GetPermission(userinfo.(*LoginInfo).Username)
  23. if (userPermission & (1 << uint(models.ModelSystem))) == 0 {
  24. c.Ctx.Redirect(302, "/accountlogin")
  25. return
  26. }
  27. c.Data["ItemList"] = models.GetItemInfoList()
  28. c.Data["infoList"] = models.GetExchangeCodeInfo()
  29. c.TplName = "exchangecode.tpl"
  30. }
  31. func (c *ExchangeCodeController) Post() {
  32. userinfo := c.GetSession("user")
  33. log.Printf("ExchangeCodeController Post userinfo[%v]", userinfo)
  34. if userinfo == nil {
  35. c.Ctx.Redirect(302, "/accountlogin")
  36. return
  37. }
  38. pcode := c.GetString("pcode")
  39. log.Printf("ExchangeCodeController Post pcode[%s]", pcode)
  40. hf := models.GetExchangeCodeInfoByCode(pcode)
  41. if hf != nil {
  42. c.Data["json"] = &map[string]interface{}{"status": false, "info": "新增失败:渠道已配置"}
  43. c.ServeJSON()
  44. return
  45. }
  46. awardTimes, _ := c.GetInt64("awardTimes")
  47. expireTime := c.GetString("expireTime")
  48. items := c.GetString("items")
  49. var eItems []*models.ExchangeItemsInfo
  50. err := json.Unmarshal([]byte(items), &eItems)
  51. if err != nil {
  52. log.Printf("ExchangeCodeController items[%s] err[%v]", items, err)
  53. c.Data["json"] = &map[string]interface{}{"status": false, "info": err.Error()}
  54. c.ServeJSON()
  55. return
  56. }
  57. u := new(models.ExchangeCodeInfo)
  58. u.Pcode = pcode
  59. u.AwardTimes = awardTimes
  60. u.ExpireTime = utils.GetTime64(expireTime)
  61. u.Items = eItems
  62. err = models.AddExchangeCodeInfo(u)
  63. if err != nil {
  64. c.Data["json"] = &map[string]interface{}{"status": false, "info": err.Error()}
  65. c.ServeJSON()
  66. return
  67. }
  68. c.Data["json"] = &map[string]interface{}{"status": true, "info": "新增兑换码成功"}
  69. c.ServeJSON()
  70. }
  71. func (c *ExchangeCodeController) Put() {
  72. userinfo := c.GetSession("user")
  73. if userinfo == nil {
  74. c.Ctx.Redirect(302, "/accountlogin")
  75. return
  76. }
  77. pcode := c.GetString("pcode")
  78. log.Printf("ExchangeCodeController Post pcode[%s]", pcode)
  79. hf := models.GetExchangeCodeInfoByCode(pcode)
  80. if hf == nil {
  81. c.Data["json"] = &map[string]interface{}{"status": false, "info": "修改失败:兑换码配置不存在"}
  82. c.ServeJSON()
  83. return
  84. }
  85. awardTimes, _ := c.GetInt64("awardTimes")
  86. expireTime := c.GetString("expireTime")
  87. items := c.GetString("items")
  88. var eItems []*models.ExchangeItemsInfo
  89. err := json.Unmarshal([]byte(items), &eItems)
  90. if err != nil {
  91. log.Printf("ExchangeCodeController items[%s] err[%v]", items, err)
  92. c.Data["json"] = &map[string]interface{}{"status": false, "info": err.Error()}
  93. c.ServeJSON()
  94. return
  95. }
  96. hf.AwardTimes = awardTimes
  97. hf.ExpireTime = utils.GetTime64(expireTime)
  98. hf.Items = eItems
  99. err = models.UpdateExchangeCodeInfo(hf)
  100. if err != nil {
  101. c.Data["json"] = &map[string]interface{}{"status": false, "info": err}
  102. c.ServeJSON()
  103. return
  104. }
  105. c.Data["json"] = &map[string]interface{}{"status": true, "info": fmt.Sprintf("修改兑换码[%s]成功", pcode)}
  106. c.ServeJSON()
  107. }
  108. func (c *ExchangeCodeController) Delete() {
  109. userinfo := c.GetSession("user")
  110. if userinfo == nil {
  111. c.Ctx.Redirect(302, "/accountlogin")
  112. return
  113. }
  114. pcode := c.GetString("pcode")
  115. log.Printf("ExchangeCodeController Post Delete[%s]", pcode)
  116. hf := models.GetExchangeCodeInfoByCode(pcode)
  117. if hf == nil {
  118. c.Data["json"] = &map[string]interface{}{"status": false, "info": "修改失败:兑换码配置不存在"}
  119. c.ServeJSON()
  120. return
  121. }
  122. models.DelExchangeCodeInfoById(pcode)
  123. c.Data["json"] = &map[string]interface{}{"status": true, "info": "删除兑换码成功"}
  124. c.ServeJSON()
  125. }