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