feat:完善信息
This commit is contained in:
6
app/enterprise/README.md
Normal file
6
app/enterprise/README.md
Normal file
@ -0,0 +1,6 @@
|
||||
## 企业后台
|
||||
|
||||
- 企业后台
|
||||
- 专家后台
|
||||
- 研究机构后台
|
||||
- 实验室后台
|
45
app/enterprise/api/account.go
Normal file
45
app/enterprise/api/account.go
Normal 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() {
|
||||
|
||||
}
|
121
app/enterprise/controller/account/login.go
Normal file
121
app/enterprise/controller/account/login.go
Normal 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{}
|
||||
}
|
19
app/enterprise/model/tenant_user.go
Normal file
19
app/enterprise/model/tenant_user.go
Normal 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()}
|
||||
}
|
Reference in New Issue
Block a user