This commit is contained in:
laodaming 2023-06-06 17:37:57 +08:00
commit cad7bdb2ef
15 changed files with 103 additions and 77 deletions

16
.gitignore vendored
View File

@ -13,3 +13,19 @@
# Dependency directories (remove the comment below to include it)
# vendor/
#vscode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets
# Local History for Visual Studio Code
.history/
# Built Visual Studio Code Extensions
*.vsix

View File

@ -5,4 +5,4 @@ SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest
Auth:
AccessSecret: fusen2023
AccessExpire: 604800
AccessExpire: 60

View File

@ -35,6 +35,8 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
},
)
server.AddRoutes(
[]rest.Route{
{
@ -44,5 +46,6 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
},
},
rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
)
}

View File

@ -10,7 +10,6 @@ import (
"fusenapi/home-user-auth/internal/logic"
"fusenapi/home-user-auth/internal/svc"
"fusenapi/home-user-auth/internal/types"
"fusenapi/utils/auth"
)
func UserSaveBasicInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
@ -26,8 +25,7 @@ func UserSaveBasicInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
}
l := logic.NewUserSaveBasicInfoLogic(r.Context(), svcCtx)
userinfo := auth.CheckAuth(r)
resp := l.UserSaveBasicInfo(&req, &userinfo)
resp := l.UserSaveBasicInfo(&req)
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {

View File

@ -35,7 +35,7 @@ func (l *GetTypeLogic) GetType(req *types.Request) (resp *types.Response) {
return
}
resp.SetStatus(basic.StatusOK, "success", data)
resp.SetStatus(basic.CodeOK, "success", data)
return resp
}

View File

@ -45,6 +45,6 @@ func (l *UserBasicInfoLogic) UserBasicInfo(req *types.Request, userinfo *auth.Us
return resp
}
resp.SetStatus(basic.StatusOK, fsUserModel)
resp.SetStatus(basic.CodeOK, fsUserModel)
return resp
}

View File

@ -33,10 +33,10 @@ func (l *UserFontsLogic) UserFonts(req *types.Request) (resp *types.Response) {
if err != nil {
// panic(err)
logx.Error(err)
resp.SetStatus(basic.StatusOK, data)
resp.SetStatus(basic.CodeOK, data)
return resp
}
resp.SetStatus(basic.StatusOK)
resp.SetStatus(basic.CodeOK)
return resp
}

View File

@ -8,6 +8,7 @@ import (
"fusenapi/model"
"fusenapi/utils/basic"
"github.com/golang-jwt/jwt"
"github.com/zeromicro/go-zero/core/logx"
)
@ -25,21 +26,30 @@ func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLog
}
}
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) {
// 必须返回response, 前端需要的是内部约定的Code码, 处理相关的逻辑. 例子(eg): resp.Set(501, "error")
resp = &types.Response{}
userModel, err := model.NewFsUserModel(l.svcCtx.MysqlConn).FindOneByEmail(l.ctx, req.Name)
// log.Printf("%t %t %v", err, model.ErrNotFound, err == model.ErrNotFound)
if err == model.ErrNotFound {
logx.Error(err)
resp.SetStatusWithMessage(basic.DefaultError, err.Error())
resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error())
return resp
}
data := &types.DataUserLogin{
Token: userModel.PasswordResetToken.String,
}
resp.SetStatus(basic.StatusOK, data)
resp.SetStatus(basic.CodeOK, data)
return resp
}

View File

