router.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. package routers
  2. import (
  3. "fmt"
  4. "kpt.xdmy/middleware/jwt"
  5. "github.com/gin-gonic/gin"
  6. "kpt.xdmy/middleware/comm"
  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. apiV1.POST("/spare/parts/requisitions", api.SparePartsRequisitions)
  121. apiV1.GET("/diesel/type/list", api.GetDieselTypeList)
  122. apiV1.POST("/diese/add", InsertDieselData)
  123. apiV1.POST("/diese/import/excel", InsertDieselImportExcel)
  124. apiV1.POST("/diese/off", DieselOff)
  125. apiV1.GET("/centra/index", CentralIndex) //各中心总指标
  126. apiV1.GET("/overall/index", OverallIndex) //集团分项指标
  127. apiV1.GET("/expense", GetExpense) //费用统计
  128. apiV1.GET("/pasture/index", GetPastureIndex) //牧场指标
  129. //apiV1.GET("/vehicleL/list", GetVehicleList)
  130. apiV1.POST("/contract/push", PostPushContractDataToSAP)
  131. apiV1.GET("/equipment/index", GetEquipmentIndex)
  132. apiV1.GET("/choosemonthwx/index", GetChooseMonthWXByDepartment)
  133. apiV1.POST("/stock/import", StockExcel)
  134. apiV1.GET("/stock/list", GetStockList)
  135. apiV1.POST("/stock/del", DelStockList)
  136. //各牧场年度用水量对比
  137. apiV1.GET("/water/nowwater", GetWaterYieldByPasture)
  138. //单头牛水量
  139. apiV1.GET("/water/singlecow", GetWaterSingleCowByPasture)
  140. //单头牛水费
  141. apiV1.GET("/water/waterrate", GetWaterRateByPasture)
  142. //水费完成率
  143. apiV1.GET("/water/ratecomplete", GetWaterRateCompleteByPasture)
  144. //每月单头牛用水量对比
  145. apiV1.GET("/water/perhead", GetWaterPerHeadByPasture)
  146. //各牧场年度用电量对比
  147. apiV1.GET("/electricity/nowelectricity", GetElectricityYieldByPasture)
  148. //单头牛电量
  149. apiV1.GET("/electricity/singlecow", GetElectricitySingleCowByPasture)
  150. //单头牛电费
  151. apiV1.GET("/electricity/electricityrate", GetElectricityRateByPasture)
  152. //电费完成率
  153. apiV1.GET("/electricity/ratecomplete", GetElectricityRateCompleteByPasture)
  154. //每月单头牛用电量对比
  155. apiV1.GET("/electricity/perhead", GetElectricityPerHeadByPasture)
  156. //各牧场年度维修对比
  157. apiV1.GET("/repair/nowelectricity", GetWxYieldByPasture)
  158. //单头牛维修
  159. apiV1.GET("/repair/singlecow", GetWxSingleCowByPasture)
  160. //每月单头牛维修对比
  161. apiV1.GET("/repair/perhead", GetWxPerHeadByPasture)
  162. //各牧场年度燃动费对比
  163. apiV1.GET("/ignition/nowwater", GetIgnitionYieldByPasture)
  164. //各牧场年度柴油费对比
  165. apiV1.GET("/diesel/nowwater", GetDieselYieldByPasture)
  166. //单头牛柴油
  167. apiV1.GET("/diesel/rate", GetDieselRateByPasture)
  168. //单头牛燃动费
  169. apiV1.GET("/ignition/rate", GetIgnitionRateByPasture)
  170. //单头牛燃动费
  171. apiV1.GET("/diesel/perhead", GetDieselPerHeadByPasture)
  172. apiV1.GET("/vehicle/list", GetVehicle)
  173. // 申购备件修改
  174. apiV1.POST("/partpurchase/edit", EditPartpurchase)
  175. //工作分类下拉,目前写死
  176. apiV1.GET("/work/class", GetWorkClass)
  177. apiV1.GET("/ensiling", GetEnsiling)
  178. //车辆运转率明细
  179. apiV1.GET("/running/rate/detail", GetRunningRateDetail)
  180. }
  181. if setting.ServerSetting.NoAuth > 0 {
  182. apiV2 := r.Group("/data") // restful 接口 tablename 是 表名字
  183. {
  184. apiV2.POST("sap/transfer/send", ManualTransfer)
  185. apiV2.POST("/getdata", api.GetData)
  186. apiV2.POST("/GetDataByName", api.GetDataByName)
  187. apiV2.POST("/GetDataByNames", api.GetDataByNames)
  188. apiV2.POST("/PostDataByName", api.PostDataByName)
  189. apiV2.POST("/PostDataByNames", api.PostDataByNames)
  190. apiV2.POST("/ExecDataByConfig", api.ExecDataByConfig)
  191. apiV2.POST("/GetDataByNameForm", api.GetDataByNameForm)
  192. apiV2.POST("/PostDataByNameForm", api.PostDataByNameForm)
  193. apiV2.POST("/ImportExcel", api.ImportExcel)
  194. //调用资产标准管理
  195. apiV2.POST("/Equipment", api.Equipment)
  196. apiV2.POST("/PastureDepartment", api.PastureDepartment)
  197. //插入和修改标准管理
  198. apiV2.POST("/updateAndInster", api.UpdateAndInsert)
  199. // 标准库存管理
  200. apiV2.POST("/StandardPart", api.StandardPart)
  201. apiV2.POST("/upload", api.UploadFile)
  202. apiV2.POST("/uploads", api.UploadFiles)
  203. apiV2.POST("/uploaderimage", api.UploaderImage)
  204. apiV2.POST("/removeimage", api.RemovePicByName)
  205. apiV2.GET("/getthumbnailimage/:filename", api.GetThumbnailImage)
  206. apiV2.GET("/getoriginimage/:filename", api.GetOriginImage)
  207. apiV2.GET("/getfileraw/:filename", api.GetFileRaw)
  208. apiV2.GET("/downloadfile/:filename", api.DownloadFile)
  209. apiV2.POST("/removefile", api.RemoveFileByName)
  210. apiV2.POST("/testdata", api.TestData)
  211. //apiV2.POST("/DingShi",api.DingShi)
  212. //apiV2.POST("/Ceshi",api.Ceshi)
  213. }
  214. }
  215. r.POST("/getopenid/:code", api.GetOpenID)
  216. apiV3 := r.Group("/thi") // restful 接口 tablename 是 表名字
  217. {
  218. apiV3.POST("/GetDataByName", api.GetDataByName)
  219. apiV3.POST("/PostDataByName", api.PostDataByName)
  220. apiV3.POST("/vehicle/edit", EditVehicle)
  221. }
  222. apiV4 := r.Group("/openapi")
  223. //apiV4.Use(jwt.JWT())
  224. {
  225. apiV4.POST("/GetDataByName", api.GetDataByNameOPEN)
  226. apiV4.POST("/PostDataByName", api.PostDataByNameOPEN)
  227. }
  228. apiV5 := r.Group("/api/v1")
  229. {
  230. apiV5.GET("/oauth2/token", OAuth2Token)
  231. apiV5.GET("/logout", OAuth2Logout)
  232. apiV5.POST("/user/add", OAuth2AddUser)
  233. }
  234. return r
  235. }
  236. // 跨域
  237. //func Cors() gin.HandlerFunc {
  238. // return func(c *gin.Context) {
  239. // //method := c.Request.Method //请求方法
  240. // //origin := c.Request.Header.Get("Origin") //请求头部
  241. // var headerKeys []string // 声明请求头keys
  242. // for k := range c.Request.Header {
  243. // headerKeys = append(headerKeys, k)
  244. // }
  245. // headerStr := strings.Join(headerKeys, ", ")
  246. // if headerStr != "" {
  247. // headerStr = fmt.Sprintf("access-control-allow-origin, access-control-allow-headers, %s", headerStr)
  248. // } else {
  249. // headerStr = "access-control-allow-origin, access-control-allow-headers"
  250. // }
  251. // //if origin != "" {
  252. // c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
  253. // c.Header("Access-Control-Allow-Origin", "*") // 这是允许访问所有域
  254. // c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE") //服务器支持的所有跨域请求的方法,为了避免浏览次请求的多次'预检'请求
  255. // // header的类型
  256. // 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")
  257. // // 允许跨域设置 可以返回其他子段
  258. // 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") // 跨域关键设置 让浏览器可以解析
  259. // c.Header("Access-Control-Max-Age", "172800") // 缓存请求信息 单位为秒
  260. // c.Header("Access-Control-Allow-Credentials", "false") // 跨域请求是否需要带cookie信息 默认设置为true
  261. // c.Set("content-type", "application/json") // 设置返回格式是json
  262. // //}
  263. //
  264. // //放行所有OPTIONS方法
  265. // //if method == "OPTIONS" {
  266. // c.JSON(http.StatusOK, "Options Request!")
  267. // //}
  268. // // 处理请求
  269. // c.Next() // 处理请求
  270. // }
  271. //}
  272. // Cors 开放所有接口的OPTIONS方法
  273. func Cors() gin.HandlerFunc {
  274. return func(c *gin.Context) {
  275. method := c.Request.Method //请求方法
  276. //origin := c.Request.Header.Get("Origin") //请求头部
  277. var headerKeys []string // 声明请求头keys
  278. for k, _ := range c.Request.Header {
  279. headerKeys = append(headerKeys, k)
  280. }
  281. headerStr := strings.Join(headerKeys, ", ")
  282. if headerStr != "" {
  283. headerStr = fmt.Sprintf("access-control-allow-origin, access-control-allow-headers, %s", headerStr)
  284. } else {
  285. headerStr = "access-control-allow-origin, access-control-allow-headers"
  286. }
  287. //if origin != "" {
  288. // origin := c.Request.Header.Get("Origin")
  289. c.Header("Access-Control-Allow-Origin", "*") // 这是允许访问所有域
  290. c.Header("Access-Control-Allow-Methods", "*") //服务器支持的所有跨域请求的方法,为了避免浏览次请求的多次'预检'请求
  291. // header的类型
  292. c.Header("Access-Control-Allow-Headers", "Authorization, Content-Length, X-CSRF-Token, Token,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")
  293. // 允许跨域设置 可以返回其他子段
  294. 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") // 跨域关键设置 让浏览器可以解析
  295. c.Header("Access-Control-Max-Age", "172800") // 缓存请求信息 单位为秒
  296. c.Header("Access-Control-Allow-Credentials", "false") // 跨域请求是否需要带cookie信息 默认设置为true
  297. c.Set("content-type", "application/json") // 设置返回格式是json
  298. //}
  299. //放行所有OPTIONS方法
  300. if method == "OPTIONS" {
  301. c.JSON(http.StatusOK, "Options Request!")
  302. }
  303. // 处理请求
  304. c.Next() // 处理请求
  305. }
  306. }