user.go 3.5 KB

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