feat:完善信息

This commit is contained in:
henry
2021-09-28 18:23:34 +08:00
parent ef9213f261
commit b876eab301
9 changed files with 319 additions and 4 deletions

6
app/enterprise/README.md Normal file
View File

@ -0,0 +1,6 @@
## 企业后台
- 企业后台
- 专家后台
- 研究机构后台
- 实验室后台

View File

@ -0,0 +1,45 @@
package api
import (
"SciencesServer/app/common/api"
"SciencesServer/app/enterprise/controller/account"
"github.com/gin-gonic/gin"
)
type Account struct{}
type (
accountLoginForm struct {
Mode int `json:"mode" form:"mode" binding:"required"`
}
accountRegisterForm struct {
}
)
func (a *Account) Login(c *gin.Context) {
form := new(accountLoginForm)
if err := api.Bind(form)(c); err != nil {
api.APIFailure(err.(error))(c)
return
}
data, err := account.NewLogin().Login()(account.LoginMode(form.Mode), nil)
api.APIResponse(err, data)
}
func (a *Account) Register() {
}
func (c *Account) BindName() {
}
func (c *Account) BindMobile() {
}
func (a *Account) Logout() {
}

View File

@ -0,0 +1,121 @@
package account
import (
model2 "SciencesServer/app/common/model"
"SciencesServer/app/enterprise/model"
"SciencesServer/app/handle"
"SciencesServer/config"
"SciencesServer/utils"
"errors"
)
type Login struct{}
type (
LoginHandle func(LoginMode, *LoginRequest) (*LoginResponse, error)
)
type (
LoginRequest struct {
Captcha struct {
Mobile string `json:"mobile"`
Captcha string `json:"captcha"`
}
Password struct {
Mobile string `json:"mobile"`
Password string `json:"password"`
}
Platform struct {
OpenID string `json:"open_id"`
}
}
LoginResponse struct {
Token string `json:"token"`
EffectTime int `json:"effect_time"`
}
)
// LoginMode 登陆模式
type LoginMode int
const (
LoginModeForCaptcha LoginMode = iota + 1e2 + 1 // 验证码登陆
LoginModeForPassword // 密码登陆
LoginModeForWechat // 微信登陆
LoginModeForQQ // QQ登陆
)
var loginHandle = map[LoginMode]func(*LoginRequest) (*model.TenantUser, error){
LoginModeForCaptcha: loginForCaptcha, LoginModeForPassword: loginForPassword,
}
// loginForCaptcha 验证码登陆
func loginForCaptcha(req *LoginRequest) (*model.TenantUser, error) {
pass, err := handle.NewCaptcha().Validate(&handle.CaptchaSms{
Mobile: req.Captcha.Mobile,
Captcha: req.Captcha.Captcha,
})
if err != nil {
return nil, err
} else if !pass {
return nil, errors.New("验证码错误或已过期")
}
var isExist bool
mTenantUsr := model.NewTenantUser()
if isExist, err = model2.FirstField(mTenantUsr.TenantUser, []string{"id", "name", "status"},
model2.NewWhere("mobile", req.Captcha.Mobile)); err != nil {
return nil, err
} else if isExist {
return nil, errors.New("当前手机号码未注册")
}
return mTenantUsr, nil
}
// loginForPassword 密码登陆
func loginForPassword(req *LoginRequest) (*model.TenantUser, error) {
mTenantUsr := model.NewTenantUser()
isExist, err := model2.FirstField(mTenantUsr.TenantUser, []string{"id", "name", "status"},
model2.NewWhere("mobile", req.Password.Mobile))
if err != nil {
return nil, err
} else if isExist {
return nil, errors.New("当前手机号码未注册")
}
if !mTenantUsr.ValidatePassword(req.Password.Password) {
return nil, errors.New("账户或密码错误")
}
return mTenantUsr, nil
}
func loginForPlatform(req *LoginRequest) error {
return nil
}
func (c *Login) Login() LoginHandle {
return func(mode LoginMode, req *LoginRequest) (*LoginResponse, error) {
handle, has := loginHandle[mode]
if !has {
return nil, errors.New("未知的登陆模式")
}
user, err := handle(req)
if err != nil {
return nil, err
}
token := utils.JWTEncrypt(config.SettingInfo.TokenEffectTime, map[string]interface{}{
config.TokenForUID: user.UUID,
})
return &LoginResponse{
Token: token, EffectTime: config.SettingInfo.TokenEffectTime,
}, nil
}
}
func NewLogin() *Login {
return &Login{}
}

View File

@ -0,0 +1,19 @@
package model
import "SciencesServer/app/common/model"
type TenantUser struct {
*model.TenantUser
}
func (m *TenantUser) UUIDToString() string {
return ""
}
func (m *TenantUser) ValidatePassword(password string) bool {
return true
}
func NewTenantUser() *TenantUser {
return &TenantUser{TenantUser: model.NewTenantUser()}
}