feat:完善信息

This commit is contained in:
henry
2021-10-13 11:23:55 +08:00
parent 17fb77e84a
commit 21dc62ba8e
11 changed files with 129 additions and 102 deletions

View File

@ -33,7 +33,7 @@ func (a *Account) Login(c *gin.Context) {
api.APIFailure(err.(error))(c)
return
}
data, err := account.NewLogin()().Launch(account.LoginMode(form.Mode), &account.LoginParams{
data, err := account.NewLogin()(api.GetLocal()(c).(uint64)).Launch(account.LoginMode(form.Mode), &account.LoginParams{
Captcha: struct {
Mobile string
Captcha string
@ -53,7 +53,7 @@ func (a *Account) Register(c *gin.Context) {
api.APIFailure(err.(error))(c)
return
}
data, err := account.NewRegister()().Launch(&account.RegisterParams{
data, err := account.NewRegister()(api.GetLocal()(c).(uint64)).Launch(&account.RegisterParams{
Name: form.Name, Mobile: form.Mobile, Captcha: form.Captcha,
Password: form.Password, RepeatPass: form.RepeatPass, Identity: form.Identity,
})
@ -72,6 +72,6 @@ func (a *Account) Logout(c *gin.Context) {
if handle != nil {
session = handle.(*service.SessionEnterprise)
}
err := account.NewLogout()(session).Launch()
err := account.NewLogout()(session, api.GetLocal()(c).(uint64)).Launch()
api.APIResponse(err)(c)
}

View File

@ -31,7 +31,8 @@ func (a *Technology) Paper(c *gin.Context) {
api.APIFailure(err.(error))(c)
return
}
data, err := technology.NewPaper()(api.GetSession()(c).(*service.SessionEnterprise)).List(form.Title, form.Page, form.PageSize)
data, err := technology.NewPaper()(api.GetSession()(c).(*service.SessionEnterprise), api.GetLocal()(c).(uint64)).
List(form.Title, form.Page, form.PageSize)
api.APIResponse(err, data)
}
@ -42,10 +43,11 @@ func (a *Technology) PaperAdd(c *gin.Context) {
api.APIFailure(err.(error))(c)
return
}
err := technology.NewPaper()(api.GetSession()(c).(*service.SessionEnterprise)).Form(&technology.PaperParams{
Title: form.Title, Ext: form.Ext, Author: form.Author, PublishAt: form.PublishAt,
Keyword: form.Keyword, Tags: form.Tags, Remark: form.Remark,
})
err := technology.NewPaper()(api.GetSession()(c).(*service.SessionEnterprise), api.GetLocal()(c).(uint64)).
Form(&technology.PaperParams{
Title: form.Title, Ext: form.Ext, Author: form.Author, PublishAt: form.PublishAt,
Keyword: form.Keyword, Tags: form.Tags, Remark: form.Remark,
})
api.APIResponse(err)
}
@ -58,10 +60,11 @@ func (a *Technology) PaperEdit(c *gin.Context) {
api.APIFailure(err.(error))(c)
return
}
err := technology.NewPaper()(api.GetSession()(c).(*service.SessionEnterprise)).Form(&technology.PaperParams{
ID: form.Convert(), Title: form.Title, Ext: form.Ext, Author: form.Author, PublishAt: form.PublishAt,
Keyword: form.Keyword, Tags: form.Tags, Remark: form.Remark,
})
err := technology.NewPaper()(api.GetSession()(c).(*service.SessionEnterprise), api.GetLocal()(c).(uint64)).
Form(&technology.PaperParams{
ID: form.Convert(), Title: form.Title, Ext: form.Ext, Author: form.Author, PublishAt: form.PublishAt,
Keyword: form.Keyword, Tags: form.Tags, Remark: form.Remark,
})
api.APIResponse(err)
}
@ -72,6 +75,7 @@ func (a *Technology) PaperDelete(c *gin.Context) {
api.APIFailure(err.(error))(c)
return
}
err := technology.NewPaper()(api.GetSession()(c).(*service.SessionEnterprise)).Delete(form.Convert())
err := technology.NewPaper()(api.GetSession()(c).(*service.SessionEnterprise), api.GetLocal()(c).(uint64)).
Delete(form.Convert())
api.APIResponse(err)
}

View File

@ -8,10 +8,10 @@ import (
"errors"
)
type Login struct{}
type Login struct{ local uint64 }
type (
LoginHandle func() *Login
LoginHandle func(local uint64) *Login
)
type (
@ -38,12 +38,12 @@ const (
LoginModeForQQ // QQ登陆
)
var loginHandle = map[LoginMode]func(*LoginParams) (*InstanceLoginParams, error){
var loginHandle = map[LoginMode]func(*LoginParams, uint64) (*InstanceLoginParams, error){
LoginModeForSmsCaptcha: loginForSmsCaptcha, LoginModeForPassword: loginForPassword,
}
// loginForSmsCaptcha 短信验证码登陆
func loginForSmsCaptcha(params *LoginParams) (*InstanceLoginParams, error) {
func loginForSmsCaptcha(params *LoginParams, local uint64) (*InstanceLoginParams, error) {
if !utils.ValidateMobile(params.Captcha.Mobile) {
return nil, errors.New("手机号码格式异常")
}
@ -56,18 +56,18 @@ func loginForSmsCaptcha(params *LoginParams) (*InstanceLoginParams, error) {
return nil, errors.New("验证码错误或已过期")
}
var isExist bool
// 查询账号信息
mUserInstance := model.NewUserInstance()
if isExist, err = model2.FirstField(mUserInstance.UserInstance, []string{"id", "uuid", "name", "mobile", "status"},
model2.NewWhere("mobile", params.Captcha.Mobile)); err != nil {
model2.NewWhere("mobile", params.Captcha.Mobile), model2.NewWhere("local", local)); err != nil {
return nil, err
}
mUserTenant := model.NewUserTenant()
mUserManage := model.NewUserManage()
if isExist {
if err = mUserTenant.LastChooseInfo(mUserInstance.UUID); err != nil {
// 查询该区域下最后一次选中的信息
if err = mUserManage.LastChooseInfo(mUserInstance.UUID); err != nil {
return nil, err
}
goto RETURNS
@ -79,22 +79,22 @@ func loginForSmsCaptcha(params *LoginParams) (*InstanceLoginParams, error) {
}
RETURNS:
return &InstanceLoginParams{
UID: mUserInstance.UUID, TenantID: mUserTenant.TenantID, TenantUID: mUserTenant.UUID,
UID: mUserInstance.UUID, TenantID: mUserManage.TenantID, TenantUID: mUserManage.UUID,
Name: mUserInstance.Name, Mobile: mUserInstance.Mobile,
Identity: mUserInstance.Identity, SelectIdentity: mUserTenant.Identity,
Identity: mUserInstance.Identity, SelectIdentity: mUserManage.Identity,
Status: mUserInstance.Status,
}, nil
}
// loginForPassword 密码登陆
func loginForPassword(params *LoginParams) (*InstanceLoginParams, error) {
func loginForPassword(params *LoginParams, local uint64) (*InstanceLoginParams, error) {
if !utils.ValidateMobile(params.Password.Mobile) {
return nil, errors.New("手机号码格式异常")
}
mUserInstance := model.NewUserInstance()
isExist, err := model2.FirstField(mUserInstance.UserInstance, []string{"id", "uuid", "mobile", "password", "salt", "status"},
model2.NewWhere("mobile", params.Password.Mobile))
model2.NewWhere("mobile", params.Password.Mobile), model2.NewWhere("local", local))
if err != nil {
return nil, err
@ -105,15 +105,15 @@ func loginForPassword(params *LoginParams) (*InstanceLoginParams, error) {
return nil, errors.New("账户或密码错误")
}
// 最后一次选中的身份信息
mUserTenant := model.NewUserTenant()
mUserManage := model.NewUserManage()
if err = mUserTenant.LastChooseInfo(mUserInstance.UUID); err != nil {
if err = mUserManage.LastChooseInfo(mUserInstance.UUID); err != nil {
return nil, err
}
return &InstanceLoginParams{
UID: mUserInstance.UUID, TenantID: mUserTenant.TenantID, TenantUID: mUserTenant.UUID,
UID: mUserInstance.UUID, TenantID: mUserManage.TenantID, TenantUID: mUserManage.UUID,
Name: mUserInstance.Name, Mobile: mUserInstance.Mobile,
Identity: mUserInstance.Identity, SelectIdentity: mUserTenant.Identity,
Identity: mUserInstance.Identity, SelectIdentity: mUserManage.Identity,
Status: mUserInstance.Status,
}, nil
}
@ -128,7 +128,7 @@ func (c *Login) Launch(mode LoginMode, params *LoginParams) (*InstanceLoginRetur
if !has {
return nil, errors.New("未知的登陆模式")
}
ret, err := _handle(params)
ret, err := _handle(params, c.local)
if err != nil {
return nil, err
@ -140,7 +140,7 @@ func (c *Login) Launch(mode LoginMode, params *LoginParams) (*InstanceLoginRetur
}
func NewLogin() LoginHandle {
return func() *Login {
return &Login{}
return func(local uint64) *Login {
return &Login{local: local}
}
}

View File

@ -6,9 +6,12 @@ import (
"SciencesServer/utils"
)
type Logout struct{ *service.SessionEnterprise }
type Logout struct {
*service.SessionEnterprise
local uint64
}
type LogoutHandle func(*service.SessionEnterprise) *Logout
type LogoutHandle func(enterprise *service.SessionEnterprise, local uint64) *Logout
func (c *Logout) Launch() error {
service.Publish(config.EventForRedisHashDestroy, config.RedisKeyForAccount, utils.UintToString(c.UID))
@ -16,7 +19,7 @@ func (c *Logout) Launch() error {
}
func NewLogout() LogoutHandle {
return func(enterprise *service.SessionEnterprise) *Logout {
return &Logout{enterprise}
return func(enterprise *service.SessionEnterprise, local uint64) *Logout {
return &Logout{enterprise, local}
}
}

View File

@ -10,9 +10,9 @@ import (
"gorm.io/gorm"
)
type Register struct{}
type Register struct{ local uint64 }
type RegisterHandle func() *Register
type RegisterHandle func(local uint64) *Register
type (
RegisterParams struct {
@ -25,65 +25,70 @@ func (c *RegisterParams) checkPassword() bool {
return c.Password == c.RepeatPass
}
func (c *RegisterParams) checkUserExist(mUserInstance *model2.UserInstance) (bool, error) {
var count int64
if err := model2.Count(mUserInstance, &count, model2.NewWhere("mobile", c.Mobile)); err != nil {
return false, err
}
return count > 0, nil
}
func (c *RegisterParams) checkCaptcha() (bool, error) {
return handle.NewCaptcha().Validate(&handle.CaptchaSms{Captcha: c.Captcha, Mobile: c.Mobile})
}
func (c *RegisterParams) checkUserExist(mUserInstance *model2.UserInstance, local uint64) (bool, error) {
var count int64
if err := model2.Count(mUserInstance, &count, model2.NewWhere("mobile", c.Mobile),
model2.NewWhere("local", local)); err != nil {
return false, err
}
return count <= 0, nil
}
// Launch 发起注册
func (c *Register) Launch(params *RegisterParams) (*InstanceLoginReturn, error) {
// 验证密码
if params.checkPassword() {
return nil, errors.New("两次密码不一致")
}
mUserInstance := model.NewUserInstance()
pass, err := params.checkUserExist(mUserInstance.UserInstance)
// 验证验证码
pass, err := params.checkCaptcha()
if err != nil {
return nil, err
} else if pass {
return nil, errors.New("当前手机号码已注册")
}
if pass, err = params.checkCaptcha(); err != nil {
return nil, err
} else if !pass {
return nil, errors.New("验证码错误或已过期")
}
// 验证账号信息
mUserInstance := model.NewUserInstance()
if pass, err = params.checkUserExist(mUserInstance.UserInstance, c.local); err != nil {
return nil, err
} else if !pass {
return nil, errors.New("当前手机号码已注册")
}
mUserInstance.Local.Local = c.local
mUserInstance.Password = utils.GetRandomString(12)
mUserInstance.Mobile = params.Mobile
mUserInstance.Password = params.Password
mUserInstance.Identity = params.Identity
mUserTenant := model.NewUserTenant()
mUserManage := model.NewUserManage()
if err = orm.GetDB().Transaction(func(tx *gorm.DB) error {
if err = model2.Create(mUserInstance.UserInstance, tx); err != nil {
return err
}
mUserTenant.UID = mUserInstance.UUID
mUserTenant.Name = params.Name
mUserTenant.Identity = params.Identity
mUserTenant.Selected = model2.UserTenantSelectedForYes
return model2.Create(mUserTenant.UserTenant, tx)
mUserManage.UID = mUserInstance.UUID
mUserManage.Name = params.Name
mUserManage.Identity = params.Identity
mUserManage.Selected = model2.UserManageSelectedForYes
return model2.Create(mUserManage.UserManage, tx)
}); err != nil {
return nil, err
}
return NewInstance()().Login()(&InstanceLoginParams{
UID: mUserTenant.UUID, Name: mUserTenant.Name, Mobile: mUserInstance.Mobile,
UID: mUserManage.UUID, Name: mUserManage.Name, Mobile: mUserInstance.Mobile,
Identity: mUserInstance.Identity, SelectIdentity: params.Identity,
}), err
}
func NewRegister() RegisterHandle {
return func() *Register {
return &Register{}
return func(local uint64) *Register {
return &Register{local: local}
}
}

View File

@ -1,4 +1,17 @@
package technology
import "SciencesServer/app/service"
// Patent 专利管理
type Patent struct{}
type Patent struct {
*service.SessionEnterprise
local uint64
}
type PatentHandle func(enterprise *service.SessionEnterprise, local uint64) *Patent
func NewPatent() PatentHandle {
return func(enterprise *service.SessionEnterprise, local uint64) *Patent {
return &Patent{SessionEnterprise: enterprise, local: local}
}
}

View File

@ -0,0 +1,21 @@
package model
import (
"SciencesServer/app/common/model"
)
type UserManage struct {
*model.UserManage
}
// LastChooseInfo 最后一次选中的信息
func (m *UserManage) LastChooseInfo(uid uint64) error {
_, err := model.FirstField(m.UserManage, []string{"id", "tenant_id", "uuid", "identity"},
model.NewWhere("uid", uid),
model.NewWhere("selected", model.UserManageSelectedForYes))
return err
}
func NewUserManage() *UserManage {
return &UserManage{UserManage: model.NewUserManage()}
}

View File

@ -1,20 +0,0 @@
package model
import (
"SciencesServer/app/common/model"
)
type UserTenant struct {
*model.UserTenant
}
func (m *UserTenant) LastChooseInfo(uid uint64) error {
_, err := model.FirstField(m.UserTenant, []string{"id", "tenant_id", "uuid", "identity"},
model.NewWhere("uid", uid),
model.NewWhere("selected", model.UserTenantSelectedForYes))
return err
}
func NewUserTenant() *UserTenant {
return &UserTenant{UserTenant: model.NewUserTenant()}
}