@ -6,7 +6,6 @@ import (
"fusenapi/home-user-auth/internal/svc"
"fusenapi/home-user-auth/internal/types"
"fusenapi/model"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"github.com/zeromicro/go-zero/core/logx"
@ -26,20 +25,22 @@ func NewUserSaveBasicInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext)
}
}
func (l *UserSaveBasicInfoLogic) UserSaveBasicInfo(req *types.RequestBasicInfoForm, userinfo *auth.UserInfo) (resp *types.Response) {
func (l *UserSaveBasicInfoLogic) UserSaveBasicInfo(req *types.RequestBasicInfoForm) (resp *types.Response) {
// 必须返回response, 前端需要的是内部约定的Code码, 处理相关的逻辑. 例子(eg): resp.Set(501, "error")
resp = &types.Response{}
// logx.Info(req)
if userinfo.UserId == 0 {
resp.SetStatusWithMessage(basic.DefaultError, "user is not exists")
return resp
}
// if userinfo.UserId == 0 {
// resp.SetStatusWithMessage(basic.DefaultError, "user is not exists")
// return resp
// }
fsUserModel, err := model.NewFsUserModel(l.svcCtx.MysqlConn).FindOne(l.ctx, userinfo.UserId)
userid := l.ctx.Value("userid").(int64)
fsUserModel, err := model.NewFsUserModel(l.svcCtx.MysqlConn).FindOne(l.ctx, userid)
if err != nil {
logx.Error(err)
}
resp.SetStatus(basic.StatusOK, fsUserModel)
resp.SetStatus(basic.CodeOK, fsUserModel)
return resp
}

View File

@ -1,6 +1,12 @@
package model
import "github.com/zeromicro/go-zero/core/stores/sqlx"
import (
"context"
"fmt"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
var _ FsCanteenTypeModel = (*customFsCanteenTypeModel)(nil)
@ -9,6 +15,7 @@ type (
// and implement the added methods in customFsCanteenTypeModel.
FsCanteenTypeModel interface {
fsCanteenTypeModel
FindGetType(ctx context.Context) ([]*FsGetTypeCanteenType, error)
}
customFsCanteenTypeModel struct {
@ -22,3 +29,25 @@ func NewFsCanteenTypeModel(conn sqlx.SqlConn) FsCanteenTypeModel {
defaultFsCanteenTypeModel: newFsCanteenTypeModel(conn),
}
}
// FsGetTypeCanteenType GetType返回前端的结构
type FsGetTypeCanteenType struct {
Id int64 `db:"id" json:"key"` // ID
Name string `db:"name" json:"name"` // 餐厅名字
}
// FindGetType 根据status = 1查询出所有,fs_canteen_type 的类型,并排序desc
func (m *defaultFsCanteenTypeModel) FindGetType(ctx context.Context) ([]*FsGetTypeCanteenType, error) {
query := fmt.Sprintf("select X.id,X.name from (select %s from %s where status = 1 order by sort desc) X", fsCanteenTypeRows, m.table)
var resp []*FsGetTypeCanteenType
err := m.conn.QueryRows(&resp, query)
switch err {
case nil:
return resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}

View File

@ -27,8 +27,6 @@ type (
FindOne(ctx context.Context, id int64) (*FsCanteenType, error)
Update(ctx context.Context, data *FsCanteenType) error
Delete(ctx context.Context, id int64) error
FindGetType(ctx context.Context) ([]*FsGetTypeCanteenType, error)
}
defaultFsCanteenTypeModel struct {

View File

@ -1,6 +1,12 @@
package model
import "github.com/zeromicro/go-zero/core/stores/sqlx"
import (
"context"
"fmt"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
var _ FsFontModel = (*customFsFontModel)(nil)
@ -9,6 +15,7 @@ type (
// and implement the added methods in customFsFontModel.
FsFontModel interface {
fsFontModel
FindAllOrderSortByDesc(ctx context.Context) ([]*FsFont, error)
}
customFsFontModel struct {
@ -22,3 +29,18 @@ func NewFsFontModel(conn sqlx.SqlConn) FsFontModel {
defaultFsFontModel: newFsFontModel(conn),
}
}
func (m *defaultFsFontModel) FindAllOrderSortByDesc(ctx context.Context) ([]*FsFont, error) {
query := fmt.Sprintf("select %s from %s order by sort desc", fsFontRows, m.table)
var resp []*FsFont
err := m.conn.QueryRows(&resp, query)
switch err {
case nil:
return resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}

View File

@ -1,29 +0,0 @@
package model
import (
"context"
"fmt"
"github.com/zeromicro/go-zero/core/stores/sqlc"
)
type FsGetTypeCanteenType struct {
Id int64 `db:"id" json:"key"` // ID
Name string `db:"name" json:"name"` // 餐厅名字
}
func (m *defaultFsCanteenTypeModel) FindGetType(ctx context.Context) ([]*FsGetTypeCanteenType, error) {
query := fmt.Sprintf("select X.id,X.name from (select %s from %s where status = 1 order by sort desc) X", fsCanteenTypeRows, m.table)
var resp []*FsGetTypeCanteenType
err := m.conn.QueryRows(&resp, query)
switch err {
case nil:
return resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}

View File

@ -1,23 +0,0 @@
package model
import (
"context"
"fmt"
"github.com/zeromicro/go-zero/core/stores/sqlc"
)
func (m *defaultFsFontModel) FindAllOrderSortByDesc(ctx context.Context) ([]*FsFont, error) {
query := fmt.Sprintf("select %s from %s order by sort desc", fsFontRows, m.table)
var resp []*FsFont
err := m.conn.QueryRows(&resp, query)
switch err {
case nil:
return resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}

View File

@ -7,6 +7,7 @@ type StatusResponse struct {
}
var (
StatusOK = &StatusResponse{200, "success"} // 成功
DefaultError = &StatusResponse{510, "unknown error"} // 错误
CodeOK = &StatusResponse{200, "success"} // 成功
CodeServiceErr = &StatusResponse{510, "unknown error"} // 错误
CodeUnAuth = &StatusResponse{401, "unauthorized"} // 未授权
)