diff --git a/app/api/enterprise/api/account.go b/app/api/enterprise/api/account.go index 18b7105..3f66e56 100644 --- a/app/api/enterprise/api/account.go +++ b/app/api/enterprise/api/account.go @@ -19,7 +19,7 @@ type ( Password string `json:"password" form:"password"` } accountRegisterForm struct { - Name string `json:"name" form:"name" binding:"required"` + Name string `json:"name" form:"name"` Mobile string `json:"mobile" form:"mobile" binding:"required"` Captcha string `json:"captcha" form:"captcha" binding:"required"` Password string `json:"password" form:"password" binding:"required"` diff --git a/app/api/enterprise/api/user.go b/app/api/enterprise/api/user.go index 26b5680..328d48d 100644 --- a/app/api/enterprise/api/user.go +++ b/app/api/enterprise/api/user.go @@ -33,7 +33,7 @@ type ( func (*User) Info(c *gin.Context) { data := user.NewInstance()(api.GetSession()(c).(*session.Enterprise)).Info() - api.APISuccess(data) + api.APISuccess(data)(c) } func (*User) BindMobile(c *gin.Context) { diff --git a/app/api/enterprise/controller/account/instance.go b/app/api/enterprise/controller/account/instance.go index 3b60add..27d88cf 100644 --- a/app/api/enterprise/controller/account/instance.go +++ b/app/api/enterprise/controller/account/instance.go @@ -3,8 +3,10 @@ package account import ( "SciencesServer/app/common/model" "SciencesServer/app/service" + "SciencesServer/app/session" "SciencesServer/config" "SciencesServer/utils" + "fmt" ) type Instance struct{} @@ -29,17 +31,17 @@ type ( func (c *Instance) Login() InstanceLoginCallback { return func(params *InstanceLoginParams) *InstanceLoginReturn { token := utils.JWTEncrypt(config.SettingInfo.TokenEffectTime, map[string]interface{}{ - config.TokenForUID: params.UID, + config.TokenForUID: fmt.Sprintf("%d", params.UID), }) - session := service.NewSessionEnterprise() - session.Token = token - session.UID = params.UID - session.Name = params.Name - session.Mobile = params.Mobile - session.Identity = params.Identity - session.SelectIdentity = params.SelectIdentity + _session := session.NewEnterprise() + _session.Token = token + _session.UID = params.UID + _session.Name = params.Name + _session.Mobile = params.Mobile + _session.Identity = params.Identity + _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} } diff --git a/app/api/enterprise/controller/account/login.go b/app/api/enterprise/controller/account/login.go index 7385660..789f441 100644 --- a/app/api/enterprise/controller/account/login.go +++ b/app/api/enterprise/controller/account/login.go @@ -45,7 +45,7 @@ var loginHandle = map[LoginMode]func(*LoginParams, string) (*InstanceLoginParams // loginForSmsCaptcha 短信验证码登陆 func loginForSmsCaptcha(params *LoginParams, local string) (*InstanceLoginParams, error) { if !utils.ValidateMobile(params.Captcha.Mobile) { - return nil, errors.New("手机号码格式异常") + return nil, errors.New("操作错误,手机号码格式异常") } pass, err := handle.NewCaptcha().Validate(&handle.CaptchaSms{ Mobile: params.Captcha.Mobile, Captcha: params.Captcha.Captcha, @@ -53,14 +53,14 @@ func loginForSmsCaptcha(params *LoginParams, local string) (*InstanceLoginParams if err != nil { return nil, err } else if !pass { - return nil, errors.New("验证码错误或已过期") + return nil, errors.New("操作错误,验证码错误或已过期") } var isExist bool // 查询账号信息 mUserInstance := model3.NewUserInstance() 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 } mUserIdentity := model3.NewUserIdentity() @@ -72,6 +72,8 @@ func loginForSmsCaptcha(params *LoginParams, local string) (*InstanceLoginParams } goto RETURNS } + mUserInstance.Name = params.Captcha.Mobile + mUserInstance.Mobile = params.Captcha.Mobile mUserInstance.Password = utils.GetRandomString(12) if err = model2.Create(mUserInstance.UserInstance); err != nil { @@ -89,20 +91,20 @@ RETURNS: // loginForPassword 密码登陆 func loginForPassword(params *LoginParams, local string) (*InstanceLoginParams, error) { if !utils.ValidateMobile(params.Password.Account) { - return nil, errors.New("手机号码格式异常") + return nil, errors.New("操作错误,手机号码格式异常") } mUserInstance := model3.NewUserInstance() - isExist, err := model2.FirstField(mUserInstance.UserInstance, []string{"id", "uuid", "mobile", "password", "salt", "status"}, - model2.NewWhere("mobile", params.Password.Account), model2.NewWhere("local", local)) + isExist, err := model2.FirstField(mUserInstance.UserInstance, []string{"id", "uuid", "name", "mobile", "password", "salt", "status"}, + model2.NewWhere("mobile", params.Password.Account)) if err != nil { return nil, err - } else if isExist { - return nil, errors.New("当前手机号码未注册") + } else if !isExist { + return nil, errors.New("操作错误,当前手机号码未注册") } if !mUserInstance.ValidatePassword(params.Password.Password) { - return nil, errors.New("账户或密码错误") + return nil, errors.New("操作错误,账户或密码错误") } // 最后一次选中的身份信息 mUserIdentity := model3.NewUserIdentity() @@ -110,7 +112,6 @@ func loginForPassword(params *LoginParams, local string) (*InstanceLoginParams, if err = mUserIdentity.LastChooseInfo(mUserInstance.UUID); err != nil { return nil, err } - return &InstanceLoginParams{ UID: mUserInstance.UUID, Name: mUserInstance.Name, Mobile: mUserInstance.Mobile, @@ -127,7 +128,7 @@ func (c *Login) Launch(mode LoginMode, params *LoginParams) (*InstanceLoginRetur _handle, has := loginHandle[mode] if !has { - return nil, errors.New("未知的登陆模式") + return nil, errors.New("操作错误,未知的登陆模式") } ret, err := _handle(params, c.local) @@ -135,7 +136,7 @@ func (c *Login) Launch(mode LoginMode, params *LoginParams) (*InstanceLoginRetur return nil, err } if ret.Status != model2.AccountStatusForEnable { - return nil, errors.New("该账号已禁止登陆,请联系管理员") + return nil, errors.New("操作错误,该账号已禁止登陆,请联系管理员") } return NewInstance()().Login()(ret), err } diff --git a/app/api/enterprise/controller/account/register.go b/app/api/enterprise/controller/account/register.go index 2a87d62..1c6fe5f 100644 --- a/app/api/enterprise/controller/account/register.go +++ b/app/api/enterprise/controller/account/register.go @@ -41,7 +41,7 @@ func (c *RegisterParams) checkUserExist(mUserInstance *model2.UserInstance, loca // Launch 发起注册 func (c *Register) Launch(params *RegisterParams) (*InstanceLoginReturn, error) { // 验证密码 - if params.checkPassword() { + if !params.checkPassword() { return nil, errors.New("两次密码不一致") } // 验证验证码 @@ -60,9 +60,13 @@ func (c *Register) Launch(params *RegisterParams) (*InstanceLoginReturn, error) } else if !pass { return nil, errors.New("当前手机号码已注册") } + if params.Name == "" { + params.Name = params.Mobile + } mUserInstance.Source = model2.UserInstanceSourceForLocal mUserInstance.Password = utils.GetRandomString(12) mUserInstance.Mobile = params.Mobile + mUserInstance.Name = params.Name mUserInstance.Password = params.Password if err = orm.GetDB().Transaction(func(tx *gorm.DB) error { diff --git a/app/session/enterprise.go b/app/session/enterprise.go index 71c0fea..e569693 100644 --- a/app/session/enterprise.go +++ b/app/session/enterprise.go @@ -21,7 +21,7 @@ func (this *Enterprise) SetToken(token string) { } func (this *Enterprise) GetToken() string { - return this.GetToken() + return this.Token } func (this *Enterprise) UIDToString() string { diff --git a/cmd/serve/serve.go b/cmd/serve/serve.go index cc79005..f504f61 100644 --- a/cmd/serve/serve.go +++ b/cmd/serve/serve.go @@ -51,7 +51,13 @@ func (this *Serve) Run() { }) }) 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() common.Init() cron.Init() diff --git a/router/address.go b/router/address.go index a635184..9f70855 100644 --- a/router/address.go +++ b/router/address.go @@ -127,7 +127,11 @@ func registerEnterpriseAPI(app *gin.Engine) { apiPrefix := "/enterprise" 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") @@ -148,6 +152,13 @@ func registerEnterpriseAPI(app *gin.Engine) { accountV1.POST("/authorize", _api.Authorize) 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 配置管理 sysV1 := v1.Group("/sys") { diff --git a/serve/orm/orm.go b/serve/orm/orm.go index 7f241d2..d09c964 100644 --- a/serve/orm/orm.go +++ b/serve/orm/orm.go @@ -22,20 +22,39 @@ var ( } ) -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, +type Instance struct { + debug bool + dbMode string + *logic.Mysql + *logic.Sqlite +} + +type Option func(instance *Instance) + +func WithDebug(debug bool) Option { + return func(instance *Instance) { + instance.debug = debug } } -func sqlite() logic.IEngine { - return &logic.Sqlite{Path: config.SettingInfo.Engine.Sqlite.Path, Name: config.SettingInfo.Engine.Sqlite.Name} +func WithDBMode(dbMode string) Option { + return func(instance *Instance) { + instance.dbMode = dbMode + } } -func Init() { - handle, has := engines[config.SettingInfo.Engine.DBMode] +func WithMysqlUser(user string) Option { + return func(instance *Instance) { + instance.Mysql.User = user + } +} + +func WithMysql() { + +} + +func (this *Instance) Init() { + handle, has := engines[this.dbMode] if !has { panic(fmt.Sprintf("Unknown Engine Mode:%d", config.SettingInfo.Engine.DBMode)) @@ -47,7 +66,7 @@ func Init() { SingularTable: !config.SettingInfo.Engine.Complex, }, } - if config.SettingInfo.Engine.Debug { + if this.debug { option.Logger = logger.New( log.New(os.Stdout, "\r\n", log.LstdFlags), logger.Config{ @@ -71,9 +90,27 @@ func Init() { orm = db } -func GetDB() *gorm.DB { - if _, err := orm.DB(); err != nil { - Init() +func NewInstance(option ...Option) *Instance { + instance := new(Instance) + + 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 }