feat: allow admins to bind member phone numbers
Members page previously only displayed phone status; add bind/replace UI and admin member update phone support for login and sync identity.
This commit is contained in:
@@ -2,12 +2,14 @@ package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"aijianzhan/platform/internal/authx"
|
||||
"aijianzhan/platform/internal/logic/applogic"
|
||||
"aijianzhan/platform/internal/svc"
|
||||
"aijianzhan/platform/internal/userstore"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"github.com/zeromicro/go-zero/rest/pathvar"
|
||||
@@ -293,23 +295,43 @@ func memberUpdateHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
id, _ := strconv.ParseInt(pathvar.Vars(r)["id"], 10, 64)
|
||||
var body struct {
|
||||
Role string `json:"role"`
|
||||
OrgUnitID int64 `json:"org_unit_id"`
|
||||
Status string `json:"status"`
|
||||
Role string `json:"role"`
|
||||
OrgUnitID int64 `json:"org_unit_id"`
|
||||
Status string `json:"status"`
|
||||
Phone *string `json:"phone"` // 传则绑定/更换;传 "" 解绑
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
authx.WriteError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
l := applogic.NewMemberLogic(applogic.NewAuthLogic(r.Context(), svcCtx))
|
||||
u, err := l.Update(id, body.Role, body.OrgUnitID, body.Status)
|
||||
if err != nil {
|
||||
authx.WriteError(w, http.StatusBadRequest, err.Error())
|
||||
var u *userstore.User
|
||||
var err error
|
||||
if body.Phone != nil {
|
||||
u, err = l.SetPhone(id, *body.Phone)
|
||||
if err != nil {
|
||||
authx.WriteError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
_ = svcCtx.Audit.Log(r.Context(), authx.TenantID(r.Context()), authx.UserID(r.Context()),
|
||||
"member.bind_phone", fmt.Sprintf("成员 #%d phone=%s", id, u.Phone))
|
||||
}
|
||||
// 仅改手机时不必强制带 role/status;若其它字段有值则再 Update
|
||||
if body.Role != "" || body.Status != "" || body.OrgUnitID != 0 || body.Phone == nil {
|
||||
u, err = l.Update(id, body.Role, body.OrgUnitID, body.Status)
|
||||
if err != nil {
|
||||
authx.WriteError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
if u == nil {
|
||||
authx.WriteError(w, http.StatusBadRequest, "nothing to update")
|
||||
return
|
||||
}
|
||||
httpx.OkJson(w, map[string]any{
|
||||
"user_id": u.UserID,
|
||||
"username": u.Username,
|
||||
"phone": u.Phone,
|
||||
"display_name": u.DisplayName,
|
||||
"role": u.Role,
|
||||
"org_unit_id": u.OrgUnitID,
|
||||
|
||||
@@ -64,3 +64,19 @@ func (l *MemberLogic) Update(userID int64, role string, orgUnitID int64, status
|
||||
}
|
||||
return l.svcCtx.Users.UpdateMember(l.ctx, tid, userID, role, orgUnitID, status)
|
||||
}
|
||||
|
||||
// SetPhone 管理员为本公司成员绑定/更换/清空手机号(空字符串解绑)。
|
||||
func (l *MemberLogic) SetPhone(userID int64, phone string) (*userstore.User, error) {
|
||||
tid := authx.TenantID(l.ctx)
|
||||
if tid <= 0 {
|
||||
return nil, fmt.Errorf("missing tenant")
|
||||
}
|
||||
u, err := l.svcCtx.Users.GetByID(l.ctx, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if u.TenantID != tid {
|
||||
return nil, fmt.Errorf("成员不属于本公司")
|
||||
}
|
||||
return l.svcCtx.Users.BindPhone(l.ctx, userID, phone)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user