Merge branch 'develop' of https://gitee.com/fusenpack/fusenapi into develop

This commit is contained in:
eson 2023-06-07 11:50:49 +08:00
commit 12fd21df01
16 changed files with 172 additions and 282 deletions

View File

@ -6,100 +6,46 @@ import (
"fusenapi/utils/basic"
)
{{.types}}
// Set 设置Response的Code和Message值
func (resp *Response) Set(Code int, Message string) {
resp.Code = Code
resp.Message = Message
}
// Set 设置整个Response
func (resp *Response) SetWithData(Code int, Message string, Data interface{}) {
resp.Code = Code
resp.Message = Message
resp.Data = Data
}
// SetMessage 设置Response的Message
func (resp *Response) SetMessage(msg string) {
resp.Message = msg
}
// SetWithData 设置Data
func (resp *Response) SetData(Data interface{}) {
resp.Data = Data
}
// SetWithData 设置Response的Code和Message值 带Data入参数
func (resp *Response) SetCode(Code int) {
resp.Code = Code
}
// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数
func (resp *Response) 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 *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) {
resp.Code = sr.Code
resp.Message = msg
if len(data) == 1 {
resp.Data = data[0]
}
}
// Set 设置Response的Code和Message值
func (resp *ResponseJwt) Set(Code int, Message string) {
resp.Code = Code
resp.Message = Message
func (resp *Response) Set(Code int, Message string) *Response {
return &Response{
Code: Code,
Message: Message,
}
}
// Set 设置整个Response
func (resp *ResponseJwt) SetWithData(Code int, Message string, Data interface{}) {
resp.Code = Code
resp.Message = Message
resp.Data = Data
func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *Response {
return &Response{
Code: Code,
Message: Message,
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]
func (resp *Response) SetStatus(sr *basic.StatusResponse, data ...interface{}) *Response {
newResp := &Response{
Code: sr.Code,
}
if len(data) == 1 {
newResp.Data = data[0]
}
return newResp
}
// 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]
func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) *Response {
newResp := &Response{
Code: sr.Code,
Message: sr.Message,
}
}
if len(data) == 1 {
newResp.Data = data[0]
}
return newResp
}

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 UserBasicInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
@ -26,8 +25,7 @@ func UserBasicInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
}
l := logic.NewUserBasicInfoLogic(r.Context(), svcCtx)
userinfo := auth.CheckAuth(r)
resp := l.UserBasicInfo(&req, &userinfo)
resp := l.UserBasicInfo(&req)
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {

View File

@ -2,8 +2,6 @@ package logic
import (
"context"
"log"
"fusenapi/home-user-auth/internal/svc"
"fusenapi/home-user-auth/internal/types"
"fusenapi/model"
@ -27,22 +25,15 @@ func NewUserBasicInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Use
}
}
func (l *UserBasicInfoLogic) UserBasicInfo(req *types.Request, userinfo *auth.UserInfo) (resp *types.Response) {
func (l *UserBasicInfoLogic) UserBasicInfo(req *types.Request) (resp *types.Response) {
// 必须返回response, 前端需要的是内部约定的Code码, 处理相关的逻辑. 例子(eg): resp.Set(501, "error")
resp = &types.Response{}
// u := l.ctx.Value("userid").(int64)
u := l.ctx.Value("userid")
log.Println(u)
if userinfo.UserId == 0 {
resp = &types.Response{
Code: 510,
Message: "user is not exists",
}
loginInfo := auth.GetUserInfoFormCtx(l.ctx)
if loginInfo.UserId == 0 {
resp.SetStatus(basic.CodeOK, "parse login info err ")
return resp
}
fsUserModel, err := model.NewFsUserModel(l.svcCtx.MysqlConn).FindOne(l.ctx, userinfo.UserId)
fsUserModel, err := model.NewFsUserModel(l.svcCtx.MysqlConn).FindOne(l.ctx, loginInfo.UserId)
if err != nil {
logx.Error(err)
resp.Set(510, err.Error())

View File

@ -2,7 +2,7 @@ package logic
import (
"context"
"strconv"
"fusenapi/utils/auth"
"fusenapi/home-user-auth/internal/svc"
"fusenapi/home-user-auth/internal/types"
@ -30,16 +30,15 @@ func (l *UserSaveBasicInfoLogic) UserSaveBasicInfo(req *types.RequestBasicInfoFo
// 必须返回response, 前端需要的是内部约定的Code码, 处理相关的逻辑. 例子(eg): resp.Set(501, "error")
resp = &types.Response{}
userid, err := strconv.ParseInt(l.ctx.Value("userid").(string), 10, 64)
if err != nil {
resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error())
loginInfo := auth.GetUserInfoFormCtx(l.ctx)
if loginInfo.UserId == 0 {
resp.SetStatus(basic.CodeOK, "parse login info err ")
return resp
}
fsUserModel, err := model.NewFsUserModel(l.svcCtx.MysqlConn).FindOne(l.ctx, userid)
fsUserModel, err := model.NewFsUserModel(l.svcCtx.MysqlConn).FindOne(l.ctx, loginInfo.UserId)
if err != nil {
if err == model.ErrNotFound {
resp.SetStatusWithMessage(basic.CodeOK, "user is not exists")
return resp
}
logx.Error(err)

View File

@ -75,13 +75,6 @@ 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"`
@ -90,93 +83,43 @@ type Auth struct {
}
// Set 设置Response的Code和Message值
func (resp *Response) Set(Code int, Message string) {
resp.Code = Code
resp.Message = Message
func (resp *Response) Set(Code int, Message string) *Response {
return &Response{
Code: Code,
Message: Message,
}
}
// Set 设置整个Response
func (resp *Response) SetWithData(Code int, Message string, Data interface{}) {
resp.Code = Code
resp.Message = Message
resp.Data = Data
}
// SetMessage 设置Response的Message
func (resp *Response) SetMessage(msg string) {
resp.Message = msg
}
// SetWithData 设置Data
func (resp *Response) SetData(Data interface{}) {
resp.Data = Data
}
// SetWithData 设置Response的Code和Message值 带Data入参数
func (resp *Response) SetCode(Code int) {
resp.Code = Code
func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *Response {
return &Response{
Code: Code,
Message: Message,
Data: Data,
}
}
// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数
func (resp *Response) SetStatus(sr *basic.StatusResponse, data ...interface{}) {
resp.Code = sr.Code
resp.Message = sr.Message
if len(data) == 1 {
resp.Data = data[0]
func (resp *Response) SetStatus(sr *basic.StatusResponse, data ...interface{}) *Response {
newResp := &Response{
Code: sr.Code,
}
if len(data) == 1 {
newResp.Data = data[0]
}
return newResp
}
// SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数
func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) {
resp.Code = sr.Code
resp.Message = msg
if len(data) == 1 {
resp.Data = data[0]
func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) *Response {
newResp := &Response{
Code: sr.Code,
Message: sr.Message,
}
}
// 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]
}
newResp.Data = data[0]
}
return newResp
}

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]
}
}

View File

@ -7,7 +7,9 @@ info (
email: ""
)
import "basic.api"
@server(
jwt: Auth
)
service product {
//获取产品列表
@handler GetProductListHandler

View File

@ -1,71 +0,0 @@
package auth
import (
"encoding/json"
"github.com/golang-jwt/jwt"
"github.com/zeromicro/go-zero/core/logx"
"net/http"
"time"
)
type UserInfo struct {
UserId int64 `json:"user_id"` //网站前台登录uid
BackendUserId int64 `json:"backend_user_id"` //管理后台uid
}
// 签名key
var signKey = "FushenFGbhgfhgKgGH556HGlXrsfJKhhjYFGKLO=="
var expireTime = int64(3600)
// 生成token
func GenJwtToken(userInfo UserInfo) (token string, err error) {
t := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"user_id": userInfo.UserId,
"backend_user_id": userInfo.BackendUserId,
"exp": time.Now().Add(time.Second * time.Duration(expireTime)).Unix(), //过期时间
"iss": "fusen",
})
token, err = t.SignedString([]byte(signKey))
if err != nil {
return "", err
}
return
}
// 解释token
func ParseJwtToken(token string) (UserInfo, error) {
t, err := jwt.ParseWithClaims(token, jwt.MapClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(signKey), nil
})
if err != nil {
return UserInfo{}, err
}
d, err := json.Marshal(t.Claims)
if err != nil {
return UserInfo{}, err
}
var userInfo UserInfo
if err = json.Unmarshal(d, &userInfo); err != nil {
return UserInfo{}, err
}
return userInfo, nil
}
// 检测授权
func CheckAuth(r *http.Request) UserInfo {
token := r.Header.Get("Authorization")
if token == "" {
token = r.Header.Get("Auth-Key")
}
if token == "" {
logx.Error("token is empty")
return UserInfo{}
}
//解析token
userInfo, err := ParseJwtToken(token)
if err != nil {
logx.Error(err)
return UserInfo{}
}
return userInfo
}

21
utils/auth/user.go Normal file
View File

@ -0,0 +1,21 @@
package auth
import (
"context"
"encoding/json"
"github.com/zeromicro/go-zero/core/logx"
)
type UserInfo struct {
UserId int64 `json:"userid"`
}
// 获取登录信息
func GetUserInfoFormCtx(ctx context.Context) UserInfo {
uid, err := ctx.Value("userid").(json.Number).Int64()
if err != nil {
logx.Error("parse uid form context err:", err.Error())
return UserInfo{}
}
return UserInfo{UserId: uid}
}