From 22facc8542735634a7dc95271aba467f2a32eee0 Mon Sep 17 00:00:00 2001 From: FantasyRL <1845983502@qq.com> Date: Wed, 13 Nov 2024 22:56:39 +0800 Subject: [PATCH] fix: customize for launch_screen --- api/handler/custom/launch_screen_service.go | 81 +++++++++++++++++++ api/pack/base.go | 34 ++++++++ api/router/custom_router.go | 5 +- config/sql/init.sql | 4 +- .../launch_screen/service/create_image.go | 13 +-- .../service/create_image_test.go | 2 +- .../launch_screen/service/update_image.go | 2 +- .../service/update_image_test.go | 2 +- pkg/errno/default.go | 5 +- pkg/upyun/launch_screen.go | 5 +- 10 files changed, 137 insertions(+), 16 deletions(-) create mode 100644 api/handler/custom/launch_screen_service.go diff --git a/api/handler/custom/launch_screen_service.go b/api/handler/custom/launch_screen_service.go new file mode 100644 index 00000000..483d3d86 --- /dev/null +++ b/api/handler/custom/launch_screen_service.go @@ -0,0 +1,81 @@ +/* +Copyright 2024 The west2-online Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package custom + +import ( + "context" + + "github.com/cloudwego/hertz/pkg/app" + + "github.com/west2-online/fzuhelper-server/api/model/api" + "github.com/west2-online/fzuhelper-server/api/pack" + "github.com/west2-online/fzuhelper-server/api/rpc" + "github.com/west2-online/fzuhelper-server/kitex_gen/launch_screen" + "github.com/west2-online/fzuhelper-server/pkg/errno" + "github.com/west2-online/fzuhelper-server/pkg/logger" +) + +// MobileGetImage . +// @router /launch_screen/api/screen [GET] +func MobileGetImage(ctx context.Context, c *app.RequestContext) { + var err error + var req api.MobileGetImageRequest + err = c.BindAndValidate(&req) + if err != nil { + logger.Errorf("api.MobileGetImage: BindAndValidate error %v", err) + pack.RespError(c, errno.ParamError.WithError(err)) + return + } + + resp := new(api.MobileGetImageResponse) + + respImageList, _, err := rpc.MobileGetImageRPC(ctx, &launch_screen.MobileGetImageRequest{ + SType: req.Type, + StudentId: req.StudentID, + College: req.College, + Device: req.Device, + }) + if err != nil { + pack.RespError(c, err) + return + } + resp.PictureList = pack.BuildLaunchScreenList(respImageList) + + pack.CustomLaunchScreenRespList(c, resp.PictureList) +} + +// AddImagePointTime . +// @router /launch_screen/api/image/point [GET] +func AddImagePointTime(ctx context.Context, c *app.RequestContext) { + var err error + var req api.AddImagePointTimeRequest + err = c.BindAndValidate(&req) + if err != nil { + logger.Errorf("api.AddImagePointTime: BindAndValidate error %v", err) + pack.RespError(c, errno.ParamError.WithError(err)) + return + } + + _, err = rpc.AddImagePointTimeRPC(ctx, &launch_screen.AddImagePointTimeRequest{ + PictureId: req.PictureID, + }) + if err != nil { + pack.RespError(c, err) + return + } + pack.CustomLaunchScreenRespSuccess(c) +} diff --git a/api/pack/base.go b/api/pack/base.go index 2d2291b7..3c0522aa 100644 --- a/api/pack/base.go +++ b/api/pack/base.go @@ -69,3 +69,37 @@ func RespList(c *app.RequestContext, items any) { } c.JSON(consts.StatusOK, resp) } + +/* + 20241113 + customize for old client of launch_screen +*/ + +type CustomLaunchScreenRespWithData struct { + Code int `json:"code"` + Msg string `json:"message"` + Data any `json:"data"` +} + +type CustomLaunchScreenBase struct { + Code int `json:"code"` + Msg string `json:"message"` +} + +func CustomLaunchScreenRespList(c *app.RequestContext, items any) { + Errno := errno.CustomLaunchScreenSuccess + resp := CustomLaunchScreenRespWithData{ + Code: int(Errno.ErrorCode), + Msg: Errno.ErrorMsg, + Data: items, + } + c.JSON(consts.StatusOK, resp) +} + +func CustomLaunchScreenRespSuccess(c *app.RequestContext) { + Errno := errno.CustomLaunchScreenSuccess + c.JSON(consts.StatusOK, CustomLaunchScreenBase{ + Code: int(Errno.ErrorCode), + Msg: Errno.ErrorMsg, + }) +} diff --git a/api/router/custom_router.go b/api/router/custom_router.go index 5755df92..074e7327 100644 --- a/api/router/custom_router.go +++ b/api/router/custom_router.go @@ -23,6 +23,7 @@ import ( "github.com/west2-online/fzuhelper-server/api/handler" "github.com/west2-online/fzuhelper-server/api/handler/api" + "github.com/west2-online/fzuhelper-server/api/handler/custom" ) // customizeRegister registers customize routers. @@ -39,8 +40,8 @@ func customizedRegister(r *server.Hertz) { 飞书文档:https://west2-online.feishu.cn/wiki/YMtTwhwAOimxkIkeZfAcfAzgnle r.GET("/api/screen", api.MobileGetImage) */ - r.GET("/api/image/point", api.AddImagePointTime) - r.GET("/api/screen", api.MobileGetImage) + r.GET("/api/image/point", custom.AddImagePointTime) + r.GET("/api/screen", custom.MobileGetImage) // url服务的兼容 /* diff --git a/config/sql/init.sql b/config/sql/init.sql index b0372ff3..1741a1e4 100644 --- a/config/sql/init.sql +++ b/config/sql/init.sql @@ -70,7 +70,7 @@ create table `fzu-helper`.`mark` )engine=InnoDB default charset=utf8mb4; create table `fzu-helper`.`launch_screen`( - `id` bigint NOT NULL COMMENT 'ID', + `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'ID', `url` varchar(512) NULL COMMENT '图片url', `href` varchar(255) NULL COMMENT '示例:"Toapp:abab"', `text` varchar(255) NULL COMMENT '图片描述', @@ -90,7 +90,7 @@ create table `fzu-helper`.`launch_screen`( `deleted_at` timestamp NULL DEFAULT NULL, constraint `id` primary key (`id`) -)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; +)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4; CREATE TABLE `fzu-helper`.`course`( `id` bigint NOT NULL COMMENT 'ID', diff --git a/internal/launch_screen/service/create_image.go b/internal/launch_screen/service/create_image.go index 7a2a9f19..b816b0b0 100644 --- a/internal/launch_screen/service/create_image.go +++ b/internal/launch_screen/service/create_image.go @@ -28,16 +28,17 @@ import ( ) func (s *LaunchScreenService) CreateImage(req *launch_screen.CreateImageRequest) (pic *model.Picture, err error) { - id, err := s.sf.NextVal() - if err != nil { - return nil, fmt.Errorf("LaunchScreen.CreateImage SFCreateIDError:%w", err) - } - imgUrl := upyun.GenerateImgName(id, req.Suffix) + /* + id, err := s.sf.NextVal() + if err != nil { + return nil, fmt.Errorf("LaunchScreen.CreateImage SFCreateIDError:%w", err) + } + */ + imgUrl := upyun.GenerateImgName(req.Suffix) var eg errgroup.Group eg.Go(func() error { pictureModel := &model.Picture{ - ID: id, Url: imgUrl, Href: *req.Href, Text: req.Text, diff --git a/internal/launch_screen/service/create_image_test.go b/internal/launch_screen/service/create_image_test.go index 77cbf035..78dedc82 100644 --- a/internal/launch_screen/service/create_image_test.go +++ b/internal/launch_screen/service/create_image_test.go @@ -101,7 +101,7 @@ func TestLaunchScreenService_CreateImage(t *testing.T) { launchScreenService := NewLaunchScreenService(context.Background(), mockClientSet) mockey.Mock((*utils.Snowflake).NextVal).To(func() (int64, error) { return expectedResult.ID, nil }).Build() - mockey.Mock(upyun.GenerateImgName).To(func(id int64, suffix string) string { return expectedResult.Url }).Build() + mockey.Mock(upyun.GenerateImgName).To(func(suffix string) string { return expectedResult.Url }).Build() mockey.Mock((*launchScreenDB.DBLaunchScreen).CreateImage).Return(tc.mockReturn, nil).Build() mockey.Mock(upyun.UploadImg).Return(tc.mockCloudReturn).Build() diff --git a/internal/launch_screen/service/update_image.go b/internal/launch_screen/service/update_image.go index 92cbbe65..d70f3ba4 100644 --- a/internal/launch_screen/service/update_image.go +++ b/internal/launch_screen/service/update_image.go @@ -33,7 +33,7 @@ func (s *LaunchScreenService) UpdateImagePath(req *launch_screen.ChangeImageRequ } delUrl := origin.Url - imgUrl := upyun.GenerateImgName(req.PictureId, req.Suffix) + imgUrl := upyun.GenerateImgName(req.Suffix) var eg errgroup.Group var err2 error diff --git a/internal/launch_screen/service/update_image_test.go b/internal/launch_screen/service/update_image_test.go index 9b95f818..57396989 100644 --- a/internal/launch_screen/service/update_image_test.go +++ b/internal/launch_screen/service/update_image_test.go @@ -126,7 +126,7 @@ func TestLaunchScreenService_UpdateImagePath(t *testing.T) { } mockey.Mock(upyun.DeleteImg).Return(tc.mockCloudReturn).Build() mockey.Mock(upyun.UploadImg).Return(tc.mockCloudReturn).Build() - mockey.Mock(upyun.GenerateImgName).To(func(id int64, suffix string) string { return expectedResult.Url }).Build() + mockey.Mock(upyun.GenerateImgName).To(func(suffix string) string { return expectedResult.Url }).Build() mockey.Mock((*launchScreenDB.DBLaunchScreen).UpdateImage).Return(tc.mockReturn, nil).Build() result, err := launchScreenService.UpdateImagePath(req) diff --git a/pkg/errno/default.go b/pkg/errno/default.go index fc06764d..ff21996b 100644 --- a/pkg/errno/default.go +++ b/pkg/errno/default.go @@ -18,9 +18,12 @@ limitations under the License. package errno +import "github.com/cloudwego/hertz/pkg/protocol/consts" + var ( // Success - Success = NewErrNo(SuccessCode, "Success") + Success = NewErrNo(SuccessCode, "Success") + CustomLaunchScreenSuccess = NewErrNo(consts.StatusOK, "Success") ParamError = NewErrNo(ParamErrorCode, "parameter error") ParamEmpty = NewErrNo(ParamEmptyCode, "some params that required are empty") diff --git a/pkg/upyun/launch_screen.go b/pkg/upyun/launch_screen.go index b1e3ecae..13e9c17a 100644 --- a/pkg/upyun/launch_screen.go +++ b/pkg/upyun/launch_screen.go @@ -67,15 +67,16 @@ func DeleteImg(url string) error { } // GenerateImgName 生成图片名字 -func GenerateImgName(id int64, suffix string) string { +func GenerateImgName(suffix string) string { currentTime := time.Now() // 获取年月日和小时分钟 year, month, day := currentTime.Date() hour, minute := currentTime.Hour(), currentTime.Minute() second := currentTime.Second() + nanoSecond := currentTime.Nanosecond() return strings.Join([]string{ config.UpYun.UssDomain, config.UpYun.Path, - fmt.Sprintf("%v_%d%02d%02d_%02d%02d%02d.", id, year, month, day, hour, minute, second), + fmt.Sprintf("%d%02d%02d_%02d%02d%02d%03d.", year, month, day, hour, minute, second, nanoSecond), suffix, }, "") }