123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- package controllers
- import (
- "Yunlot/RunCode/ctl"
- "Yunlot/conf"
- "Yunlot/lib"
- "Yunlot/logs"
- "Yunlot/models/Product"
- "archive/zip"
- "bytes"
- "encoding/json"
- beego "github.com/beego/beego/v2/server/web"
- "io"
- "os"
- "time"
- )
- type ProductProtController struct {
- beego.Controller
- }
- func (c *ProductProtController) ProductModeLists() {
- ProductModeLists_r, Total := Product.ProductModeLists()
- c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: lib.C_Page(ProductModeLists_r, 0, 0, Total)}
- c.ServeJSON()
- return
- }
- 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_ID() {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ID E!"}
- c.ServeJSON()
- return
- }
- c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductProt}
- c.ServeJSON()
- return
- }
- func (this *ProductProtController) Download() {
- ProductProt := Product.ProductProt{}
- this.ParseForm(&ProductProt)
- if !ProductProt.Read_ID() {
- this.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ID E!"}
- this.ServeJSON()
- return
- }
- if len(ProductProt.T_analysis) == 0 {
- this.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_analysis == 0 E!"}
- this.ServeJSON()
- return
- }
- ProductProt.T_log = time.Now().Format("2006-01-02 15:04:05") + " 导出协议包! \n"
- data, err := json.Marshal(ProductProt)
- if err != nil {
- this.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ProductProt json E!"}
- this.ServeJSON()
- return
- }
- // 创建一个新的zip文件
- buf := new(bytes.Buffer)
- zipWriter := zip.NewWriter(buf)
- // 添加文件到zip
- file1, err := zipWriter.Create("ProductProt.json")
- if err != nil {
- this.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ProductProt E!"}
- this.ServeJSON()
- return
- }
- file1.Write(data)
- //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
- w, err := zipWriter.Create(ProductProt.T_analysis + ".so")
- if err != nil {
- this.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_analysis Create E!"}
- this.ServeJSON()
- return
- }
- logs.Println(conf.Analysis_Dir + ProductProt.T_analysis + ".so")
- // 打开待压缩的文件
- f, err := os.Open(conf.Analysis_Dir + ProductProt.T_analysis + ".so")
- if err != nil {
- this.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_analysis Dir E!"}
- this.ServeJSON()
- return
- }
- defer f.Close()
- //需要压缩更多文件到压缩包的话,只需要重复第三步即可,只需要更换下文件名
- if _, err := io.Copy(w, f); err != nil {
- this.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_analysis Copy E!"}
- this.ServeJSON()
- return
- }
- // 关闭zip writer
- err = zipWriter.Close()
- if err != nil {
- this.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "zipWriter E!"}
- this.ServeJSON()
- return
- }
- // 设置响应头
- this.Ctx.Output.Header("Content-Disposition", "attachment; filename="+ProductProt.T_name+".sozip")
- this.Ctx.Output.Header("Content-Type", "application/sozip")
- // 将zip文件内容作为响应返回
- io.Copy(this.Ctx.ResponseWriter, buf)
- return
- }
- func (c *ProductProtController) List() {
- PageIndex, _ := c.GetInt("PageIndex", 0)
- PageSize, _ := c.GetInt("PageSize", 10)
- ProductProtr := Product.ProductProt{}
- c.ParseForm(&ProductProtr)
- is, user_r := lib.Verification(c.Ctx.GetCookie("T_tokey"), c.GetString("T_tokey"))
- if !is {
- // 公共库
- ProductProtr.T_uuid = ""
- ProductProtr.T_State = 1
- 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
- }
- ProductProtr.T_uuid = user_r.T_uuid
- // 内部管理员时,UUID 为空,查看全部
- if user_r.T_user == "11111111111" {
- ProductProtr.T_uuid = ""
- }
- 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) Update() {
- is, user_r := lib.Verification(c.Ctx.GetCookie("T_tokey"), c.GetString("T_tokey"))
- if !is {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_tokey!"}
- c.ServeJSON()
- return
- }
- // 为空时 添加
- if c.GetString("Id") == "" {
- ProductProt_r := Product.ProductProt{}
- c.ParseForm(&ProductProt_r)
- // 没有就添加
- ProductProt_r.T_uuid = user_r.T_uuid
- ProductProt_r.T_State = 2 // 2 等待审核
- ProductProt_r.T_log = time.Now().Format("2006-01-02 15:04:05") + " 开发者添加 |-&"
- ProductProt_r.Add()
- c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductProt_r}
- c.ServeJSON()
- return
- }
- ProductProtr := Product.ProductProt{Id: c.GetString("Id")}
- if !ProductProtr.Read_ID() {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "Id E!"}
- c.ServeJSON()
- return
- }
- c.ParseForm(&ProductProtr)
- //println("T_State:",c.GetString("T_State"))
- if user_r.T_user == "11111111111" && c.GetString("T_State") != "" {
- ProductProtr = Product.ProductProt{Id: c.GetString("Id")}
- if !ProductProtr.Read_ID() {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "Id E!"}
- c.ServeJSON()
- return
- }
- T_State_int, _ := c.GetInt("T_State")
- ProductProtr.T_State = T_State_int
- // 管理员
- if ProductProtr.T_State == 1 {
- ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 审核通过 |-&"
- go ctl.BuildGenerate(&ProductProtr)
- } else if ProductProtr.T_State == 3 {
- ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 审核失败 |-&"
- }
- if !ProductProtr.Update("T_State", "T_log") {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "E!"}
- c.ServeJSON()
- return
- }
- } else {
- // 用户
- ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 开发者修改 |-&"
- ProductProtr.T_State = 2
- if !ProductProtr.Update("T_name", "T_mode", "T_lang", "T_analysis", "T_text", "T_describe", "T_State", "T_log") {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "E!"}
- c.ServeJSON()
- return
- }
- }
- c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductProtr}
- c.ServeJSON()
- return
- }
- func (c *ProductProtController) Delete() {
- is, user_r := lib.Verification(c.Ctx.GetCookie("T_tokey"), c.GetString("T_tokey"))
- if !is {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_tokey!"}
- c.ServeJSON()
- return
- }
- ProductProtr := Product.ProductProt{}
- c.ParseForm(&ProductProtr)
- if !ProductProtr.Read_ID() {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ID E!"}
- c.ServeJSON()
- return
- }
- if ProductProtr.T_uuid != user_r.T_uuid {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_tokey!"}
- 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
- }
|