gaoluyang
6 天以前 79fe66bef2cfc5460fc19599810e17224adaee6f
src/hooks/useDelete.js
@@ -7,8 +7,8 @@
/**
 * 创建删除功能
 * @param {Object} options 配置选项
 * @param {Function} options.deleteApi 删除API函数
 * @param {Function} options.getList 重新获取列表数据的函数
 * @param {Function|Function} options.deleteApi 删除API函数或返回API函数的函数
 * @param {Function|Function} options.getList 重新获取列表数据的函数或返回函数的函数
 * @param {Ref} options.selectedRows 选中行的响应式引用
 * @param {Ref} options.tableData 表格数据的响应式引用
 * @param {Ref} options.total 总数的响应式引用
@@ -28,6 +28,41 @@
    successText = "删除成功",
    useLocalUpdate = false
  } = options;
  /**
   * 获取实际的删除API函数
   * 支持直接传入函数或返回函数的函数
   */
  const getDeleteApi = () => {
    if (typeof deleteApi === 'function') {
      // 尝试调用看是否返回函数
      try {
        const result = deleteApi();
        return typeof result === 'function' ? result : deleteApi;
      } catch (error) {
        // 如果调用出错,说明这本身就是API函数
        return deleteApi;
      }
    }
    return deleteApi;
  };
  /**
   * 获取实际的获取列表函数
   * 支持直接传入函数或返回函数的函数
   */
  const getListFunction = () => {
    if (typeof getList === 'function') {
      try {
        const result = getList();
        return typeof result === 'function' ? result : getList;
      } catch (error) {
        // 如果调用出错,说明这本身就是列表函数
        return getList;
      }
    }
    return getList;
  };
  /**
   * 批量删除方法
@@ -55,9 +90,16 @@
      // 提取ID
      const ids = rowsToDelete.map(item => item.id);
      // 获取当前的删除API函数
      const currentDeleteApi = getDeleteApi();
      if (!currentDeleteApi) {
        ElMessage.error("删除API未配置");
        return false;
      }
      // 调用删除API
      const res = await deleteApi(ids);
      const res = await currentDeleteApi(ids);
      if (res.code === 200) {
        // 根据配置选择更新方式
@@ -69,8 +111,9 @@
          }
        } else {
          // 重新获取数据
          if (getList) {
            await getList();
          const currentGetList = getListFunction();
          if (currentGetList) {
            await currentGetList();
          }
        }