79 lines
2.2 KiB
Go
79 lines
2.2 KiB
Go
package api
|
||
|
||
import (
|
||
"SciencesServer/app/api/enterprise/controller/account"
|
||
"SciencesServer/app/basic/api"
|
||
"SciencesServer/app/service"
|
||
"SciencesServer/app/session"
|
||
"SciencesServer/config"
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
type Account struct{}
|
||
|
||
type (
|
||
accountLoginForm struct {
|
||
Mode int `json:"mode" form:"mode" binding:"required"`
|
||
Account string `json:"account" form:"account"`
|
||
Captcha string `json:"captcha" form:"captcha"`
|
||
Password string `json:"password" form:"password"`
|
||
}
|
||
accountRegisterForm struct {
|
||
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"`
|
||
RepeatPass string `json:"repeat_pass" form:"repeat_pass" binding:"required"`
|
||
}
|
||
)
|
||
|
||
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()(api.GetTenantID()(c).(uint64)).Launch(account.LoginMode(form.Mode), &account.LoginParams{
|
||
Captcha: struct {
|
||
Mobile string
|
||
Captcha string
|
||
}{Mobile: form.Account, Captcha: form.Captcha},
|
||
Password: struct {
|
||
Account string
|
||
Password string
|
||
}{Account: form.Account, Password: form.Password},
|
||
})
|
||
api.APIResponse(err, data)(c)
|
||
}
|
||
|
||
func (a *Account) Register(c *gin.Context) {
|
||
form := new(accountRegisterForm)
|
||
|
||
if err := api.Bind(form)(c); err != nil {
|
||
api.APIFailure(err.(error))(c)
|
||
return
|
||
}
|
||
data, err := account.NewRegister()(api.GetTenantID()(c).(uint64)).Launch(&account.RegisterParams{
|
||
Name: form.Name, Mobile: form.Mobile, Captcha: form.Captcha,
|
||
Password: form.Password, RepeatPass: form.RepeatPass,
|
||
})
|
||
api.APIResponse(err, data)(c)
|
||
}
|
||
|
||
func (*Account) Authorize(c *gin.Context) {
|
||
|
||
}
|
||
|
||
func (*Account) Logout(c *gin.Context) {
|
||
// 因跳过中间键,故只能自己去获取token用户信息
|
||
token := c.GetHeader(config.APIRequestToken)
|
||
|
||
_session := session.NewEnterprise()
|
||
|
||
_ = service.NewAuthToken(token).Auth(config.RedisKeyForAccountEnterprise, _session)
|
||
|
||
err := account.NewLogout()(_session).Launch()
|
||
api.APIResponse(err)(c)
|
||
}
|