收藏
回答

wx.getFileSystemManager在鸿蒙系统上,经常会出现1300202错误?

const baseFilePath = `${wx.env.USER_DATA_PATH}/testStorge.json`;
const storgSetFile = function (params) {
  var fs = wx.getFileSystemManager();
  const txtSize = getTxtSize(JSON.stringify(params));
    //首先获取需要写入的数据内容大小。ps:基本上数据内容都很小,不会超过100k,都是json格式数据
  console.log('需要写入的文件大小:',txtSize)
  return new Promise(function(resolve, reject) {
    fs.access({
      path: baseFilePath,
      success(res) {
        console.log('查询缓存文件成功--------')
                //正常流程,忽略
        ...
      },
      fail(res){
        console.log('查询缓存文件失败--------')
        console.log('查询缓存文件错认信息',res)
        console.log('错认类型码',res.errno)
        ensureDirectoryForFile(baseFilePath).then(() => {
          createNewFile(baseFilePath, params,resolve);
        }).catch(err => {
          resolve({errCode: -1, msg: 'error', data: err})
        });
      },
    })
  })
}
const getTxtSize = function(jsonString) {
  const charCount = jsonString.length;
  const encoder = new TextEncoder();
  const utf8Size = encoder.encode(jsonString).length;
  return formatBytes(utf8Size);
}
function ensureDirectoryForFile(filePath) {
  return new Promise((resolve, reject) => {
    console.log('ensureDirectoryForFile 被调用,filePath:', filePath);
    const fs = wx.getFileSystemManager();
    // 更安全的路径提取逻辑
    let dirPath;
    try {
      // 确保 filePath 是字符串
      if (typeof filePath !== 'string') {
        console.error('filePath 不是字符串:', filePath);
        reject(new Error('filePath 必须是字符串'));
        return;
      }
      // 从完整文件路径中提取目录路径
      const lastSlashIndex = filePath.lastIndexOf('/');
      if (lastSlashIndex !== -1) {
        dirPath = filePath.substring(0, lastSlashIndex);
      } else {
        // 如果没有斜杠,使用默认的用户数据路径
        dirPath = wx.env.USER_DATA_PATH;
      }
      // 确保 dirPath 不为空
      if (!dirPath) {
        dirPath = wx.env.USER_DATA_PATH;
      }
    } catch (error) {
      console.error('提取目录路径时出错:', error);
      dirPath = wx.env.USER_DATA_PATH;
    }
    // 最终验证和备用方案
    if (!dirPath || typeof dirPath !== 'string') {
      console.error('最终 dirPath 无效,使用默认路径');
      dirPath = wx.env.USER_DATA_PATH;
    }
    console.log('最终使用的目录路径:', dirPath);
    
    // 检查目录是否已经存在
    fs.access({
      path: dirPath,
      success: () => {
        console.log('目录已存在:', dirPath);
        resolve();
      },
      fail: () => {
        // 目录不存在,需要创建
        fs.mkdir({
          dirPath: dirPath, // 确保使用正确的参数名
          recursive: true,
          success: () => {
            console.log('目录创建成功:', dirPath);
            resolve();
          },
          fail: (err) => {
            console.error('创建目录失败,详细错误:', JSON.stringify(err));
            if (err.errMsg && err.errMsg.includes('already exists')) {
              console.log('目录已存在(通过失败信息检测):', dirPath);
              resolve();
            } else {
              console.error('创建目录失败:', err);
              // 如果目录创建失败,尝试直接写入文件
              console.log('尝试直接进行文件操作,忽略目录创建错误');
              resolve();
            }
          }
        });
      }
    });
  });
}

function createNewFile(filePath, params, resolve) {
  const fs = wx.getFileSystemManager();
  var main = {...params};
  fs.writeFile({
    filePath: filePath,
    data: JSON.stringify(main),
    encoding: 'utf8',
    success(res) {
      resolve(res);
    },
    fail(res) {
      console.log('查询文件失败后,尝试写入新文件,写入缓存文件失败--------');
      console.log('写入失败错误内容', JSON.stringify(res));
      handleWriteFileError(res, resolve);
    }
  });
}
// 新增辅助函数:统一处理写入文件错误
function handleWriteFileError(res, resolve) {
  if (res.errno == '1300202') {
    console.log('准备获取微信储存空间大小');
    wx.getStorageInfo({
      success: (succ) => {
        console.log('获取微信储存空间成功-------');
        console.log(JSON.stringify(succ));
        const remainingSpace = succ.limitSize - succ.currentSize;
        console.log(`剩余存储空间: ${remainingSpace}KB`);
        wx.showToast({
          title: `当前微信存储空间大小为${remainingSpace/1024}MB,请先清理微信缓存`,
          icon: 'none',
          mask: true
        });
        resolve({errCode: -1, msg: 'error', data: res});
      },
      fail(err) {
        wx.showToast({
          title: '存储空间不足,或文件大小超出上限',
          icon: 'none',
          mask: true
        });
        console.log(`获取存储空间失败`, JSON.stringify(err));
        resolve({errCode: -1, msg: 'error', data: res});
      }
    });
  } else {
    resolve({errCode: -1, msg: 'error', data: res});
  }
}



前面为封装的wx.getFileSystemManager组件内容

截图为用户鸿蒙系统下,运行到文件缓存组件时,打印的日志信息。可以看出,在“fs.writeFile()”写入新文件时,会返回1300202

错误码,此时获取微信储存空间大小,得到接近10M的返回值,那么问题就来了,为什么在空间足够的情况下,写入新的缓存文件会提示空间不足的错误?这SB问题老是在鸿蒙系统上反反复复的出现,其他手机系统上倒是没反馈。不是,鸿蒙这系统怎么这么多问题啊,有没有靠谱的来把这个问题解决啦,客户经常遇到这个问题(目前我们给用户的解决方案是:先手动清除微信文件缓存,再重启小程序),每次这么搞真的是烦不胜烦



最后一次编辑于  11-21
回答关注问题邀请回答
收藏

2 个回答

  • null
    null
    11-21

    这问题经常在鸿蒙系统上出现,之前在红米手机上出现过一次。真的是无力吐槽,明明写入的内容并不大,小到几十b,最大几十kb,总会提示1300202,但是获取微信的存储空间又是充足的,剩余接近10M,这不是逻辑死循环了吗

    11-21
    有用
    回复 1
    • null
      null
      11-21
      目前做了适配调整:如果终端操作系统是鸿蒙系统,不用本地缓存文件流程,切换回getStorage/setStorage方法来维护本地缓存。
      11-21
      回复
  • 智能回答 智能回答 本次回答由AI生成
    11-21
    有用
登录 后发表内容