package handler import ( _ "embed" "errors" "github.com/loveuer/nf" "github.com/loveuer/nf/nft/resp" "gorm.io/gorm" "uauth/model" "uauth/pkg/store" "uauth/tool" ) func ClientRegistry(c *nf.Ctx) error { type Req struct { ClientId string `json:"client_id"` Icon string `json:"icon"` // url Name string `json:"name"` } var ( err error req = new(Req) ) if err = c.BodyParser(req); err != nil { return resp.Resp400(c, err.Error()) } Secret := tool.RandomString(32) platform := &model.Client{ ClientId: req.ClientId, Icon: req.Icon, Name: req.Name, ClientSecret: Secret, } if err = store.Default.Session(tool.TimeoutCtx(c.Context(), 3)). Create(platform).Error; err != nil { if errors.Is(err, gorm.ErrDuplicatedKey) { return resp.Resp400(c, err, "当前平台已经存在") } return resp.Resp500(c, err) } return resp.Resp200(c, platform) } var ( //go:embed serve_registry.html registryLogin string ) func UserRegistryPage(c *nf.Ctx) error { return c.HTML(registryLogin) } func UserRegistryAction(c *nf.Ctx) error { type Req struct { Username string `form:"username"` Nickname string `form:"nickname"` Password string `form:"password"` ConfirmPassword string `form:"confirm_password"` } var ( err error req = new(Req) ) if err = c.BodyParser(req); err != nil { return resp.Resp400(c, err.Error()) } if err = tool.CheckPassword(req.Password); err != nil { return resp.Resp400(c, req, err.Error()) } op := &model.User{ Username: req.Username, Nickname: req.Nickname, Password: tool.NewPassword(req.Password), } if err = store.Default.Session(tool.TimeoutCtx(c.Context(), 3)). Create(op).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return resp.Resp400(c, err, "用户名已存在") } return resp.Resp500(c, err) } return c.HTML(` 注册成功

注册成功

快去试试吧

`) }