收藏
回答

物流组件真的可以用吗?为什么调用的时候总是出现 "invalid url"?

这个云函数调用的时候一直报错:

云函数返回结果: {errMsg: "cloud.callFunction:ok", result: {…}, requestID: "7cd07d46-2454-4024-934f-6a37a34b364b"}errMsg: "cloud.callFunction:ok"requestID: "7cd07d46-2454-4024-934f-6a37a34b364b"result: {success: false, error: "invalid url rid: 67f8b07c-55bea4f6-516feb3b", errorCode: 40066}__proto__: Object
index.js? [sm]:755 获取 waybillToken 失败: {success: false, error: "invalid url rid: 67f8b07c-55bea4f6-516feb3b", errorCode: 40066}(env: Windows,mp,1.06.2503281; lib: 3.7.12)
(anonymous) @ index.js? [sm]:755
Promise.then (async)
getWaybillToken @ index.js? [sm]:728
(anonymous) @ index.js? [sm]:613
Promise.then (async)
checkLogistics @ index.js? [sm]:564
index.js? [sm]:761 错误信息: invalid url rid: 67f8b07c-55bea4f6-516feb3b(env: Windows,mp,1.06.2503281; lib: 3.7.12)

下面是云函数代码:
// 云函数 getwaybilltoken/index.js
const cloud = require('wx-server-sdk');
cloud.init({
  env'xxxxxxxxxxxxxxxx' 
});


// 引入请求库
const rp = require('request-promise');


exports.main = async (event, context) => {
  console.log('接收到的参数:', event);
  
  try {
    // 获取access_token
    const tokenRes = await cloud.callFunction({
      name'getaccesstoken'
    });
    
    const accessToken = tokenRes.result.accessToken;
    console.log('获取到的accessToken:', accessToken);
    
    if (!accessToken) {
      console.log('未获取到有效的accessToken');
      return {
        successfalse,
        error'Failed to get access_token'
      };
    }
    
    // 获取当前用户的openid
    const openid = event.openid || cloud.getWXContext().OPENID;
    console.log('使用的openid:', openid);
    
    // 处理商品信息,确保图片URL有效
    let detailList = [];
    const goods = event.goods;
    const defaultImgUrl = "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg"// 微信官方示例图片
    
    // 准备商品数据
    if (goods && Array.isArray(goods) && goods.length > 0) {
      // 临时存储需要转换的云文件ID
      const cloudFileIDs = [];
      const goodsItemsMap = {}; // 用于关联云文件ID和商品项
      
      goods.forEach((item, index) => {
        const imgUrl = item.image_url || item.image || '';
        
        // 检查是否是云存储路径
        if (imgUrl && imgUrl.startsWith('cloud://')) {
          cloudFileIDs.push(imgUrl);
          goodsItemsMap[imgUrl] = index;
        }
      });
      
      // 如果有云存储路径,先转换为临时URL
      if (cloudFileIDs.length > 0) {
        try {
          console.log('开始转换云文件URL,文件数:', cloudFileIDs.length);
          const tempFileResult = await cloud.getTempFileURL({
            fileList: cloudFileIDs
          });
          
          console.log('获取临时文件URL结果:', tempFileResult);
          
          // 处理临时URL结果
          if (tempFileResult && tempFileResult.fileList) {
            tempFileResult.fileList.forEach(fileItem => {
              if (fileItem.tempFileURL && goodsItemsMap[fileItem.fileID] !== undefined) {
                const index = goodsItemsMap[fileItem.fileID];
                goods[index].convertedImageUrl = fileItem.tempFileURL;
              }
            });
          }
        } catch (fileErr) {
          console.error('获取临时文件URL失败:', fileErr);
        }
      }
      
      // 构建最终的商品详情列表
      detailList = goods.map(item => {
        // 优先使用转换后的URL,然后是原始URL,最后是默认URL
        let finalImgUrl = item.convertedImageUrl || item.image_url || item.image || '';
        
        // 如果URL仍然不是http/https开头,使用默认URL
        if (!finalImgUrl || !finalImgUrl.startsWith('http')) {
          finalImgUrl = defaultImgUrl;
        }
        
        return {
          goods_name: item.name || '商品',  
          goods_img_url: finalImgUrl      
        };
      });
    }
    
    // 如果没有有效的商品信息,使用默认
    if (detailList.length === 0) {
      detailList = [{
        goods_name: item.name || '商品',  
        goods_img_url: finalImgUrl
      }];
    }
    
    // 构建请求参数
    const waybillId = event.waybillId;
    const deliveryId = event.deliveryId;
    const receiverPhone = event.receiverPhone || '';
    const transId = event.transId || '';
    
    console.log('构建请求参数:', {
      waybillId,
      deliveryId,
      receiverPhone,
      transId,
      detailList: detailList
    });
    
    // 构建请求体
    const requestData = {
      waybill_id: waybillId,
      delivery_id: deliveryId,
      openid: openid,
      version1,
      goods_info: detailList,
      receiver_phone: receiverPhone || '',
      trans_id: transId || ''
    };
    
    console.log('完整请求数据:'JSON.stringify(requestData));
    
    // 调用微信物流API
    const result = await rp({
      method'POST',
      url`https://api.weixin.qq.com/cgi-bin/express/business/logistics/getpath?access_token=${accessToken}`,
      body: requestData,
      jsontrue
    });


    console.log('物流API返回结果:', result);
    
    // 处理API响应
    if (result.errcode === 0) {
      return {
        successtrue,
        waybillToken: result.waybill_token
      };
    } else {
      console.error('物流API返回错误:', result);
      return {
        successfalse,
        error: result.errmsg || '获取物流路径失败',
        errorCode: result.errcode
      };
    }
  } catch (error) {
    console.error('云函数执行异常:', error);
    return {
      successfalse,
      error: error.message || '云函数执行异常'
    };

  }};


回答关注问题邀请回答
收藏

1 个回答

  • Summer✨
    Summer✨
    05-20

    你好,建议按照提示检查url是否正确

    05-20
    有用
    回复
登录 后发表内容