在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('用户点击取消');
        }
      }
    });
  };
相关推荐
HashTang10 小时前
【AI 编程实战】第 11 篇:让小程序飞起来 - 性能优化实战指南
前端·uni-app·ai编程
lruri17 小时前
记录一个修复nvue文件在vscode里面提示ts-plugin报错
uni-app
蓝帆傲亦17 小时前
Web前端Mock数据实战指南:正确使用Mock.js提升开发效率
微信小程序·小程序·uni-app
00后程序员张17 小时前
iOS 应用代码混淆,对已编译 IPA 进行类与方法混淆
android·ios·小程序·https·uni-app·iphone·webview
木子啊1 天前
Uni-app社会化功能:登录支付分享全攻略
uni-app
笨笨狗吞噬者1 天前
【2025】加入 uniapp 的一年
前端·uni-app
2501_915918412 天前
mobileprovision 描述文件内容查看,查看 Bundle ID、证书指纹等来定位安装与签名问题
android·ios·小程序·https·uni-app·iphone·webview
2501_915918412 天前
HTTPS 端口解析,除了 443 端口还有哪些
android·ios·小程序·https·uni-app·iphone·webview
三十_2 天前
uniApp WebView 动态配置加载状态监控与容错方案
前端·uni-app
2501_916007472 天前
在 Windows 环境下抓取 iPhone 网络流量的操作流程
android·ios·小程序·https·uni-app·iphone·webview