From b4174a64123a35c25b48b3478421f7a03d24ea02 Mon Sep 17 00:00:00 2001 From: Austin Date: Mon, 21 Sep 2026 13:11:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9EIM=E8=81=94=E7=B3=BB?= =?UTF-8?q?=E6=88=91=E4=BB=AC=E5=AD=97=E5=85=B8=E9=85=8D=E7=BD=AE=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=94=A8=E6=88=B7=E6=90=9C=E7=B4=A2=E4=B8=8E?= =?UTF-8?q?=E9=82=80=E8=AF=B7=E7=BB=91=E5=AE=9A=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 优化IM用户列表搜索逻辑,支持长数字用户ID精确匹配 2. 修复前端雪花ID精度丢失问题,改用字符串传输用户ID 3. 新增邀请码批量绑定用户功能,支持搜索绑定指定用户 4. 新增字典配置接口,新增联系我们字典SQL脚本 5. 完善字典数据变更事件推送,优化弹窗关闭与页面刷新逻辑 --- .DS_Store | Bin 0 -> 6148 bytes docs/im/IM-联系我们字典配置.sql | 19 ++++ .../api/admin/imconfiginternal/im_config.go | 15 +++ .../internalapi/imconfig/im_config.go | 24 +++++ server/internal/logic/sys/dict_data.go | 29 ++++++ server/internal/logic/sys/im_invite.go | 29 ++++-- server/internal/logic/sys/im_user.go | 7 +- .../internal/model/input/sysin/im_invite.go | 4 + server/internal/model/input/sysin/im_user.go | 2 +- .../im/registrationInvite/BindUserModal.vue | 3 +- .../im/registrationInvite/CreateModal.vue | 91 +++++++++++++++++- web/src/views/imSecurityPolicy/index.vue | 2 + 12 files changed, 215 insertions(+), 10 deletions(-) create mode 100644 .DS_Store create mode 100644 docs/im/IM-联系我们字典配置.sql diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5a8c13d56148bf4a8e4ee36c0b9b098a9351f438 GIT binary patch literal 6148 zcmeHL%}N|W5U$Rw%MvdOxrnE^34tW;0|=9Kd;swhZ@P|b7IvA%pX55=3-}72B)6RO zA_NbCT=D`Tc#j~-H7e*=-Muq4JG&l5qzbyfp029-sy~?S?Ij}9-cMRYO(JTbG1gw9 zn_%qcR(3aA24Q33vTYxI`7)T2H5{RZEXpVBu-`xx_4?h-rLvS7ff8oH{a{6m 0 { @@ -40,6 +42,7 @@ func (s *sSysDictData) Delete(ctx context.Context, in *sysin.DictDataDeleteInp) err = gerror.Wrap(err, consts.ErrorORM) return err } + s.publishDictUpdated(ctx, oldTypes) return nil } @@ -52,11 +55,13 @@ func (s *sSysDictData) Edit(ctx context.Context, in *sysin.DictDataEditInp) (err } else if count > 0 { return gerror.New("内部密钥字典禁止通过通用接口操作") } + oldTypes := s.getTypesByIds(ctx, in.Id) _, err = dao.SysDictData.Ctx(ctx).Fields(sysin.DictDataUpdateFields{}).WherePri(in.Id).Data(in).Update() if err != nil { err = gerror.Wrap(err, consts.ErrorORM) return err } + s.publishDictUpdated(ctx, append(oldTypes, in.Type)) return nil } @@ -79,9 +84,33 @@ func (s *sSysDictData) Edit(ctx context.Context, in *sysin.DictDataEditInp) (err err = gerror.Wrap(err, consts.ErrorORM) return err } + s.publishDictUpdated(ctx, []string{in.Type}) return nil } +// getTypesByIds 查询指定字典数据ID的类型标识,用于变更后失效缓存 +func (s *sSysDictData) getTypesByIds(ctx context.Context, ids interface{}) []string { + columns, err := dao.SysDictData.Ctx(ctx).Where(dao.SysDictData.Columns().Id, ids).Fields(dao.SysDictData.Columns().Type).Array() + if err != nil { + g.Log().Warningf(ctx, "查询字典类型失败 err:%+v", err) + return nil + } + return g.NewVar(columns).Strings() +} + +// publishDictUpdated 发布字典变更事件,通知 im-server 实时失效字典配置缓存 +func (s *sSysDictData) publishDictUpdated(ctx context.Context, types []string) { + for _, t := range types { + t = strings.TrimSpace(t) + if t == "" || t == consts.ImInternalSecretDictType { + continue + } + if err := publishImPolicyEvent(ctx, 0, "*", consts.ImConfigEventDictionaryUpdated, t, 0); err != nil { + g.Log().Warningf(ctx, "发布字典变更事件失败 type:%s err:%+v", t, err) + } + } +} + // List 获取列表 func (s *sSysDictData) List(ctx context.Context, in *sysin.DictDataListInp) (list []*sysin.DictDataListModel, totalCount int, err error) { mod := dao.SysDictData.Ctx(ctx).WhereNot(dao.SysDictData.Columns().Type, consts.ImInternalSecretDictType) diff --git a/server/internal/logic/sys/im_invite.go b/server/internal/logic/sys/im_invite.go index d2f93e5..619e6fe 100644 --- a/server/internal/logic/sys/im_invite.go +++ b/server/internal/logic/sys/im_invite.go @@ -116,7 +116,7 @@ func (s *sSysImInvite) Create(ctx context.Context, in *sysin.ImInviteCreateInp) expiresAt := in.ExpiresAt.Time.UTC().Round(time.Millisecond) var item imInviteCodeResponse if key := strings.TrimSpace(in.IdempotencyKey); key != "" { - batch, codes, createErr := createInviteBatch(ctx, db, scope.TenantId, scope.OperatorId, 1, expiresAt, in.Remark, key) + batch, codes, createErr := createInviteBatch(ctx, db, scope.TenantId, scope.OperatorId, 1, expiresAt, in.Remark, key, 0, "") if createErr != nil { err = createErr } else if len(codes) > 0 { @@ -150,15 +150,27 @@ func (s *sSysImInvite) BatchCreate(ctx context.Context, in *sysin.ImInviteBatchC if err != nil { return nil, err } + var boundUserLabel string + if in.UserId > 0 { + tenant, terr := resolveImAdminTenant(ctx, scope.TenantId) + if terr != nil { + return nil, terr + } + label, lerr := inviteBoundUserLabel(ctx, tenant, in.UserId) + if lerr != nil { + return nil, lerr + } + boundUserLabel = label + } key := strings.TrimSpace(in.IdempotencyKey) - batch, _, err := createInviteBatch(ctx, db, scope.TenantId, scope.OperatorId, in.Quantity, in.ExpiresAt.Time.UTC().Round(time.Millisecond), in.Remark, key) + batch, _, err := createInviteBatch(ctx, db, scope.TenantId, scope.OperatorId, in.Quantity, in.ExpiresAt.Time.UTC().Round(time.Millisecond), in.Remark, key, in.UserId, boundUserLabel) result := "success" if err != nil { result = "failed" } else { res = convertInviteBatch(batch) } - _ = writeImAdminAudit(ctx, scope.TenantId, "invite_batch_create", "invite_batch", batchID(batch), result, g.Map{"quantity": in.Quantity, "expiresAt": in.ExpiresAt, "remark": in.Remark}) + _ = writeImAdminAudit(ctx, scope.TenantId, "invite_batch_create", "invite_batch", batchID(batch), result, g.Map{"quantity": in.Quantity, "expiresAt": in.ExpiresAt, "remark": in.Remark, "boundUserId": in.UserId}) return res, err } @@ -358,7 +370,7 @@ func createSingleInvite(ctx context.Context, db gdb.DB, tenantID, operatorID int return imInviteCodeResponse{}, gerror.New("无法生成不重复的邀请码,请重试") } -func createInviteBatch(ctx context.Context, db gdb.DB, tenantID, operatorID int64, quantity int, expiresAt time.Time, remark, key string) (*imInviteBatchResponse, []imInviteCodeResponse, error) { +func createInviteBatch(ctx context.Context, db gdb.DB, tenantID, operatorID int64, quantity int, expiresAt time.Time, remark, key string, boundUserID int64, boundUserLabel string) (*imInviteBatchResponse, []imInviteCodeResponse, error) { if existing, err := getInviteBatch(ctx, db, tenantID, "b.idempotency_key=?", key); err == nil { if !sameInviteBatch(existing, quantity, expiresAt, remark) { return nil, nil, gerror.New("幂等键已被其他批量生成请求使用") @@ -402,11 +414,16 @@ func createInviteBatch(ctx context.Context, db gdb.DB, tenantID, operatorID int6 } for i := range codes { if _, txErr := tx.Exec(`INSERT INTO im_registration_invite_codes - (id,tenant_id,batch_id,code,status,expires_at,max_uses,usage_count,last_used_at,created_by,remark,created_at,updated_at) - VALUES(?,?,?,?,'active',?,0,0,NULL,?,?,?,?)`, codes[i].ID, tenantID, batchID, codes[i].Code, expiresAt, operatorID, remark, now, now); txErr != nil { + (id,tenant_id,batch_id,code,status,expires_at,max_uses,usage_count,last_used_at,created_by,remark,created_at,updated_at,bound_user_id,bound_user_label) + VALUES(?,?,?,?,'active',?,0,0,NULL,?,?,?,?,?,?)`, codes[i].ID, tenantID, batchID, codes[i].Code, expiresAt, operatorID, remark, now, now, boundUserID, boundUserLabel); txErr != nil { return txErr } codes[i].TenantID, codes[i].Status, codes[i].ExpiresAt, codes[i].CreatedBy, codes[i].Remark, codes[i].CreatedAt = tenantID, "active", expiresAt, operatorID, remark, now + if boundUserID > 0 { + id := boundUserID + codes[i].BoundUserID = &id + codes[i].BoundUserLabel = boundUserLabel + } } return nil }) diff --git a/server/internal/logic/sys/im_user.go b/server/internal/logic/sys/im_user.go index e43b70c..293142c 100644 --- a/server/internal/logic/sys/im_user.go +++ b/server/internal/logic/sys/im_user.go @@ -206,7 +206,12 @@ func (s *sSysImUser) List(ctx context.Context, in *sysin.ImUserListInp) (list [] values.Set("user_ids", strings.Join(userIds, ",")) } if in.UserId != "" { - values.Set("account_no", in.UserId) + // 长数字按用户ID(u.id)精确匹配,短数字按后台展示编号(account_no)匹配 + if len(in.UserId) >= 12 { + values.Set("user_id", in.UserId) + } else { + values.Set("account_no", in.UserId) + } } if in.Nickname != "" { values.Set("nickname", in.Nickname) diff --git a/server/internal/model/input/sysin/im_invite.go b/server/internal/model/input/sysin/im_invite.go index b7cb29f..c3150ce 100644 --- a/server/internal/model/input/sysin/im_invite.go +++ b/server/internal/model/input/sysin/im_invite.go @@ -76,6 +76,7 @@ type ImInviteBatchCreateInp struct { ExpiresAt *gtime.Time `json:"expiresAt" v:"required#到期时间不能为空" dc:"到期时间"` Remark string `json:"remark" dc:"备注"` IdempotencyKey string `json:"idempotencyKey" dc:"幂等键"` + UserId int64 `json:"userId" dc:"绑定C端用户ID,非必填,>0时为批次内所有邀请码绑定该用户"` } func (in *ImInviteBatchCreateInp) Filter(ctx context.Context) (err error) { @@ -92,6 +93,9 @@ func (in *ImInviteBatchCreateInp) Filter(ctx context.Context) (err error) { if len(in.IdempotencyKey) == 0 || len(in.IdempotencyKey) > 128 { return gerror.New("批量生成必须提供不超过128个字符的幂等键") } + if in.UserId < 0 { + return gerror.New("绑定用户ID不合法") + } return } diff --git a/server/internal/model/input/sysin/im_user.go b/server/internal/model/input/sysin/im_user.go index 2759726..d4249fb 100644 --- a/server/internal/model/input/sysin/im_user.go +++ b/server/internal/model/input/sysin/im_user.go @@ -18,7 +18,7 @@ import ( type ImUserListInp struct { form.PageReq TenantId int64 `json:"tenantId" dc:"租户ID"` - UserId string `json:"userId" dc:"后台展示编号"` + UserId string `json:"userId" dc:"用户ID(长数字)或后台展示编号"` GroupId int64 `json:"groupId" dc:"用户策略分组ID"` Keyword string `json:"keyword" dc:"用户名、昵称、手机号或编号"` Nickname string `json:"nickname" dc:"昵称"` diff --git a/web/src/views/im/registrationInvite/BindUserModal.vue b/web/src/views/im/registrationInvite/BindUserModal.vue index 7ddac28..9f8d661 100644 --- a/web/src/views/im/registrationInvite/BindUserModal.vue +++ b/web/src/views/im/registrationInvite/BindUserModal.vue @@ -132,7 +132,8 @@ } submitting.value = true; try { - await BindUser({ tenantId: tenantId.value, id: inviteId.value, userId: Number(selectedUserId.value) }); + // 雪花ID超出 JS Number 安全整数范围,保持字符串传输避免精度丢失 + await BindUser({ tenantId: tenantId.value, id: inviteId.value, userId: selectedUserId.value }); message.success('绑定成功'); showModal.value = false; emit('reload'); diff --git a/web/src/views/im/registrationInvite/CreateModal.vue b/web/src/views/im/registrationInvite/CreateModal.vue index 45708ec..5aac578 100644 --- a/web/src/views/im/registrationInvite/CreateModal.vue +++ b/web/src/views/im/registrationInvite/CreateModal.vue @@ -4,6 +4,23 @@ + +
+ + 搜索 +
+
+ + {{ boundUser.nickname || boundUser.username || boundUser.phone || '--' }}({{ boundUser.userId }}) + 清除 +
+
@@ -24,7 +41,9 @@ import { reactive, ref } from 'vue'; import { useMessage } from 'naive-ui'; import { BatchCreate, Create } from '@/api/im/registrationInvite'; + import { List as UserList } from '@/api/imUser'; import { localRfc3339 } from './model'; + import defaultAvatar from '@/assets/images/schoolboy.png'; const emit = defineEmits(['reload']); const message = useMessage(); @@ -33,11 +52,15 @@ const formRef = ref(); const tenantId = ref(0); const isBatch = ref(false); + const bindUserInput = ref(''); + const searching = ref(false); + const boundUser = ref(null); const formValue = reactive({ quantity: 100, expiresAt: Date.now() + 7 * 24 * 3600 * 1000, remark: '', idempotencyKey: '', + boundUserId: '', }); const rules = { @@ -52,6 +75,9 @@ formValue.expiresAt = Date.now() + 7 * 24 * 3600 * 1000; formValue.remark = ''; formValue.idempotencyKey = crypto.randomUUID(); + formValue.boundUserId = ''; + bindUserInput.value = ''; + boundUser.value = null; showModal.value = true; } @@ -59,17 +85,61 @@ showModal.value = false; } + async function searchUser() { + const keyword = bindUserInput.value.trim(); + if (!keyword) { + message.warning('请输入用户ID/编号'); + return; + } + searching.value = true; + try { + const res = await UserList({ + tenantId: tenantId.value, + userId: keyword, + keyword: '', + page: 1, + perPage: 1, + }); + const user = res?.list?.[0]; + if (!user) { + message.warning('未找到该用户,将不绑定用户'); + boundUser.value = null; + formValue.boundUserId = ''; + return; + } + boundUser.value = user; + // 雪花ID超出 JS Number 安全整数范围,必须保持字符串传输,避免精度丢失 + formValue.boundUserId = String(user.userId || ''); + message.success('已选中用户'); + } catch (error: any) { + message.error(error?.toString() || '用户查询失败'); + boundUser.value = null; + formValue.boundUserId = ''; + } finally { + searching.value = false; + } + } + + function clearBoundUser() { + boundUser.value = null; + formValue.boundUserId = ''; + bindUserInput.value = ''; + } + async function submit() { await formRef.value?.validate(); loading.value = true; try { - const params = { + const params: any = { tenantId: tenantId.value, expiresAt: localRfc3339(formValue.expiresAt), remark: formValue.remark, idempotencyKey: formValue.idempotencyKey, }; if (isBatch.value) { + if (formValue.boundUserId) { + params.userId = formValue.boundUserId; + } await BatchCreate({ ...params, quantity: formValue.quantity }); } else { await Create(params); @@ -87,3 +157,22 @@ defineExpose({ openModal }); + + diff --git a/web/src/views/imSecurityPolicy/index.vue b/web/src/views/imSecurityPolicy/index.vue index ed89b1b..8bcc47c 100644 --- a/web/src/views/imSecurityPolicy/index.vue +++ b/web/src/views/imSecurityPolicy/index.vue @@ -515,6 +515,8 @@ windowMinutes: lockoutForm.value.windowMinutes, }); message.success('已保存,配置即时生效'); + policyVisible.value = false; + reloadAll(); } catch (e: any) { message.error(e.message || '保存失败'); } finally {