package routers import ( "encoding/base64" "encoding/json" "fmt" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" "kpt.xdmy/apiserver/model/http" "kpt.xdmy/pkg/app" "kpt.xdmy/pkg/e" "kpt.xdmy/pkg/util" http1 "net/http" "strings" ) func OverallIndex(c *gin.Context) { appG := app.Gin{C: c} data, err := svc.OverallIndex() if err != nil { c.JSON(200, map[string]interface{}{"code": 200, "data": err.Error(), "msg": "fail"}) return } appG.Response(200, e.SUCCESS, data) } func CentralIndex(c *gin.Context) { appG := app.Gin{C: c} data, err := svc.CentralIndex() if err != nil { c.JSON(200, map[string]interface{}{"code": 200, "data": err.Error(), "msg": "fail"}) return } appG.Response(200, e.SUCCESS, data) } func GetExpense(c *gin.Context) { appG := app.Gin{C: c} pastureName := c.Query("pastureName") data, err := svc.GetExpense(pastureName) if err != nil { c.JSON(200, map[string]interface{}{"code": 200, "data": err.Error(), "msg": "fail"}) return } appG.Response(200, e.SUCCESS, data) } func GetPastureIndex(c *gin.Context) { appG := app.Gin{C: c} //p := new(home.GetPastureIndexReq) pastureName := c.Query("pastureName") data, err := svc.GetPastureIndex(pastureName) if err != nil { c.JSON(200, map[string]interface{}{"code": 200, "data": err.Error(), "msg": "fail"}) return } appG.Response(200, e.SUCCESS, data) } func GetEquipmentIndex(c *gin.Context) { appG := app.Gin{C: c} //p := new(home.GetPastureIndexReq) pastureId := c.Query("pastureId") startTime := c.Query("startTime") endTime := c.Query("endTime") data, err := svc.GetEquipmentIndex(pastureId, startTime, endTime) if err != nil { fmt.Println(err) c.JSON(200, map[string]interface{}{"code": 200, "data": err.Error(), "msg": "fail"}) return } appG.Response(200, e.SUCCESS, data) } func GetChooseMonthWXByDepartment(c *gin.Context) { appG := app.Gin{C: c} //p := new(home.GetPastureIndexReq) pastureId := c.Query("pastureId") startTime := c.Query("startTime") endTime := c.Query("endTime") data, err := svc.GetChooseMonthWXByDepartment(pastureId, startTime, endTime) if err != nil { c.JSON(200, map[string]interface{}{"code": 200, "data": err.Error(), "msg": "fail"}) return } appG.Response(200, e.SUCCESS, data) } func OAuth2Token(c *gin.Context) { //appG := app.Gin{C: c} code := c.Query("code") fmt.Println(c.Request.Host) host := "" requestHost := c.Request.Host if strings.Index(requestHost, "Eam") > -1 { host = "eam" } else { host = "http://192.168.61.41" } data, err := svc.OAuth2Token(code, host) fmt.Println(err) if err != nil { logrus.Error(err) c.JSON(401, map[string]interface{}{"code": 401, "data": "认证失败", "msg": "fail"}) return } //c.Header("token", data.AccessToken) //c.Writer.Header().Set("token", data.AccessToken) c.Redirect(http1.StatusSeeOther, fmt.Sprintf("/?access=%s", base64.StdEncoding.EncodeToString([]byte(data.AccessToken)))) //newRequest, err := http1.NewRequest("GET", "/", nil) //if err != nil { // c.String(http1.StatusInternalServerError, "Failed to create new request") // return //} // //// 设置新的请求头 //newRequest.Header.Set("Authorization", data.AccessToken) // //// 执行重定向 //resp, err := http1.DefaultClient.Do(newRequest) //if err != nil { // c.String(http1.StatusInternalServerError, "Failed to redirect request") // return //} //defer resp.Body.Close() //appG.Response(200, e.SUCCESS, data) } func OAuth2Logout(c *gin.Context) { appG := app.Gin{C: c} //token := c.Query("token") token := c.GetHeader("token") host := "" requestHost := c.Request.Host if strings.Index(requestHost, "Eam") > -1 { host = "eam" } else { host = "http://192.168.61.41" } err := svc.OAuth2Logout(token, host) if err != nil { logrus.Error(err) c.JSON(500, map[string]interface{}{"code": 500, "data": "注销失败!", "msg": "fail"}) return } //c.Redirect(303, "/") appG.Response(200, e.SUCCESS, true) } func OAuth2AddUser(c *gin.Context) { req := new(http.OAuth2AddUserReq) err := c.ShouldBind(&req) if err != nil { fmt.Println(err) c.JSON(500, map[string]interface{}{"code": 500, "data": "同步失败!", "msg": "fail"}) return } user := new(http.OAuth2AddUser) fmt.Println(req.Data) err = json.Unmarshal([]byte(req.Data), &user) data := make(map[string]string, 0) //{"password":"Xdmy@123456","name":"米鹏","disabled":false,"id":"UR8busm+AzrA1LqMRHs4NhY4LzU=","pasture":"1","userId":"20231206215655324-5EA8-20662791E","username":"07007"} userInfo, err := svc.OAuth2AddUser(user) if err != nil { fmt.Println(err) c.JSON(500, map[string]interface{}{"code": 500, "data": "同步失败!", "msg": "fail"}) return } data["id"] = util.AesEncryptByECB(fmt.Sprintf("%d", userInfo.ID)) fmt.Println(data) c.JSON(200, map[string]interface{}{"code": 200, "data": data, "msg": "ok"}) }