package routers import ( "fmt" "kpt.xdmy/middleware/jwt" "github.com/gin-gonic/gin" "kpt.xdmy/middleware/comm" // "kpt.xdmy/middleware/permission" "net/http" "strings" swaggerFiles "github.com/swaggo/files" ginSwagger "github.com/swaggo/gin-swagger" // "github.com/swaggo/gin-swagger/swaggerFiles" "kpt.xdmy/apiserver/routers/api" "kpt.xdmy/pkg/setting" ) func InitRouter() *gin.Engine { r := gin.New() r.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string { // 你的自定义格式 return fmt.Sprintf(" %+v \n", param.Keys, ) })) r.Use(gin.Logger()) // 日志 // r.Use(gzip.Gzip(gzip.DefaultCompression)) r.Use(Cors()) // 跨域请求rolemenus r.Use(gin.Recovery()) gin.SetMode(setting.ServerSetting.RunMode) r.MaxMultipartMemory = 1 r.Static("/static", setting.CurrentPath+"/dist/static") // 添加资源路径 r.Static("/datas", setting.CurrentPath+"/dist/datas") // 添加资源路径 r.StaticFile("/", setting.CurrentPath+"/dist/index.html") //前端接口 r.StaticFile("/favicon.ico", setting.CurrentPath+"/dist/favicon.ico") //前端接口 r.StaticFile("/myMap.json", setting.CurrentPath+"/data/myMap.json") //前端接口 r.Static("/file", setting.CurrentPath+"/uploads/file") r.Static("/uploads", setting.CurrentPath+"/uploads") r.POST("/auth", api.Auth) // 获取登录token r.POST("/authdata/test", Exec) r.POST("/authdata/department", DepartmentExec) r.POST("/authdata/provider", ProviderExec) r.POST("/authdata/equipmentExecAdd", EquipmentExecAdd) r.POST("/authdata/contractExec", ContractExec) r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) // API 注释 if setting.CommSetting.PortName != "" { r.GET("/comm/:id/:x/:y/", comm.ProcessHttp) r.GET("/cps/:id/:limit", comm.ProcessHttpCPS) r.POST("/comm/:id/:x/:y/", comm.ProcessHttp) r.POST("/cps/:id/:limit", comm.ProcessHttpCPS) } r.POST("/quickResponse", QuickResponse) r.POST("/test", Test) //r.POST("/dingding", Dingding) eqm := r.Group("/manage/api") eqm.POST("/login", api.Auth) apiV1 := r.Group("/authdata") apiV1.Use(jwt.JWT()) // token 验证 { //查询报表管理,启停记录 apiV1.POST("/startStopRecord", StartStopRecord) //查看督办信息 apiV1.POST("/oversee", GetOversee) apiV1.POST("/maintain/pasture", MaintainListPid) apiV1.POST("/maintain", MaintainList) apiV1.POST("/maintain/equipment", MaintainListEquipment) apiV1.POST("/equipment", EquipmentExec) //srm手动推送 apiV1.POST("srm/order/send", ManaulSrmOrderJob) //sap手动推送 apiV1.POST("sap/order/send", ManaulSapOrder) //手动入库 apiV1.POST("sap/laidproof/send", LaidProof) //手动出库 apiV1.POST("sap/useproof/send", UseProof) //手动退货 apiV1.POST("sap/refundproof/send", RefundProof) //手动退库 apiV1.POST("sap/quitproof/send", QuitProof) //sap手动调拨 apiV1.POST("sap/transfer/send", ManualTransfer) apiV1.POST("part/eas/batch", EasBatch) apiV1.POST("/sap/proof/off/list", ProofOffList) apiV1.POST("/sap/proof/off", ProofOff) apiV1.POST("/sap/proof/detail", ProofList) apiV1.GET("/mcs/accounts", McsAccounts) apiV1.POST("/mcs/record", McsControl) apiV1.POST("/rolemenus", api.GetRecuDataByName) apiV1.GET("/userinfo", api.GetUserinfo) apiV1.POST("/logout", api.UserLogout) apiV1.POST("/GetRecuDataByName", api.GetRecuDataByName) //// apiV1.POST("/GetDataByName", api.GetDataByName) apiV1.POST("/GetDataByNames", api.GetDataByNames) apiV1.POST("/PostDataByName", api.PostDataByName) apiV1.POST("/PostThirdPartyData", api.PostThirdPartyData) apiV1.POST("/PostDataByNames", api.PostDataByNames) apiV1.POST("/UpdateDataRelation", api.UpdateDataRelation) //log.error apiV1.POST("/GetDataByNameForm", api.GetDataByNameForm) apiV1.POST("/PostDataByNameForm", api.PostDataByNameForm) apiV1.POST("/GetUpkeepPlan", api.GetUpkeepPlan) apiV1.POST("/GetReportform", api.GetReportform) apiV1.POST("/GetAccount", api.GetAccount) apiV1.POST("/ExecDataByConfig", api.ExecDataByConfig) apiV1.POST("/ImportExcel", api.ImportExcel) apiV1.POST("/upload/:id/:name", api.UploadFile) apiV1.POST("/uploads", api.UploadFiles) apiV1.POST("/uploaderimage", api.UploaderImage) apiV1.POST("/removeimage", api.RemovePicByName) apiV1.GET("/getthumbnailimage/:filename", api.GetThumbnailImage) apiV1.POST("/getoriginimage/:filename", api.GetOriginImage) apiV1.GET("/getfileraw/:filename", api.GetFileRaw) apiV1.GET("/downloadfile/:filename", api.DownloadFile) apiV1.POST("/downloadmailreport", api.DownloadMailReport) apiV1.POST("/removefile/:id", api.RemoveFileByName) apiV1.POST("/testdata", api.TestData) apiV1.POST("/Testpartitions", api.Testpartitions) // eas 对接 apiV1.POST("/EasSync", api.EasSync) apiV1.POST("/partslistSGv2", api.GetPartsListSGv2) apiV1.POST("/waterandelectricity", api.GetWaterAndElectricity) // 备件申购-特殊申购 apiV1.POST("/spare/parts/requisitions", api.SparePartsRequisitions) apiV1.GET("/diesel/type/list", api.GetDieselTypeList) apiV1.POST("/diese/add", InsertDieselData) apiV1.POST("/diese/import/excel", InsertDieselImportExcel) apiV1.POST("/diese/off", DieselOff) apiV1.GET("/centra/index", CentralIndex) //各中心总指标 apiV1.GET("/overall/index", OverallIndex) //集团分项指标 apiV1.GET("/expense", GetExpense) //费用统计 apiV1.GET("/pasture/index", GetPastureIndex) //牧场指标 //apiV1.GET("/vehicleL/list", GetVehicleList) apiV1.POST("/contract/push", PostPushContractDataToSAP) apiV1.GET("/equipment/index", GetEquipmentIndex) apiV1.GET("/choosemonthwx/index", GetChooseMonthWXByDepartment) apiV1.POST("/stock/import", StockExcel) apiV1.GET("/stock/list", GetStockList) apiV1.POST("/stock/del", DelStockList) //各牧场年度用水量对比 apiV1.GET("/water/nowwater", GetWaterYieldByPasture) //单头牛水量 apiV1.GET("/water/singlecow", GetWaterSingleCowByPasture) //单头牛水费 apiV1.GET("/water/waterrate", GetWaterRateByPasture) //水费完成率 apiV1.GET("/water/ratecomplete", GetWaterRateCompleteByPasture) //每月单头牛用水量对比 apiV1.GET("/water/perhead", GetWaterPerHeadByPasture) //各牧场年度用电量对比 apiV1.GET("/electricity/nowelectricity", GetElectricityYieldByPasture) //单头牛电量 apiV1.GET("/electricity/singlecow", GetElectricitySingleCowByPasture) //单头牛电费 apiV1.GET("/electricity/electricityrate", GetElectricityRateByPasture) //电费完成率 apiV1.GET("/electricity/ratecomplete", GetElectricityRateCompleteByPasture) //每月单头牛用电量对比 apiV1.GET("/electricity/perhead", GetElectricityPerHeadByPasture) //各牧场年度维修对比 apiV1.GET("/repair/nowelectricity", GetWxYieldByPasture) //单头牛维修 apiV1.GET("/repair/singlecow", GetWxSingleCowByPasture) //每月单头牛维修对比 apiV1.GET("/repair/perhead", GetWxPerHeadByPasture) //各牧场年度燃动费对比 apiV1.GET("/ignition/nowwater", GetIgnitionYieldByPasture) //各牧场年度柴油费对比 apiV1.GET("/diesel/nowwater", GetDieselYieldByPasture) //单头牛柴油 apiV1.GET("/diesel/rate", GetDieselRateByPasture) //单头牛燃动费 apiV1.GET("/ignition/rate", GetIgnitionRateByPasture) //单头牛燃动费 apiV1.GET("/diesel/perhead", GetDieselPerHeadByPasture) apiV1.GET("/vehicle/list", GetVehicle) // 申购备件修改 apiV1.POST("/partpurchase/edit", EditPartpurchase) //工作分类下拉,目前写死 apiV1.GET("/work/class", GetWorkClass) apiV1.GET("/ensiling", GetEnsiling) //车辆运转率明细 apiV1.GET("/running/rate/detail", GetRunningRateDetail) } if setting.ServerSetting.NoAuth > 0 { apiV2 := r.Group("/data") // restful 接口 tablename 是 表名字 { apiV2.POST("sap/transfer/send", ManualTransfer) apiV2.POST("/getdata", api.GetData) apiV2.POST("/GetDataByName", api.GetDataByName) apiV2.POST("/GetDataByNames", api.GetDataByNames) apiV2.POST("/PostDataByName", api.PostDataByName) apiV2.POST("/PostDataByNames", api.PostDataByNames) apiV2.POST("/ExecDataByConfig", api.ExecDataByConfig) apiV2.POST("/GetDataByNameForm", api.GetDataByNameForm) apiV2.POST("/PostDataByNameForm", api.PostDataByNameForm) apiV2.POST("/ImportExcel", api.ImportExcel) //调用资产标准管理 apiV2.POST("/Equipment", api.Equipment) apiV2.POST("/PastureDepartment", api.PastureDepartment) //插入和修改标准管理 apiV2.POST("/updateAndInster", api.UpdateAndInsert) // 标准库存管理 apiV2.POST("/StandardPart", api.StandardPart) apiV2.POST("/upload", api.UploadFile) apiV2.POST("/uploads", api.UploadFiles) apiV2.POST("/uploaderimage", api.UploaderImage) apiV2.POST("/removeimage", api.RemovePicByName) apiV2.GET("/getthumbnailimage/:filename", api.GetThumbnailImage) apiV2.GET("/getoriginimage/:filename", api.GetOriginImage) apiV2.GET("/getfileraw/:filename", api.GetFileRaw) apiV2.GET("/downloadfile/:filename", api.DownloadFile) apiV2.POST("/removefile", api.RemoveFileByName) apiV2.POST("/testdata", api.TestData) //apiV2.POST("/DingShi",api.DingShi) //apiV2.POST("/Ceshi",api.Ceshi) } } r.POST("/getopenid/:code", api.GetOpenID) apiV3 := r.Group("/thi") // restful 接口 tablename 是 表名字 { apiV3.POST("/GetDataByName", api.GetDataByName) apiV3.POST("/PostDataByName", api.PostDataByName) apiV3.POST("/vehicle/edit", EditVehicle) } apiV4 := r.Group("/openapi") //apiV4.Use(jwt.JWT()) { apiV4.POST("/GetDataByName", api.GetDataByNameOPEN) apiV4.POST("/PostDataByName", api.PostDataByNameOPEN) } apiV5 := r.Group("/api/v1") { apiV5.GET("/oauth2/token", OAuth2Token) apiV5.GET("/logout", OAuth2Logout) apiV5.POST("/user/add", OAuth2AddUser) } return r } // 跨域 //func Cors() gin.HandlerFunc { // return func(c *gin.Context) { // //method := c.Request.Method //请求方法 // //origin := c.Request.Header.Get("Origin") //请求头部 // var headerKeys []string // 声明请求头keys // for k := range c.Request.Header { // headerKeys = append(headerKeys, k) // } // headerStr := strings.Join(headerKeys, ", ") // if headerStr != "" { // headerStr = fmt.Sprintf("access-control-allow-origin, access-control-allow-headers, %s", headerStr) // } else { // headerStr = "access-control-allow-origin, access-control-allow-headers" // } // //if origin != "" { // c.Writer.Header().Set("Access-Control-Allow-Origin", "*") // c.Header("Access-Control-Allow-Origin", "*") // 这是允许访问所有域 // c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE") //服务器支持的所有跨域请求的方法,为了避免浏览次请求的多次'预检'请求 // // header的类型 // 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") // // 允许跨域设置 可以返回其他子段 // 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") // 跨域关键设置 让浏览器可以解析 // c.Header("Access-Control-Max-Age", "172800") // 缓存请求信息 单位为秒 // c.Header("Access-Control-Allow-Credentials", "false") // 跨域请求是否需要带cookie信息 默认设置为true // c.Set("content-type", "application/json") // 设置返回格式是json // //} // // //放行所有OPTIONS方法 // //if method == "OPTIONS" { // c.JSON(http.StatusOK, "Options Request!") // //} // // 处理请求 // c.Next() // 处理请求 // } //} // Cors 开放所有接口的OPTIONS方法 func Cors() gin.HandlerFunc { return func(c *gin.Context) { method := c.Request.Method //请求方法 //origin := c.Request.Header.Get("Origin") //请求头部 var headerKeys []string // 声明请求头keys for k, _ := range c.Request.Header { headerKeys = append(headerKeys, k) } headerStr := strings.Join(headerKeys, ", ") if headerStr != "" { headerStr = fmt.Sprintf("access-control-allow-origin, access-control-allow-headers, %s", headerStr) } else { headerStr = "access-control-allow-origin, access-control-allow-headers" } //if origin != "" { // origin := c.Request.Header.Get("Origin") c.Header("Access-Control-Allow-Origin", "*") // 这是允许访问所有域 c.Header("Access-Control-Allow-Methods", "*") //服务器支持的所有跨域请求的方法,为了避免浏览次请求的多次'预检'请求 // header的类型 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") // 允许跨域设置 可以返回其他子段 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") // 跨域关键设置 让浏览器可以解析 c.Header("Access-Control-Max-Age", "172800") // 缓存请求信息 单位为秒 c.Header("Access-Control-Allow-Credentials", "false") // 跨域请求是否需要带cookie信息 默认设置为true c.Set("content-type", "application/json") // 设置返回格式是json //} //放行所有OPTIONS方法 if method == "OPTIONS" { c.JSON(http.StatusOK, "Options Request!") } // 处理请求 c.Next() // 处理请求 } }