Generate unique Mao Hai parent nicknames for new users
This commit is contained in:
@@ -2,9 +2,11 @@ package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
@@ -79,8 +81,9 @@ func (a *API) devLogin(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
name := strings.TrimSpace(in.Nickname)
|
||||
if name == "" {
|
||||
name = "柯基家长"
|
||||
if len([]rune(name)) > 64 {
|
||||
fail(c, http.StatusBadRequest, "昵称不能超过 64 字")
|
||||
return
|
||||
}
|
||||
user, err := a.findOrCreateUser("dev:"+in.Key, name)
|
||||
if err != nil {
|
||||
@@ -123,7 +126,7 @@ func (a *API) login(c *gin.Context) {
|
||||
fail(c, http.StatusUnauthorized, "微信登录凭证无效")
|
||||
return
|
||||
}
|
||||
user, err := a.findOrCreateUser(wx.OpenID, "柯基家长")
|
||||
user, err := a.findOrCreateUser(wx.OpenID, "")
|
||||
if err != nil {
|
||||
fail(c, http.StatusInternalServerError, "登录失败")
|
||||
return
|
||||
@@ -140,17 +143,61 @@ func (a *API) findOrCreateUser(openID, nickname string) (model.User, error) {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return user, err
|
||||
}
|
||||
user = model.User{OpenID: openID, Nickname: nickname}
|
||||
if err := a.db.Create(&user).Error; err != nil {
|
||||
// A concurrent login may have inserted the same open_id.
|
||||
if lookup := a.db.Where("open_id = ?", openID).First(&user).Error; lookup == nil {
|
||||
return user, nil
|
||||
// Serialize new-user naming across server instances without requiring custom nicknames to be unique.
|
||||
err = a.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Exec("SELECT pg_advisory_xact_lock(?)", int64(7270139527)).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// Another login for this open ID may have completed while waiting for the lock.
|
||||
if err := tx.Where("open_id = ?", openID).First(&user).Error; err == nil {
|
||||
return nil
|
||||
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
name := strings.TrimSpace(nickname)
|
||||
if name == "" {
|
||||
generated, err := defaultNickname(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
name = generated
|
||||
}
|
||||
user = model.User{OpenID: openID, Nickname: name}
|
||||
return tx.Create(&user).Error
|
||||
})
|
||||
if err != nil {
|
||||
return user, fmt.Errorf("create user: %w", err)
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func defaultNickname(tx *gorm.DB) (string, error) {
|
||||
// Prefer four digits. Expand when occupied names make retries frequent.
|
||||
for digits := 4; digits <= 6; digits++ {
|
||||
limit := int64(10000)
|
||||
if digits == 5 {
|
||||
limit = 100000
|
||||
} else if digits == 6 {
|
||||
limit = 1000000
|
||||
}
|
||||
for attempt := 0; attempt < 20; attempt++ {
|
||||
number, err := rand.Int(rand.Reader, big.NewInt(limit))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("generate nickname: %w", err)
|
||||
}
|
||||
name := fmt.Sprintf("毛孩家长%0*d", digits, number.Int64())
|
||||
var count int64
|
||||
if err := tx.Model(&model.User{}).Where("nickname = ?", name).Count(&count).Error; err != nil {
|
||||
return "", err
|
||||
}
|
||||
if count == 0 {
|
||||
return name, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", errors.New("default nickname allocation failed")
|
||||
}
|
||||
|
||||
func (a *API) loginUser(c *gin.Context, user model.User) {
|
||||
if user.Disabled {
|
||||
fail(c, http.StatusForbidden, "账号已停用")
|
||||
|
||||
Reference in New Issue
Block a user