user.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package api
  2. import (
  3. "../../pkg/app"
  4. "../../pkg/e"
  5. "../../pkg/util"
  6. "../../routers/restful"
  7. "../../service/user_service"
  8. "fmt"
  9. "github.com/astaxie/beego/validation"
  10. "github.com/gin-gonic/gin"
  11. "net/http"
  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. Imei string `json:"imei"`
  20. PastureId string `json:"pastureid"`
  21. TypeIn int `json:"typein"`
  22. }
  23. // @Summary 获取登录token 信息
  24. // @Tags auth
  25. // @Accept json
  26. // @Produce json
  27. // @Param username formData string true "admin"
  28. // @Param password formData string true "123456"
  29. // @Success 200 {string} json "{ "code": 200e, "data": { "token": "xxx" }, "msg": "ok" }"
  30. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  31. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  32. // @Router /auth [POST]
  33. func Auth(c *gin.Context) {
  34. appG := app.Gin{C: c}
  35. var reqInfo auth
  36. err := c.BindJSON(&reqInfo)
  37. //c.Request.ParseForm()
  38. //reqInfo.Username = c.PostForm("username")
  39. //reqInfo.Password = c.PostForm("pwd")
  40. valid := validation.Validation{}
  41. valid.MaxSize(reqInfo.Username, 100, "username").Message("最长为100字符")
  42. valid.MaxSize(reqInfo.Password, 100, "password").Message("最长为100字符")
  43. if valid.HasErrors() {
  44. app.MarkErrors(valid.Errors)
  45. appG.Response(http.StatusInternalServerError, e.ERROR_ADD_FAIL, valid.Errors)
  46. return
  47. }
  48. authService := user_service.User{Username: reqInfo.Username, Password: reqInfo.Password}
  49. isExist, err := authService.Check()
  50. if err != nil {
  51. appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_CHECK_TOKEN_FAIL, err)
  52. return
  53. }
  54. if !isExist {
  55. //appG.Response(http.StatusUnauthorized, e.ERROR_AUTH, nil)
  56. appG.Response(http.StatusOK, e.ERROR_AUTH, "用户名或密码错误")
  57. return
  58. }
  59. token, err := util.GenerateToken(reqInfo.Username, reqInfo.Password)
  60. if err != nil {
  61. appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_TOKEN, err)
  62. return
  63. }
  64. appG.Response(http.StatusOK, e.SUCCESS, map[string]string{
  65. "token": token,
  66. })
  67. }
  68. // @Summary 获取登录token 信息
  69. // @Tags auth
  70. // @Accept json
  71. // @Produce json
  72. // @Param username formData string true "admin"
  73. // @Param password formData string true "123456"
  74. // @Success 200 {string} json "{ "code": 200e, "data": { "token": "xxx" }, "msg": "ok" }"
  75. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  76. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  77. // @Router /auth [POST]
  78. func AuthLogin(c *gin.Context) {
  79. appG := app.Gin{C: c}
  80. var reqInfo auth
  81. err := c.BindJSON(&reqInfo)
  82. fmt.Println("++++++++++++++++",reqInfo)
  83. valid := validation.Validation{}
  84. valid.MaxSize(reqInfo.Username, 100, "username").Message("最长为100字符")
  85. if valid.HasErrors() {
  86. app.MarkErrors(valid.Errors)
  87. appG.Response(http.StatusInternalServerError, e.ERROR_ADD_FAIL, valid.Errors)
  88. return
  89. }
  90. err =restful.CheckUserFace(reqInfo.Username,reqInfo.Imei,reqInfo.TypeIn)
  91. if err != nil {
  92. appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_CHECK_TOKEN_FAIL, err.Error())
  93. return
  94. }
  95. token, err := util.GenerateToken(reqInfo.Username, reqInfo.Password)
  96. if err != nil {
  97. appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_TOKEN, err.Error())
  98. return
  99. }
  100. appG.Response(http.StatusOK, e.SUCCESS, map[string]string{
  101. "token": token,
  102. })
  103. }
  104. // @Summary 获取单个用户信息
  105. // @Tags users
  106. // @Accept json
  107. // @Produce json
  108. // @Param id body int true "id"
  109. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  110. // @Router /authdata/userinfo [GET]
  111. func UserLogout(c *gin.Context) {
  112. appG := app.Gin{C: c}
  113. data := make(map[string]interface{})
  114. data["name"] =""
  115. data["avatar"] = ""
  116. data["introduction"] = ""
  117. appG.Response(http.StatusOK, e.SUCCESS, data)
  118. }
  119. // @Summary 获取单个用户信息
  120. // @Tags users
  121. // @Accept json
  122. // @Produce json
  123. // @Param id path int true "iddd"
  124. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  125. // @Router /authdata/userinfo [GET]
  126. func GetUserinfo(c *gin.Context) {
  127. appG := app.Gin{C: c}
  128. data := restful.GetUserInfo(c.MustGet("jwt_username").(string))
  129. appG.Response(http.StatusOK, e.SUCCESS, data)
  130. }
  131. func GetOpenID(c *gin.Context) {
  132. code := c.Param("code")
  133. appG := app.Gin{C: c}
  134. openid, err := util.SendWxAuthAPI(code)
  135. if err != nil {
  136. appG.Response(http.StatusOK, e.SUCCESS, map[string]string{
  137. "err": "openid 获取失败 :"+err.Error(),
  138. })
  139. return
  140. }
  141. appG.Response(http.StatusOK, e.SUCCESS, map[string]string{
  142. "openid": openid,
  143. })
  144. }