github.com/SupenBysz/gf-admin-community@v0.7.4/utility/sys_rules/check_rule.go (about)

     1  package sys_rules
     2  
     3  import (
     4  	"context"
     5  	"github.com/SupenBysz/gf-admin-community/sys_consts"
     6  	"github.com/SupenBysz/gf-admin-community/sys_model"
     7  	"github.com/SupenBysz/gf-admin-community/sys_service"
     8  	"github.com/SupenBysz/gf-admin-community/utility/invite_id"
     9  	"github.com/gogf/gf/v2/errors/gcode"
    10  	"github.com/gogf/gf/v2/errors/gerror"
    11  	"github.com/gogf/gf/v2/frame/g"
    12  	"github.com/gogf/gf/v2/util/gconv"
    13  	"github.com/kysion/base-library/utility/base_verify"
    14  )
    15  
    16  // CheckLoginRule 校验用户是否可以通过此方式登陆
    17  func CheckLoginRule(ctx context.Context, loginIdentifier string) bool {
    18  	arr := g.Cfg().MustGet(ctx, "service.loginRule").Array()
    19  
    20  	if base_verify.IsPhone(loginIdentifier) {
    21  		for _, v := range arr {
    22  			if gconv.Int(v) == 2 {
    23  				return true
    24  			}
    25  		}
    26  
    27  	} else if base_verify.IsEmail(loginIdentifier) {
    28  		for _, v := range arr {
    29  			if gconv.Int(v) == 4 {
    30  				return true
    31  			}
    32  		}
    33  	} else {
    34  		for _, v := range arr {
    35  			if gconv.Int(v) == 1 {
    36  				return true
    37  			}
    38  		}
    39  	}
    40  
    41  	return false
    42  }
    43  
    44  // CheckRegisterRule 校验用户是否可以通过此方式注册
    45  func CheckRegisterRule(ctx context.Context, registerIdentifier string) bool {
    46  	arr := g.Cfg().MustGet(ctx, "service.registerRule").Array()
    47  
    48  	if base_verify.IsPhone(registerIdentifier) {
    49  		for _, v := range arr {
    50  			if gconv.Int(v) == 2 {
    51  				return true
    52  			}
    53  		}
    54  
    55  	} else if base_verify.IsEmail(registerIdentifier) {
    56  		for _, v := range arr {
    57  			if gconv.Int(v) == 4 {
    58  				return true
    59  			}
    60  		}
    61  	} else {
    62  		for _, v := range arr {
    63  			if gconv.Int(v) == 1 {
    64  				return true
    65  			}
    66  		}
    67  	}
    68  
    69  	return false
    70  }
    71  
    72  func CheckInviteCode(ctx context.Context, code string) (res *sys_model.InviteRes, err error) {
    73  	// 判断是否填写邀约码
    74  	if sys_consts.Global.RegisterIsNeedInviteCode && code == "" {
    75  		return nil, gerror.NewCode(gcode.CodeBusinessValidationFailed, "系统要求注册必需填写邀约码!")
    76  	}
    77  	// 只要填写了必需进行校验
    78  	if code != "" {
    79  		id := invite_id.CodeToInviteId(code)
    80  		res, err = sys_service.SysInvite().GetInviteById(ctx, id)
    81  		if err != nil {
    82  			return nil, gerror.NewCode(gcode.CodeBusinessValidationFailed, "填写的邀约码错误,请检查!")
    83  		}
    84  	}
    85  
    86  	return res, err
    87  }