account_code.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package common
  2. import (
  3. "strings"
  4. )
  5. const (
  6. OPER_CODE_PREFIX = "OPER_"
  7. ORGAN_CODE_PREFIX = "ORGAN_"
  8. OPER_CODE_PREFIX_LEN = len(OPER_CODE_PREFIX)
  9. )
  10. func EncodeOperAccount(account string) string {
  11. return OPER_CODE_PREFIX + account
  12. }
  13. func EncodeOrganAccount(account string) string {
  14. return ORGAN_CODE_PREFIX + account
  15. }
  16. func DecodeOperAccount(account string) string {
  17. return account[OPER_CODE_PREFIX_LEN:]
  18. }
  19. func GetAccountPrefix(account string) string {
  20. i := strings.IndexAny(account, "_")
  21. if i == -1 {
  22. return ""
  23. }
  24. return account[0 : i+1]
  25. }
  26. func DecodeAccount(account string) string {
  27. i := strings.IndexAny(account, "_")
  28. if i == -1 {
  29. return ""
  30. }
  31. return account[i+1:]
  32. }
  33. //4-22字符,只允许小写字母、数字、下划线,且首位须为字母
  34. func CheckAccountFormat(account string) bool {
  35. len := strings.Count(account, "") - 1
  36. if len < 4 || len > 22 {
  37. return false
  38. }
  39. for i, _ := range account {
  40. if i == 0 {
  41. if !(account[0:1] >= "a" && account[0:1] <= "z") {
  42. return false
  43. }
  44. } else {
  45. if (account[i:i+1] >= "0" && account[i:i+1] <= "9") ||
  46. (account[i:i+1] >= "a" && account[i:i+1] <= "z") ||
  47. account[i:i+1] == "_" {
  48. continue
  49. } else {
  50. return false
  51. }
  52. }
  53. }
  54. return true
  55. }