package controllers import ( "Yunlot/conf" "Yunlot/lib" "Yunlot/logs" "Yunlot/models/Product" "archive/zip" "encoding/json" "strings" beego "github.com/beego/beego/v2/server/web" "io" "os" ) type ProductProtController struct { beego.Controller } func (c *ProductProtController) ProductLangList() { ProductProt_r, Total := Product.ProductLangLists() c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: lib.C_Page(ProductProt_r, 0, 0, Total)} c.ServeJSON() return } func (c *ProductProtController) Get() { ProductProt := Product.ProductProt{} c.ParseForm(&ProductProt) if !ProductProt.Read() { c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ID E!"} c.ServeJSON() return } // 验证 TOKEY if ProductProt.T_uuid != c.GetString("T_uuid") { c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"} c.ServeJSON() return } c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductProt} c.ServeJSON() return } func (c *ProductProtController) List() { PageIndex, _ := c.GetInt("PageIndex", 0) PageSize, _ := c.GetInt("PageSize", 10) ProductProtr := Product.ProductProt{} c.ParseForm(&ProductProtr) // 验证 TOKEY if c.Ctx.Input.Host() != "127.0.0.1" { if len(ProductProtr.T_uuid) != 8 { c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"} c.ServeJSON() return } } ProductProt_r, Total := ProductProtr.Lists(PageIndex, PageSize) c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: lib.C_Page(ProductProt_r, PageIndex, PageSize, Total)} c.ServeJSON() return } func (c *ProductProtController) Import() { // 验证 TOKEY if len(c.GetString("T_uuid")) == 0 { c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"} c.ServeJSON() return } ProductProt_r := Product.ProductProt{} c.ParseForm(&ProductProt_r) if len(ProductProt_r.T_uuid) != 8 { c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"} c.ServeJSON() return } // 获取上传的zip文件 zipFile, _, err := c.GetFile("sozip") if err != nil { logs.Println("Error getting zip file:", err) c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "sozip!"} c.ServeJSON() return } defer zipFile.Close() // 创建一个临时文件来保存上传的zip文件 tempFile, err := os.Create("temp.zip") if err != nil { logs.Println("Error creating temp file:", err) c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "sozip!"} c.ServeJSON() return } defer tempFile.Close() // 将上传的zip文件内容复制到临时文件中 _, err = io.Copy(tempFile, zipFile) if err != nil { logs.Println("Error copying zip file to temp file:", err) c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "Copy!"} c.ServeJSON() return } // 打开临时文件 zipReader, err := zip.OpenReader("temp.zip") if err != nil { logs.Println("Error opening temp file:", err) c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "OpenReader!"} c.ServeJSON() return } defer zipReader.Close() // 解压文件 for _, file := range zipReader.File { logs.Println("file.Name:", file.Name) if file.Name == "ProductProt.json" { // 打开文件 rc, err := file.Open() if err != nil { c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "file.Open!"} c.ServeJSON() return } defer rc.Close() var contents strings.Builder _, err = io.Copy(&contents, rc) if err != nil { c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "file.Open!"} c.ServeJSON() return } logs.Println("contents:", contents.String()) // 处理JSON文件 err = json.Unmarshal([]byte(contents.String()), &ProductProt_r) // 处理jsonData,例如打印到控制台 logs.Println("jsonData:", ProductProt_r) } else { // 检查文件是否存在 if _, err := os.Stat(conf.Analysis_Dir + file.Name); os.IsNotExist(err) { // 打开zip文件中的文件 zippedFile, err := file.Open() if err != nil { logs.Println("Error opening zipped file:", err) c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "opening zipped file!"} c.ServeJSON() return } defer zippedFile.Close() // 创建一个新文件来保存解压后的文件 newFile, err := os.Create(conf.Analysis_Dir + file.Name) if err != nil { logs.Println("Error creating new file:", err) c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "Error creating new file!"} c.ServeJSON() return } defer newFile.Close() // 将解压后的文件内容复制到新文件中 _, err = io.Copy(newFile, zippedFile) if err != nil { logs.Println("Error copying zipped file to new file:", err) c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "io.Copy!"} c.ServeJSON() return } } else { logs.Println("文件已经存在:", conf.Analysis_Dir+file.Name) } } } ProductProt_r.Id = 0 ProductProt_r.T_uuid = c.GetString("T_uuid") ProductProt_r.Add() c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductProt_r} c.ServeJSON() return } func (c *ProductProtController) Update() { Id, _ := c.GetInt("Id", 0) // 验证 TOKEY if len(c.GetString("T_uuid")) != 8 { c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"} c.ServeJSON() return } if Id <= 0 { // 添加设备 ProductProt_r := Product.ProductProt{} c.ParseForm(&ProductProt_r) ProductProt_r.Id = 0 ProductProt_r.Add() c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductProt_r} c.ServeJSON() return } ProductProtr := Product.ProductProt{Id: Id} if !ProductProtr.Read() { c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_ProductID E!"} c.ServeJSON() return } // 验证 TOKEY if ProductProtr.T_uuid != c.GetString("T_uuid") { c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"} c.ServeJSON() return } c.ParseForm(&ProductProtr) if !ProductProtr.Update("T_name", "T_mode", "T_lang", "T_analysis", "T_text", "T_describe") { c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "E!"} c.ServeJSON() return } lib.DownloadSo(ProductProtr.T_analysis) // 下载SO 文件 c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"} c.ServeJSON() return } func (c *ProductProtController) Delete() { ProductProtr := Product.ProductProt{} c.ParseForm(&ProductProtr) if !ProductProtr.Read() { c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ID E!"} c.ServeJSON() return } // 验证 TOKEY if ProductProtr.T_uuid != c.GetString("T_uuid") { c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_uuid!"} c.ServeJSON() return } if !ProductProtr.Delete() { c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "E!"} c.ServeJSON() return } c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"} c.ServeJSON() return }