router.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. r.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
  19. // 你的自定义格式
  20. return fmt.Sprintf(" %+v \n",
  21. param.Keys,
  22. )
  23. }))
  24. r.Use(gin.Logger()) // 日志
  25. r.Use(Cors()) // 跨域请求rolemenus
  26. r.Use(gin.Recovery())
  27. gin.SetMode(setting.ServerSetting.RunMode)
  28. r.Static("/static", setting.CurrentPath+"/dist/static") // 添加资源路径
  29. r.Static("/datas", setting.CurrentPath+"/dist/datas") // 添加资源路径
  30. r.StaticFile("/", setting.CurrentPath+"/dist/index.html") //前端接口
  31. r.StaticFile("/favicon.ico", setting.CurrentPath+"/dist/favicon.ico") //前端接口
  32. r.StaticFile("/myMap.json", setting.CurrentPath+"/data/myMap.json") //前端接口
  33. r.Static("/file", setting.CurrentPath+"/uploads/file")
  34. r.Static("/uploads", setting.CurrentPath+"/uploads")
  35. r.POST("/auth", api.Auth) // 获取登录token
  36. r.POST("/uploadfileCRM", api.UploadFileCRM)
  37. r.POST("/downloadfileCRM", api.DownloadFileCRM)
  38. r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) // API 注释
  39. if setting.CommSetting.PortName != "" {
  40. r.GET("/comm/:id/:x/:y/", comm.ProcessHttp)
  41. r.GET("/cps/:id/:limit", comm.ProcessHttpCPS)
  42. r.POST("/comm/:id/:x/:y/", comm.ProcessHttp)
  43. r.POST("/cps/:id/:limit", comm.ProcessHttpCPS)
  44. }
  45. eqm := r.Group("/manage/api")
  46. eqm.POST("/login", api.Auth)
  47. r.POST("/authdata/calf/feeding", api.GetCalfFeeding)
  48. r.POST("/authdata/calf/feeding/edit", api.EditCalfFeeding)
  49. apiV1 := r.Group("/authdata")
  50. apiV1.Use(jwt.JWT()) // token 验证
  51. {
  52. apiV1.POST("/rolemenus", api.GetRecuDataByName)
  53. apiV1.GET("/userinfo", api.GetUserinfo)
  54. apiV1.POST("/logout", api.UserLogout)
  55. apiV1.POST("/GetRecuDataByName", api.GetRecuDataByName) ////
  56. apiV1.POST("/GetDataByName", api.GetDataByName)
  57. apiV1.POST("/GetDataByNames", api.GetDataByNames)
  58. apiV1.POST("/PostDataByName", api.PostDataByName)
  59. apiV1.POST("/PostThirdPartyData", api.PostThirdPartyData)
  60. apiV1.POST("/PostDataByNames", api.PostDataByNames)
  61. apiV1.POST("/UpdateDataRelation", api.UpdateDataRelation) //log.error
  62. apiV1.POST("/GetDataByNameForm", api.GetDataByNameForm)
  63. apiV1.POST("/PostDataByNameForm", api.PostDataByNameForm)
  64. apiV1.POST("/GetUpkeepPlan", api.GetUpkeepPlan)
  65. apiV1.POST("/GetReportform", api.GetReportform)
  66. apiV1.POST("/GetAccount", api.GetAccount)
  67. apiV1.POST("/ExecDataByConfig", api.ExecDataByConfig)
  68. apiV1.POST("/ImportExcel", api.ImportExcel)
  69. apiV1.POST("/upload/:id/:name", api.UploadFile)
  70. apiV1.POST("/uploads", api.UploadFiles)
  71. apiV1.POST("/uploaderimage", api.UploaderImage)
  72. apiV1.POST("/removeimage", api.RemovePicByName)
  73. apiV1.GET("/getthumbnailimage/:filename", api.GetThumbnailImage)
  74. apiV1.POST("/getoriginimage/:filename", api.GetOriginImage)
  75. apiV1.GET("/getfileraw/:filename", api.GetFileRaw)
  76. apiV1.GET("/downloadfile/:filename", api.DownloadFile)
  77. apiV1.POST("/downloadmailreport", api.DownloadMailReport)
  78. apiV1.POST("/removefile/:id", api.RemoveFileByName)
  79. //新增修改预设饲喂模板
  80. apiV1.POST("/defaulttemplate/add", api.AddDefaultTemplate)
  81. // 查看预设饲喂模板
  82. apiV1.POST("/defaulttemplate", api.GetDefaultTemplate)
  83. }
  84. if setting.ServerSetting.NoAuth > 0 {
  85. apiV2 := r.Group("/data") // restful 接口 tablename 是 表名字
  86. {
  87. apiV2.POST("/getdata", api.GetData)
  88. apiV2.POST("/GetDataByName", api.GetDataByName)
  89. apiV2.POST("/GetDataByNames", api.GetDataByNames)
  90. apiV2.POST("/PostDataByName", api.PostDataByName)
  91. apiV2.POST("/PostDataByNames", api.PostDataByNames)
  92. apiV2.POST("/ExecDataByConfig", api.ExecDataByConfig)
  93. apiV2.POST("/GetDataByNameForm", api.GetDataByNameForm)
  94. apiV2.POST("/PostDataByNameForm", api.PostDataByNameForm)
  95. apiV2.POST("/ImportExcel", api.ImportExcel)
  96. //调用资产标准管理
  97. apiV2.POST("/Equipment", api.Equipment)
  98. apiV2.POST("/PastureDepartment", api.PastureDepartment)
  99. //插入和修改标准管理
  100. apiV2.POST("/updateAndInster", api.UpdateAndInsert)
  101. // 标准库存管理
  102. apiV2.POST("/StandardPart", api.StandardPart)
  103. apiV2.POST("/upload", api.UploadFile)
  104. apiV2.POST("/uploads", api.UploadFiles)
  105. apiV2.POST("/uploaderimage", api.UploaderImage)
  106. apiV2.POST("/removeimage", api.RemovePicByName)
  107. apiV2.GET("/getthumbnailimage/:filename", api.GetThumbnailImage)
  108. apiV2.GET("/getoriginimage/:filename", api.GetOriginImage)
  109. apiV2.GET("/getfileraw/:filename", api.GetFileRaw)
  110. apiV2.GET("/downloadfile/:filename", api.DownloadFile)
  111. apiV2.POST("/removefile", api.RemoveFileByName)
  112. apiV2.POST("/testdata", api.TestData)
  113. //apiV2.POST("/DingShi",api.DingShi)
  114. //apiV2.POST("/Ceshi",api.Ceshi)
  115. }
  116. }
  117. r.POST("/getopenid/:code", api.GetOpenID)
  118. apiV3 := r.Group("/thi") // restful 接口 tablename 是 表名字
  119. {
  120. apiV3.POST("/GetDataByName", api.GetDataByName)
  121. apiV3.POST("/PostDataByName", api.PostDataByName)
  122. }
  123. apiV4 := r.Group("/openapi")
  124. //apiV4.Use(jwt.JWT())
  125. {
  126. apiV4.POST("/GetDataByName", api.GetDataByNameOPEN)
  127. apiV4.POST("/PostDataByName", api.PostDataByNameOPEN)
  128. }
  129. return r
  130. }
  131. // 跨域
  132. func Cors() gin.HandlerFunc {
  133. return func(c *gin.Context) {
  134. method := c.Request.Method //请求方法
  135. origin := c.Request.Header.Get("Origin") //请求头部
  136. var headerKeys []string // 声明请求头keys
  137. for k, _ := range c.Request.Header {
  138. headerKeys = append(headerKeys, k)
  139. }
  140. headerStr := strings.Join(headerKeys, ", ")
  141. if headerStr != "" {
  142. headerStr = fmt.Sprintf("access-control-allow-origin, access-control-allow-headers, %s", headerStr)
  143. } else {
  144. headerStr = "access-control-allow-origin, access-control-allow-headers"
  145. }
  146. if origin != "" {
  147. c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
  148. c.Header("Access-Control-Allow-Origin", "*") // 这是允许访问所有域
  149. c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE") //服务器支持的所有跨域请求的方法,为了避免浏览次请求的多次'预检'请求
  150. // header的类型
  151. 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")
  152. // 允许跨域设置 可以返回其他子段
  153. 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") // 跨域关键设置 让浏览器可以解析
  154. c.Header("Access-Control-Max-Age", "172800") // 缓存请求信息 单位为秒
  155. c.Header("Access-Control-Allow-Credentials", "false") // 跨域请求是否需要带cookie信息 默认设置为true
  156. c.Set("content-type", "application/json") // 设置返回格式是json
  157. }
  158. //放行所有OPTIONS方法
  159. if method == "OPTIONS" {
  160. c.JSON(http.StatusOK, "Options Request!")
  161. }
  162. // 处理请求
  163. c.Next() // 处理请求
  164. }
  165. }