ProductType.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package controllers
  2. import (
  3. "Yunlot/lib"
  4. "Yunlot/models/Device"
  5. "Yunlot/models/Product"
  6. beego "github.com/beego/beego/v2/server/web"
  7. )
  8. type ProductTypeController struct {
  9. beego.Controller
  10. }
  11. func (c *ProductTypeController) List() {
  12. PageIndex, _ := c.GetInt("PageIndex", 0)
  13. PageSize, _ := c.GetInt("PageSize", 10)
  14. ProductTyper := Product.ProductType{}
  15. c.ParseForm(&ProductTyper)
  16. // 验证 TOKEY
  17. if c.Ctx.Input.Host() != "127.0.0.1" {
  18. if len(ProductTyper.T_tokey) != 8 {
  19. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_tokey!"}
  20. c.ServeJSON()
  21. return
  22. }
  23. }
  24. ProductType_r, Total := ProductTyper.Lists(PageIndex, PageSize)
  25. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: lib.C_Page(ProductType_r, PageIndex, PageSize, Total)}
  26. c.ServeJSON()
  27. return
  28. }
  29. func (c *ProductTypeController) Get() {
  30. ProductTyper := Product.ProductType{}
  31. c.ParseForm(&ProductTyper)
  32. if !ProductTyper.Read() {
  33. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ProductID E!"}
  34. c.ServeJSON()
  35. return
  36. }
  37. // 验证 TOKEY
  38. if ProductTyper.T_tokey != c.GetString("T_tokey") {
  39. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_tokey!"}
  40. c.ServeJSON()
  41. return
  42. }
  43. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductTyper}
  44. c.ServeJSON()
  45. return
  46. }
  47. func (c *ProductTypeController) Add() {
  48. ProductType_r := Product.ProductType{}
  49. c.ParseForm(&ProductType_r)
  50. if len(ProductType_r.T_tokey) != 8 {
  51. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_tokey!"}
  52. c.ServeJSON()
  53. return
  54. }
  55. ProductType_r.T_ProductID = lib.GetRandstring(8, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 0)
  56. ProductType_r.T_akey = lib.GetRandstring(32, "", 0)
  57. ProductType_r.Add()
  58. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductType_r}
  59. c.ServeJSON()
  60. return
  61. }
  62. func (c *ProductTypeController) Update() {
  63. ProductTyper := Product.ProductType{T_ProductID: c.GetString("T_ProductID")}
  64. if !ProductTyper.Read() {
  65. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_ProductID E!"}
  66. c.ServeJSON()
  67. return
  68. }
  69. // 验证 TOKEY
  70. if ProductTyper.T_tokey != c.GetString("T_tokey") {
  71. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_tokey!"}
  72. c.ServeJSON()
  73. return
  74. }
  75. c.ParseForm(&ProductTyper)
  76. if !ProductTyper.Update("T_name", "T_img", "T_prot", "T_TabData", "T_RelayData") {
  77. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "E!"}
  78. c.ServeJSON()
  79. return
  80. }
  81. Device.Device_CacheDelK_All(ProductTyper.T_ProductID) // 删除 关联设备缓存
  82. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
  83. c.ServeJSON()
  84. return
  85. }
  86. func (c *ProductTypeController) Delete() {
  87. ProductTyper := Product.ProductType{}
  88. c.ParseForm(&ProductTyper)
  89. if !ProductTyper.Read() {
  90. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_ProductID E!"}
  91. c.ServeJSON()
  92. return
  93. }
  94. // 验证 TOKEY
  95. if ProductTyper.T_tokey != c.GetString("T_tokey") {
  96. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_tokey!"}
  97. c.ServeJSON()
  98. return
  99. }
  100. if !ProductTyper.Delete() {
  101. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "E!"}
  102. c.ServeJSON()
  103. return
  104. }
  105. Device.Device_CacheDelK_All(ProductTyper.T_ProductID) // 删除 关联设备缓存
  106. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
  107. c.ServeJSON()
  108. return
  109. }