84eb7c22ea54c1a9db483076216a8e54fb29f1fe.svn-base 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package api
  2. import (
  3. "../../pkg/app"
  4. "../../pkg/e"
  5. "../../pkg/util"
  6. "../../routers/restful"
  7. "../../service/user_service"
  8. "github.com/astaxie/beego/validation"
  9. "github.com/gin-gonic/gin"
  10. "net/http"
  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. Imei string `json:"imei"`
  19. PastureId string `json:"pastureid"`
  20. TypeIn int `json:"typein"`
  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 获取登录token 信息
  68. // @Tags auth
  69. // @Accept json
  70. // @Produce json
  71. // @Param username formData string true "admin"
  72. // @Param password formData string true "123456"
  73. // @Success 200 {string} json "{ "code": 200e, "data": { "token": "xxx" }, "msg": "ok" }"
  74. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  75. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  76. // @Router /auth [POST]
  77. func AuthLogin(c *gin.Context) {
  78. appG := app.Gin{C: c}
  79. var reqInfo auth
  80. err := c.BindJSON(&reqInfo)
  81. valid := validation.Validation{}
  82. valid.MaxSize(reqInfo.Username, 100, "username").Message("最长为100字符")
  83. if valid.HasErrors() {
  84. app.MarkErrors(valid.Errors)
  85. appG.Response(http.StatusInternalServerError, e.ERROR_ADD_FAIL, valid.Errors)
  86. return
  87. }
  88. err =restful.CheckUserFace(reqInfo.Username,reqInfo.Imei,reqInfo.TypeIn)
  89. if err != nil {
  90. appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_CHECK_TOKEN_FAIL, err.Error())
  91. return
  92. }
  93. token, err := util.GenerateToken(reqInfo.Username, reqInfo.Password)
  94. if err != nil {
  95. appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_TOKEN, err.Error())
  96. return
  97. }
  98. appG.Response(http.StatusOK, e.SUCCESS, map[string]string{
  99. "token": token,
  100. })
  101. }
  102. // @Summary 获取单个用户信息
  103. // @Tags users
  104. // @Accept json
  105. // @Produce json
  106. // @Param id body int true "id"
  107. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  108. // @Router /authdata/userinfo [GET]
  109. func UserLogout(c *gin.Context) {
  110. appG := app.Gin{C: c}
  111. data := make(map[string]interface{})
  112. data["name"] =""
  113. data["avatar"] = ""
  114. data["introduction"] = ""
  115. appG.Response(http.StatusOK, e.SUCCESS, data)
  116. }
  117. // @Summary 获取单个用户信息
  118. // @Tags users
  119. // @Accept json
  120. // @Produce json
  121. // @Param id path int true "iddd"
  122. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  123. // @Router /authdata/userinfo [GET]
  124. func GetUserinfo(c *gin.Context) {
  125. appG := app.Gin{C: c}
  126. data := restful.GetUserInfo(c.MustGet("jwt_username").(string))
  127. appG.Response(http.StatusOK, e.SUCCESS, data)
  128. }
  129. func GetOpenID(c *gin.Context) {
  130. code := c.Param("code")
  131. appG := app.Gin{C: c}
  132. openid, err := util.SendWxAuthAPI(code)
  133. if err != nil {
  134. appG.Response(http.StatusOK, e.SUCCESS, map[string]string{
  135. "err": "openid 获取失败 :"+err.Error(),
  136. })
  137. return
  138. }
  139. appG.Response(http.StatusOK, e.SUCCESS, map[string]string{
  140. "openid": openid,
  141. })
  142. }