feat(api): 接入微信登录并原生实现咨询域(ECR-049/050)
小程序可在 Go 上完成微信手机号登录、测评、预约和下单,不再反代 Java。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -17,22 +17,27 @@ type AuthRepo struct {
|
||||
|
||||
// AccountRow is a registered user snapshot.
|
||||
type AccountRow struct {
|
||||
ID uuid.UUID
|
||||
Phone string
|
||||
PasswordHash string
|
||||
Nickname string
|
||||
AvatarURL string
|
||||
Status string
|
||||
ID uuid.UUID
|
||||
Phone string
|
||||
PasswordHash string
|
||||
Nickname string
|
||||
AvatarURL string
|
||||
Status string
|
||||
WxOpenID string
|
||||
WxUnionID string
|
||||
JavaPlatformUserID int64
|
||||
}
|
||||
|
||||
// GetByPhone loads a registered user by phone.
|
||||
func (r *AuthRepo) GetByPhone(ctx context.Context, phone string) (*AccountRow, error) {
|
||||
row := &AccountRow{}
|
||||
err := r.Pool.QueryRow(ctx, `
|
||||
SELECT id, phone, password_hash, COALESCE(nickname,''), COALESCE(avatar_url,''), status
|
||||
SELECT id, phone, password_hash, COALESCE(nickname,''), COALESCE(avatar_url,''), status,
|
||||
COALESCE(wx_openid,''), COALESCE(wx_unionid,''), COALESCE(java_platform_user_id,0)
|
||||
FROM users
|
||||
WHERE phone=$1 AND deleted_at IS NULL`, phone,
|
||||
).Scan(&row.ID, &row.Phone, &row.PasswordHash, &row.Nickname, &row.AvatarURL, &row.Status)
|
||||
).Scan(&row.ID, &row.Phone, &row.PasswordHash, &row.Nickname, &row.AvatarURL, &row.Status,
|
||||
&row.WxOpenID, &row.WxUnionID, &row.JavaPlatformUserID)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, err
|
||||
}
|
||||
@@ -47,9 +52,11 @@ func (r *AuthRepo) GetAccount(ctx context.Context, userID uuid.UUID) (*AccountRo
|
||||
row := &AccountRow{}
|
||||
var phone, hash *string
|
||||
err := r.Pool.QueryRow(ctx, `
|
||||
SELECT id, phone, password_hash, COALESCE(nickname,''), COALESCE(avatar_url,''), status
|
||||
SELECT id, phone, password_hash, COALESCE(nickname,''), COALESCE(avatar_url,''), status,
|
||||
COALESCE(wx_openid,''), COALESCE(wx_unionid,''), COALESCE(java_platform_user_id,0)
|
||||
FROM users WHERE id=$1 AND deleted_at IS NULL`, userID,
|
||||
).Scan(&row.ID, &phone, &hash, &row.Nickname, &row.AvatarURL, &row.Status)
|
||||
).Scan(&row.ID, &phone, &hash, &row.Nickname, &row.AvatarURL, &row.Status,
|
||||
&row.WxOpenID, &row.WxUnionID, &row.JavaPlatformUserID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -179,6 +186,83 @@ func (r *AuthRepo) RevokeSession(ctx context.Context, token string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// GetByWxOpenid loads a user by WeChat openid.
|
||||
func (r *AuthRepo) GetByWxOpenid(ctx context.Context, openid string) (*AccountRow, error) {
|
||||
row := &AccountRow{}
|
||||
err := r.Pool.QueryRow(ctx, `
|
||||
SELECT id, phone, password_hash, COALESCE(nickname,''), COALESCE(avatar_url,''), status,
|
||||
COALESCE(wx_openid,''), COALESCE(wx_unionid,''), COALESCE(java_platform_user_id,0)
|
||||
FROM users
|
||||
WHERE wx_openid=$1 AND deleted_at IS NULL`, openid,
|
||||
).Scan(&row.ID, &row.Phone, &row.PasswordHash, &row.Nickname, &row.AvatarURL, &row.Status,
|
||||
&row.WxOpenID, &row.WxUnionID, &row.JavaPlatformUserID)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, err
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return row, nil
|
||||
}
|
||||
|
||||
// BindWeChat writes openid/unionid (and optional phone) onto a user.
|
||||
func (r *AuthRepo) BindWeChat(ctx context.Context, userID uuid.UUID, openid, unionID, phone string) error {
|
||||
_, err := r.Pool.Exec(ctx, `
|
||||
UPDATE users
|
||||
SET wx_openid=$2,
|
||||
wx_unionid=COALESCE(NULLIF($3,''), wx_unionid),
|
||||
phone=COALESCE(NULLIF($4,''), phone),
|
||||
updated_at=now()
|
||||
WHERE id=$1 AND deleted_at IS NULL`,
|
||||
userID, openid, unionID, phone,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
// SetJavaPlatformUserID stores the Java t_platform_user.id.
|
||||
func (r *AuthRepo) SetJavaPlatformUserID(ctx context.Context, userID uuid.UUID, javaID int64) error {
|
||||
_, err := r.Pool.Exec(ctx, `
|
||||
UPDATE users SET java_platform_user_id=$2, updated_at=now()
|
||||
WHERE id=$1 AND deleted_at IS NULL`, userID, javaID)
|
||||
return err
|
||||
}
|
||||
|
||||
// SetSessionJavaToken stores the Java Redis token on a Go session.
|
||||
func (r *AuthRepo) SetSessionJavaToken(ctx context.Context, goToken, javaToken string) error {
|
||||
_, err := r.Pool.Exec(ctx, `
|
||||
UPDATE user_sessions SET java_access_token=$2
|
||||
WHERE token=$1 AND revoked_at IS NULL`, goToken, javaToken)
|
||||
return err
|
||||
}
|
||||
|
||||
// JavaTokenByGoToken returns the cached Java token for a live Go session.
|
||||
func (r *AuthRepo) JavaTokenByGoToken(ctx context.Context, goToken string) (string, error) {
|
||||
var tok *string
|
||||
err := r.Pool.QueryRow(ctx, `
|
||||
SELECT java_access_token FROM user_sessions
|
||||
WHERE token=$1 AND revoked_at IS NULL AND expires_at > now()`, goToken,
|
||||
).Scan(&tok)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if tok == nil {
|
||||
return "", nil
|
||||
}
|
||||
return *tok, nil
|
||||
}
|
||||
|
||||
// CreateUserWithWeChat inserts a registered user bound to WeChat.
|
||||
func (r *AuthRepo) CreateUserWithWeChat(ctx context.Context, phone, hash, nickname, openid, unionID string) (uuid.UUID, error) {
|
||||
var id uuid.UUID
|
||||
err := r.Pool.QueryRow(ctx, `
|
||||
INSERT INTO users(phone, password_hash, nickname, wx_openid, wx_unionid)
|
||||
VALUES ($1,$2,NULLIF($3,''),$4,NULLIF($5,''))
|
||||
RETURNING id`,
|
||||
phone, hash, nickname, openid, unionID,
|
||||
).Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
|
||||
// IsRegistered reports whether user has phone.
|
||||
func (r *AuthRepo) IsRegistered(ctx context.Context, userID uuid.UUID) (bool, error) {
|
||||
var ok bool
|
||||
|
||||
Reference in New Issue
Block a user