user.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package api
  2. import (
  3. "fmt"
  4. "github.com/Anderson-Lu/gofasion/gofasion"
  5. "github.com/astaxie/beego/validation"
  6. "github.com/gin-gonic/gin"
  7. "github.com/kptyun/KPTCOMM/pkg/app"
  8. "github.com/kptyun/KPTCOMM/pkg/e"
  9. "github.com/kptyun/KPTCOMM/pkg/util"
  10. "github.com/kptyun/KPTCOMM/routers/restful"
  11. "github.com/kptyun/KPTCOMM/service/user_service"
  12. "io/ioutil"
  13. "net/http"
  14. )
  15. type auth struct {
  16. Id int `json:"id"`
  17. Username string `json:"username"`
  18. Password string `json:"password"`
  19. CaptchaKey string `json:"CaptchaKey"`
  20. Role int `json:"role_id"`
  21. }
  22. // @Summary 获取登录token 信息
  23. // @Tags auth
  24. // @Accept json
  25. // @Produce json
  26. // @Param username formData string true "admin"
  27. // @Param password formData string true "123456"
  28. // @Success 200 {string} json "{ "code": 200e, "data": { "token": "xxx" }, "msg": "ok" }"
  29. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  30. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  31. // @Router /auth [POST]
  32. func Auth(c *gin.Context) {
  33. appG := app.Gin{C: c}
  34. var reqInfo auth
  35. err := c.BindJSON(&reqInfo)
  36. //c.Request.ParseForm()
  37. //reqInfo.Username = c.PostForm("username")
  38. //reqInfo.Password = c.PostForm("pwd")
  39. valid := validation.Validation{}
  40. valid.MaxSize(reqInfo.Username, 100, "username").Message("最长为100字符")
  41. valid.MaxSize(reqInfo.Password, 100, "password").Message("最长为100字符")
  42. if valid.HasErrors() {
  43. app.MarkErrors(valid.Errors)
  44. appG.Response(http.StatusInternalServerError, e.ERROR_ADD_FAIL, valid.Errors)
  45. return
  46. }
  47. authService := user_service.User{Username: reqInfo.Username, Password: reqInfo.Password}
  48. isExist, err := authService.Check()
  49. if err != nil {
  50. appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_CHECK_TOKEN_FAIL, err)
  51. return
  52. }
  53. if !isExist {
  54. //appG.Response(http.StatusUnauthorized, e.ERROR_AUTH, nil)
  55. appG.Response(http.StatusOK, e.ERROR_AUTH, "用户名或密码错误")
  56. return
  57. }
  58. token, err := util.GenerateToken(reqInfo.Username, reqInfo.Password)
  59. if err != nil {
  60. appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_TOKEN, err)
  61. return
  62. }
  63. appG.Response(http.StatusOK, e.SUCCESS, map[string]string{
  64. "token": token,
  65. })
  66. }
  67. // @Summary 获取单个用户信息
  68. // @Tags users
  69. // @Accept json
  70. // @Produce json
  71. // @Param id body int true "id"
  72. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  73. // @Router /authdata/userinfo [GET]
  74. func UserLogout(c *gin.Context) {
  75. appG := app.Gin{C: c}
  76. data := make(map[string]interface{})
  77. data["name"] = ""
  78. data["avatar"] = ""
  79. data["introduction"] = ""
  80. appG.Response(http.StatusOK, e.SUCCESS, data)
  81. }
  82. // @Summary 获取单个用户信息
  83. // @Tags users
  84. // @Accept json
  85. // @Produce json
  86. // @Param id path int true "iddd"
  87. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  88. // @Router /authdata/userinfo [GET]
  89. func GetUserinfo(c *gin.Context) {
  90. appG := app.Gin{C: c}
  91. data := restful.GetUserInfo(c.MustGet("jwt_username").(string))
  92. appG.Response(http.StatusOK, e.SUCCESS, data)
  93. }
  94. // @Summary 注册牛场
  95. // @Tags users
  96. // @Accept json
  97. // @Produce json
  98. // @Param id path int true "iddd"
  99. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  100. // @Router /authdata/userinfo [GET]
  101. func RegPasture(c *gin.Context) {
  102. appG := app.Gin{C: c}
  103. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  104. fsion := gofasion.NewFasion(string(dataByte))
  105. imei := fsion.Get("imei").ValueStr()
  106. fmt.Println(imei)
  107. data := imei //restful.GetUserInfo(imei)
  108. appG.Response(http.StatusOK, e.SUCCESS, data)
  109. }