This commit is contained in:
laodaming 2023-06-12 17:00:37 +08:00
parent b1393950f2
commit 7f19e29bb3
11 changed files with 203 additions and 34 deletions

View File

@ -1,6 +1,10 @@
package gmodel
import "gorm.io/gorm"
import (
"context"
"errors"
"gorm.io/gorm"
)
type FsQrcodeSet struct {
Id int64 `gorm:"primary_key" json:"id"` // id
@ -23,3 +27,17 @@ type FsQrcodeSetModel struct {
func NewFsQrcodeSetModel(db *gorm.DB) *FsQrcodeSetModel {
return &FsQrcodeSetModel{db}
}
func (q *FsQrcodeSetModel) GetAll(ctx context.Context) (resp []FsQrcodeSet, err error) {
err = q.db.WithContext(ctx).Model(&FsQrcodeSetModel{}).Where("`status` = ?", 1).Find(&resp).Error
if err != nil {
return nil, err
}
return
}
func (q *FsQrcodeSetModel) FindOne(ctx context.Context, id int64) (resp FsQrcodeSet, err error) {
err = q.db.WithContext(ctx).Model(&FsQrcodeSetModel{}).Where("`id` = ?", id).First(&resp).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return FsQrcodeSet{}, err
}
return
}

View File

@ -1,6 +1,9 @@
package gmodel
import "gorm.io/gorm"
import (
"context"
"gorm.io/gorm"
)
type FsStandardLogo struct {
Id int64 `gorm:"primary_key" json:"id"` // ID
@ -16,3 +19,10 @@ type FsStandardLogoModel struct {
func NewFsStandardLogoModel(db *gorm.DB) *FsStandardLogoModel {
return &FsStandardLogoModel{db}
}
func (l *FsStandardLogoModel) GetAll(ctx context.Context) (resp []FsStandardLogo, err error) {
err = l.db.WithContext(ctx).Model(&FsStandardLogoModel{}).Where("`status` = ? ", 1).Find(&resp).Error
if err != nil {
return nil, err
}
return
}

View File

@ -2,20 +2,46 @@ package handler
import (
"errors"
"fusenapi/server/data-transfer/internal/types"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/server/data-transfer/internal/logic"
"fusenapi/server/data-transfer/internal/svc"
)
// 获取二维码配置列表
func GetQrCodeSetListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 解析jwtToken
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
// 从Token里获取对应的信息
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
l := logic.NewGetQrCodeSetListLogic(r.Context(), svcCtx)
resp := l.GetQrCodeSetList()
resp := l.GetQrCodeSetList(userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
// 否则发送500内部服务器错误的JSON响应并记录错误消息logx.Error。
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {

View File

@ -2,18 +2,46 @@ package handler
import (
"errors"
logic2 "fusenapi/server/data-transfer/internal/logic"
svc2 "fusenapi/server/data-transfer/internal/svc"
"fusenapi/server/data-transfer/internal/types"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/server/data-transfer/internal/logic"
"fusenapi/server/data-transfer/internal/svc"
)
func GetStandardLogoListHandler(svcCtx *svc2.ServiceContext) http.HandlerFunc {
func GetStandardLogoListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l := logic2.NewGetStandardLogoListLogic(r.Context(), svcCtx)
resp := l.GetStandardLogoList()
// 解析jwtToken
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
// 从Token里获取对应的信息
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
l := logic.NewGetStandardLogoListLogic(r.Context(), svcCtx)
resp := l.GetStandardLogoList(userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
// 否则发送500内部服务器错误的JSON响应并记录错误消息logx.Error。
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {

View File

@ -7,6 +7,8 @@ import (
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/server/data-transfer/internal/logic"
"fusenapi/server/data-transfer/internal/svc"
"fusenapi/server/data-transfer/internal/types"
@ -14,7 +16,30 @@ import (
func UploadLogoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 解析jwtToken
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
// 从Token里获取对应的信息
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
var req types.UploadLogoReq
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 510,
@ -23,9 +48,11 @@ func UploadLogoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
logx.Info(err)
return
}
// 创建一个业务逻辑层实例
l := logic.NewUploadLogoLogic(r.Context(), svcCtx)
resp := l.UploadLogo(&req)
resp := l.UploadLogo(&req, userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
// 否则发送500内部服务器错误的JSON响应并记录错误消息logx.Error。
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {

View File

@ -7,6 +7,8 @@ import (
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/server/data-transfer/internal/logic"
"fusenapi/server/data-transfer/internal/svc"
"fusenapi/server/data-transfer/internal/types"
@ -14,7 +16,30 @@ import (
func UploadQrcodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 解析jwtToken
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
// 从Token里获取对应的信息
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
var req types.UploadQrcodeReq
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 510,
@ -23,9 +48,11 @@ func UploadQrcodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
logx.Info(err)
return
}
// 创建一个业务逻辑层实例
l := logic.NewUploadQrcodeLogic(r.Context(), svcCtx)
resp := l.UploadQrcode(&req)
resp := l.UploadQrcode(&req, userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
// 否则发送500内部服务器错误的JSON响应并记录错误消息logx.Error。
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {

View File

@ -2,7 +2,8 @@ package logic
import (
"context"
"fusenapi/model"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/server/data-transfer/internal/svc"
@ -26,8 +27,8 @@ func NewGetQrCodeSetListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
}
// 获取二维码配置列表
func (l *GetQrCodeSetListLogic) GetQrCodeSetList() (resp *types.Response) {
qrCodeModel := model.NewFsQrcodeSetModel(l.svcCtx.MysqlConn)
func (l *GetQrCodeSetListLogic) GetQrCodeSetList(loginInfo *auth.UserInfo) (resp *types.Response) {
qrCodeModel := gmodel.NewFsQrcodeSetModel(l.svcCtx.MysqlConn)
qrCodeList, err := qrCodeModel.GetAll(l.ctx)
if err != nil {
logx.Error(err)
@ -37,7 +38,7 @@ func (l *GetQrCodeSetListLogic) GetQrCodeSetList() (resp *types.Response) {
for _, v := range qrCodeList {
list = append(list, types.GetQrCodeSetListRsp{
Id: v.Id,
Name: v.Name,
Name: *v.Name,
})
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", list)

View File

@ -2,9 +2,10 @@ package logic
import (
"context"
"fusenapi/model"
"fusenapi/model/gmodel"
svc2 "fusenapi/server/data-transfer/internal/svc"
types2 "fusenapi/server/data-transfer/internal/types"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"github.com/zeromicro/go-zero/core/logx"
@ -25,8 +26,8 @@ func NewGetStandardLogoListLogic(ctx context.Context, svcCtx *svc2.ServiceContex
}
// 获取标准logo列表
func (l *GetStandardLogoListLogic) GetStandardLogoList() (resp *types2.Response) {
standardLogoModel := model.NewFsStandardLogoModel(l.svcCtx.MysqlConn)
func (l *GetStandardLogoListLogic) GetStandardLogoList(loginInfo *auth.UserInfo) (resp *types2.Response) {
standardLogoModel := gmodel.NewFsStandardLogoModel(l.svcCtx.MysqlConn)
logoList, err := standardLogoModel.GetAll(l.ctx)
if err != nil {
logx.Error(err)
@ -36,8 +37,8 @@ func (l *GetStandardLogoListLogic) GetStandardLogoList() (resp *types2.Response)
for _, v := range logoList {
list = append(list, types2.GetStandardLogoListRsp{
Id: v.Id,
Name: v.Name,
Url: v.Image,
Name: *v.Name,
Url: *v.Image,
})
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", list)

View File

@ -2,6 +2,7 @@ package logic
import (
"context"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/server/data-transfer/internal/svc"
@ -24,7 +25,7 @@ func NewUploadLogoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Upload
}
}
func (l *UploadLogoLogic) UploadLogo(req *types.UploadLogoReq) (resp *types.Response) {
func (l *UploadLogoLogic) UploadLogo(req *types.UploadLogoReq, loginInfo *auth.UserInfo) (resp *types.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
return resp.SetStatus(basic.CodeOK)

View File

@ -2,14 +2,13 @@ package logic
import (
"context"
"errors"
"fusenapi/model"
"fusenapi/model/gmodel"
"fusenapi/server/data-transfer/internal/svc"
"fusenapi/server/data-transfer/internal/types"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/qrcode"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"strings"
)
@ -28,7 +27,7 @@ func NewUploadQrcodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Uplo
}
// 生成base64二维码
func (l *UploadQrcodeLogic) UploadQrcode(req *types.UploadQrcodeReq) (resp *types.Response) {
func (l *UploadQrcodeLogic) UploadQrcode(req *types.UploadQrcodeReq, loginInfo *auth.UserInfo) (resp *types.Response) {
if req.Url == "" {
resp.SetStatus(basic.CodeApiErr, "param url is empty")
}
@ -36,23 +35,23 @@ func (l *UploadQrcodeLogic) UploadQrcode(req *types.UploadQrcodeReq) (resp *type
resp.SetStatus(basic.CodeApiErr, "param QRcodeType must large than 0")
}
//获取二维码模板信息
qrCodeModel := model.NewFsQrcodeSetModel(l.svcCtx.MysqlConn)
qrCodeModel := gmodel.NewFsQrcodeSetModel(l.svcCtx.MysqlConn)
qrCodeSet, err := qrCodeModel.FindOne(l.ctx, req.QRcodeType)
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
if err != nil {
logx.Error(err)
resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get qrcode setting")
}
if qrCodeSet == nil {
if qrCodeSet.Id == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "qrcode setting is not exists")
}
qrType := qrCodeSet.SvgWebsite.String
qrType := *qrCodeSet.SvgWebsite
if strings.Contains(req.Url, "www.instagram.com") {
qrType = qrCodeSet.SvgInstagram.String
qrType = *qrCodeSet.SvgInstagram
} else if strings.Contains(req.Url, "www.facebook.com") {
qrType = qrCodeSet.SvgFacebook.String
qrType = *qrCodeSet.SvgFacebook
}
//生成二维码
imgBase64, err := qrcode.CreateQrCodeBs64WithLogo(req.Url, "", "", 512, int(qrCodeSet.IndexX), int(qrCodeSet.IndexY), true)
imgBase64, err := qrcode.CreateQrCodeBs64WithLogo(req.Url, "", "", 512, int(*qrCodeSet.IndexX), int(*qrCodeSet.IndexY), true)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to generate qrcode")

View File

@ -1,9 +1,13 @@
package svc
import (
"errors"
"fmt"
"fusenapi/initalize"
"fusenapi/server/data-transfer/internal/config"
"github.com/golang-jwt/jwt"
"gorm.io/gorm"
"net/http"
)
type ServiceContext struct {
@ -13,8 +17,35 @@ type ServiceContext struct {
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
MysqlConn: initalize.InitMysql(c.SourceMysql),
}
}
func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) {
AuthKey := r.Header.Get("Authorization")
if len(AuthKey) <= 50 {
return nil, errors.New(fmt.Sprint("Error parsing token, len:", len(AuthKey)))
}
token, err := jwt.Parse(AuthKey, func(token *jwt.Token) (interface{}, error) {
// 检查签名方法是否为 HS256
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
// 返回用于验证签名的密钥
return svcCxt.Config.Auth.AccessSecret, nil
})
if err != nil {
return nil, errors.New(fmt.Sprint("Error parsing token:", err))
}
// 验证成功返回
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
return claims, nil
}
return nil, errors.New(fmt.Sprint("Invalid token", err))
}