更新一次
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
package config
|
||||
|
||||
import "github.com/zeromicro/go-zero/rest"
|
||||
import (
|
||||
"fusenapi/home-user-auth/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
DataSource string
|
||||
Auth types.Auth
|
||||
}
|
||||
|
||||
@@ -12,6 +12,11 @@ import (
|
||||
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/user/login",
|
||||
Handler: UserLoginHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/user/fonts",
|
||||
@@ -24,4 +29,15 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/user/basic-info",
|
||||
Handler: UserBasicInfoHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
|
||||
)
|
||||
}
|
||||
|
||||
29
home-user-auth/internal/handler/userbasicinfohandler.go
Normal file
29
home-user-auth/internal/handler/userbasicinfohandler.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"fusenapi/home-user-auth/internal/logic"
|
||||
"fusenapi/home-user-auth/internal/svc"
|
||||
"fusenapi/home-user-auth/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func UserBasicInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.Request
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := logic.NewUserBasicInfoLogic(r.Context(), svcCtx)
|
||||
resp, err := l.UserBasicInfo(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
28
home-user-auth/internal/handler/userloginhandler.go
Normal file
28
home-user-auth/internal/handler/userloginhandler.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"fusenapi/home-user-auth/internal/logic"
|
||||
"fusenapi/home-user-auth/internal/svc"
|
||||
"fusenapi/home-user-auth/internal/types"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func UserLoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.RequestUserLogin
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := logic.NewUserLoginLogic(r.Context(), svcCtx)
|
||||
resp, err := l.UserLogin(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
31
home-user-auth/internal/logic/userbasicinfologic.go
Normal file
31
home-user-auth/internal/logic/userbasicinfologic.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"fusenapi/home-user-auth/internal/svc"
|
||||
"fusenapi/home-user-auth/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UserBasicInfoLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUserBasicInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserBasicInfoLogic {
|
||||
return &UserBasicInfoLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UserBasicInfoLogic) UserBasicInfo(req *types.Request) (resp *types.Response, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
// l.svcCtx.FsUserModel.FindOne(l.ctx, )
|
||||
return
|
||||
}
|
||||
62
home-user-auth/internal/logic/userloginlogic.go
Normal file
62
home-user-auth/internal/logic/userloginlogic.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"fusenapi/home-user-auth/internal/svc"
|
||||
"fusenapi/home-user-auth/internal/types"
|
||||
"fusenapi/model"
|
||||
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UserLoginLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
|
||||
return &UserLoginLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UserLoginLogic) getJwtToken(secretKey string, iat, seconds, userId int64) (string, error) {
|
||||
claims := make(jwt.MapClaims)
|
||||
claims["exp"] = iat + seconds
|
||||
claims["iat"] = iat
|
||||
claims["userId"] = userId
|
||||
token := jwt.New(jwt.SigningMethodHS256)
|
||||
token.Claims = claims
|
||||
return token.SignedString([]byte(secretKey))
|
||||
}
|
||||
|
||||
func (l *UserLoginLogic) UserLogin(req *types.RequestUserLogin) (resp *types.Response, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
data, err := l.svcCtx.FsUserModel.FindOneByEmail(l.ctx, req.Name)
|
||||
// logx.Info(err.Error())
|
||||
log.Printf("%t %t %v", err, model.ErrNotFound, err == model.ErrNotFound)
|
||||
if err == model.ErrNotFound {
|
||||
// logx.Error(err)
|
||||
resp = &types.Response{
|
||||
Code: 304,
|
||||
Message: "fail",
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
resp = &types.Response{
|
||||
Code: 200,
|
||||
Message: "success",
|
||||
Data: &types.DataUserLogin{
|
||||
Token: data.PasswordResetToken.String,
|
||||
},
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
@@ -11,6 +11,7 @@ type ServiceContext struct {
|
||||
Config config.Config
|
||||
FsFontModel model.FsFontModel
|
||||
FsCanteenTypeModel model.FsCanteenTypeModel
|
||||
FsUserModel model.FsUserModel
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
@@ -19,5 +20,6 @@ func NewServiceContext(c config.Config) *ServiceContext {
|
||||
Config: c,
|
||||
FsFontModel: model.NewFsFontModel(conn),
|
||||
FsCanteenTypeModel: model.NewFsCanteenTypeModel(conn),
|
||||
FsUserModel: model.NewFsUserModel(conn),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,45 @@ package types
|
||||
type Request struct {
|
||||
}
|
||||
|
||||
type GetTypeData struct {
|
||||
type RequestUserLogin struct {
|
||||
Name string `form:"name"`
|
||||
Password string `form:"pwd"`
|
||||
}
|
||||
|
||||
type DataUserLogin struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
type DataUserBasicInfo struct {
|
||||
Id int64 `db:"id"` // ID
|
||||
FaceId int64 `db:"face_id"` // facebook的userid
|
||||
Sub int64 `db:"sub"` // google的sub
|
||||
FirstName string `db:"first_name"` // FirstName
|
||||
LastName string `db:"last_name"` // LastName
|
||||
Username string `db:"username"` // 用户名
|
||||
Company string `db:"company"` // 公司名称
|
||||
Mobile string `db:"mobile"` // 手机号码
|
||||
AuthKey string `db:"auth_key"`
|
||||
PasswordHash string `db:"password_hash"`
|
||||
VerificationToken string `db:"verification_token"`
|
||||
PasswordResetToken string `db:"password_reset_token"`
|
||||
Email string `db:"email"` // 邮箱
|
||||
Type int64 `db:"type"` // 1普通餐厅 2连锁餐厅
|
||||
Status int64 `db:"status"` // 1正常 0不正常
|
||||
IsDel int64 `db:"is_del"` // 是否删除 1删除
|
||||
CreatedAt int64 `db:"created_at"` // 添加时间
|
||||
UpdatedAt int64 `db:"updated_at"` // 更新时间
|
||||
IsOrderStatusEmail int64 `db:"is_order_status_email"` // 订单状态改变时是否接收邮件
|
||||
IsEmailAdvertisement int64 `db:"is_email_advertisement"` // 是否接收邮件广告
|
||||
IsOrderStatusPhone int64 `db:"is_order_status_phone"` // 订单状态改变是是否接收电话
|
||||
IsPhoneAdvertisement int64 `db:"is_phone_advertisement"` // 是否接收短信广告
|
||||
IsOpenRender int64 `db:"is_open_render"` // 是否打开个性化渲染(1:开启,0:关闭)
|
||||
IsThousandFace int64 `db:"is_thousand_face"` // 是否已经存在千人千面(1:存在,0:不存在)
|
||||
IsLowRendering int64 `db:"is_low_rendering"` // 是否开启低渲染模型渲染
|
||||
IsRemoveBg int64 `db:"is_remove_bg"` // 用户上传logo是否去除背景
|
||||
}
|
||||
|
||||
type DataGetType struct {
|
||||
Id int64 `db:"id" json:"key"` // ID
|
||||
Name string `db:"name" json:"name"` // 餐厅名字
|
||||
}
|
||||
@@ -14,3 +52,8 @@ type Response struct {
|
||||
Message string `json:"msg"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
type Auth struct {
|
||||
AccessSecret string `json:"AccessSecret"`
|
||||
AccessExpire int `json:"AccessExpire"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user