在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('用户点击取消');
        }
      }
    });
  };
相关推荐
小徐_23332 天前
uni-app vue3 也能使用 Echarts?Wot Starter 是这样做的!
前端·uni-app·echarts
iOS阿玮2 天前
永远不要站在用户的对立面,挑战大众的公知。
uni-app·app·apple
xw52 天前
uni-app中v-if使用”异常”
前端·uni-app
!win !2 天前
uni-app中v-if使用”异常”
前端·uni-app
2501_915918412 天前
iOS 上架全流程指南 iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传 ipa 与审核实战经验分享
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张2 天前
iOS App 混淆与加固对比 源码混淆与ipa文件混淆的区别、iOS代码保护与应用安全场景最佳实践
android·安全·ios·小程序·uni-app·iphone·webview
00后程序员张3 天前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
海绵宝宝不喜欢侬3 天前
uniapp-微信小程序分享功能-onShareAppMessage
微信小程序·小程序·uni-app
2501_915106323 天前
Xcode 上传 ipa 全流程详解 App Store 上架流程、uni-app 生成 ipa 文件上传与审核指南
android·macos·ios·小程序·uni-app·iphone·xcode
xkxnq3 天前
Uniapp崩溃监控体系构建:内存泄漏三维定位法(堆栈/资源/线程)
uni-app