yxh 4 жил өмнө
parent
commit
0e6e9fab97

+ 17 - 11
internal/app/common/service/captcha.go

@@ -16,17 +16,23 @@ type captchaImpl struct {
 	store  base64Captcha.Store
 }
 
-var Captcha ICaptcha = &captchaImpl{
-	driver: &base64Captcha.DriverString{
-		Height:          80,
-		Width:           240,
-		NoiseCount:      50,
-		ShowLineOptions: 20,
-		Length:          4,
-		Source:          "abcdefghjkmnpqrstuvwxyz23456789",
-		Fonts:           []string{"chromohv.ttf"},
-	},
-	store: base64Captcha.DefaultMemStore,
+var (
+	captcha = captchaImpl{
+		driver: &base64Captcha.DriverString{
+			Height:          80,
+			Width:           240,
+			NoiseCount:      50,
+			ShowLineOptions: 20,
+			Length:          4,
+			Source:          "abcdefghjkmnpqrstuvwxyz23456789",
+			Fonts:           []string{"chromohv.ttf"},
+		},
+		store: base64Captcha.DefaultMemStore,
+	}
+)
+
+func Captcha() ICaptcha {
+	return ICaptcha(&captcha)
 }
 
 // GetVerifyImgString 获取字母数字混合验证码

+ 3 - 2
internal/app/system/controller/user.go

@@ -6,6 +6,7 @@ import (
 	"github.com/gogf/gf/v2/os/genv"
 	"github.com/tiger1103/gfast/v3/apiv1/system"
 	commonService "github.com/tiger1103/gfast/v3/internal/app/common/service"
+	"github.com/tiger1103/gfast/v3/internal/app/system/service"
 )
 
 var (
@@ -20,13 +21,13 @@ func (c *UserController) Login(ctx context.Context, req *system.UserLoginReq) (r
 	//判断验证码是否正确
 	debug := genv.GetWithCmd("gf.debug")
 	if debug.Int() != 1 {
-		if !commonService.Captcha.VerifyString(req.VerifyKey, req.VerifyCode) {
+		if !commonService.Captcha().VerifyString(req.VerifyKey, req.VerifyCode) {
 			err = gerror.New("验证码输入错误")
 			return
 		}
 	}
 	//ip := libUtils.GetClientIp(ctx)
 	//userAgent := libUtils.GetUserAgent(ctx)
-
+	service.User().GetAdminUserByUsernamePassword(ctx, req)
 	return
 }

+ 21 - 0
internal/app/system/model/sys_user.go

@@ -0,0 +1,21 @@
+/*
+* @desc:用户模型对象
+* @company:云南奇讯科技有限公司
+* @Author: yixiaohu
+* @Date:   2022/3/7 11:47
+ */
+
+package model
+
+// LoginUserRes 登录返回
+type LoginUserRes struct {
+	Id           uint64 `orm:"id,primary"       json:"id"`           //
+	UserName     string `orm:"user_name,unique" json:"userName"`     // 用户名
+	UserNickname string `orm:"user_nickname"    json:"userNickname"` // 用户昵称
+	UserPassword string `orm:"user_password"    json:"userPassword"` // 登录密码;cmf_password加密
+	UserSalt     string `orm:"user_salt"        json:"userSalt"`     // 加密盐
+	UserStatus   uint   `orm:"user_status"      json:"userStatus"`   // 用户状态;0:禁用,1:正常,2:未验证
+	IsAdmin      int    `orm:"is_admin"         json:"isAdmin"`      // 是否后台管理员 1 是  0   否
+	Avatar       string `orm:"avatar" json:"avatar"`                 //头像
+	DeptId       uint64 `orm:"dept_id"       json:"deptId"`          //部门id
+}

+ 61 - 0
internal/app/system/service/sys_user.go

@@ -0,0 +1,61 @@
+/*
+* @desc:用户处理
+* @company:云南奇讯科技有限公司
+* @Author: yixiaohu
+* @Date:   2022/3/7 9:50
+ */
+
+package service
+
+import (
+	"context"
+	"github.com/gogf/gf/v2/errors/gerror"
+	"github.com/gogf/gf/v2/frame/g"
+	"github.com/tiger1103/gfast/v3/apiv1/system"
+	"github.com/tiger1103/gfast/v3/internal/app/system/model"
+	"github.com/tiger1103/gfast/v3/internal/app/system/service/internal/dao"
+	"github.com/tiger1103/gfast/v3/library/libUtils"
+	"github.com/tiger1103/gfast/v3/library/liberr"
+)
+
+var (
+	user = userImpl{}
+)
+
+func User() IUser {
+	return IUser(&user)
+}
+
+type IUser interface {
+	GetAdminUserByUsernamePassword(ctx context.Context, req *system.UserLoginReq) (user *model.LoginUserRes, err error)
+}
+
+type userImpl struct {
+}
+
+func (s *userImpl) GetAdminUserByUsernamePassword(ctx context.Context, req *system.UserLoginReq) (user *model.LoginUserRes, err error) {
+	err = g.Try(func() {
+		user, err = s.GetUserByUsername(ctx, req.Username)
+		liberr.ErrIsNil(ctx, err)
+		liberr.ValueIsNil(user, "账号密码错误")
+		//验证密码
+		if libUtils.EncryptPassword(req.Password, user.UserSalt) != user.UserPassword {
+			liberr.ErrIsNil(ctx, gerror.New("账号密码错误"))
+		}
+		//账号状态
+		if user.UserStatus == 0 {
+			liberr.ErrIsNil(ctx, gerror.New("账号已被冻结"))
+		}
+	})
+	return
+}
+
+// GetUserByUsername 通过用户名获取用户信息
+func (s *userImpl) GetUserByUsername(ctx context.Context, userName string) (user *model.LoginUserRes, err error) {
+	err = g.Try(func() {
+		user = &model.LoginUserRes{}
+		err = dao.SysUser.Ctx(ctx).Fields(user).Where(dao.SysUser.Columns().UserName, userName).Scan(user)
+		liberr.ErrIsNil(ctx, err, "获取用户信息失败")
+	})
+	return
+}

+ 7 - 1
library/liberr/err.go

@@ -13,7 +13,7 @@ import (
 )
 
 func ErrIsNil(ctx context.Context, err error, msg ...string) {
-	if err != nil {
+	if !g.IsNil(err) {
 		g.Log().Error(ctx, err.Error())
 		if len(msg) > 0 {
 			panic(msg[0])
@@ -22,3 +22,9 @@ func ErrIsNil(ctx context.Context, err error, msg ...string) {
 		}
 	}
 }
+
+func ValueIsNil(value interface{}, msg string) {
+	if g.IsNil(value) {
+		panic(msg)
+	}
+}