在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('用户点击取消');
        }
      }
    });
  };
相关推荐
xiangxiongfly9155 小时前
uni-app 组件总结
前端·javascript·uni-app
2501_915918415 小时前
iOS性能数据监控:从概念到工具实践,让应用运行更流畅
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_915909061 天前
全面解析前端开发中常用的浏览器调试工具及其使用场景
android·ios·小程序·https·uni-app·iphone·webview
wuxianda10301 天前
uniapp项目上架苹果商店4.3a被拒,3天极速解决方案2026.5.8
前端·人工智能·flutter·uni-app·ios上架·苹果上架·苹果4.3a
小盼江1 天前
Uniapp小程序鲜花商城推荐系统 买家卖家双端(web+uniapp)
前端·小程序·uni-app
fakaifa1 天前
【最新版】CRMEB Pro版v4.0系统源码 全开源+uniapp/PC前端+搭建教程
uni-app·开源·商城小程序·crmeb·crmebpro
小徐_23332 天前
Wot UI v1 升级 v2?这份迁移指南帮你少踩坑!
前端·微信小程序·uni-app
游戏开发爱好者82 天前
使用Fiddler设置HTTPS抓包诊断Power Query网络问题
android·ios·小程序·https·uni-app·iphone·webview
棋宣2 天前
uni-app编译到微信小程序中,父传子props首次传递数据不接收的bug
微信小程序·uni-app·bug
阳光先做3 天前
uniapp打包鸿蒙安装包问题
uni-app