This commit is contained in:
laodaming
2023-06-07 11:35:04 +08:00
parent ef50dbb6fe
commit 3d492ff720
14 changed files with 117 additions and 117 deletions

View File

@@ -1,4 +1,8 @@
Name: product
Host: 0.0.0.0
Port: 8888
DataSource: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest
Port: 8889
DataSource: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest
Auth:
AccessSecret: fusen2023
AccessExpire: 60
RefreshAfter: 60

View File

@@ -1,8 +1,12 @@
package config
import "github.com/zeromicro/go-zero/rest"
import (
"fusenapi/product/internal/types"
"github.com/zeromicro/go-zero/rest"
)
type Config struct {
rest.RestConf
DataSource string
Auth types.Auth
}

View File

@@ -2,7 +2,6 @@ package handler
import (
"errors"
"fusenapi/utils/auth"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
@@ -15,8 +14,6 @@ import (
func GetProductListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
//用户登录信息
userInfo := auth.CheckAuth(r)
var req types.GetProductListReq
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
@@ -28,7 +25,7 @@ func GetProductListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
}
l := logic.NewGetProductListLogic(r.Context(), svcCtx)
resp := l.GetProductList(&req, userInfo)
resp := l.GetProductList(&req)
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {

View File

@@ -2,7 +2,6 @@ package handler
import (
"errors"
"fusenapi/utils/auth"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
@@ -15,8 +14,6 @@ import (
func GetSuccessRecommandHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
//用户登录信息
userInfo := auth.CheckAuth(r)
var req types.GetSuccessRecommandReq
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
@@ -28,7 +25,7 @@ func GetSuccessRecommandHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
}
l := logic.NewGetSuccessRecommandLogic(r.Context(), svcCtx)
resp := l.GetSuccessRecommand(&req, userInfo)
resp := l.GetSuccessRecommand(&req)
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {

View File

@@ -23,5 +23,6 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Handler: GetSuccessRecommandHandler(serverCtx),
},
},
rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
)
}

View File

@@ -33,12 +33,11 @@ func NewGetProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ge
}
// 获取产品列表
func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, loginInfo auth.UserInfo) (resp *types.Response) {
func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp *types.Response) {
resp = &types.Response{}
//校验前台登录情况
loginInfo := auth.GetUserInfoFormCtx(l.ctx)
if loginInfo.UserId == 0 {
resp.Set(constants.CODE_UNAUTH, "please sign in")
return
resp.Set(constants.CODE_SERVICE_ERR, "get login user info err")
}
//如果是demo
if req.IsDemo == 1 {

View File

@@ -28,12 +28,11 @@ func NewGetSuccessRecommandLogic(ctx context.Context, svcCtx *svc.ServiceContext
}
// 获取推荐的产品列表
func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessRecommandReq, loginInfo auth.UserInfo) (resp *types.Response) {
func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessRecommandReq) (resp *types.Response) {
resp = &types.Response{}
//校验前台登录情况
loginInfo := auth.GetUserInfoFormCtx(l.ctx)
if loginInfo.UserId == 0 {
resp.Set(constants.CODE_UNAUTH, "please sign in")
return
resp.Set(constants.CODE_SERVICE_ERR, "get login user info err")
}
//获取用户信息
userModel := model.NewFsUserModel(l.svcCtx.MysqlConn)

View File

@@ -78,6 +78,20 @@ type Response struct {
Data interface{} `json:"data"`
}
type ResponseJwt struct {
Code int `json:"code"`
Message string `json:"msg"`
Data interface{} `json:"data"`
AccessSecret string `json:"accessSecret"`
AccessExpire int64 `json:"accessExpire"`
}
type Auth struct {
AccessSecret string `json:"accessSecret"`
AccessExpire int64 `json:"accessExpire"`
RefreshAfter int64 `json:"refreshAfter"`
}
// Set 设置Response的Code和Message值
func (resp *Response) Set(Code int, Message string) {
resp.Code = Code
@@ -123,3 +137,49 @@ func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string,
resp.Data = data[0]
}
}
// Set 设置Response的Code和Message值
func (resp *ResponseJwt) Set(Code int, Message string) {
resp.Code = Code
resp.Message = Message
}
// Set 设置整个Response
func (resp *ResponseJwt) SetWithData(Code int, Message string, Data interface{}) {
resp.Code = Code
resp.Message = Message
resp.Data = Data
}
// SetMessage 设置Response的Message
func (resp *ResponseJwt) SetMessage(msg string) {
resp.Message = msg
}
// SetWithData 设置Data
func (resp *ResponseJwt) SetData(Data interface{}) {
resp.Data = Data
}
// SetWithData 设置Response的Code和Message值 带Data入参数
func (resp *ResponseJwt) SetCode(Code int) {
resp.Code = Code
}
// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数
func (resp *ResponseJwt) SetStatus(sr *basic.StatusResponse, data ...interface{}) {
resp.Code = sr.Code
resp.Message = sr.Message
if len(data) == 1 {
resp.Data = data[0]
}
}
// SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数
func (resp *ResponseJwt) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) {
resp.Code = sr.Code
resp.Message = msg
if len(data) == 1 {
resp.Data = data[0]
}
}