refactor(消息面板): 重构消息撤回逻辑,提取公共函数
将消息撤回的条件判断逻辑提取为独立函数 canAddRevokeOption 简化主逻辑代码,提高可读性和可维护性
This commit is contained in:
parent
8e645226b8
commit
28938aba66
@ -10,15 +10,34 @@ interface IDropdown {
|
||||
item: any
|
||||
}
|
||||
|
||||
const isRevoke = (uid: any, item: any): boolean => {
|
||||
if (uid != item.user_id) {
|
||||
return false
|
||||
const isRevoke = (uid: number, item: any): boolean => {
|
||||
// 不是自己发的消息不能撤回
|
||||
if (uid !== item.user_id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const messageTime = dayjs(item.created_at)
|
||||
const now = dayjs()
|
||||
const diffInMinutes = now.diff(messageTime, 'minute')
|
||||
return diffInMinutes <= 5
|
||||
|
||||
// 检查消息是否在撤回时间限制内(5分钟)
|
||||
const messageTime = dayjs(item.created_at);
|
||||
const now = dayjs();
|
||||
const diffInMinutes = now.diff(messageTime, 'minute');
|
||||
return diffInMinutes <= 5;
|
||||
}
|
||||
// 判断是否可以添加撤回选项的函数
|
||||
const canAddRevokeOption = (uid: number, item: any, isManager: boolean): boolean => {
|
||||
// 单聊情况:自己发的且在时间限制内
|
||||
if (item.talk_type === 1) {
|
||||
return isRevoke(uid, item) && item.float === 'right';
|
||||
}
|
||||
// 群聊情况
|
||||
else if (item.talk_type === 2) {
|
||||
// 管理员可以撤回任何消息
|
||||
if (isManager) {
|
||||
return true;
|
||||
}
|
||||
// 普通成员只能撤回自己的且在时间限制内的消息
|
||||
return isRevoke(uid, item) && item.float === 'right';
|
||||
}
|
||||
return false;
|
||||
}
|
||||
const dialogueStore = useDialogueStore()
|
||||
export function useMenu() {
|
||||
@ -49,23 +68,8 @@ export function useMenu() {
|
||||
|
||||
dropdown.options.push({ label: '多选', key: 'multiSelect' })
|
||||
dropdown.options.push({ label: '引用', key: 'quote' })
|
||||
//如果是单聊
|
||||
if(item.talk_type===1){
|
||||
//撤回时间限制内,并且是自己发的
|
||||
if(isRevoke(uid, item)&&item.float==='right'){
|
||||
dropdown.options.push({ label: `撤回`, key: 'revoke' })
|
||||
}
|
||||
//群聊
|
||||
}else if(item.talk_type===2){
|
||||
//管理员可以强制撤回所有成员信息
|
||||
if ((dialogueStore.groupInfo as any).is_manager) {
|
||||
dropdown.options.push({ label: `撤回`, key: 'revoke' })
|
||||
}else{
|
||||
//撤回时间限制内,并且是自己发的
|
||||
if(isRevoke(uid, item)&&item.float==='right'){
|
||||
dropdown.options.push({ label: `撤回`, key:'revoke' })
|
||||
}
|
||||
}
|
||||
if (canAddRevokeOption(uid, item, (dialogueStore.groupInfo as any).is_manager)) {
|
||||
dropdown.options.push({ label: '撤回', key: 'revoke' });
|
||||
}
|
||||
|
||||
dropdown.options.push({ label: '删除', key: 'delete' })
|
||||
|
Loading…
Reference in New Issue
Block a user