user.go 3.3 KB

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