router.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package routers
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "kpt.xdmy/middleware/comm"
  6. "kpt.xdmy/middleware/jwt"
  7. // "kpt.xdmy/middleware/permission"
  8. "net/http"
  9. "strings"
  10. swaggerFiles "github.com/swaggo/files"
  11. ginSwagger "github.com/swaggo/gin-swagger"
  12. // "github.com/swaggo/gin-swagger/swaggerFiles"
  13. "kpt.xdmy/apiserver/routers/api"
  14. "kpt.xdmy/pkg/setting"
  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(gzip.Gzip(gzip.DefaultCompression))
  26. r.Use(Cors()) // 跨域请求rolemenus
  27. r.Use(gin.Recovery())
  28. gin.SetMode(setting.ServerSetting.RunMode)
  29. r.MaxMultipartMemory = 1
  30. r.Static("/static", setting.CurrentPath+"/dist/static") // 添加资源路径
  31. r.Static("/datas", setting.CurrentPath+"/dist/datas") // 添加资源路径
  32. r.StaticFile("/", setting.CurrentPath+"/dist/index.html") //前端接口
  33. r.StaticFile("/favicon.ico", setting.CurrentPath+"/dist/favicon.ico") //前端接口
  34. r.StaticFile("/myMap.json", setting.CurrentPath+"/data/myMap.json") //前端接口
  35. r.Static("/file", setting.CurrentPath+"/uploads/file")
  36. r.Static("/uploads", setting.CurrentPath+"/uploads")
  37. r.POST("/auth", api.Auth) // 获取登录token
  38. r.POST("/authdata/test", Exec)
  39. r.POST("/authdata/department", DepartmentExec)
  40. r.POST("/authdata/provider", ProviderExec)
  41. r.POST("/authdata/equipmentExecAdd", EquipmentExecAdd)
  42. r.POST("/authdata/contractExec", ContractExec)
  43. r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) // API 注释
  44. if setting.CommSetting.PortName != "" {
  45. r.GET("/comm/:id/:x/:y/", comm.ProcessHttp)
  46. r.GET("/cps/:id/:limit", comm.ProcessHttpCPS)
  47. r.POST("/comm/:id/:x/:y/", comm.ProcessHttp)
  48. r.POST("/cps/:id/:limit", comm.ProcessHttpCPS)
  49. }
  50. r.POST("/quickResponse", QuickResponse)
  51. r.POST("/test", Test)
  52. //r.POST("/dingding", Dingding)
  53. eqm := r.Group("/manage/api")
  54. eqm.POST("/login", api.Auth)
  55. apiV1 := r.Group("/authdata")
  56. apiV1.Use(jwt.JWT()) // token 验证
  57. {
  58. //查询报表管理,启停记录
  59. apiV1.POST("/startStopRecord", StartStopRecord)
  60. //查看督办信息
  61. apiV1.POST("/oversee", GetOversee)
  62. apiV1.POST("/maintain/pasture", MaintainListPid)
  63. apiV1.POST("/maintain", MaintainList)
  64. apiV1.POST("/maintain/equipment", MaintainListEquipment)
  65. apiV1.POST("/equipment", EquipmentExec)
  66. //srm手动推送
  67. apiV1.POST("srm/order/send", ManaulSrmOrderJob)
  68. //sap手动推送
  69. apiV1.POST("sap/order/send", ManaulSapOrder)
  70. //手动入库
  71. apiV1.POST("sap/laidproof/send", LaidProof)
  72. //手动出库
  73. apiV1.POST("sap/useproof/send", UseProof)
  74. //手动退货
  75. apiV1.POST("sap/refundproof/send", RefundProof)
  76. //手动退库
  77. apiV1.POST("sap/quitproof/send", QuitProof)
  78. //sap手动调拨
  79. apiV1.POST("sap/transfer/send", ManualTransfer)
  80. apiV1.POST("part/eas/batch", EasBatch)
  81. apiV1.POST("/sap/proof/off/list", ProofOffList)
  82. apiV1.POST("/sap/proof/off", ProofOff)
  83. apiV1.POST("/sap/proof/detail", ProofList)
  84. apiV1.GET("/mcs/accounts", McsAccounts)
  85. apiV1.POST("/mcs/record", McsControl)
  86. apiV1.POST("/rolemenus", api.GetRecuDataByName)
  87. apiV1.GET("/userinfo", api.GetUserinfo)
  88. apiV1.POST("/logout", api.UserLogout)
  89. apiV1.POST("/GetRecuDataByName", api.GetRecuDataByName) ////
  90. apiV1.POST("/GetDataByName", api.GetDataByName)
  91. apiV1.POST("/GetDataByNames", api.GetDataByNames)
  92. apiV1.POST("/PostDataByName", api.PostDataByName)
  93. apiV1.POST("/PostThirdPartyData", api.PostThirdPartyData)
  94. apiV1.POST("/PostDataByNames", api.PostDataByNames)
  95. apiV1.POST("/UpdateDataRelation", api.UpdateDataRelation) //log.error
  96. apiV1.POST("/GetDataByNameForm", api.GetDataByNameForm)
  97. apiV1.POST("/PostDataByNameForm", api.PostDataByNameForm)
  98. apiV1.POST("/GetUpkeepPlan", api.GetUpkeepPlan)
  99. apiV1.POST("/GetReportform", api.GetReportform)
  100. apiV1.POST("/GetAccount", api.GetAccount)
  101. apiV1.POST("/ExecDataByConfig", api.ExecDataByConfig)
  102. apiV1.POST("/ImportExcel", api.ImportExcel)
  103. apiV1.POST("/upload/:id/:name", api.UploadFile)
  104. apiV1.POST("/uploads", api.UploadFiles)
  105. apiV1.POST("/uploaderimage", api.UploaderImage)
  106. apiV1.POST("/removeimage", api.RemovePicByName)
  107. apiV1.GET("/getthumbnailimage/:filename", api.GetThumbnailImage)
  108. apiV1.POST("/getoriginimage/:filename", api.GetOriginImage)
  109. apiV1.GET("/getfileraw/:filename", api.GetFileRaw)
  110. apiV1.GET("/downloadfile/:filename", api.DownloadFile)
  111. apiV1.POST("/downloadmailreport", api.DownloadMailReport)
  112. apiV1.POST("/removefile/:id", api.RemoveFileByName)
  113. apiV1.POST("/testdata", api.TestData)
  114. apiV1.POST("/Testpartitions", api.Testpartitions)
  115. // eas 对接
  116. apiV1.POST("/EasSync", api.EasSync)
  117. apiV1.POST("/partslistSGv2", api.GetPartsListSGv2)
  118. apiV1.POST("/waterandelectricity", api.GetWaterAndElectricity)
  119. }
  120. if setting.ServerSetting.NoAuth > 0 {
  121. apiV2 := r.Group("/data") // restful 接口 tablename 是 表名字
  122. {
  123. apiV2.POST("sap/transfer/send", ManualTransfer)
  124. apiV2.POST("/getdata", api.GetData)
  125. apiV2.POST("/GetDataByName", api.GetDataByName)
  126. apiV2.POST("/GetDataByNames", api.GetDataByNames)
  127. apiV2.POST("/PostDataByName", api.PostDataByName)
  128. apiV2.POST("/PostDataByNames", api.PostDataByNames)
  129. apiV2.POST("/ExecDataByConfig", api.ExecDataByConfig)
  130. apiV2.POST("/GetDataByNameForm", api.GetDataByNameForm)
  131. apiV2.POST("/PostDataByNameForm", api.PostDataByNameForm)
  132. apiV2.POST("/ImportExcel", api.ImportExcel)
  133. //调用资产标准管理
  134. apiV2.POST("/Equipment", api.Equipment)
  135. apiV2.POST("/PastureDepartment", api.PastureDepartment)
  136. //插入和修改标准管理
  137. apiV2.POST("/updateAndInster", api.UpdateAndInsert)
  138. // 标准库存管理
  139. apiV2.POST("/StandardPart", api.StandardPart)
  140. apiV2.POST("/upload", api.UploadFile)
  141. apiV2.POST("/uploads", api.UploadFiles)
  142. apiV2.POST("/uploaderimage", api.UploaderImage)
  143. apiV2.POST("/removeimage", api.RemovePicByName)
  144. apiV2.GET("/getthumbnailimage/:filename", api.GetThumbnailImage)
  145. apiV2.GET("/getoriginimage/:filename", api.GetOriginImage)
  146. apiV2.GET("/getfileraw/:filename", api.GetFileRaw)
  147. apiV2.GET("/downloadfile/:filename", api.DownloadFile)
  148. apiV2.POST("/removefile", api.RemoveFileByName)
  149. apiV2.POST("/testdata", api.TestData)
  150. //apiV2.POST("/DingShi",api.DingShi)
  151. //apiV2.POST("/Ceshi",api.Ceshi)
  152. }
  153. }
  154. r.POST("/getopenid/:code", api.GetOpenID)
  155. apiV3 := r.Group("/thi") // restful 接口 tablename 是 表名字
  156. {
  157. apiV3.POST("/GetDataByName", api.GetDataByName)
  158. apiV3.POST("/PostDataByName", api.PostDataByName)
  159. }
  160. apiV4 := r.Group("/openapi")
  161. //apiV4.Use(jwt.JWT())
  162. {
  163. apiV4.POST("/GetDataByName", api.GetDataByNameOPEN)
  164. apiV4.POST("/PostDataByName", api.PostDataByNameOPEN)
  165. }
  166. return r
  167. }
  168. // 跨域
  169. func Cors() gin.HandlerFunc {
  170. return func(c *gin.Context) {
  171. method := c.Request.Method //请求方法
  172. origin := c.Request.Header.Get("Origin") //请求头部
  173. var headerKeys []string // 声明请求头keys
  174. for k := range c.Request.Header {
  175. headerKeys = append(headerKeys, k)
  176. }
  177. headerStr := strings.Join(headerKeys, ", ")
  178. if headerStr != "" {
  179. headerStr = fmt.Sprintf("access-control-allow-origin, access-control-allow-headers, %s", headerStr)
  180. } else {
  181. headerStr = "access-control-allow-origin, access-control-allow-headers"
  182. }
  183. if origin != "" {
  184. c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
  185. c.Header("Access-Control-Allow-Origin", "*") // 这是允许访问所有域
  186. c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE") //服务器支持的所有跨域请求的方法,为了避免浏览次请求的多次'预检'请求
  187. // header的类型
  188. 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")
  189. // 允许跨域设置 可以返回其他子段
  190. 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") // 跨域关键设置 让浏览器可以解析
  191. c.Header("Access-Control-Max-Age", "172800") // 缓存请求信息 单位为秒
  192. c.Header("Access-Control-Allow-Credentials", "false") // 跨域请求是否需要带cookie信息 默认设置为true
  193. c.Set("content-type", "application/json") // 设置返回格式是json
  194. }
  195. //放行所有OPTIONS方法
  196. if method == "OPTIONS" {
  197. c.JSON(http.StatusOK, "Options Request!")
  198. }
  199. // 处理请求
  200. c.Next() // 处理请求
  201. }
  202. }