forked from all_user/stat_center
441 lines
11 KiB
Go
441 lines
11 KiB
Go
package adminApi
|
|
|
|
import (
|
|
"encoding/base32"
|
|
"fmt"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/skip2/go-qrcode"
|
|
"stat/api/proto"
|
|
"stat/common"
|
|
"stat/common/googauth"
|
|
"stat/common/stdLog"
|
|
"stat/common/utils"
|
|
"stat/models/adminMod"
|
|
"stat/models/authorityMod"
|
|
"stat/service/adminSer"
|
|
"stat/service/opLogSer"
|
|
)
|
|
|
|
// @Tags 系统管理
|
|
// @Summary 管理员登录
|
|
// @Description 传入用户信息登录
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param param body proto.AdminLoginReq true "参数"
|
|
// @Success 200 {string} string "成功"
|
|
// @Success 400 {string} string "失败"
|
|
// @Router /api/stat/admin/login [POST]
|
|
func AdminLogin(ctx *gin.Context) {
|
|
var param struct {
|
|
proto.AdminLoginReq
|
|
}
|
|
if err := ctx.ShouldBindJSON(¶m); err != nil {
|
|
stdLog.ServerJSON(ctx, stdLog.ParamError, err)
|
|
return
|
|
}
|
|
adminInfo, err := adminMod.FindOneByAccount(ctx, *param.Account)
|
|
if err != nil || adminInfo.Account == "" {
|
|
stdLog.ServerJSON(ctx, stdLog.AdminAccountNotExists, err)
|
|
return
|
|
}
|
|
if adminInfo.Secret == "" {
|
|
secret := base32.StdEncoding.EncodeToString([]byte(fmt.Sprintf("admin%s", common.RandStr(10))))
|
|
_ = adminMod.TwoStepSecretSet(ctx, adminInfo.Account, secret)
|
|
}
|
|
|
|
// 通过验证,再次验证密码是否正确
|
|
if adminInfo.Password != *param.Password || adminInfo.BlocRole == 1 {
|
|
stdLog.ServerJSON(ctx, stdLog.AdminPasswordError, err)
|
|
return
|
|
}
|
|
|
|
qrCodePNG := make([]byte, 0)
|
|
if !adminInfo.HasBind {
|
|
url := googauth.New(adminInfo.Secret, adminInfo.Account+"@sc")
|
|
qrCodePNG, err = qrcode.Encode(url, qrcode.Medium, 256)
|
|
if err != nil {
|
|
stdLog.ServerJSON(ctx, stdLog.AdminAccountNotExists, "Admin login get QRCodePNG error: "+err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
stdLog.ServerJSON(ctx, stdLog.Success, gin.H{
|
|
"qrCodePNG": qrCodePNG,
|
|
})
|
|
}
|
|
|
|
// @Tags 系统管理
|
|
// @Summary 管理员登录
|
|
// @Description 传入用户信息登录
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param param body proto.AdminLoginReq true "参数"
|
|
// @Success 200 {string} string "成功"
|
|
// @Success 400 {string} string "失败"
|
|
// @Router /api/stat/adminbloc/login [POST]
|
|
func AdminLoginBloc(ctx *gin.Context) {
|
|
param := proto.AdminLoginReq{}
|
|
if err := ctx.ShouldBindJSON(¶m); err != nil {
|
|
stdLog.ServerJSON(ctx, stdLog.ParamError, err)
|
|
return
|
|
}
|
|
|
|
adminInfo, err := adminMod.FindOneByAccount(ctx, *param.Account)
|
|
if err != nil || adminInfo.Account == "" {
|
|
stdLog.ServerJSON(ctx, stdLog.AdminAccountNotExists, err)
|
|
return
|
|
}
|
|
|
|
if adminInfo.Secret == "" {
|
|
secret := base32.StdEncoding.EncodeToString([]byte(fmt.Sprintf("admin%s", common.RandStr(10))))
|
|
_ = adminMod.TwoStepSecretSet(ctx, adminInfo.Account, secret)
|
|
}
|
|
|
|
// 通过验证,再次验证密码是否正确
|
|
if adminInfo.Password != *param.Password {
|
|
stdLog.ServerJSON(ctx, stdLog.AdminPasswordError, err)
|
|
return
|
|
}
|
|
|
|
qrCodePNG := make([]byte, 0)
|
|
if !adminInfo.HasBind {
|
|
url := googauth.New(adminInfo.Secret, adminInfo.Account+"@sc")
|
|
qrCodePNG, err = qrcode.Encode(url, qrcode.Medium, 256)
|
|
if err != nil {
|
|
stdLog.ServerJSON(ctx, stdLog.AdminAccountNotExists, "Admin login get QRCodePNG error: "+err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
stdLog.ServerJSON(ctx, stdLog.Success, gin.H{
|
|
"qrCodePNG": qrCodePNG,
|
|
})
|
|
}
|
|
|
|
// @Tags 系统管理
|
|
// @Summary 管理员登录验证
|
|
// @Description 谷歌验证
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param param body proto.AdminVerifyReq true "参数"
|
|
// @Success 200 {object} proto.AdminLoginRes "成功"
|
|
// @Success 400 {string} string "失败"
|
|
// @Router /api/stat/admin/verify [POST]
|
|
func AdminVerify(ctx *gin.Context) {
|
|
param := proto.AdminVerifyReq{}
|
|
if err := ctx.ShouldBindJSON(¶m); err != nil {
|
|
stdLog.ServerJSON(ctx, stdLog.ParamError, err)
|
|
return
|
|
}
|
|
|
|
adminInfo, err := adminMod.FindOneByAccount(ctx, *param.Account)
|
|
if err != nil || adminInfo.Account == "" {
|
|
stdLog.ServerJSON(ctx, stdLog.AdminAccountNotExists, err)
|
|
return
|
|
}
|
|
|
|
//测试模式不使用两步验证
|
|
if gin.Mode() == gin.ReleaseMode {
|
|
ok, err := googauth.Verify(adminInfo.Secret, *param.VerifyCode)
|
|
if err != nil {
|
|
stdLog.ServerJSON(ctx, stdLog.GoogleAuthFailed, "admin Verification 2-step error: "+err.Error())
|
|
return
|
|
}
|
|
if !ok {
|
|
stdLog.ServerJSON(ctx, stdLog.GoogleAuthFailed, "admin Verification 2-step verification failed")
|
|
return
|
|
}
|
|
}
|
|
|
|
code, adminInfo, err := adminSer.AdminLogin(ctx, param)
|
|
if err != nil || code != stdLog.Success {
|
|
stdLog.ServerJSON(ctx, code, err)
|
|
return
|
|
}
|
|
|
|
roleInfo, err := authorityMod.GetAuthorityById(ctx, adminInfo.RoleId)
|
|
if err != nil {
|
|
stdLog.ServerJSON(ctx, stdLog.AdminGetAuthorityFailed, err)
|
|
return
|
|
}
|
|
|
|
resp := proto.AdminLoginRes{
|
|
Account: adminInfo.Account,
|
|
Token: adminInfo.Token,
|
|
Name: adminInfo.Name,
|
|
RoleInfo: proto.RoleInfo{
|
|
RoleId: roleInfo.Id,
|
|
Role: roleInfo.Role,
|
|
AuthJson: roleInfo.AuthJson,
|
|
},
|
|
}
|
|
|
|
stdLog.ServerJSON(ctx, code, resp)
|
|
}
|
|
|
|
// @Tags 系统管理
|
|
// @Summary 管理员列表
|
|
// @Description 管理员列表
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security ApiKeyAuth
|
|
// @Param param body proto.AdminListReq true "参数列表"
|
|
// @Success 200 {object} proto.AdminListRes "成功"
|
|
// @Failure 400 {string} string "失败"
|
|
// @Security ApiKeyAuth
|
|
// @Router /api/stat/admin/list [POST]
|
|
func AdminList(ctx *gin.Context) {
|
|
param := proto.AdminListReq{}
|
|
if err := ctx.ShouldBind(¶m); err != nil {
|
|
stdLog.ServerJSON(ctx, stdLog.ParamError, err.Error())
|
|
return
|
|
}
|
|
code, data, err := adminSer.AdminList(ctx, param)
|
|
if err != nil {
|
|
stdLog.ServerJSON(ctx, code, err)
|
|
return
|
|
}
|
|
|
|
stdLog.ServerJSON(ctx, code, data)
|
|
}
|
|
|
|
// @Tags 系统管理
|
|
// @Summary 增加管理员
|
|
// @Description 增加管理员
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security ApiKeyAuth
|
|
// @Param param body proto.AddAdminReq true "参数"
|
|
// @Success 200 {string} string "成功"
|
|
// @Success 400 {string} string "失败"
|
|
// @Security ApiKeyAuth
|
|
// @Router /api/stat/admin/add [POST]
|
|
func AddAdmin(ctx *gin.Context) {
|
|
account, err := utils.GetAdmin(ctx)
|
|
if err != nil {
|
|
stdLog.ServerJSON(ctx, stdLog.NoToken, err.Error())
|
|
return
|
|
}
|
|
|
|
param := proto.AddAdminReq{}
|
|
if err := ctx.ShouldBind(¶m); err != nil {
|
|
stdLog.ServerJSON(ctx, stdLog.ParamError, err.Error())
|
|
return
|
|
}
|
|
code, data, err := adminSer.AddAdmin(ctx, param)
|
|
if err != nil {
|
|
stdLog.ServerJSON(ctx, code, err)
|
|
return
|
|
}
|
|
|
|
// 增加操作日志
|
|
opLogSer.AddOpLog(ctx, account, true, param)
|
|
|
|
stdLog.ServerJSON(ctx, code, data)
|
|
}
|
|
|
|
// @Tags 系统管理
|
|
// @Summary 修改管理员
|
|
// @Description 修改管理员
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security ApiKeyAuth
|
|
// @Param param body proto.UpdateAdminReq true "参数列表"
|
|
// @Success 200 {string} string "成功"
|
|
// @Failure 400 {string} string "失败"
|
|
// @Security ApiKeyAuth
|
|
// @Router /api/stat/admin/update [POST]
|
|
func UpdateAdmin(ctx *gin.Context) {
|
|
account, err := utils.GetAdmin(ctx)
|
|
if err != nil {
|
|
stdLog.ServerJSON(ctx, stdLog.NoToken, err.Error())
|
|
return
|
|
}
|
|
|
|
param := proto.UpdateAdminReq{}
|
|
if err := ctx.ShouldBind(¶m); err != nil {
|
|
stdLog.ServerJSON(ctx, stdLog.ParamError, err.Error())
|
|
return
|
|
}
|
|
code, data, err := adminSer.UpdateAdmin(ctx, param)
|
|
if err != nil {
|
|
stdLog.ServerJSON(ctx, code, err)
|
|
return
|
|
}
|
|
|
|
// 增加操作日志
|
|
opLogSer.AddOpLog(ctx, account, true, param)
|
|
|
|
stdLog.ServerJSON(ctx, code, data)
|
|
}
|
|
|
|
// @Tags 系统管理
|
|
// @Summary 删除管理员
|
|
// @Description 删除管理员
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security ApiKeyAuth
|
|
// @Param param body proto.DelAdminReq true "参数列表"
|
|
// @Success 200 {string} string "成功"
|
|
// @Failure 400 {string} string "失败"
|
|
// @Security ApiKeyAuth
|
|
// @Router /api/stat/admin/del [DELETE]
|
|
func DelAdmin(ctx *gin.Context) {
|
|
account, err := utils.GetAdmin(ctx)
|
|
if err != nil {
|
|
stdLog.ServerJSON(ctx, stdLog.NoToken, err.Error())
|
|
return
|
|
}
|
|
|
|
param := proto.DelAdminReq{}
|
|
if err := ctx.ShouldBind(¶m); err != nil {
|
|
stdLog.ServerJSON(ctx, stdLog.ParamError, err.Error())
|
|
return
|
|
}
|
|
code, data, err := adminSer.DelAdmin(ctx, param)
|
|
if err != nil {
|
|
stdLog.ServerJSON(ctx, code, err)
|
|
return
|
|
}
|
|
|
|
// 增加操作日志
|
|
opLogSer.AddOpLog(ctx, account, true, param)
|
|
|
|
stdLog.ServerJSON(ctx, code, data)
|
|
}
|
|
|
|
// @Tags 系统管理
|
|
// @Summary 管理员列表
|
|
// @Description 管理员列表
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security ApiKeyAuth
|
|
// @Param param body proto.AdminListReq true "参数列表"
|
|
// @Success 200 {object} proto.AdminListRes "成功"
|
|
// @Failure 400 {string} string "失败"
|
|
// @Security ApiKeyAuth
|
|
// @Router /api/stat/adminbloc/list [POST]
|
|
func AdminListBloc(ctx *gin.Context) {
|
|
param := proto.AdminListReq{}
|
|
if err := ctx.ShouldBind(¶m); err != nil {
|
|
stdLog.ServerJSON(ctx, stdLog.ParamError, err.Error())
|
|
return
|
|
}
|
|
blocRole := 1
|
|
param.BlocRole = &blocRole
|
|
code, data, err := adminSer.AdminList(ctx, param)
|
|
if err != nil {
|
|
stdLog.ServerJSON(ctx, code, err)
|
|
return
|
|
}
|
|
|
|
stdLog.ServerJSON(ctx, code, data)
|
|
}
|
|
|
|
// @Security ApiKeyAuth
|
|
// @Param param body proto.AddAdminReq true "参数"
|
|
// @Success 200 {string} string "成功"
|
|
// @Success 400 {string} string "失败"
|
|
// @Security ApiKeyAuth
|
|
// @Router /api/stat/adminbloc/add [POST]
|
|
func AddAdminBloc(ctx *gin.Context) {
|
|
account, err := utils.GetAdmin(ctx)
|
|
if err != nil {
|
|
stdLog.ServerJSON(ctx, stdLog.NoToken, err.Error())
|
|
return
|
|
}
|
|
|
|
param := proto.AddAdminReq{}
|
|
if err := ctx.ShouldBind(¶m); err != nil {
|
|
stdLog.ServerJSON(ctx, stdLog.ParamError, err.Error())
|
|
return
|
|
}
|
|
blocRole := 1
|
|
param.BlocRole = &blocRole
|
|
|
|
code, data, err := adminSer.AddAdmin(ctx, param)
|
|
if err != nil {
|
|
stdLog.ServerJSON(ctx, code, err)
|
|
return
|
|
}
|
|
|
|
// 增加操作日志
|
|
opLogSer.AddOpLog(ctx, account, true, param)
|
|
|
|
stdLog.ServerJSON(ctx, code, data)
|
|
}
|
|
|
|
// @Tags 系统管理
|
|
// @Summary 修改管理员
|
|
// @Description 修改管理员
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security ApiKeyAuth
|
|
// @Param param body proto.UpdateAdminReq true "参数列表"
|
|
// @Success 200 {string} string "成功"
|
|
// @Failure 400 {string} string "失败"
|
|
// @Security ApiKeyAuth
|
|
// @Router /api/stat/adminbloc/update [POST]
|
|
func UpdateAdminBloc(ctx *gin.Context) {
|
|
account, err := utils.GetAdmin(ctx)
|
|
if err != nil {
|
|
stdLog.ServerJSON(ctx, stdLog.NoToken, err.Error())
|
|
return
|
|
}
|
|
|
|
param := proto.UpdateAdminReq{}
|
|
if err := ctx.ShouldBind(¶m); err != nil {
|
|
stdLog.ServerJSON(ctx, stdLog.ParamError, err.Error())
|
|
return
|
|
}
|
|
blocRole := 1
|
|
param.BlocRole = &blocRole
|
|
|
|
code, data, err := adminSer.UpdateAdmin(ctx, param)
|
|
if err != nil {
|
|
stdLog.ServerJSON(ctx, code, err)
|
|
return
|
|
}
|
|
|
|
// 增加操作日志
|
|
opLogSer.AddOpLog(ctx, account, true, param)
|
|
|
|
stdLog.ServerJSON(ctx, code, data)
|
|
}
|
|
|
|
// @Tags 系统管理
|
|
// @Summary 删除管理员
|
|
// @Description 删除管理员
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security ApiKeyAuth
|
|
// @Param param body proto.DelAdminReq true "参数列表"
|
|
// @Success 200 {string} string "成功"
|
|
// @Failure 400 {string} string "失败"
|
|
// @Security ApiKeyAuth
|
|
// @Router /api/stat/adminbloc/del [DELETE]
|
|
func DelAdminBloc(ctx *gin.Context) {
|
|
account, err := utils.GetAdmin(ctx)
|
|
if err != nil {
|
|
stdLog.ServerJSON(ctx, stdLog.NoToken, err.Error())
|
|
return
|
|
}
|
|
|
|
param := proto.DelAdminReq{}
|
|
if err := ctx.ShouldBind(¶m); err != nil {
|
|
stdLog.ServerJSON(ctx, stdLog.ParamError, err.Error())
|
|
return
|
|
}
|
|
code, data, err := adminSer.DelAdmin(ctx, param)
|
|
if err != nil {
|
|
stdLog.ServerJSON(ctx, code, err)
|
|
return
|
|
}
|
|
|
|
// 增加操作日志
|
|
opLogSer.AddOpLog(ctx, account, true, param)
|
|
|
|
stdLog.ServerJSON(ctx, code, data)
|
|
}
|