home.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package routers
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "github.com/sirupsen/logrus"
  8. "kpt.xdmy/apiserver/model/http"
  9. "kpt.xdmy/pkg/app"
  10. "kpt.xdmy/pkg/e"
  11. "kpt.xdmy/pkg/util"
  12. http1 "net/http"
  13. "strings"
  14. )
  15. func OverallIndex(c *gin.Context) {
  16. appG := app.Gin{C: c}
  17. data, err := svc.OverallIndex()
  18. if err != nil {
  19. c.JSON(200, map[string]interface{}{"code": 200, "data": err.Error(), "msg": "fail"})
  20. return
  21. }
  22. appG.Response(200, e.SUCCESS, data)
  23. }
  24. func CentralIndex(c *gin.Context) {
  25. appG := app.Gin{C: c}
  26. data, err := svc.CentralIndex()
  27. if err != nil {
  28. c.JSON(200, map[string]interface{}{"code": 200, "data": err.Error(), "msg": "fail"})
  29. return
  30. }
  31. appG.Response(200, e.SUCCESS, data)
  32. }
  33. func GetExpense(c *gin.Context) {
  34. appG := app.Gin{C: c}
  35. pastureName := c.Query("pastureName")
  36. data, err := svc.GetExpense(pastureName)
  37. if err != nil {
  38. c.JSON(200, map[string]interface{}{"code": 200, "data": err.Error(), "msg": "fail"})
  39. return
  40. }
  41. appG.Response(200, e.SUCCESS, data)
  42. }
  43. func GetPastureIndex(c *gin.Context) {
  44. appG := app.Gin{C: c}
  45. //p := new(home.GetPastureIndexReq)
  46. pastureName := c.Query("pastureName")
  47. data, err := svc.GetPastureIndex(pastureName)
  48. if err != nil {
  49. c.JSON(200, map[string]interface{}{"code": 200, "data": err.Error(), "msg": "fail"})
  50. return
  51. }
  52. appG.Response(200, e.SUCCESS, data)
  53. }
  54. func GetEquipmentIndex(c *gin.Context) {
  55. appG := app.Gin{C: c}
  56. //p := new(home.GetPastureIndexReq)
  57. pastureId := c.Query("pastureId")
  58. startTime := c.Query("startTime")
  59. endTime := c.Query("endTime")
  60. data, err := svc.GetEquipmentIndex(pastureId, startTime, endTime)
  61. if err != nil {
  62. fmt.Println(err)
  63. c.JSON(200, map[string]interface{}{"code": 200, "data": err.Error(), "msg": "fail"})
  64. return
  65. }
  66. appG.Response(200, e.SUCCESS, data)
  67. }
  68. func GetChooseMonthWXByDepartment(c *gin.Context) {
  69. appG := app.Gin{C: c}
  70. //p := new(home.GetPastureIndexReq)
  71. pastureId := c.Query("pastureId")
  72. startTime := c.Query("startTime")
  73. endTime := c.Query("endTime")
  74. data, err := svc.GetChooseMonthWXByDepartment(pastureId, startTime, endTime)
  75. if err != nil {
  76. c.JSON(200, map[string]interface{}{"code": 200, "data": err.Error(), "msg": "fail"})
  77. return
  78. }
  79. appG.Response(200, e.SUCCESS, data)
  80. }
  81. func OAuth2Token(c *gin.Context) {
  82. //appG := app.Gin{C: c}
  83. code := c.Query("code")
  84. fmt.Println(c.Request.Host)
  85. host := ""
  86. requestHost := c.Request.Host
  87. if strings.Index(requestHost, "Eam") > -1 {
  88. host = "eam"
  89. } else {
  90. host = "http://192.168.61.41"
  91. }
  92. data, err := svc.OAuth2Token(code, host)
  93. fmt.Println(err)
  94. if err != nil {
  95. logrus.Error(err)
  96. c.JSON(401, map[string]interface{}{"code": 401, "data": "认证失败", "msg": "fail"})
  97. return
  98. }
  99. //c.Header("token", data.AccessToken)
  100. //c.Writer.Header().Set("token", data.AccessToken)
  101. c.Redirect(http1.StatusSeeOther, fmt.Sprintf("/?access=%s", base64.StdEncoding.EncodeToString([]byte(data.AccessToken))))
  102. //newRequest, err := http1.NewRequest("GET", "/", nil)
  103. //if err != nil {
  104. // c.String(http1.StatusInternalServerError, "Failed to create new request")
  105. // return
  106. //}
  107. //
  108. //// 设置新的请求头
  109. //newRequest.Header.Set("Authorization", data.AccessToken)
  110. //
  111. //// 执行重定向
  112. //resp, err := http1.DefaultClient.Do(newRequest)
  113. //if err != nil {
  114. // c.String(http1.StatusInternalServerError, "Failed to redirect request")
  115. // return
  116. //}
  117. //defer resp.Body.Close()
  118. //appG.Response(200, e.SUCCESS, data)
  119. }
  120. func OAuth2Logout(c *gin.Context) {
  121. appG := app.Gin{C: c}
  122. //token := c.Query("token")
  123. token := c.GetHeader("token")
  124. host := ""
  125. requestHost := c.Request.Host
  126. if strings.Index(requestHost, "Eam") > -1 {
  127. host = "eam"
  128. } else {
  129. host = "http://192.168.61.41"
  130. }
  131. err := svc.OAuth2Logout(token, host)
  132. if err != nil {
  133. logrus.Error(err)
  134. c.JSON(500, map[string]interface{}{"code": 500, "data": "注销失败!", "msg": "fail"})
  135. return
  136. }
  137. //c.Redirect(303, "/")
  138. appG.Response(200, e.SUCCESS, true)
  139. }
  140. func OAuth2AddUser(c *gin.Context) {
  141. req := new(http.OAuth2AddUserReq)
  142. err := c.ShouldBind(&req)
  143. if err != nil {
  144. fmt.Println(err)
  145. c.JSON(500, map[string]interface{}{"code": 500, "data": "同步失败!", "msg": "fail"})
  146. return
  147. }
  148. user := new(http.OAuth2AddUser)
  149. fmt.Println(req.Data)
  150. err = json.Unmarshal([]byte(req.Data), &user)
  151. data := make(map[string]string, 0)
  152. //{"password":"Xdmy@123456","name":"米鹏","disabled":false,"id":"UR8busm+AzrA1LqMRHs4NhY4LzU=","pasture":"1","userId":"20231206215655324-5EA8-20662791E","username":"07007"}
  153. userInfo, err := svc.OAuth2AddUser(user)
  154. if err != nil {
  155. fmt.Println(err)
  156. c.JSON(500, map[string]interface{}{"code": 500, "data": "同步失败!", "msg": "fail"})
  157. return
  158. }
  159. data["id"] = util.AesEncryptByECB(fmt.Sprintf("%d", userInfo.ID))
  160. fmt.Println(data)
  161. c.JSON(200, map[string]interface{}{"code": 200, "data": data, "msg": "ok"})
  162. }