Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: customize for launch_screen #100

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions api/handler/custom/launch_screen_service.go
Original file line number Diff line number Diff line change
@@ -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)
}
34 changes: 34 additions & 0 deletions api/pack/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
5 changes: 3 additions & 2 deletions api/router/custom_router.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions config/sql/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 '图片描述',
Expand All @@ -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',
Expand Down
13 changes: 7 additions & 6 deletions internal/launch_screen/service/create_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion internal/launch_screen/service/create_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion internal/launch_screen/service/update_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/launch_screen/service/update_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion pkg/errno/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 3 additions & 2 deletions pkg/upyun/launch_screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}, "")
}
Loading