代码拉取完成,页面将自动刷新
package web
import (
"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/utils"
"gitee.com/banyanhouse/distill-infra/utils/supports"
"gitee.com/banyanhouse/distill-infra/web"
"strconv"
"strings"
"time"
"github.com/kataras/golog"
"github.com/kataras/iris"
"github.com/kataras/iris/hero"
)
func init() {
api.RegisterApi(new(UserApi))
}
type UserApi struct {
}
func (u *UserApi) Init() {
groupRouter := web.IrisMainParty()
u.UserHub(groupRouter)
}
func (u *UserApi) Setup() {
}
func (u *UserApi) UserHub(party iris.Party) {
var user = party.Party("/user")
//p.Use(middleware.BasicAuth)
user.Post("/registe", hero.Handler(u.Registe))
user.Post("/login", hero.Handler(u.Login))
user.Post("/logout", hero.Handler(u.LoginOut))
}
func (u *UserApi) Registe(ctx iris.Context) {
var (
err error
user = new(models.UserApp)
effect int64
)
if err = ctx.ReadJSON(&user); err != nil {
ctx.Application().Logger().Errorf("用户[%s]注册失败。%s", user.UserName, err.Error())
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 {
ctx.Application().Logger().Errorf("用户[%s]注册失败。%s", user.UserName, err.Error())
goto FAIL
}
supports.Ok_(ctx, supports.RegisteSuccess)
return
FAIL:
supports.Error(ctx, iris.StatusInternalServerError, supports.RegisteFailur, nil)
return
}
func (u *UserApi) LoginOut(ctx iris.Context) {
supports.Ok(ctx, supports.LoginSuccess, nil)
return
}
func (u *UserApi) Login(ctx iris.Context) {
var (
err error
user = new(models.UserApp)
mUser = new(models.UserApp) // 查询数据后填充了数据的user
ckPassword bool
token string
jwtTimeout int
secret string
)
if err = ctx.ReadJSON(&user); err != nil {
ctx.Application().Logger().Errorf("用户[%s]登录失败。%s", "", err.Error())
supports.Error(ctx, iris.StatusBadRequest, supports.LoginFailur, nil)
return
}
mUser.UserName = user.UserName
has, err := models.GetUserByUsername(mUser)
if err != nil {
ctx.Application().Logger().Errorf("用户[%s]登录失败。%s", user.UserName, err.Error())
supports.Error(ctx, iris.StatusInternalServerError, supports.LoginFailur, nil)
return
}
if !has { // 用户名不正确
supports.Unauthorized(ctx, supports.UsernameFailur, nil)
return
}
ckPassword = utils.CheckPWD(user.Pwd, mUser.Pwd)
if !ckPassword {
supports.Unauthorized(ctx, supports.PasswordFailur, nil)
return
}
jwtTimeout = int(ctx.Application().ConfigurationReadOnly().GetOther()["JWTTimeout"].(float64))
secret = ctx.Application().ConfigurationReadOnly().GetOther()["Secret"].(string)
token, err = jwts.GenerateToken(mUser, jwtTimeout, secret)
golog.Infof("用户[%s], 登录生成token [%s]", mUser.UserName, token)
if err != nil {
ctx.Application().Logger().Errorf("用户[%s]登录,生成token出错。%s", user.UserName, err.Error())
supports.Error(ctx, iris.StatusInternalServerError, supports.TokenCreateFailur, nil)
return
}
supports.Ok(ctx, supports.LoginSuccess, vo.BuildUserVO(token, mUser))
return
}
//func UserDepTree(ctx iris.Context) {
// var (
// err error
// deps []*models.Dep
// )
//
// if deps, err = models.GetAllDep(); err != nil {
// supports.Error(ctx, iris.StatusInternalServerError, supports.OptionFailur, nil)
// return
// }
//
// supports.Ok(ctx, supports.OptionSuccess, vo.BuildDepTreeForUser(deps))
// return
//}
// 用户报表
//func UserTable(ctx iris.Context) {
// var (
// err error
// page *supports.Pagination
// total int64
// userList []*models.UserApp
// //depId int
// )
// //depId, err = ctx.URLParamInt("depId")
// page, err = supports.NewPagination(ctx)
// if err != nil {
// ctx.Application().Logger().Error(err)
// //golog.Error(err)
// goto FAIL
// }
//
// userList, total, err = models.GetPaginationUsers(page)
// if err != nil {
// ctx.Application().Logger().Error(err)
// //golog.Error(err)
// goto ERR
// }
//
// ctx.JSON(vo.TableVO{
// Total: total,
// Rows: vo.BuildUserVOList(userList...),
// })
// return
//
//FAIL:
// supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
// return
//ERR:
// supports.Error(ctx, iris.StatusInternalServerError, supports.OptionFailur, nil)
// return
//}
func (u *UserApi) UpdateUser(ctx iris.Context) {
user := new(models.UserApp)
if err := ctx.ReadJSON(&user); err != nil {
ctx.Application().Logger().Errorf("更新用户[%s]失败。%s", "", err.Error())
supports.Error(ctx, iris.StatusBadRequest, supports.OptionFailur, nil)
return
}
effect, err := models.UpdateUserById(user)
if err != nil {
ctx.Application().Logger().Errorf("更新用户[%s]失败。%s", "", err.Error())
supports.Error(ctx, iris.StatusInternalServerError, supports.OptionFailur, nil)
return
}
supports.Ok(ctx, supports.OptionSuccess, effect)
}
// 删除用户
func (u *UserApi) DeleteUsers(ctx iris.Context, uids string) {
uidList := strings.Split(uids, ",")
if len(uidList) == 0 {
ctx.Application().Logger().Error("删除用户错误, 参数不对.")
supports.Error(ctx, iris.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 {
ctx.Application().Logger().Errorf("删除用户错误, %s", err.Error())
supports.Error(ctx, iris.StatusInternalServerError, supports.ParseParamsFailur, nil)
return
}
dUids = append(dUids, uid)
}
effect, err := models.DeleteByUsers(dUids)
if err != nil {
ctx.Application().Logger().Errorf("删除用户错误, %s", err.Error())
supports.Error(ctx, iris.StatusInternalServerError, supports.DeleteUsersFailur, nil)
return
}
supports.Ok(ctx, supports.DeleteUsersSuccess, effect)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。