diff --git a/docker-compose.yml b/docker-compose.yml index 23ed86b..a3277a0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,8 +6,6 @@ services: context: ./ container_name: json-gen restart: always - ports: - - '8777:8000' networks: - fpm_proxy-tier environment: diff --git a/main.go b/main.go index e09b0c2..ddf6891 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" web "json-gen/dist" "net/http" @@ -30,5 +31,37 @@ func main() { c.String(http.StatusOK, string(res)) }) r.StaticFS("/", http.FS(web.Static)) + r.Use(Cors()) r.Run(":8000") } + +func Cors() gin.HandlerFunc { + return func(c *gin.Context) { + method := c.Request.Method + origin := c.Request.Header.Get("Origin") //请求头部 + if origin != "" { + //接收客户端发送的origin (重要!) + c.Writer.Header().Set("Access-Control-Allow-Origin", origin) + //服务器支持的所有跨域请求的方法 + c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE,UPDATE") + //允许跨域设置可以返回其他子段,可以自定义字段 + c.Header("Access-Control-Allow-Headers", "Authorization, Content-Length, X-CSRF-Token, Token,session,Accept, Origin, Host, Connection, Accept-Encoding, Accept-Language,DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Pragma") + // 允许浏览器(客户端)可以解析的头部 (重要) + c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers") + //允许客户端传递校验信息比如 cookie (重要) + c.Header("Access-Control-Allow-Credentials", "true") + } + + //允许类型校验 + if method == "OPTIONS" { + c.String(http.StatusOK, "OK!") + } + + defer func() { + if err := recover(); err != nil { + fmt.Printf("Panic info is: %v", err) + } + }() + c.Next() + } +} diff --git a/src/components/HelloWorld.vue b/src/components/HelloWorld.vue index fa92813..0761ce2 100644 --- a/src/components/HelloWorld.vue +++ b/src/components/HelloWorld.vue @@ -78,7 +78,7 @@ import axios from "axios"; // import "highlight.js/styles/atelier-plateau-light.css"; const cli = axios.create({ - baseURL: "http://json.cobb.wang/", + baseURL: "https://json.cobb.wang/", timeout: 1000, headers: { "content-type": "application/json" }, }); @@ -182,6 +182,8 @@ export default { this.output["model"] = this.handleModelOutput(v); this.output["service"] = this.handleServiceOutput(v); this.output["api"] = this.handleApiOutput(v); + this.output["route"] = this.handleRouteOutput(v); + this.output["js-api"] = this.handleJsApi(v); this.renderKey++; this.activeName = ""; this.dialogVisible = true; @@ -207,9 +209,72 @@ export default { this.output["model"] = this.handleModelOutput(v); this.output["service"] = this.handleServiceOutput(v); this.output["api"] = this.handleApiOutput(v); - console.log("output", this.output); + this.output["route"] = this.handleRouteOutput(v); + this.output["js-api"] = this.handleJsApi(v); this.schemaKey = Date.now(); }, + handleRouteOutput(v) { + const tableName = toCamelCase(v.title); + const lowTableName = v.title; + const Output = ` + { + ${lowTableName} := admin.Group("${lowTableName}") + ${lowTableName}.POST("list", a.${tableName}.List${tableName}) + ${lowTableName}.POST("create", a.${tableName}.Create${tableName}) + ${lowTableName}.POST("detail", a.${tableName}.Get${tableName}One) + ${lowTableName}.POST("del", a.${tableName}.Delete${tableName}) + } + `; + return Output; + }, + handleJsApi(v) { + const tableName = toCamelCase(v.title); + const lowTableName = v.title; + const Output = ` + import request from '@/utils/request'; + export function get${tableName}(data) { + return request({ + url: '/api/admin/${lowTableName}/detail', + method: 'post', + data: data + }) + } + + export function list${tableName}(data) { + return request({ + url: '/api/admin/${lowTableName}/list', + method: 'post', + data: data + }) + } + + export function list${tableName}Export(data) { + return request({ + url: '/api/admin/${lowTableName}/list', + method: 'post', + data: data, + responseType: 'blob' + }) + } + + export function create${tableName}(data) { + return request({ + url: '/api/admin/${lowTableName}/create', + method: 'post', + data: data + }) + } + + export function del${tableName}(data) { + return request({ + url: '/api/admin/${lowTableName}/del', + method: 'post', + data: data + }) + } + `; + return Output; + }, handleApiOutput(v) { const tableName = toCamelCase(v.title); const apiOutput = `