ProductType.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package controllers
  2. import (
  3. "AIOTCOER/conf"
  4. "AIOTCOER/lib"
  5. "AIOTCOER/models/Device"
  6. "AIOTCOER/models/Product"
  7. beego "github.com/beego/beego/v2/server/web"
  8. )
  9. type ProductTypeController struct {
  10. beego.Controller
  11. }
  12. // *T_uuid T_name
  13. func (c *ProductTypeController) List() {
  14. PageIndex, _ := c.GetInt("PageIndex", 0)
  15. PageSize, _ := c.GetInt("PageSize", 10)
  16. ProductTyper := Product.ProductType{}
  17. c.ParseForm(&ProductTyper)
  18. ProductTyper.T_uuid = lib.Take_one(c.Ctx.GetCookie("T_uuid"), c.GetString("T_uuid"))
  19. //// 验证 TOKEY
  20. //if c.Ctx.Input.Host() != "127.0.0.1" {
  21. if len(ProductTyper.T_uuid) == 0 {
  22. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  23. c.ServeJSON()
  24. return
  25. }
  26. //}
  27. ProductType_r, Total := ProductTyper.Lists(PageIndex, PageSize)
  28. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: lib.C_Page(ProductType_r, PageIndex, PageSize, Total)}
  29. c.ServeJSON()
  30. return
  31. }
  32. func (c *ProductTypeController) Get() {
  33. ProductTyper := Product.ProductType{}
  34. c.ParseForm(&ProductTyper)
  35. if !ProductTyper.Read() {
  36. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ProductID E!"}
  37. c.ServeJSON()
  38. return
  39. }
  40. // 验证 T_uuid
  41. T_uuid := lib.Take_one(c.Ctx.GetCookie("T_uuid"), c.GetString("T_uuid"))
  42. if len(T_uuid) != 0 {
  43. if ProductTyper.T_uuid != T_uuid {
  44. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  45. c.ServeJSON()
  46. return
  47. }
  48. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductTyper}
  49. c.ServeJSON()
  50. return
  51. }
  52. ProductTyper_r := Product.ProductType_R{
  53. T_name: ProductTyper.T_name,
  54. T_panel: ProductTyper.T_panel,
  55. T_TabDataJson: ProductTyper.T_TabDataJson,
  56. CreateTime: ProductTyper.CreateTime,
  57. UpdateTime: ProductTyper.UpdateTime,
  58. }
  59. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductTyper_r}
  60. c.ServeJSON()
  61. return
  62. }
  63. //func (c *ProductTypeController) Add() {
  64. // ProductType_r := Product.ProductType{}
  65. // c.ParseForm(&ProductType_r)
  66. //
  67. // if len(ProductType_r.T_uuid) != 8 {
  68. // c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  69. // c.ServeJSON()
  70. // return
  71. // }
  72. //
  73. // ProductType_r.T_ProductID = lib.GetRandstring(8, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 0)
  74. // ProductType_r.T_akey = lib.GetRandstring(32, "", 0)
  75. //
  76. // ProductType_r.Add()
  77. //
  78. // c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductType_r}
  79. // c.ServeJSON()
  80. // return
  81. //}
  82. func (c *ProductTypeController) Update() {
  83. T_ProductID := c.GetString("T_ProductID")
  84. T_uuid := lib.Take_one(c.Ctx.GetCookie("T_uuid"), c.GetString("T_uuid"))
  85. //logs.Println("T_uuid:",T_uuid)
  86. // 验证 TOKEY
  87. if len(T_uuid) == 0 {
  88. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  89. c.ServeJSON()
  90. return
  91. }
  92. if len(T_ProductID) == 0 {
  93. if !conf.Config_AddProductType {
  94. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "此服务节点负载过大,禁止添加新的产品,请选择其他节点添加!"}
  95. c.ServeJSON()
  96. return
  97. }
  98. ProductType_r := Product.ProductType{}
  99. c.ParseForm(&ProductType_r)
  100. ProductType_r.T_uuid = T_uuid
  101. ProductType_r.T_ProductID = lib.GetRandstring(8, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 0)
  102. ProductType_r.T_ProductKey = lib.GetRandstring(32, "", 0)
  103. ProductType_r.T_state = 1
  104. if len(ProductType_r.T_uuid) == 0 {
  105. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  106. c.ServeJSON()
  107. return
  108. }
  109. ProductType_r.Add()
  110. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductType_r}
  111. c.ServeJSON()
  112. return
  113. }
  114. ProductTyper := Product.ProductType{T_ProductID: c.GetString("T_ProductID")}
  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 != T_uuid {
  122. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid !"}
  123. c.ServeJSON()
  124. return
  125. }
  126. c.ParseForm(&ProductTyper)
  127. if !ProductTyper.Update("T_name","T_prot", "T_mode", "T_TabData") {
  128. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "E!"}
  129. c.ServeJSON()
  130. return
  131. }
  132. Device.Device_CacheDelK_All(ProductTyper.T_ProductID) // 删除 关联设备缓存
  133. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
  134. c.ServeJSON()
  135. return
  136. }
  137. func (c *ProductTypeController) Delete() {
  138. ProductTyper := Product.ProductType{}
  139. c.ParseForm(&ProductTyper)
  140. T_uuid := lib.Take_one(c.Ctx.GetCookie("T_uuid"), c.GetString("T_uuid"))
  141. if !ProductTyper.Read() {
  142. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_ProductID E!"}
  143. c.ServeJSON()
  144. return
  145. }
  146. // 验证 TOKEY
  147. if ProductTyper.T_uuid != T_uuid {
  148. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  149. c.ServeJSON()
  150. return
  151. }
  152. ProductTyper.T_state = 0
  153. if !ProductTyper.Update("T_state") {
  154. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "E!"}
  155. c.ServeJSON()
  156. return
  157. }
  158. //Device.Device_CacheDelK_All(ProductTyper.T_ProductID) // 删除 关联设备缓存
  159. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
  160. c.ServeJSON()
  161. return
  162. }