ProductType.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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_uuid) != 8 {
  19. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  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_uuid != c.GetString("T_uuid") {
  39. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  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. //
  51. // if len(ProductType_r.T_uuid) != 8 {
  52. // c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  53. // c.ServeJSON()
  54. // return
  55. // }
  56. //
  57. // ProductType_r.T_ProductID = lib.GetRandstring(8, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 0)
  58. // ProductType_r.T_akey = lib.GetRandstring(32, "", 0)
  59. //
  60. // ProductType_r.Add()
  61. //
  62. // c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductType_r}
  63. // c.ServeJSON()
  64. // return
  65. //}
  66. func (c *ProductTypeController) Update() {
  67. T_ProductID := c.GetString("T_ProductID")
  68. // 验证 TOKEY
  69. if len(c.GetString("T_uuid")) == 0 {
  70. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  71. c.ServeJSON()
  72. return
  73. }
  74. if len(T_ProductID) == 0 {
  75. ProductType_r := Product.ProductType{}
  76. c.ParseForm(&ProductType_r)
  77. if len(ProductType_r.T_uuid) != 8 {
  78. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  79. c.ServeJSON()
  80. return
  81. }
  82. ProductType_r.T_ProductID = lib.GetRandstring(8, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 0)
  83. ProductType_r.T_akey = lib.GetRandstring(32, "", 0)
  84. ProductType_r.Add()
  85. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductType_r}
  86. c.ServeJSON()
  87. return
  88. }
  89. ProductTyper := Product.ProductType{T_ProductID: c.GetString("T_ProductID")}
  90. if !ProductTyper.Read() {
  91. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_ProductID E!"}
  92. c.ServeJSON()
  93. return
  94. }
  95. // 验证 TOKEY
  96. if ProductTyper.T_uuid != c.GetString("T_uuid") {
  97. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  98. c.ServeJSON()
  99. return
  100. }
  101. c.ParseForm(&ProductTyper)
  102. if !ProductTyper.Update("T_name", "T_img", "T_prot", "T_TabData", "T_RelayData") {
  103. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "E!"}
  104. c.ServeJSON()
  105. return
  106. }
  107. Device.Device_CacheDelK_All(ProductTyper.T_ProductID) // 删除 关联设备缓存
  108. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
  109. c.ServeJSON()
  110. return
  111. }
  112. func (c *ProductTypeController) Delete() {
  113. ProductTyper := Product.ProductType{}
  114. c.ParseForm(&ProductTyper)
  115. if !ProductTyper.Read() {
  116. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_ProductID E!"}
  117. c.ServeJSON()
  118. return
  119. }
  120. // 验证 TOKEY
  121. if ProductTyper.T_uuid != c.GetString("T_uuid") {
  122. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  123. c.ServeJSON()
  124. return
  125. }
  126. if !ProductTyper.Delete() {
  127. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "E!"}
  128. c.ServeJSON()
  129. return
  130. }
  131. Device.Device_CacheDelK_All(ProductTyper.T_ProductID) // 删除 关联设备缓存
  132. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
  133. c.ServeJSON()
  134. return
  135. }