chore: initial commit of ai site platform
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
34
platform/internal/userstore/phone.go
Normal file
34
platform/internal/userstore/phone.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package userstore
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var chinaMobileRe = regexp.MustCompile(`^1[3-9]\d{9}$`)
|
||||
|
||||
// NormalizePhone 规范化中国大陆手机号(去空格、去 +86/86 前缀)。
|
||||
func NormalizePhone(raw string) (string, error) {
|
||||
s := strings.TrimSpace(raw)
|
||||
s = strings.ReplaceAll(s, " ", "")
|
||||
s = strings.ReplaceAll(s, "-", "")
|
||||
if strings.HasPrefix(s, "+86") {
|
||||
s = s[3:]
|
||||
} else if strings.HasPrefix(s, "86") && len(s) == 13 {
|
||||
s = s[2:]
|
||||
}
|
||||
if s == "" {
|
||||
return "", fmt.Errorf("请填写手机号")
|
||||
}
|
||||
if !chinaMobileRe.MatchString(s) {
|
||||
return "", fmt.Errorf("手机号格式不正确(需 11 位大陆号)")
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// LooksLikePhone 判断登录账号是否按手机号解析。
|
||||
func LooksLikePhone(raw string) bool {
|
||||
_, err := NormalizePhone(raw)
|
||||
return err == nil
|
||||
}
|
||||
Reference in New Issue
Block a user