Skip to content

Commit

Permalink
增加WEB功能
Browse files Browse the repository at this point in the history
  • Loading branch information
keac committed Jan 16, 2024
1 parent 306334f commit 58304a7
Show file tree
Hide file tree
Showing 11 changed files with 648 additions and 298 deletions.
133 changes: 133 additions & 0 deletions api/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,139 @@ import (
"time"
)

// RunWeb 轻量web模式
func RunWeb(options *common.ENOptions) {
gin.SetMode(gin.TestMode)
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"code": 200,
"message": "OK",
})
})

r.GET("/api/info", func(ginCtx *gin.Context) {
//搜索参数
orgname := ginCtx.Query("orgname")
search := ginCtx.Query("search")
types := ginCtx.Query("type")
//筛选参数,查询参数
field := ginCtx.Query("field")
duplicate := ginCtx.Query("duplicate")

depth, _ := strconv.Atoi(ginCtx.Query("depth"))
InvestNum, _ := strconv.Atoi(ginCtx.Query("invest"))
holds := ginCtx.Query("holds")
supplier := ginCtx.Query("supplier")
if ginCtx.Query("show") == "" {
options.IsOnline = true
}

if ginCtx.Query("branch") == "true" {
options.IsGetBranch = true
} else {
options.IsGetBranch = false
}
outputs := ginCtx.Query("output")
if orgname == "" && search == "" {
ginCtx.JSON(400, gin.H{
"code": 400,
"message": "orgname or search is empty",
})
return
}
if search != "" {
if types == "" {
types = "aqc"
} else {
if _, ok := common.ScanTypeKeys[types]; !ok {
gologger.Errorf("没有这个%s查询方式\n支持列表\n%s", types, common.ScanTypeKeys)
ginCtx.JSON(500, gin.H{
"code": 500,
"message": fmt.Sprintf("没有%s方式,支持列表:%s", types, common.ScanTypeKeys),
})
return
}
}
orgname = runner.SearchByKeyword(search, types, options)
}
IsDuplicate := true
if duplicate == "true" {
IsDuplicate = true
} else if duplicate == "false" {
IsDuplicate = false
}
if types != "" {
options.ScanType = types
}
options.GetFlags = field
options.ScanType = "aqc"
options.GetType = []string{}
common.Parse(options)
reEnsList := make(map[string][]map[string]interface{})
searchList := []string{"enterprise_info"}
searchList = append(searchList, options.GetField...)
if holds == "true" {
searchList = append(searchList, "holds")
}
if supplier == "true" {
searchList = append(searchList, "supplier")
}
if InvestNum != 0 {
searchList = append(searchList, "invest")
}
gologger.Debugf("searchList: %s\n", searchList)
if depth == 0 {
depth = 0
}
reEnsList = runner.GetWebInfo(runner.InfoDto{OrgName: orgname, SearchType: options.GetType, SearchList: searchList, InvestNum: InvestNum, Depth: depth, IsGetNext: false, IsDuplicate: IsDuplicate, DuMap: make(map[string]map[string]bool)}, reEnsList, 0, options)

if len(reEnsList) > 0 {
if outputs == "file" {
ginCtx.Header("Content-Type", "application/octet-stream")
ginCtx.Header("Content-Disposition", "attachment; filename="+orgname+".xlsx")
ginCtx.Header("Content-Transfer-Encoding", "binary")
//fmt.Println(reEnsList)
err := outputfile.OutPutExcelByMergeJson(reEnsList, ginCtx.Writer)
if err != nil {
ginCtx.JSON(500, gin.H{
"code": 500,
"message": "导出失败",
})
return
}
} else {
ginCtx.JSON(200, gin.H{
"code": 200,
"message": "OK",
"data": reEnsList,
"columns": outputfile.ENSMapLN,
})
return
}

} else {

ginCtx.JSON(200, gin.H{
"code": 404,
"message": fmt.Sprintf("没有查询到 %s", orgname),
})
return
}

})
//先写死,之后再说~
options.ENConfig.Web.Port = "3000"
gologger.Infof("WEB 模式已开启 http://127.0.0.1:%s\n", options.ENConfig.Web.Port)
err := r.Run(":" + options.ENConfig.Web.Port)
if err != nil {
gologger.Fatalf("web api run error: %v", err)
return
} else {
gologger.Infof("web api run success\n\n")
}
}

func RunApiWeb(options *common.ENOptions) {
gin.SetMode(gin.TestMode)
r := gin.Default()
Expand Down
27 changes: 27 additions & 0 deletions common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ type ENOptions struct {
Deep int
IsJsonOutput bool
IsApiMode bool
IsWebMode bool
IsMergeOut bool //聚合
IsMerge bool //聚合
ClientMode string //客户端模式
IsOnline bool
ENConfig *ENConfig
}

Expand Down Expand Up @@ -80,6 +82,19 @@ type ENConfig struct {
Mongodb string `yaml:"mongodb"`
Redis string `yaml:"redis"`
}
Web struct {
Port string `yaml:"port"`
Database struct {
Type string `yaml:"type"`
Host string `yaml:"host"`
Port int `yaml:"port"`
User string `yaml:"user"`
Password string `yaml:"password"`
Name string `yaml:"name"`
DBFile string `yaml:"db_file"`
TablePrefix string `yaml:"table_prefix"`
}
}
Cookies struct {
Aldzs string `yaml:"aldzs"`
Xlb string `yaml:"xlb"`
Expand All @@ -95,6 +110,7 @@ type ENConfig struct {

type EnInfos struct {
Id primitive.ObjectID `bson:"_id"`
Search string
Name string
Pid string
LegalPerson string
Expand Down Expand Up @@ -175,6 +191,17 @@ var configYaml = `version: 0.4
common:
output: "" # 导出文件位置
field: [ ] # 查询字段 如["website"]
web:
port: "32000"
database:
type: "sqlite3"
host: ""
port: 0
user: ""
password: ""
name: ""
db_file: "enscan.db"
table_prefix: "e_"
cookies:
aiqicha: '' # 爱企查 Cookie
tianyancha: '' # 天眼查 Cookie
Expand Down
1 change: 1 addition & 0 deletions common/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func Flag(Info *ENOptions) {
flag.BoolVar(&Info.IsGetBranch, "branch", false, "查询分支机构(分公司)信息")
flag.BoolVar(&Info.IsSearchBranch, "is-branch", false, "深度查询分支机构信息(数量巨大)")
//web api
flag.BoolVar(&Info.IsWebMode, "web", false, "是否开启web")
flag.BoolVar(&Info.IsApiMode, "api", false, "是否API模式")
flag.StringVar(&Info.ClientMode, "client", "", "客户端模式通道 eg: task")
flag.BoolVar(&Info.IsDebug, "debug", false, "是否显示debug详细信息")
Expand Down
Loading

0 comments on commit 58304a7

Please sign in to comment.