Normalize phone input and default user phone

Signed-off-by: Sean Du <do4suki@gmail.com>
This commit is contained in:
2026-04-28 20:08:21 +08:00
parent 581990a52f
commit 1e52fd04af
4 changed files with 23 additions and 24 deletions
+18 -1
View File
@@ -62,7 +62,7 @@ func login(ctx *gin.Context) {
if ss.CheckErr(err) {
return
}
phone, password := strings.TrimSpace(params.Get("phone")), params.Get("password")
phone, password := normalizePhone(params.Get("phone")), params.Get("password")
if phone == "" || password == "" {
ss.Abort(errors.New("invalid login params"))
return
@@ -95,6 +95,10 @@ func AddUser(phone, password string, isDefault bool) (ghomeschema.LoginData, int
loginMsg := "ok"
loginTime := time.Now().Unix()
created := false
phone = normalizePhone(phone)
if phone == "" {
return loginData, loginCode, loginMsg, created, errors.New("invalid phone")
}
var userID int
var pass, autoKey, ticket string
@@ -373,6 +377,19 @@ func getAvailableUserID(seed int) (int, error) {
return 0, errors.New("failed to allocate user id")
}
func normalizePhone(phone string) string {
phone = strings.TrimSpace(phone)
if phone == "" {
return ""
}
// 客户端上传的手机号可能带国家/地区区号,如 "86-13800000000"。
if idx := strings.Index(phone, "-"); idx >= 0 && idx+1 < len(phone) {
return strings.TrimSpace(phone[idx+1:])
}
return phone
}
func init() {
router.AddHandler("v1", "POST", "/account/login", login)
}
+2 -4
View File
@@ -14,10 +14,9 @@ import (
)
func login(ctx *gin.Context) {
area := ctx.PostForm("area")
user := ctx.PostForm("user")
pass := ctx.PostForm("pass")
if area == "" || user == "" || pass == "" {
if user == "" || pass == "" {
ctx.JSON(http.StatusOK, webuischema.Msg{
Code: 1,
Message: "参数不完整!",
@@ -26,10 +25,9 @@ func login(ctx *gin.Context) {
return
}
userName := " " + area + "-" + user
var userId int
exists, err := db.UserEng.Table("users").
Where("phone = ? AND password = ?", userName, openssl.Md5ToString(pass)).
Where("phone = ? AND password = ?", user, openssl.Md5ToString(pass)).
Cols("user_id").Get(&userId)
utils.CheckErr(err)
if !exists {
+1 -1
View File
@@ -6,7 +6,7 @@ import (
)
const (
defaultPhone = "86-1"
defaultPhone = "1"
defaultPassword = "klsbgames"
)