ProductProt.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package controllers
  2. import (
  3. "Yunlot/conf"
  4. "Yunlot/lib"
  5. "Yunlot/logs"
  6. "Yunlot/models/Product"
  7. "archive/zip"
  8. "encoding/json"
  9. "strings"
  10. beego "github.com/beego/beego/v2/server/web"
  11. "io"
  12. "os"
  13. )
  14. type ProductProtController struct {
  15. beego.Controller
  16. }
  17. func (c *ProductProtController) ProductLangList() {
  18. ProductProt_r, Total := Product.ProductLangLists()
  19. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: lib.C_Page(ProductProt_r, 0, 0, Total)}
  20. c.ServeJSON()
  21. return
  22. }
  23. func (c *ProductProtController) Get() {
  24. ProductProt := Product.ProductProt{}
  25. c.ParseForm(&ProductProt)
  26. if !ProductProt.Read() {
  27. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ID E!"}
  28. c.ServeJSON()
  29. return
  30. }
  31. // 验证 TOKEY
  32. if ProductProt.T_uuid != c.GetString("T_uuid") {
  33. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  34. c.ServeJSON()
  35. return
  36. }
  37. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductProt}
  38. c.ServeJSON()
  39. return
  40. }
  41. func (c *ProductProtController) List() {
  42. PageIndex, _ := c.GetInt("PageIndex", 0)
  43. PageSize, _ := c.GetInt("PageSize", 10)
  44. ProductProtr := Product.ProductProt{}
  45. c.ParseForm(&ProductProtr)
  46. // 验证 TOKEY
  47. if c.Ctx.Input.Host() != "127.0.0.1" {
  48. if len(ProductProtr.T_uuid) != 8 {
  49. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  50. c.ServeJSON()
  51. return
  52. }
  53. }
  54. ProductProt_r, Total := ProductProtr.Lists(PageIndex, PageSize)
  55. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: lib.C_Page(ProductProt_r, PageIndex, PageSize, Total)}
  56. c.ServeJSON()
  57. return
  58. }
  59. func (c *ProductProtController) Import() {
  60. // 验证 TOKEY
  61. if len(c.GetString("T_uuid")) == 0 {
  62. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  63. c.ServeJSON()
  64. return
  65. }
  66. ProductProt_r := Product.ProductProt{}
  67. c.ParseForm(&ProductProt_r)
  68. if len(ProductProt_r.T_uuid) != 8 {
  69. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  70. c.ServeJSON()
  71. return
  72. }
  73. // 获取上传的zip文件
  74. zipFile, _, err := c.GetFile("sozip")
  75. if err != nil {
  76. logs.Println("Error getting zip file:", err)
  77. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "sozip!"}
  78. c.ServeJSON()
  79. return
  80. }
  81. defer zipFile.Close()
  82. // 创建一个临时文件来保存上传的zip文件
  83. tempFile, err := os.Create("temp.zip")
  84. if err != nil {
  85. logs.Println("Error creating temp file:", err)
  86. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "sozip!"}
  87. c.ServeJSON()
  88. return
  89. }
  90. defer tempFile.Close()
  91. // 将上传的zip文件内容复制到临时文件中
  92. _, err = io.Copy(tempFile, zipFile)
  93. if err != nil {
  94. logs.Println("Error copying zip file to temp file:", err)
  95. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "Copy!"}
  96. c.ServeJSON()
  97. return
  98. }
  99. // 打开临时文件
  100. zipReader, err := zip.OpenReader("temp.zip")
  101. if err != nil {
  102. logs.Println("Error opening temp file:", err)
  103. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "OpenReader!"}
  104. c.ServeJSON()
  105. return
  106. }
  107. defer zipReader.Close()
  108. // 解压文件
  109. for _, file := range zipReader.File {
  110. logs.Println("file.Name:", file.Name)
  111. if file.Name == "ProductProt.json" {
  112. // 打开文件
  113. rc, err := file.Open()
  114. if err != nil {
  115. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "file.Open!"}
  116. c.ServeJSON()
  117. return
  118. }
  119. defer rc.Close()
  120. var contents strings.Builder
  121. _, err = io.Copy(&contents, rc)
  122. if err != nil {
  123. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "file.Open!"}
  124. c.ServeJSON()
  125. return
  126. }
  127. logs.Println("contents:", contents.String())
  128. // 处理JSON文件
  129. err = json.Unmarshal([]byte(contents.String()), &ProductProt_r)
  130. // 处理jsonData,例如打印到控制台
  131. logs.Println("jsonData:", ProductProt_r)
  132. } else {
  133. // 检查文件是否存在
  134. if _, err := os.Stat(conf.Analysis_Dir + file.Name); os.IsNotExist(err) {
  135. // 打开zip文件中的文件
  136. zippedFile, err := file.Open()
  137. if err != nil {
  138. logs.Println("Error opening zipped file:", err)
  139. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "opening zipped file!"}
  140. c.ServeJSON()
  141. return
  142. }
  143. defer zippedFile.Close()
  144. // 创建一个新文件来保存解压后的文件
  145. newFile, err := os.Create(conf.Analysis_Dir + file.Name)
  146. if err != nil {
  147. logs.Println("Error creating new file:", err)
  148. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "Error creating new file!"}
  149. c.ServeJSON()
  150. return
  151. }
  152. defer newFile.Close()
  153. // 将解压后的文件内容复制到新文件中
  154. _, err = io.Copy(newFile, zippedFile)
  155. if err != nil {
  156. logs.Println("Error copying zipped file to new file:", err)
  157. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "io.Copy!"}
  158. c.ServeJSON()
  159. return
  160. }
  161. } else {
  162. logs.Println("文件已经存在:", conf.Analysis_Dir+file.Name)
  163. }
  164. }
  165. }
  166. ProductProt_r.Id = 0
  167. ProductProt_r.T_uuid = c.GetString("T_uuid")
  168. ProductProt_r.Add()
  169. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductProt_r}
  170. c.ServeJSON()
  171. return
  172. }
  173. func (c *ProductProtController) Update() {
  174. Id, _ := c.GetInt("Id", 0)
  175. // 验证 TOKEY
  176. if len(c.GetString("T_uuid")) != 8 {
  177. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  178. c.ServeJSON()
  179. return
  180. }
  181. if Id <= 0 {
  182. // 添加设备
  183. ProductProt_r := Product.ProductProt{}
  184. c.ParseForm(&ProductProt_r)
  185. ProductProt_r.Id = 0
  186. ProductProt_r.Add()
  187. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductProt_r}
  188. c.ServeJSON()
  189. return
  190. }
  191. ProductProtr := Product.ProductProt{Id: Id}
  192. if !ProductProtr.Read() {
  193. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_ProductID E!"}
  194. c.ServeJSON()
  195. return
  196. }
  197. // 验证 TOKEY
  198. if ProductProtr.T_uuid != c.GetString("T_uuid") {
  199. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  200. c.ServeJSON()
  201. return
  202. }
  203. c.ParseForm(&ProductProtr)
  204. if !ProductProtr.Update("T_name", "T_mode", "T_lang", "T_analysis", "T_text", "T_describe") {
  205. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "E!"}
  206. c.ServeJSON()
  207. return
  208. }
  209. lib.DownloadSo(ProductProtr.T_analysis) // 下载SO 文件
  210. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
  211. c.ServeJSON()
  212. return
  213. }
  214. func (c *ProductProtController) Delete() {
  215. ProductProtr := Product.ProductProt{}
  216. c.ParseForm(&ProductProtr)
  217. if !ProductProtr.Read() {
  218. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ID E!"}
  219. c.ServeJSON()
  220. return
  221. }
  222. // 验证 TOKEY
  223. if ProductProtr.T_uuid != c.GetString("T_uuid") {
  224. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"}
  225. c.ServeJSON()
  226. return
  227. }
  228. if !ProductProtr.Delete() {
  229. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "E!"}
  230. c.ServeJSON()
  231. return
  232. }
  233. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
  234. c.ServeJSON()
  235. return
  236. }