123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- 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
- }
|