ProductProt.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package controllers
  2. import (
  3. "Yunlot/RunCode/ctl"
  4. "Yunlot/conf"
  5. "Yunlot/lib"
  6. "Yunlot/logs"
  7. "Yunlot/models/Product"
  8. "archive/zip"
  9. "bytes"
  10. "encoding/json"
  11. beego "github.com/beego/beego/v2/server/web"
  12. "io"
  13. "os"
  14. "time"
  15. )
  16. type ProductProtController struct {
  17. beego.Controller
  18. }
  19. func (c *ProductProtController) ProductModeLists() {
  20. ProductModeLists_r, Total := Product.ProductModeLists()
  21. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: lib.C_Page(ProductModeLists_r, 0, 0, Total)}
  22. c.ServeJSON()
  23. return
  24. }
  25. func (c *ProductProtController) ProductLangList() {
  26. ProductProt_r, Total := Product.ProductLangLists()
  27. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: lib.C_Page(ProductProt_r, 0, 0, Total)}
  28. c.ServeJSON()
  29. return
  30. }
  31. func (c *ProductProtController) Get() {
  32. ProductProt := Product.ProductProt{}
  33. c.ParseForm(&ProductProt)
  34. if !ProductProt.Read_ID() {
  35. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ID E!"}
  36. c.ServeJSON()
  37. return
  38. }
  39. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductProt}
  40. c.ServeJSON()
  41. return
  42. }
  43. func (this *ProductProtController) Download() {
  44. ProductProt := Product.ProductProt{}
  45. this.ParseForm(&ProductProt)
  46. if !ProductProt.Read_ID() {
  47. this.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ID E!"}
  48. this.ServeJSON()
  49. return
  50. }
  51. if len(ProductProt.T_analysis) == 0 {
  52. this.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_analysis == 0 E!"}
  53. this.ServeJSON()
  54. return
  55. }
  56. ProductProt.T_log = time.Now().Format("2006-01-02 15:04:05") + " 导出协议包! \n"
  57. data, err := json.Marshal(ProductProt)
  58. if err != nil {
  59. this.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ProductProt json E!"}
  60. this.ServeJSON()
  61. return
  62. }
  63. // 创建一个新的zip文件
  64. buf := new(bytes.Buffer)
  65. zipWriter := zip.NewWriter(buf)
  66. // 添加文件到zip
  67. file1, err := zipWriter.Create("ProductProt.json")
  68. if err != nil {
  69. this.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ProductProt E!"}
  70. this.ServeJSON()
  71. return
  72. }
  73. file1.Write(data)
  74. //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  75. w, err := zipWriter.Create(ProductProt.T_analysis + ".so")
  76. if err != nil {
  77. this.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_analysis Create E!"}
  78. this.ServeJSON()
  79. return
  80. }
  81. logs.Println(conf.Analysis_Dir + ProductProt.T_analysis + ".so")
  82. // 打开待压缩的文件
  83. f, err := os.Open(conf.Analysis_Dir + ProductProt.T_analysis + ".so")
  84. if err != nil {
  85. this.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_analysis Dir E!"}
  86. this.ServeJSON()
  87. return
  88. }
  89. defer f.Close()
  90. //需要压缩更多文件到压缩包的话,只需要重复第三步即可,只需要更换下文件名
  91. if _, err := io.Copy(w, f); err != nil {
  92. this.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_analysis Copy E!"}
  93. this.ServeJSON()
  94. return
  95. }
  96. // 关闭zip writer
  97. err = zipWriter.Close()
  98. if err != nil {
  99. this.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "zipWriter E!"}
  100. this.ServeJSON()
  101. return
  102. }
  103. // 设置响应头
  104. this.Ctx.Output.Header("Content-Disposition", "attachment; filename="+ProductProt.T_name+".sozip")
  105. this.Ctx.Output.Header("Content-Type", "application/sozip")
  106. // 将zip文件内容作为响应返回
  107. io.Copy(this.Ctx.ResponseWriter, buf)
  108. return
  109. }
  110. func (c *ProductProtController) List() {
  111. PageIndex, _ := c.GetInt("PageIndex", 0)
  112. PageSize, _ := c.GetInt("PageSize", 10)
  113. ProductProtr := Product.ProductProt{}
  114. c.ParseForm(&ProductProtr)
  115. is, user_r := lib.Verification(c.Ctx.GetCookie("T_tokey"), c.GetString("T_tokey"))
  116. if !is {
  117. // 公共库
  118. ProductProtr.T_uuid = ""
  119. ProductProtr.T_State = 1
  120. ProductProt_r, Total := ProductProtr.Lists(PageIndex, PageSize)
  121. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: lib.C_Page(ProductProt_r, PageIndex, PageSize, Total)}
  122. c.ServeJSON()
  123. return
  124. }
  125. ProductProtr.T_uuid = user_r.T_uuid
  126. // 内部管理员时,UUID 为空,查看全部
  127. if user_r.T_user == "11111111111" {
  128. ProductProtr.T_uuid = ""
  129. }
  130. ProductProt_r, Total := ProductProtr.Lists(PageIndex, PageSize)
  131. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: lib.C_Page(ProductProt_r, PageIndex, PageSize, Total)}
  132. c.ServeJSON()
  133. return
  134. }
  135. func (c *ProductProtController) Update() {
  136. is, user_r := lib.Verification(c.Ctx.GetCookie("T_tokey"), c.GetString("T_tokey"))
  137. if !is {
  138. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_tokey!"}
  139. c.ServeJSON()
  140. return
  141. }
  142. // 为空时 添加
  143. if c.GetString("Id") == "" {
  144. ProductProt_r := Product.ProductProt{}
  145. c.ParseForm(&ProductProt_r)
  146. // 没有就添加
  147. ProductProt_r.T_uuid = user_r.T_uuid
  148. ProductProt_r.T_State = 2 // 2 等待审核
  149. ProductProt_r.T_log = time.Now().Format("2006-01-02 15:04:05") + " 开发者添加 |-&"
  150. ProductProt_r.Add()
  151. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductProt_r}
  152. c.ServeJSON()
  153. return
  154. }
  155. ProductProtr := Product.ProductProt{Id: c.GetString("Id")}
  156. if !ProductProtr.Read_ID() {
  157. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "Id E!"}
  158. c.ServeJSON()
  159. return
  160. }
  161. c.ParseForm(&ProductProtr)
  162. //println("T_State:",c.GetString("T_State"))
  163. if user_r.T_user == "11111111111" && c.GetString("T_State") != "" {
  164. ProductProtr = Product.ProductProt{Id: c.GetString("Id")}
  165. if !ProductProtr.Read_ID() {
  166. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "Id E!"}
  167. c.ServeJSON()
  168. return
  169. }
  170. T_State_int, _ := c.GetInt("T_State")
  171. ProductProtr.T_State = T_State_int
  172. // 管理员
  173. if ProductProtr.T_State == 1 {
  174. ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 审核通过 |-&"
  175. go ctl.BuildGenerate(&ProductProtr)
  176. } else if ProductProtr.T_State == 3 {
  177. ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 审核失败 |-&"
  178. }
  179. if !ProductProtr.Update("T_State", "T_log") {
  180. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "E!"}
  181. c.ServeJSON()
  182. return
  183. }
  184. } else {
  185. // 用户
  186. ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 开发者修改 |-&"
  187. ProductProtr.T_State = 2
  188. if !ProductProtr.Update("T_name", "T_mode", "T_lang", "T_analysis", "T_text", "T_describe", "T_State", "T_log") {
  189. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "E!"}
  190. c.ServeJSON()
  191. return
  192. }
  193. }
  194. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductProtr}
  195. c.ServeJSON()
  196. return
  197. }
  198. func (c *ProductProtController) Delete() {
  199. is, user_r := lib.Verification(c.Ctx.GetCookie("T_tokey"), c.GetString("T_tokey"))
  200. if !is {
  201. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_tokey!"}
  202. c.ServeJSON()
  203. return
  204. }
  205. ProductProtr := Product.ProductProt{}
  206. c.ParseForm(&ProductProtr)
  207. if !ProductProtr.Read_ID() {
  208. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ID E!"}
  209. c.ServeJSON()
  210. return
  211. }
  212. if ProductProtr.T_uuid != user_r.T_uuid {
  213. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_tokey!"}
  214. c.ServeJSON()
  215. return
  216. }
  217. if !ProductProtr.Delete() {
  218. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "E!"}
  219. c.ServeJSON()
  220. return
  221. }
  222. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
  223. c.ServeJSON()
  224. return
  225. }