2021-11-21 13:23:27 +08:00
|
|
|
package tool
|
2021-03-28 15:52:02 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
|
2021-11-20 15:01:50 +08:00
|
|
|
"github.com/xinliangnote/go-gin-api/internal/code"
|
2021-03-28 15:52:02 +08:00
|
|
|
"github.com/xinliangnote/go-gin-api/internal/pkg/core"
|
|
|
|
|
|
|
|
|
|
"github.com/spf13/cast"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type hashIdsEncodeRequest struct {
|
|
|
|
|
Id int32 `uri:"id"` // 需加密的数字
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type hashIdsEncodeResponse struct {
|
|
|
|
|
Val string `json:"val"` // 加密后的值
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HashIdsEncode HashIds 加密
|
|
|
|
|
// @Summary HashIds 加密
|
|
|
|
|
// @Description HashIds 加密
|
|
|
|
|
// @Tags API.tool
|
2021-11-28 13:25:27 +08:00
|
|
|
// @Accept application/x-www-form-urlencoded
|
2021-03-28 15:52:02 +08:00
|
|
|
// @Produce json
|
|
|
|
|
// @Param id path string true "需加密的数字"
|
|
|
|
|
// @Success 200 {object} hashIdsEncodeResponse
|
|
|
|
|
// @Failure 400 {object} code.Failure
|
|
|
|
|
// @Router /api/tool/hashids/encode/{id} [get]
|
2021-11-28 13:25:27 +08:00
|
|
|
// @Security LoginToken
|
2021-03-28 15:52:02 +08:00
|
|
|
func (h *handler) HashIdsEncode() core.HandlerFunc {
|
|
|
|
|
return func(c core.Context) {
|
|
|
|
|
req := new(hashIdsEncodeRequest)
|
|
|
|
|
res := new(hashIdsEncodeResponse)
|
|
|
|
|
if err := c.ShouldBindURI(req); err != nil {
|
2021-11-28 13:25:27 +08:00
|
|
|
c.AbortWithError(core.Error(
|
2021-03-28 15:52:02 +08:00
|
|
|
http.StatusBadRequest,
|
|
|
|
|
code.ParamBindError,
|
2021-11-28 13:25:27 +08:00
|
|
|
code.Text(code.ParamBindError)).WithError(err),
|
2021-03-28 15:52:02 +08:00
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hashId, err := h.hashids.HashidsEncode([]int{cast.ToInt(req.Id)})
|
|
|
|
|
if err != nil {
|
2021-11-28 13:25:27 +08:00
|
|
|
c.AbortWithError(core.Error(
|
2021-03-28 15:52:02 +08:00
|
|
|
http.StatusBadRequest,
|
2021-06-26 12:06:20 +08:00
|
|
|
code.HashIdsEncodeError,
|
2021-11-28 13:25:27 +08:00
|
|
|
code.Text(code.HashIdsEncodeError)).WithError(err),
|
2021-03-28 15:52:02 +08:00
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.Val = hashId
|
|
|
|
|
|
|
|
|
|
c.Payload(res)
|
|
|
|
|
}
|
|
|
|
|
}
|