background/controller/openapi.go

195 lines
3.7 KiB
Go

package controller
import (
"background/utils"
"container/list"
"encoding/xml"
"io"
"log"
"github.com/gin-gonic/gin"
)
type OpenApiController struct {
}
type DiffRequest struct {
Origin string `json:"origin"`
New string `json:"new"`
}
//from: 发送人邮箱
//to:接收邮件,可以是"290198252@qq.com;29019822@qq.com;2901982@qq.com" 邮箱之间用分号隔开
//template:模板名字
//content :网页模板的参数 key-value结构
//temp_data 模板内具体要替换的变量名字 Key-value结构
//generate 是否生成静态html
func (this *OpenApiController) OndiffCallback(c *gin.Context) {
var req DiffRequest
var resp RespBase
defer func() {
c.JSON(200, resp)
}()
e := c.Bind(&req)
if e != nil {
log.Println(e.Error())
resp.Msg = "ParaErr"
return
}
resp.Msg = "OK"
}
type ReqDDL struct {
Data string `json:"data"`
}
func (this *OpenApiController) DDL2ORM(c *gin.Context) {
var req ReqDDL
resp := RespBase{
Msg: "err",
}
defer func() {
c.JSON(200, resp)
}()
e := c.Bind(&req)
if e != nil {
log.Println(e.Error())
resp.Msg = "ParaErr"
return
}
tbname, fields := utils.DDL2Field(req.Data)
log.Print(tbname, fields)
gocode := utils.CreateGoStruct(tbname,fields)
resp.Data = gocode
resp.Msg = "OK"
resp.Status = 0
}
func getAttr(name string, attr []xml.Attr) string {
for _, a := range attr {
if a.Name.Local == name {
return a.Value
}
}
return ""
}
type Widget struct{
Name string
Class string
Parent *Widget
Childs list.List
}
func RangeWidget(p *Widget ) string{
if nil == p{
return ""
}
ret := ""
parent := ""
tmp := p
for ;tmp != nil;tmp = tmp.Parent{
parent = tmp.Class + " #" + tmp.Name + " " + parent
}
ret += parent + " \r\n{\r\n}\r\n"
if(p.Childs.Len() > 0){
wid := p.Childs.Front()
for ;wid != nil;wid = wid.Next(){
tmp := wid.Value.(*Widget)
ret += RangeWidget(tmp)
}
}
// log.Print(ret)
return ret
}
func GetUifrom(path io.Reader) string{
list := list.List{}
decoder := xml.NewDecoder(path)
for {
token, _ := decoder.Token()
if token == nil {
break
}
switch startElement := token.(type) {
case xml.StartElement:
if(startElement.Name.Local == "widget"){
if(list.Len() == 0){
ins := new(Widget)
ins.Class = getAttr("class",startElement.Attr)
ins.Name = getAttr("name",startElement.Attr)
ins.Parent = nil
list.PushBack(ins)
}else{
ins := new(Widget)
ins.Class = getAttr("class",startElement.Attr)
ins.Name = getAttr("name",startElement.Attr)
ins.Parent = list.Back().Value.(*Widget)
list.Back().Value.(*Widget).Childs.PushBack(ins)
list.PushBack(ins)
}
}
break
case xml.EndElement:
if(startElement.Name.Local == "widget"){
if(list.Len() > 0){
if(list.Back().Value.(*Widget).Parent != nil){
list.Remove(list.Back())
}
}
}
break
}
}
p := list.Back().Value.(*Widget)
ret := RangeWidget(p)
log.Print(ret)
return ret
}
func (this *OpenApiController) DDL2Markdown(c *gin.Context) {
var req ReqDDL
resp := RespBase{
Msg: "err",
}
defer func() {
c.JSON(200, resp)
}()
e := c.Bind(&req)
if e != nil {
log.Println(e.Error())
resp.Msg = "ParaErr"
return
}
tbname, fields := utils.DDL2Field(req.Data)
log.Print(tbname, fields)
gocode := utils.CreateMarkdownTable(tbname,fields)
resp.Data = gocode
resp.Msg = "OK"
resp.Status = 0
}
func (this *OpenApiController) UI2CSS(c *gin.Context) {
f, err := c.FormFile("ui") //根据name返回给第一个文件
if err != nil {
c.String(200,"上传文件错误")
return
}
file,e := f.Open()
if nil != e{
c.String(200,"上传文件错误")
return
}
ret := GetUifrom(file)
c.String(200,ret)
}