代码拉取完成,页面将自动刷新
package restweb
import (
"net/http"
"strconv"
"strings"
"time"
restfulspec "github.com/emicklei/go-restful-openapi/v2"
gorestful "github.com/emicklei/go-restful/v3"
"github.com/kataras/golog"
klog "k8s.io/klog/v2"
"gitee.com/banyanhouse/distill-infra/api"
"gitee.com/banyanhouse/distill-infra/authentication/middleware/jwts"
"gitee.com/banyanhouse/distill-infra/authentication/middleware/jwts/models"
"gitee.com/banyanhouse/distill-infra/authentication/vo"
"gitee.com/banyanhouse/distill-infra/base"
"gitee.com/banyanhouse/distill-infra/utils"
"gitee.com/banyanhouse/distill-infra/utils/supports"
"gitee.com/banyanhouse/distill-infra/web"
)
/**
@Project: distill-infra
@Description:
@Version 1.0
@Author: lyndon Create 1.0 9/16/21
@Copyright (c)2021-2022 gridsum.com
@Modified By:
*/
func init() {
userApi := new(UserApi)
api.RegisterApi(userApi)
api.SetupApi(userApi)
}
type UserApi struct {
}
func (u *UserApi) Init() {
klog.Info("UserApi Init")
}
func (u *UserApi) Setup() {
klog.Info("UserApi Setup")
webService := web.RestWebService()
webService.Route(webService.POST("/user/registe").To(u.Registe).
Doc("user register").
Metadata(restfulspec.KeyOpenAPITags, []string{"registe"}).
Writes("user register").
Returns(http.StatusOK, "OK", "user register"))
webService.Route(webService.POST("/user/login").To(u.Login).
Doc("user login").
Metadata(restfulspec.KeyOpenAPITags, []string{"userLogin"}).
Writes("user login").
Returns(http.StatusOK, "OK", "user login"))
webService.Route(webService.POST("/user/logout").To(u.LoginOut).
Doc("user logout").
Metadata(restfulspec.KeyOpenAPITags, []string{"userLogOut"}).
Writes("user logout").
Returns(http.StatusOK, "OK", "user logout"))
webService.Route(webService.PUT("/user").To(u.UpdateUser).
Doc("user update").
Metadata(restfulspec.KeyOpenAPITags, []string{"userUpdate"}).
Writes("user update").
Returns(http.StatusOK, "OK", "user update"))
webService.Route(webService.DELETE("/user/{uids}").To(u.DeleteUsers).
Doc("user delete").
Metadata(restfulspec.KeyOpenAPITags, []string{"userDelete"}).
Writes("user delete").
Returns(http.StatusOK, "OK", "user delete"))
}
func (u *UserApi) Registe(request *gorestful.Request, response *gorestful.Response) {
var (
err error
user = new(models.UserApp)
effect int64
)
if err = request.ReadEntity(&user); err != nil {
klog.Errorf("用户[%s]注册失败: %s", user.UserName, err)
goto FAIL
}
user.Pwd = utils.AESEncrypt([]byte(user.Pwd))
user.Enable = 1
user.CreatedAt = time.Now()
effect, err = models.CreateUser(user)
if effect <= 0 || err != nil {
klog.Errorf("用户[%s]注册失败: %s", user.UserName, err)
goto FAIL
}
supports.RestOk_(response, supports.RegisteSuccess)
return
FAIL:
supports.RestError(response, http.StatusInternalServerError, supports.RegisteFailur, nil)
return
}
func (u *UserApi) LoginOut(request *gorestful.Request, response *gorestful.Response) {
supports.RestOk(response, supports.LoginSuccess, nil)
return
}
func (u *UserApi) Login(request *gorestful.Request, response *gorestful.Response) {
var (
err error
user = new(models.UserApp)
mUser = new(models.UserApp) // 查询数据后填充了数据的user
ckPassword bool
token string
jwtTimeout int
secret string
)
if err = request.ReadEntity(&user); err != nil {
klog.Errorf("用户[%s]登录失败: %s", "", err.Error())
supports.RestError(response, http.StatusBadRequest, supports.LoginFailur, nil)
return
}
mUser.UserName = user.UserName
has, err := models.GetUserByUsername(mUser)
if err != nil {
klog.Errorf("用户[%s]登录失败。%s", user.UserName, err.Error())
supports.RestError(response, http.StatusInternalServerError, supports.LoginFailur, nil)
return
}
if !has { // 用户名不正确
supports.RestUnauthorized(response, supports.UsernameFailur, nil)
return
}
ckPassword = utils.CheckPWD(user.Pwd, mUser.Pwd)
if !ckPassword {
supports.RestUnauthorized(response, supports.PasswordFailur, nil)
return
}
jwtTimeout = base.Props().Jwt.JWTTimeout
secret = base.Props().Jwt.Secret
token, err = jwts.GenerateToken(mUser, jwtTimeout, secret)
golog.Infof("用户[%s], 登录生成token [%s]", mUser.UserName, token)
if err != nil {
klog.Errorf("用户[%s]登录,生成token出错。%s", user.UserName, err.Error())
supports.RestError(response, http.StatusInternalServerError, supports.TokenCreateFailur, nil)
return
}
supports.RestOk(response, supports.LoginSuccess, vo.BuildUserVO(token, mUser))
return
}
func (u *UserApi) UpdateUser(request *gorestful.Request, response *gorestful.Response) {
user := new(models.UserApp)
if err := request.ReadEntity(&user); err != nil {
klog.Errorf("更新用户[%s]失败。%s", "", err.Error())
supports.RestError(response, http.StatusBadRequest, supports.OptionFailur, nil)
return
}
effect, err := models.UpdateUserById(user)
if err != nil {
klog.Errorf("更新用户[%s]失败。%s", "", err.Error())
supports.RestError(response, http.StatusInternalServerError, supports.OptionFailur, nil)
return
}
supports.RestOk(response, supports.OptionSuccess, effect)
}
// 删除用户
func (u *UserApi) DeleteUsers(request *gorestful.Request, response *gorestful.Response) {
uids := request.PathParameter("uids")
uidList := strings.Split(uids, ",")
if len(uidList) == 0 {
klog.Error("删除用户错误, 参数不对.")
supports.RestError(response, http.StatusBadRequest, supports.ParseParamsFailur, nil)
return
}
dUids := make([]int64, 0)
for _, v := range uidList {
if v == "" {
continue
}
uid, err := strconv.ParseInt(v, 10, 64)
if err != nil {
klog.Errorf("删除用户错误, %s", err.Error())
supports.RestError(response, http.StatusInternalServerError, supports.ParseParamsFailur, nil)
return
}
dUids = append(dUids, uid)
}
effect, err := models.DeleteByUsers(dUids)
if err != nil {
klog.Errorf("删除用户错误, %s", err.Error())
supports.RestError(response, http.StatusInternalServerError, supports.DeleteUsersFailur, nil)
return
}
supports.RestOk(response, supports.DeleteUsersSuccess, effect)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。