authorized.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package authorized
  2. import (
  3. "net/http"
  4. "github.com/xinliangnote/go-gin-api/internal/code"
  5. "github.com/xinliangnote/go-gin-api/internal/pkg/core"
  6. "github.com/xinliangnote/go-gin-api/internal/repository/mysql"
  7. "github.com/xinliangnote/go-gin-api/internal/repository/redis"
  8. "go.uber.org/zap"
  9. )
  10. type handler struct {
  11. db mysql.Repo
  12. logger *zap.Logger
  13. cache redis.Repo
  14. }
  15. func New(logger *zap.Logger, db mysql.Repo, cache redis.Repo) *handler {
  16. return &handler{
  17. logger: logger,
  18. cache: cache,
  19. db: db,
  20. }
  21. }
  22. func (h *handler) Add() core.HandlerFunc {
  23. return func(ctx core.Context) {
  24. ctx.HTML("authorized_add", nil)
  25. }
  26. }
  27. func (h *handler) Demo() core.HandlerFunc {
  28. return func(ctx core.Context) {
  29. ctx.HTML("authorized_demo", nil)
  30. }
  31. }
  32. func (h *handler) List() core.HandlerFunc {
  33. return func(ctx core.Context) {
  34. ctx.HTML("authorized_list", nil)
  35. }
  36. }
  37. func (h *handler) Api() core.HandlerFunc {
  38. type apiRequest struct {
  39. Id string `uri:"id"` // 主键ID
  40. }
  41. type apiResponse struct {
  42. HashID string `json:"hash_id"` // hashID
  43. }
  44. return func(ctx core.Context) {
  45. req := new(apiRequest)
  46. if err := ctx.ShouldBindURI(req); err != nil {
  47. ctx.AbortWithError(core.Error(
  48. http.StatusBadRequest,
  49. code.ParamBindError,
  50. code.Text(code.ParamBindError)).WithError(err),
  51. )
  52. return
  53. }
  54. obj := new(apiResponse)
  55. obj.HashID = req.Id
  56. ctx.HTML("authorized_api", obj)
  57. }
  58. }