exchangecode.go 3.9 KB

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