Files

79 lines
2.2 KiB
Go
Raw Normal View History

2021-09-28 18:23:34 +08:00
package api
import (
2021-11-24 10:50:09 +08:00
"SciencesServer/app/api/enterprise/controller/account"
2021-09-29 16:43:40 +08:00
"SciencesServer/app/basic/api"
2021-11-24 10:50:09 +08:00
"SciencesServer/app/service"
"SciencesServer/app/session"
"SciencesServer/config"
2021-09-28 18:23:34 +08:00
"github.com/gin-gonic/gin"
)
type Account struct{}
type (
accountLoginForm struct {
2021-09-29 16:25:56 +08:00
Mode int `json:"mode" form:"mode" binding:"required"`
2021-11-24 09:59:29 +08:00
Account string `json:"account" form:"account"`
2021-09-29 16:25:56 +08:00
Captcha string `json:"captcha" form:"captcha"`
Password string `json:"password" form:"password"`
2021-09-28 18:23:34 +08:00
}
accountRegisterForm struct {
2021-12-03 11:32:26 +08:00
Name string `json:"name" form:"name"`
2021-09-29 16:25:56 +08:00
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"`
2021-09-28 18:23:34 +08:00
}
)
func (a *Account) Login(c *gin.Context) {
form := new(accountLoginForm)
if err := api.Bind(form)(c); err != nil {
api.APIFailure(err.(error))(c)
return
}
2022-01-05 11:29:27 +08:00
data, err := account.NewLogin()(api.GetTenantID()(c).(string)).Launch(account.LoginMode(form.Mode), &account.LoginParams{
2021-09-29 16:25:56 +08:00
Captcha: struct {
Mobile string
Captcha string
2021-11-24 09:59:29 +08:00
}{Mobile: form.Account, Captcha: form.Captcha},
2021-09-29 16:25:56 +08:00
Password: struct {
2021-11-24 09:59:29 +08:00
Account string
2021-09-29 16:25:56 +08:00
Password string
2021-11-24 09:59:29 +08:00
}{Account: form.Account, Password: form.Password},
2021-09-29 16:25:56 +08:00
})
2021-10-13 14:35:24 +08:00
api.APIResponse(err, data)(c)
2021-09-28 18:23:34 +08:00
}
2021-09-29 16:25:56 +08:00
func (a *Account) Register(c *gin.Context) {
form := new(accountRegisterForm)
2021-09-28 18:23:34 +08:00
2021-09-29 16:25:56 +08:00
if err := api.Bind(form)(c); err != nil {
api.APIFailure(err.(error))(c)
return
}
2022-01-05 11:29:27 +08:00
data, err := account.NewRegister()(api.GetTenantID()(c).(string)).Launch(&account.RegisterParams{
2021-09-29 16:25:56 +08:00
Name: form.Name, Mobile: form.Mobile, Captcha: form.Captcha,
2021-12-01 14:12:23 +08:00
Password: form.Password, RepeatPass: form.RepeatPass,
2021-09-29 16:25:56 +08:00
})
2021-10-13 14:35:24 +08:00
api.APIResponse(err, data)(c)
2021-09-28 18:23:34 +08:00
}
2021-11-24 09:59:29 +08:00
func (*Account) Authorize(c *gin.Context) {
}
func (*Account) Logout(c *gin.Context) {
// 因跳过中间键故只能自己去获取token用户信息
2021-11-24 10:50:09 +08:00
token := c.GetHeader(config.APIRequestToken)
_session := session.NewEnterprise()
2021-09-28 18:23:34 +08:00
2022-01-04 11:59:58 +08:00
_ = service.NewAuthToken(token).Auth(config.RedisKeyForAccountEnterprise, _session)
2021-09-28 18:23:34 +08:00
2021-11-24 10:50:09 +08:00
err := account.NewLogout()(_session).Launch()
2021-09-29 16:25:56 +08:00
api.APIResponse(err)(c)
2021-09-28 18:23:34 +08:00
}