新增了获取oss签名

This commit is contained in:
wucongxing8150 2024-08-20 15:27:06 +08:00
parent 702e482659
commit 6dd6172be9
4 changed files with 54 additions and 1 deletions

View File

@ -63,3 +63,29 @@ func (b *Public) GetIndex(c *gin.Context) {
responses.OkWithData(g, c)
}
// GetOssSign 获取oss签名
func (b *Public) GetOssSign(c *gin.Context) {
publicRequest := requests.PublicRequest{}
req := publicRequest.GetOssSign
if err := c.ShouldBind(&req); err != nil {
responses.FailWithMessage(err.Error(), c)
return
}
// 参数验证
if err := global.Validate.Struct(req); err != nil {
responses.FailWithMessage(utils.Translate(err), c)
return
}
// 获取oss签名
publicService := service.PublicService{}
ossSign, err := publicService.GetOssSign(req.Scene)
if err != nil {
responses.FailWithMessage(err.Error(), c)
return
}
responses.OkWithData(ossSign, c)
}

View File

@ -2,6 +2,7 @@ package requests
type PublicRequest struct {
GetPhoneCode // 获取手机验证码
GetOssSign // 获取oss签名
}
// GetPhoneCode 获取手机验证码
@ -9,3 +10,8 @@ type GetPhoneCode struct {
Phone string `json:"phone" form:"phone" label:"手机号" validate:"required,Mobile"`
Scene int `json:"scene" form:"scene" label:"场景值" validate:"required,number,min=1,max=1"`
}
// GetOssSign 获取oss签名
type GetOssSign struct {
Scene int `json:"scene" form:"scene" validate:"required,oneof=1 2 3 4" label:"场景"` // 1:头像 2:证书 3:名片)
}

View File

@ -127,7 +127,12 @@ func publicRouter(r *gin.Engine, api controller.Api) {
// adminRouter 公共路由-验证权限
func adminRouter(r *gin.Engine, api controller.Api) {
// 签名
signGroup := r.Group("/sign")
{
// 获取oss签名
signGroup.GET("/oss", api.Public.GetOssSign)
}
}
// basicRouter 基础数据-验证权限

View File

@ -131,3 +131,19 @@ func (r *PublicService) GetUserIP(h *http.Request) string {
}
return h.RemoteAddr
}
// GetOssSign 获取oss签名
func (a *PublicService) GetOssSign(scene int) (*aliyun.GetOssSignResponse, error) {
var dir string
if scene == 1 {
dir = dir + "user/avatar/"
}
if dir == "" {
return nil, errors.New("获取签名失败")
}
// 生成签名
ossSign, _ := aliyun.GetOssSign(dir)
return ossSign, nil
}