Taro,前端如何对接微信物流查询插件

本文介绍了如何在Taro小程序中配置微信物流查询组件,包括在app.config.ts中的配置,处理因插件导致的打包体积问题,以及在项目中使用和遇到的常见错误及解决方法。

taro小程序开发 对接微信物流查询组件(一切的基础在于 后台配置了这个插件)

一、在app.config.ts中进行配置

由于插件的使用可能导致打包体积过大,采取分包加载的形式,将插件写入要使用的包中

export default defineAppConfig({
  pages: [
    'pages/home/default/index', 
    'pages/tab1/default/index', 
    'pages/tab2/default/index', 
    'pages/tab3/index', 
  ],
  subpackages: [
    {
      root: 'packageA',
      name: 'packageA',
      pages: [
        'project/index', 
        'project/video-play', 
      ],
    },
    {
      root: 'packageB',
      name: 'packageB',
      pages: [
        'address/index', 
        'address/addAddress/index', 
      ],
      // 添加插件需要授权使用
      plugins: {
        logisticsPlugin: {
          version: '2.2.19',
          provider: 'wx9ad912bf20548d92',
        },
      },
    },
  ],
  window: {
    backgroundTextStyle: 'light',
    navigationBarBackgroundColor: '#fff',
    navigationBarTitleText: 'xxxxxxxxx',
    navigationBarTextStyle: 'black',
  },
  tabBar: {
    position: 'bottom',
    selectedColor: '#44CBB4',
    borderStyle: 'black',
    list: [
      {
        pagePath: 'picUrl',
        text: '首页',
        iconPath: 'picUrl',
        selectedIconPath: 'picUrl',
      },
      {
        pagePath: 'picUrl',
        text: 'tab2',
        iconPath: 'picUrl',
        selectedIconPath: 'picUrl',
      },
      {
        pagePath: 'picUrl',
        text: 'tab3',
        iconPath: 'picUrl',
        selectedIconPath: 'picUrl',
      },
      {
        pagePath: 'picUrl',
        text: 'tab4',
        iconPath: 'picUrl',
        selectedIconPath: 'picUrl',
      },
    ],
  },

});

二、常见错误

在app.config.ts注册插件之后,再次运行yarn dev:weapp运行项目可能会遇到一些错误

TypeError: Function.prototype.apply was called on WeakMap.prototype.get, which is a string and not a function

解决这个错误 安装qs指定版本即可

yarn add qs@6.9.6  / npm install qs@6.9.6  安装qs指定版本依赖包

三、使用

  import Taro, { requirePlugin } from '@tarojs/taro';
    const plugin = requirePlugin('logisticsPlugin');

  //此接口由后端提供
  const goLogistics = async (id) => {
    const { data } = await getDeliver({
      id: id, //订单id
      appid: '传入自己的appid',
    });
    const { waybill_token } = JSON.parse(data);
    // 物流插件
    plugin.openWaybillTracking({
      waybillToken: waybill_token,
    });
  };

ps: 物流查询插件只能查询通过微信支付的订单

### Taro 框架中调用微信支付 API 的方法 在 Taro 框架中调用微信支付 API,可以通过 `Taro.requestPayment` 方法实现。以下是详细的说明和代码示例: #### 1. 调用微信支付的基本方法 Taro 提供了 `Taro.requestPayment` 方法来发起微信支付请求[^1]。此方法需要传递一组参数,包括时间戳、随机字符串、签名类型、签名值等。以下是一个基本的代码示例: ```javascript Taro.requestPayment({ timeStamp: '1637245678', // 时间戳 nonceStr: 'randomString', // 随机字符串 package: 'prepay_id=xxx', // 统一下单接口返回的 prepay_id 参数值 signType: 'MD5', // 签名类型,默认为 MD5 paySign: 'signature', // 支付签名 success: function (res) { console.log('支付成功:', res); }, fail: function (err) { console.error('支付失败:', err); } }); ``` #### 2. 获取支付参数 在调用 `Taro.requestPayment` 之前,需要通过微信支付的统一下单接口获取支付所需的参数。以下是后端生成支付参数的伪代码示例(以 Node.js 为例): ```javascript const crypto = require('crypto'); function generatePayParams(orderId, amount) { const params = { appid: 'yourAppId', // 微信分配的小程序ID mch_id: 'yourMchId', // 商户号 nonce_str: 'randomString', // 随机字符串 body: '商品描述', // 商品描述 out_trade_no: orderId, // 商户订单号 total_fee: amount, // 订单金额,单位为分 spbill_create_ip: '127.0.0.1', // 客户端IP notify_url: 'https://yourdomain.com/notify', // 接收微信支付异步通知回调地址 trade_type: 'JSAPI', // 交易类型 openid: 'userOpenId' // 用户标识 }; // 生成签名 const stringA = Object.keys(params) .sort() .map(key => `${key}=${params[key]}`) .join('&'); const stringSignTemp = `${stringA}&key=yourApiKey`; const sign = crypto.createHash('md5').update(stringSignTemp).digest('hex').toUpperCase(); params.sign = sign; return params; } ``` #### 3. 处理支付结果 支付成功或失败时,`Taro.requestPayment` 的回调函数会分别触发 `success` 和 `fail` 方法。可以根据返回的结果进行后续处理,例如更新订单状态或提示用户。 #### 4. 配置 Universal Links(可选) 如果项目需要支持 Universal Links,可以按照以下方式配置[^2]: - 在微信开放平台设置 Universal Links,例如 `https://aaa.github.io/`。 - 在 Xcode 中配置 Targets 的 Domains,添加 `applinks:aaa.github.io`。 - 在应用启动时注册 Universal Links,确保微信 SDK 正确识别链接。 #### 5. 使用优惠券插件(扩展功能) 如果需要结合优惠券功能,可以使用微信提供的优惠券插件[^3]。以下是配置和使用的简要步骤: - 在 `src/app.config.ts` 中添加插件配置: ```typescript { "plugins": { "sendCoupon": { "version": "1.1.5", "provider": "wxf3f436ba9bd4be7b" } }, usingComponents: { 'send-coupon': 'plugin://sendCoupon/send-coupon' } } ``` - 在组件中使用 `<send-coupon>` 插件: ```jsx <send-coupon send_coupon_params={params.send_coupon_params} sign={params.sign} send_coupon_merchant={params.send_coupon_merchant} onCustomevent={e => { const { errcode, msg, send_coupon_result: result } = e.detail; if (errcode === 0) { // 用户领取优惠券成功 getCoupon({ coupon_codes: result.coupon_code }).then(res => { console.log('优惠券领取成功:', res); }); } }} > <View class="text">领取</View> </send-coupon> ``` #### 注意事项 - 在调用支付接口前,确保已经完成微信支付商户号的申请和配置。 - 支付签名的生成逻辑必须在后端完成,避免将敏感信息暴露在前端。 - 如果支付失败,建议检查错误码并根据微信支付官方文档排查问题。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值