123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package api
- import (
- "net/http"
- "github.com/astaxie/beego/validation"
- "github.com/gin-gonic/gin"
- "kpt.xdmy/apiserver/routers/restful"
- "kpt.xdmy/apiserver/service/user_service"
- "kpt.xdmy/pkg/app"
- "kpt.xdmy/pkg/e"
- "kpt.xdmy/pkg/util"
- )
- type auth struct {
- Id int `json:"id"`
- Username string `json:"username"`
- Password string `json:"password"`
- CaptchaKey string `json:"CaptchaKey"`
- Role int `json:"role_id"`
- }
- // @Summary 获取登录token 信息
- // @Tags auth
- // @Accept json
- // @Produce json
- // @Param username formData string true "admin"
- // @Param password formData string true "123456"
- // @Success 200 {string} json "{ "code": 200e, "data": { "token": "xxx" }, "msg": "ok" }"
- // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
- // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
- // @Router /auth [POST]
- func Auth(c *gin.Context) {
- appG := app.Gin{C: c}
- var reqInfo auth
- err := c.BindJSON(&reqInfo)
- //c.Request.ParseForm()
- //reqInfo.Username = c.PostForm("username")
- //reqInfo.Password = c.PostForm("pwd")
- valid := validation.Validation{}
- valid.MaxSize(reqInfo.Username, 100, "username").Message("最长为100字符")
- valid.MaxSize(reqInfo.Password, 100, "password").Message("最长为100字符")
- if valid.HasErrors() {
- app.MarkErrors(valid.Errors)
- appG.Response(http.StatusInternalServerError, e.ERROR_ADD_FAIL, valid.Errors)
- return
- }
- authService := user_service.User{Username: reqInfo.Username, Password: reqInfo.Password}
- isExist, err := authService.Check()
- if err != nil {
- appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_CHECK_TOKEN_FAIL, err)
- return
- }
- if !isExist {
- //appG.Response(http.StatusUnauthorized, e.ERROR_AUTH, nil)
- appG.Response(http.StatusOK, e.ERROR_AUTH, "用户名或密码错误")
- return
- }
- token, err := util.GenerateToken(reqInfo.Username, reqInfo.Password)
- if err != nil {
- appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_TOKEN, err)
- return
- }
- appG.Response(http.StatusOK, e.SUCCESS, map[string]string{
- "token": token,
- })
- }
- // @Summary 获取单个用户信息
- // @Tags users
- // @Accept json
- // @Produce json
- // @Param id body int true "id"
- // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
- // @Router /authdata/userinfo [GET]
- func UserLogout(c *gin.Context) {
- appG := app.Gin{C: c}
- data := make(map[string]interface{})
- data["name"] = ""
- data["avatar"] = ""
- data["introduction"] = ""
- appG.Response(http.StatusOK, e.SUCCESS, data)
- }
- // @Summary 获取单个用户信息
- // @Tags users
- // @Accept json
- // @Produce json
- // @Param id path int true "iddd"
- // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
- // @Router /authdata/userinfo [GET]
- func GetUserinfo(c *gin.Context) {
- appG := app.Gin{C: c}
- data := restful.GetUserInfo(c.MustGet("jwt_username").(string))
- appG.Response(http.StatusOK, e.SUCCESS, data)
- }
- func GetOpenID(c *gin.Context) {
- code := c.Param("code")
- appG := app.Gin{C: c}
- openid, err := util.SendWxAuthAPI(code)
- if err != nil {
- appG.Response(http.StatusOK, e.SUCCESS, map[string]string{
- "err": "openid 获取失败 :" + err.Error(),
- })
- return
- }
- appG.Response(http.StatusOK, e.SUCCESS, map[string]string{
- "openid": openid,
- })
- }
|