feat:完善入驻信息管理

This commit is contained in:
henry
2021-12-03 11:32:26 +08:00
parent d9cbdd2486
commit 95b12fdb07
9 changed files with 101 additions and 40 deletions

View File

@ -19,7 +19,7 @@ type (
Password string `json:"password" form:"password"` Password string `json:"password" form:"password"`
} }
accountRegisterForm struct { accountRegisterForm struct {
Name string `json:"name" form:"name" binding:"required"` Name string `json:"name" form:"name"`
Mobile string `json:"mobile" form:"mobile" binding:"required"` Mobile string `json:"mobile" form:"mobile" binding:"required"`
Captcha string `json:"captcha" form:"captcha" binding:"required"` Captcha string `json:"captcha" form:"captcha" binding:"required"`
Password string `json:"password" form:"password" binding:"required"` Password string `json:"password" form:"password" binding:"required"`

View File

@ -33,7 +33,7 @@ type (
func (*User) Info(c *gin.Context) { func (*User) Info(c *gin.Context) {
data := user.NewInstance()(api.GetSession()(c).(*session.Enterprise)).Info() data := user.NewInstance()(api.GetSession()(c).(*session.Enterprise)).Info()
api.APISuccess(data) api.APISuccess(data)(c)
} }
func (*User) BindMobile(c *gin.Context) { func (*User) BindMobile(c *gin.Context) {

View File

@ -3,8 +3,10 @@ package account
import ( import (
"SciencesServer/app/common/model" "SciencesServer/app/common/model"
"SciencesServer/app/service" "SciencesServer/app/service"
"SciencesServer/app/session"
"SciencesServer/config" "SciencesServer/config"
"SciencesServer/utils" "SciencesServer/utils"
"fmt"
) )
type Instance struct{} type Instance struct{}
@ -29,17 +31,17 @@ type (
func (c *Instance) Login() InstanceLoginCallback { func (c *Instance) Login() InstanceLoginCallback {
return func(params *InstanceLoginParams) *InstanceLoginReturn { return func(params *InstanceLoginParams) *InstanceLoginReturn {
token := utils.JWTEncrypt(config.SettingInfo.TokenEffectTime, map[string]interface{}{ token := utils.JWTEncrypt(config.SettingInfo.TokenEffectTime, map[string]interface{}{
config.TokenForUID: params.UID, config.TokenForUID: fmt.Sprintf("%d", params.UID),
}) })
session := service.NewSessionEnterprise() _session := session.NewEnterprise()
session.Token = token _session.Token = token
session.UID = params.UID _session.UID = params.UID
session.Name = params.Name _session.Name = params.Name
session.Mobile = params.Mobile _session.Mobile = params.Mobile
session.Identity = params.Identity _session.Identity = params.Identity
session.SelectIdentity = params.SelectIdentity _session.SelectIdentity = params.SelectIdentity
service.Publish(config.EventForRedisHashProduce, config.RedisKeyForAccount, session.UIDToString(), session) service.Publish(config.EventForRedisHashProduce, config.RedisKeyForAccount, _session.UIDToString(), _session)
return &InstanceLoginReturn{Token: token, EffectTime: config.SettingInfo.TokenEffectTime} return &InstanceLoginReturn{Token: token, EffectTime: config.SettingInfo.TokenEffectTime}
} }

View File

@ -45,7 +45,7 @@ var loginHandle = map[LoginMode]func(*LoginParams, string) (*InstanceLoginParams
// loginForSmsCaptcha 短信验证码登陆 // loginForSmsCaptcha 短信验证码登陆
func loginForSmsCaptcha(params *LoginParams, local string) (*InstanceLoginParams, error) { func loginForSmsCaptcha(params *LoginParams, local string) (*InstanceLoginParams, error) {
if !utils.ValidateMobile(params.Captcha.Mobile) { if !utils.ValidateMobile(params.Captcha.Mobile) {
return nil, errors.New("手机号码格式异常") return nil, errors.New("操作错误,手机号码格式异常")
} }
pass, err := handle.NewCaptcha().Validate(&handle.CaptchaSms{ pass, err := handle.NewCaptcha().Validate(&handle.CaptchaSms{
Mobile: params.Captcha.Mobile, Captcha: params.Captcha.Captcha, Mobile: params.Captcha.Mobile, Captcha: params.Captcha.Captcha,
@ -53,14 +53,14 @@ func loginForSmsCaptcha(params *LoginParams, local string) (*InstanceLoginParams
if err != nil { if err != nil {
return nil, err return nil, err
} else if !pass { } else if !pass {
return nil, errors.New("验证码错误或已过期") return nil, errors.New("操作错误,验证码错误或已过期")
} }
var isExist bool var isExist bool
// 查询账号信息 // 查询账号信息
mUserInstance := model3.NewUserInstance() mUserInstance := model3.NewUserInstance()
if isExist, err = model2.FirstField(mUserInstance.UserInstance, []string{"id", "uuid", "name", "mobile", "status"}, if isExist, err = model2.FirstField(mUserInstance.UserInstance, []string{"id", "uuid", "name", "mobile", "status"},
model2.NewWhere("mobile", params.Captcha.Mobile), model2.NewWhere("local", local)); err != nil { model2.NewWhere("mobile", params.Captcha.Mobile)); err != nil {
return nil, err return nil, err
} }
mUserIdentity := model3.NewUserIdentity() mUserIdentity := model3.NewUserIdentity()
@ -72,6 +72,8 @@ func loginForSmsCaptcha(params *LoginParams, local string) (*InstanceLoginParams
} }
goto RETURNS goto RETURNS
} }
mUserInstance.Name = params.Captcha.Mobile
mUserInstance.Mobile = params.Captcha.Mobile
mUserInstance.Password = utils.GetRandomString(12) mUserInstance.Password = utils.GetRandomString(12)
if err = model2.Create(mUserInstance.UserInstance); err != nil { if err = model2.Create(mUserInstance.UserInstance); err != nil {
@ -89,20 +91,20 @@ RETURNS:
// loginForPassword 密码登陆 // loginForPassword 密码登陆
func loginForPassword(params *LoginParams, local string) (*InstanceLoginParams, error) { func loginForPassword(params *LoginParams, local string) (*InstanceLoginParams, error) {
if !utils.ValidateMobile(params.Password.Account) { if !utils.ValidateMobile(params.Password.Account) {
return nil, errors.New("手机号码格式异常") return nil, errors.New("操作错误,手机号码格式异常")
} }
mUserInstance := model3.NewUserInstance() mUserInstance := model3.NewUserInstance()
isExist, err := model2.FirstField(mUserInstance.UserInstance, []string{"id", "uuid", "mobile", "password", "salt", "status"}, isExist, err := model2.FirstField(mUserInstance.UserInstance, []string{"id", "uuid", "name", "mobile", "password", "salt", "status"},
model2.NewWhere("mobile", params.Password.Account), model2.NewWhere("local", local)) model2.NewWhere("mobile", params.Password.Account))
if err != nil { if err != nil {
return nil, err return nil, err
} else if isExist { } else if !isExist {
return nil, errors.New("当前手机号码未注册") return nil, errors.New("操作错误,当前手机号码未注册")
} }
if !mUserInstance.ValidatePassword(params.Password.Password) { if !mUserInstance.ValidatePassword(params.Password.Password) {
return nil, errors.New("账户或密码错误") return nil, errors.New("操作错误,账户或密码错误")
} }
// 最后一次选中的身份信息 // 最后一次选中的身份信息
mUserIdentity := model3.NewUserIdentity() mUserIdentity := model3.NewUserIdentity()
@ -110,7 +112,6 @@ func loginForPassword(params *LoginParams, local string) (*InstanceLoginParams,
if err = mUserIdentity.LastChooseInfo(mUserInstance.UUID); err != nil { if err = mUserIdentity.LastChooseInfo(mUserInstance.UUID); err != nil {
return nil, err return nil, err
} }
return &InstanceLoginParams{ return &InstanceLoginParams{
UID: mUserInstance.UUID, UID: mUserInstance.UUID,
Name: mUserInstance.Name, Mobile: mUserInstance.Mobile, Name: mUserInstance.Name, Mobile: mUserInstance.Mobile,
@ -127,7 +128,7 @@ func (c *Login) Launch(mode LoginMode, params *LoginParams) (*InstanceLoginRetur
_handle, has := loginHandle[mode] _handle, has := loginHandle[mode]
if !has { if !has {
return nil, errors.New("未知的登陆模式") return nil, errors.New("操作错误,未知的登陆模式")
} }
ret, err := _handle(params, c.local) ret, err := _handle(params, c.local)
@ -135,7 +136,7 @@ func (c *Login) Launch(mode LoginMode, params *LoginParams) (*InstanceLoginRetur
return nil, err return nil, err
} }
if ret.Status != model2.AccountStatusForEnable { if ret.Status != model2.AccountStatusForEnable {
return nil, errors.New("该账号已禁止登陆,请联系管理员") return nil, errors.New("操作错误,该账号已禁止登陆,请联系管理员")
} }
return NewInstance()().Login()(ret), err return NewInstance()().Login()(ret), err
} }

View File

@ -41,7 +41,7 @@ func (c *RegisterParams) checkUserExist(mUserInstance *model2.UserInstance, loca
// Launch 发起注册 // Launch 发起注册
func (c *Register) Launch(params *RegisterParams) (*InstanceLoginReturn, error) { func (c *Register) Launch(params *RegisterParams) (*InstanceLoginReturn, error) {
// 验证密码 // 验证密码
if params.checkPassword() { if !params.checkPassword() {
return nil, errors.New("两次密码不一致") return nil, errors.New("两次密码不一致")
} }
// 验证验证码 // 验证验证码
@ -60,9 +60,13 @@ func (c *Register) Launch(params *RegisterParams) (*InstanceLoginReturn, error)
} else if !pass { } else if !pass {
return nil, errors.New("当前手机号码已注册") return nil, errors.New("当前手机号码已注册")
} }
if params.Name == "" {
params.Name = params.Mobile
}
mUserInstance.Source = model2.UserInstanceSourceForLocal mUserInstance.Source = model2.UserInstanceSourceForLocal
mUserInstance.Password = utils.GetRandomString(12) mUserInstance.Password = utils.GetRandomString(12)
mUserInstance.Mobile = params.Mobile mUserInstance.Mobile = params.Mobile
mUserInstance.Name = params.Name
mUserInstance.Password = params.Password mUserInstance.Password = params.Password
if err = orm.GetDB().Transaction(func(tx *gorm.DB) error { if err = orm.GetDB().Transaction(func(tx *gorm.DB) error {

View File

@ -21,7 +21,7 @@ func (this *Enterprise) SetToken(token string) {
} }
func (this *Enterprise) GetToken() string { func (this *Enterprise) GetToken() string {
return this.GetToken() return this.Token
} }
func (this *Enterprise) UIDToString() string { func (this *Enterprise) UIDToString() string {

View File

@ -51,7 +51,13 @@ func (this *Serve) Run() {
}) })
}) })
cache.Init() cache.Init()
orm.Init() //orm.Init()
// TODO待优化完善
orm.NewInstance(
orm.WithDebug(config.SettingInfo.Engine.Debug),
orm.WithDBMode(config.SettingInfo.Engine.DBMode),
//orm.WithMysqlUser(config.SettingInfo.Engine.Mysql.User),
).Init()
task.Init() task.Init()
common.Init() common.Init()
cron.Init() cron.Init()

View File

@ -127,7 +127,11 @@ func registerEnterpriseAPI(app *gin.Engine) {
apiPrefix := "/enterprise" apiPrefix := "/enterprise"
g := app.Group(apiPrefix) g := app.Group(apiPrefix)
//g.Use(NeedLogin(session.NewManage(), AddSkipperURL([]string{}...))) g.Use(NeedLogin(session.NewEnterprise(), AddSkipperURL([]string{
apiPrefix + "/v1/account/login",
apiPrefix + "/v1/account/register",
apiPrefix + "/v1/account/authorize",
}...)))
v1 := g.Group("/v1") v1 := g.Group("/v1")
@ -148,6 +152,13 @@ func registerEnterpriseAPI(app *gin.Engine) {
accountV1.POST("/authorize", _api.Authorize) accountV1.POST("/authorize", _api.Authorize)
accountV1.POST("/logout", _api.Logout) accountV1.POST("/logout", _api.Logout)
} }
// User 用户管理
userV1 := v1.Group("/user")
{
_api := new(api2.User)
userV1.GET("/info", _api.Info)
userV1.GET("/patent", _api.Patent)
}
// Sys 配置管理 // Sys 配置管理
sysV1 := v1.Group("/sys") sysV1 := v1.Group("/sys")
{ {

View File

@ -22,20 +22,39 @@ var (
} }
) )
func mysql() logic.IEngine { type Instance struct {
return &logic.Mysql{ debug bool
User: config.SettingInfo.Engine.Mysql.User, Password: config.SettingInfo.Engine.Mysql.Password, dbMode string
Host: config.SettingInfo.Engine.Mysql.Host, Port: config.SettingInfo.Engine.Mysql.Port, *logic.Mysql
DBName: config.SettingInfo.Engine.Mysql.DBName, Parameters: config.SettingInfo.Engine.Mysql.Parameters, *logic.Sqlite
}
type Option func(instance *Instance)
func WithDebug(debug bool) Option {
return func(instance *Instance) {
instance.debug = debug
} }
} }
func sqlite() logic.IEngine { func WithDBMode(dbMode string) Option {
return &logic.Sqlite{Path: config.SettingInfo.Engine.Sqlite.Path, Name: config.SettingInfo.Engine.Sqlite.Name} return func(instance *Instance) {
instance.dbMode = dbMode
}
} }
func Init() { func WithMysqlUser(user string) Option {
handle, has := engines[config.SettingInfo.Engine.DBMode] return func(instance *Instance) {
instance.Mysql.User = user
}
}
func WithMysql() {
}
func (this *Instance) Init() {
handle, has := engines[this.dbMode]
if !has { if !has {
panic(fmt.Sprintf("Unknown Engine Mode%d", config.SettingInfo.Engine.DBMode)) panic(fmt.Sprintf("Unknown Engine Mode%d", config.SettingInfo.Engine.DBMode))
@ -47,7 +66,7 @@ func Init() {
SingularTable: !config.SettingInfo.Engine.Complex, SingularTable: !config.SettingInfo.Engine.Complex,
}, },
} }
if config.SettingInfo.Engine.Debug { if this.debug {
option.Logger = logger.New( option.Logger = logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), log.New(os.Stdout, "\r\n", log.LstdFlags),
logger.Config{ logger.Config{
@ -71,9 +90,27 @@ func Init() {
orm = db orm = db
} }
func GetDB() *gorm.DB { func NewInstance(option ...Option) *Instance {
if _, err := orm.DB(); err != nil { instance := new(Instance)
Init()
for _, v := range option {
v(instance)
} }
return instance
}
func mysql() logic.IEngine {
return &logic.Mysql{
User: config.SettingInfo.Engine.Mysql.User, Password: config.SettingInfo.Engine.Mysql.Password,
Host: config.SettingInfo.Engine.Mysql.Host, Port: config.SettingInfo.Engine.Mysql.Port,
DBName: config.SettingInfo.Engine.Mysql.DBName, Parameters: config.SettingInfo.Engine.Mysql.Parameters,
}
}
func sqlite() logic.IEngine {
return &logic.Sqlite{Path: config.SettingInfo.Engine.Sqlite.Path, Name: config.SettingInfo.Engine.Sqlite.Name}
}
func GetDB() *gorm.DB {
return orm return orm
} }