在uniapp中实现长按聊天对话框可以弹出对话框然后可以删除该条对话,单击可以进入该条对话框的对话页面

效果展示

效果描述

长按【大于1s】某一条对话框会弹出一个对话框,点击确定按钮就可以将当前对话框从列表中进行删除,如果点击取消则不做额外操作。

如果只是点击了一下,时间【小于1s】的情况下会直接引入到与该用户的对话框详情页面。

代码实现

渲染层代码

html 复制代码
      <view v-for="(item,index) in dialogList" :key="index" class="m-item" @touchstart="touchstart(item)"
        @touchend="touchend(item)">
        <view class="mi-left">
          <image :src="userId==item.msgSendId? item.msgRevHeadImg:item.msgSendHeadImg" mode="aspectFill"></image>
          <view class="mil-num" v-if="item.readNum!=0">{{item.readNum>99?'99+':item.readNum}}</view>
        </view>
        <view class="mi-right">
          <view class="mir-top">
            <text class="mirt-name danhang">{{item.msgSendName}}</text>
            <text style="color: rgba(16,16,16,0.7);">{{formatTime(item.createTime)}}</text>
          </view>
          <text class="danhang">{{formatType(item.textType,item)}}</text>
        </view>
      </view>

逻辑层代码

javascript 复制代码
  let seconds = ref(0); // 用于跟踪经过的秒数
  let timeoutId = null; // 存储 setTimeout 的 ID
  let hasShownDeleteDialog = false; // 标记是否已弹出删除对话框

  const touchstart = (item) => {
    seconds.value = 0; // 重置秒数
    hasShownDeleteDialog = false; // 重置标记
    timeoutId = setTimeout(() => {
      showDeleteDialog(item);
    }, 1000);
  };

  const touchend = (item) => {
    clearTimeout(timeoutId); // 清除 setTimeout
    if (!hasShownDeleteDialog) {
      handleDetail(item);
    }
    seconds.value = 0; // 重置秒数
  };

  const showDeleteDialog = (item) => {
    hasShownDeleteDialog = true; // 设置标记为已弹出删除对话框
    clearInterval(timeoutId); // 清除 setTimeout
    uni.showModal({
      title: `删除对话框`,
      content: `你确定要删除与【${item.msgSendName}】的对话框吗,删除后将无法恢复!!!`,
      success: function(res) {
        if (res.confirm) {
          console.log('用户点击确定', item);
          http.post(api.removeChatRecord, { revUserId: item.msgRevId, sendUserId: item.msgSendId }).then(
            res => {
              getDialogList();
              uni.showToast({
                icon: "none",
                title: "删除成功"
              })
            }
          );
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }
    });
  };
相关推荐
码间留白3 天前
六轮真机实验后,我把鸿蒙直播推流的 UI 全逼成了原生——uni-app 适配 HarmonyOS NEXT 完整复盘
uni-app·鸿蒙·harmonyos next·直播推流·arkts/arkui
wuxianda10304 天前
2026 苹果审核 4.3 (a) 被拒全解析|Guideline 4.3a spam 马甲包拒审原因 + 避坑方案
flutter·uni-app·苹果上架
头发还在的女程序员6 天前
基于 Spring Boot + Vue 3 + uni-app 的灵活用工系统:四端架构与业务流程设计
vue.js·spring boot·uni-app·灵活用工·灵工系统
Dumbness8 天前
订阅消息弹窗必须在用户点击里调:联调实验室里 timing 和 Mock 支付通知踩的坑
javascript·uni-app
码间留白9 天前
uni-app 鸿蒙离线打包:一个改动从源码到真机要穿过几条链
uni-app·鸿蒙·deveco·离线打包·uts插件
多客:duokelijie9 天前
代练订单系统选型思路:跨品类游戏接入、订单分层管理、自动结算逻辑拆解!
数据库·mysql·微信小程序·前端框架·uni-app·php·持续部署
码间留白10 天前
鸿蒙端对接 BLE 血压计:从一次握手到读数入库(二)设备篇
物联网·uni-app·鸿蒙·蓝牙ble·血压计
华玥作者11 天前
Vue 3 + TypeScript + uni-app 组件库推荐:hy-app(华玥组件库)
vue.js·typescript·uni-app
FinelyYang11 天前
uniapp使用kkfileview在线预览ppt,excel,word,pdf类型文件
pdf·uni-app·powerpoint
2501_9160088911 天前
iPhone 设备信息查看,用 KeyMob 看型号、序列号、电池健康和 UDID
android·ios·小程序·https·uni-app·iphone·webview