page.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package utils
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. )
  8. const (
  9. TBDelimiter = ","
  10. )
  11. // 分割分页的top/bottom
  12. func SplitTopBottomData(param string) (base, num, extra int64, err error) {
  13. if param == "" {
  14. return 0.0, 0, 0.0, nil
  15. }
  16. mems := strings.Split(param, TBDelimiter)
  17. switch {
  18. case len(mems) == 0:
  19. return 0.0, 0, 0.0, errors.New("member is not enough")
  20. case len(mems) == 1:
  21. base, _ = strconv.ParseInt(mems[0], 10, 64)
  22. case len(mems) == 2:
  23. if base, err = strconv.ParseInt(mems[0], 10, 64); err != nil {
  24. return 0.0, 0, 0.0, err
  25. } else {
  26. num, _ = strconv.ParseInt(mems[1], 10, 64)
  27. }
  28. case len(mems) >= 3:
  29. if base, err = strconv.ParseInt(mems[0], 10, 64); err != nil {
  30. return 0.0, 0, 0.0, err
  31. } else {
  32. num, _ = strconv.ParseInt(mems[1], 10, 64)
  33. extra, _ = strconv.ParseInt(mems[2], 10, 64)
  34. }
  35. default:
  36. return 0.0, 0, 0.0, nil
  37. }
  38. return
  39. }
  40. // 组装分页的top/bottom
  41. func JoinTopBottomData(base, num, extra int64) string {
  42. return fmt.Sprintf("%d%s%d%s%d", base, TBDelimiter, num, TBDelimiter, extra)
  43. }