(email string)
| 459 | } |
| 460 | |
| 461 | func emailToUsername(email string) string { |
| 462 | parts := strings.SplitN(email, "@", 2) |
| 463 | name := parts[0] |
| 464 | // 只保留字母、数字、连字符、下划线、点 |
| 465 | var b strings.Builder |
| 466 | for _, c := range name { |
| 467 | if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '_' || c == '.' { |
| 468 | b.WriteRune(c) |
| 469 | } |
| 470 | } |
| 471 | result := b.String() |
| 472 | if result == "" { |
| 473 | // 8 字节 hex(64 bits 熵)确保唯一性 |
| 474 | result = "user-" + randomHex(8) |
| 475 | } |
| 476 | return result |
| 477 | } |
| 478 | |
| 479 | func randomHex(n int) string { |
| 480 | buf := make([]byte, n) |
no test coverage detected