router.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package routers
  2. import (
  3. "fmt"
  4. "../comm"
  5. _ "../docs"
  6. "../middleware/jwt"
  7. "github.com/gin-gonic/gin"
  8. // "../middleware/permission"
  9. "net/http"
  10. "strings"
  11. "../pkg/setting"
  12. "../routers/api"
  13. ginSwagger "github.com/swaggo/gin-swagger"
  14. "github.com/swaggo/gin-swagger/swaggerFiles"
  15. )
  16. func InitRouter() *gin.Engine {
  17. r := gin.New()
  18. // ginpprof.Wrap(r)
  19. r.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
  20. // 你的自定义格式
  21. return fmt.Sprintf(" %+v \n",
  22. param.Keys,
  23. )
  24. }))
  25. r.Use(gin.Logger()) // 日志
  26. r.Use(Cors()) // 跨域请求rolemenus
  27. r.Use(gin.Recovery())
  28. gin.SetMode(setting.ServerSetting.RunMode)
  29. r.Static("/static", setting.CurrentPath+"/dist/static") // 添加资源路径
  30. r.StaticFile("/", setting.CurrentPath+"/dist/index.html") //前端接口
  31. r.StaticFile("/favicon.ico", setting.CurrentPath+"/dist/favicon.ico") //前端接口
  32. r.Static("/file", setting.CurrentPath+"/uploads/file")
  33. r.Static("/uploads", setting.CurrentPath+"/uploads")
  34. r.POST("/auth", api.Auth) // 获取登录token
  35. r.POST("/authlogin", api.AuthLogin) // 获取登录token
  36. r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) // API 注释
  37. if setting.CommSetting.PortName != "" {
  38. r.GET("/comm/:id/:x/:y/", comm.ProcessHttp)
  39. r.GET("/cps/:id/:limit", comm.ProcessHttpCPS)
  40. r.POST("/comm/:id/:x/:y/", comm.ProcessHttp)
  41. r.POST("/cps/:id/:limit", comm.ProcessHttpCPS)
  42. }
  43. eqm := r.Group("/manage/api")
  44. eqm.POST("/login", api.Auth)
  45. apiV1 := r.Group("/authdata")
  46. apiV1.Use(jwt.JWT()) // token 验证
  47. {
  48. apiV1.POST("/rolemenus", api.GetRecuDataByName)
  49. apiV1.GET("/userinfo", api.GetUserinfo)
  50. apiV1.POST("/logout", api.UserLogout)
  51. apiV1.POST("/GetRecuDataByName", api.GetRecuDataByName) ////
  52. apiV1.POST("/GetDataByName", api.GetDataByName)
  53. apiV1.POST("/GetDataBySqlIN", api.GetDataBySqlIN)
  54. apiV1.POST("/GetDataByNames", api.GetDataByNames)
  55. apiV1.POST("/PostDataByName", api.PostDataByName)
  56. apiV1.POST("/PostDataByNames", api.PostDataByNames)
  57. apiV1.POST("/UpdateDataRelation", api.UpdateDataRelation) //log.error
  58. apiV1.POST("/GetDataByNameForm", api.GetDataByNameForm)
  59. apiV1.POST("/PostDataByNameForm", api.PostDataByNameForm)
  60. apiV1.POST("/GetUpkeepPlan", api.GetUpkeepPlan)
  61. apiV1.POST("/GetReportform", api.GetReportform)
  62. apiV1.POST("/GetArrList", api.GetArrList)
  63. apiV1.POST("/GETNurJudge", api.GETNurJudge)
  64. apiV1.POST("/GETNurJudgeRport", api.GETNurJudgeRport)
  65. apiV1.POST("/ExecDataByConfig", api.ExecDataByConfig)
  66. apiV1.POST("/ImportExcel", api.ImportExcel)
  67. apiV1.POST("/upload/:id/:name", api.UploadFile)
  68. apiV1.POST("/uploads", api.UploadFiles)
  69. apiV1.POST("/uploaderimage", api.UploaderImage)
  70. apiV1.POST("/uploaderTmrImage", api.UploaderTmrImage)
  71. apiV1.POST("/removeimage", api.RemovePicByName)
  72. apiV1.GET("/getthumbnailimage/:filename", api.GetThumbnailImage)
  73. apiV1.POST("/getoriginimage/:filename", api.GetOriginImage)
  74. apiV1.GET("/getfileraw/:filename", api.GetFileRaw)
  75. apiV1.GET("/downloadfile/:filename", api.DownloadFile)
  76. apiV1.POST("/removefile/:id", api.RemoveFileByName)
  77. apiV1.POST("/testdata", api.TestData)
  78. apiV1.POST("/Testpartitions", api.Testpartitions)
  79. //自定义报表
  80. apiV1.POST("/Createchart", api.Createchart)
  81. apiV1.POST("/Updatechart", api.Createchart)
  82. apiV1.POST("/exesql", api.ExecSQL)
  83. apiV1.POST("/Createdashboard", api.Updatedashboard)
  84. apiV1.POST("/Updatedashboard", api.Updatedashboard)
  85. apiV1.POST("/Dashboards", api.Dashboards)
  86. // 格润富德 API 数据获取
  87. apiV1.POST("/getDorm", api.GetDorm) // 牛舍信息
  88. apiV1.POST("/GetRJSBData", api.GetRJSBData)
  89. apiV1.POST("/PostRJSBData", api.PostRJSBData)
  90. apiV1.POST("/PostRJSBDatas", api.PostRJSBDatas)
  91. apiV1.POST("/autogeneration", api.Autogeneration)
  92. apiV1.POST("/spillage", api.GetSpillage)
  93. apiV1.POST("/trains", api.UpdateTrains)
  94. apiV1.POST("/lpplan/edit", api.UpdateLpplan)
  95. apiV1.POST("/lpplan/add", api.AddLpplan)
  96. apiV1.POST("/lpplandtl", api.GetLpplandtl)
  97. apiV1.POST("/lpplandtl/restore", api.RestoreLpplandtl)
  98. apiV1.POST("/lpplandtl/del", api.DelLpplandtl)
  99. }
  100. //不需要登录验证的接口
  101. r.POST("/getopenid/:code", api.GetOpenID)
  102. apiV3 := r.Group("/thi") // restful 接口 tablename 是 表名字
  103. {
  104. apiV3.POST("/GetDataByName", api.GetDataByName)
  105. apiV3.POST("/PostDataByName", api.PostDataByName)
  106. }
  107. apiV4 := r.Group("/customreport") // restful 接口 tablename 是 表名字
  108. {
  109. apiV4.POST("/createchart", api.Createchart)
  110. apiV4.POST("/PostDataByName", api.PostDataByName)
  111. }
  112. return r
  113. }
  114. // 跨域
  115. func Cors() gin.HandlerFunc {
  116. return func(c *gin.Context) {
  117. method := c.Request.Method //请求方法
  118. origin := c.Request.Header.Get("Origin") //请求头部
  119. var headerKeys []string // 声明请求头keys
  120. for k, _ := range c.Request.Header {
  121. headerKeys = append(headerKeys, k)
  122. }
  123. headerStr := strings.Join(headerKeys, ", ")
  124. if headerStr != "" {
  125. headerStr = fmt.Sprintf("access-control-allow-origin, access-control-allow-headers, %s", headerStr)
  126. } else {
  127. headerStr = "access-control-allow-origin, access-control-allow-headers"
  128. }
  129. if origin != "" {
  130. c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
  131. c.Header("Access-Control-Allow-Origin", "*") // 这是允许访问所有域
  132. c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE") //服务器支持的所有跨域请求的方法,为了避免浏览次请求的多次'预检'请求
  133. // header的类型
  134. c.Header("Access-Control-Allow-Headers", "Authorization, Content-Length, X-CSRF-Token, id, Token, name, optname, thumbnail, session, X_Requested_With, 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")
  135. // 允许跨域设置 可以返回其他子段
  136. c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, Pragma, FooBar") // 跨域关键设置 让浏览器可以解析
  137. c.Header("Access-Control-Max-Age", "172800") // 缓存请求信息 单位为秒
  138. c.Header("Access-Control-Allow-Credentials", "false") // 跨域请求是否需要带cookie信息 默认设置为true
  139. c.Set("content-type", "application/json") // 设置返回格式是json
  140. }
  141. //放行所有OPTIONS方法
  142. if method == "OPTIONS" {
  143. c.JSON(http.StatusOK, "Options Request!")
  144. }
  145. // 处理请求
  146. c.Next() // 处理请求
  147. }
  148. }