在uni-app中使用jweixin-module插件

uni-app 中,jweixin-module 插件主要用于实现与微信相关的功能,通常用于微信公众号或小程序开发中,帮助调用微信的 SDK 实现一些功能,如分享、支付、获取微信用户信息等。

使用场景

  1. 微信支付:可以集成微信支付功能,支持发起支付请求。
  2. 微信分享:可以实现分享到朋友圈、微信群、微信好友等。
  3. 获取微信用户信息:可以获取到用户的头像、昵称等信息。
  4. 调起微信扫一扫功能:实现扫码功能,用于二维码识别。
  5. 调起微信小程序:在 H5 页面中跳转到微信小程序。
  6. 支付回调与支付状态:处理支付的回调以及支付状态。

安装 jweixin-module

你需要先通过 npm 安装该插件:

npm install jweixin-module --save

然后通过 import 引入并初始化:

import wx from 'jweixin-module';

API功能及常见使用

1. 初始化配置

在使用 jweixin-module 之前,需要进行初始化配置,设置微信 JSSDK 的相关参数(如签名信息):

wx.config({
  debug: true,  // 开启调试模式,调用某些接口时会弹出调试信息
  appId: 'your-app-id',  // 必填,公众号的唯一标识
  timestamp: 123456789,  // 必填,生成签名的时间戳
  nonceStr: 'nonce-str',  // 必填,生成签名的随机串
  signature: 'signature',  // 必填,签名,见附录1
  jsApiList: ['chooseImage', 'scanQRCode', 'onMenuShareAppMessage', 'onMenuShareTimeline'] // 必填,需要使用的JS接口列表
});
2. 微信分享功能
  • 分享给朋友

    wx.onMenuShareAppMessage({
      title: '分享标题', // 分享标题
      desc: '分享描述',  // 分享描述
      link: 'https://www.example.com', // 分享链接
      imgUrl: 'https://www.example.com/img.jpg', // 分享图标
      success: function() {
        console.log('分享成功');
      },
      cancel: function() {
        console.log('取消分享');
      }
    });
    
  • 分享至朋友圈

    wx.onMenuShareTimeline({
      title: '朋友圈标题', // 分享标题
      link: 'https://www.example.com', // 分享链接
      imgUrl: 'https://www.example.com/img.jpg', // 分享图标
      success: function() {
        console.log('分享成功');
      },
      cancel: function() {
        console.log('取消分享');
      }
    });
    
  • 多图选择并分享
     // 选择图片
     wx.chooseImage({
      count: 9,  // 可以选择的最大图片数量
      sizeType: ['original', 'compressed'],  // 可以选择原图或者压缩图
      sourceType: ['album', 'camera'],  // 可以选择相册或者拍照
      success: (res) => {
        const images = res.localIds;  // 获取选中的图片ID
    
        // 分享选中的图片
        wx.onMenuShareAppMessage({
          title: '分享图片',
          desc: '分享多张图片',
          link: 'https://www.example.com',
          imgUrl: images[0],  // 这里只用第一张图片作为分享图标
          success() {
            console.log('分享成功');
          },
          cancel() {
            console.log('分享取消');
          }
        });
      }
    });
3. 微信支付功能

微信支付需要服务器生成支付参数并传递给前端进行支付请求。前端使用 wx.chooseWXPay 发起支付:

wx.chooseWXPay({
  timestamp: 123456789,  // 支付签名时间戳
  nonceStr: 'nonce-str',  // 支付签名随机串
  package: 'prepay_id=123456',  // 预支付交易会话ID
  signType: 'MD5',  // 签名方式
  paySign: 'pay-sign',  // 支付签名
  success: function(res) {
    console.log('支付成功', res);
  },
  fail: function(res) {
    console.log('支付失败', res);
  }
});
4. 获取用户信息

通过 wx.getUserInfo 获取微信用户的基本信息:

wx.getUserInfo({
  success: function(res) {
    console.log('用户信息:', res.userInfo);  // 包含昵称、头像等信息
  },
  fail: function(res) {
    console.log('获取用户信息失败', res);
  }
});
5. 扫一扫功能

调起微信扫一扫,扫码后可获取二维码数据:

wx.scanQRCode({
  needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果
  scanType: ['qrCode', 'barCode'], // 可以指定扫二维码或条形码
  success: function(res) {
    console.log('扫码结果:', res.resultStr);  // 返回扫描结果
  }
  fail: (res) => {
    console.error('扫码失败', res);
  }
});
6. 分享到QQ或其他应用

jweixin-module 除了支持微信分享外,还可以实现分享到其他平台,如 QQ:

wx.onMenuShareQQ({
  title: '分享标题',
  desc: '分享描述',
  link: 'https://www.example.com',
  imgUrl: 'https://www.example.com/img.jpg',
  success: function() {
    console.log('分享成功');
  },
  cancel: function() {
    console.log('取消分享');
  }
});

总结

jweixin-module 插件是一个集成微信JS SDK的工具,使得在 uni-app 中能够快速实现微信支付、分享、扫一扫等微信功能。通过配置相关的签名参数并调用相应的 API,开发者可以轻松实现与微信相关的交互。需要注意的是,很多功能(如支付、获取用户信息)需要依赖微信的开放平台或公众号的设置,因此在使用时需要确保公众号或小程序已进行相关设置。

//微信充值 //支付接口测试 function balance(url, data) { uni.request({ url: cfg.originUrl + '/wx/mp/js_sig.do', data: { route: url }, method: 'GET', success: (res) => { jweixin.config({ debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来 appId: res.data.appId, // 必填,公众号的唯一标识 timestamp: res.data.timestamp, // 必填,生成签名的时间戳 nonceStr: res.data.nonceStr, // 必填,生成签名的随机串 signature: res.data.signature, // 必填,签名 jsApiList: ['chooseWXPay'] // 必填,需要使用的JS接口列表 }); jweixin.ready(function() { uni.request({ url: cfg.originUrl + '/wx/recharge/pay.do', method: 'POST', header: { 'Content-type': "application/x-www-form-urlencoded", }, data: JSON.stringify(data), success: function(res) { alert("下单成功"); alert(JSON.stringify(res)); alert(res.data.order_id); all.globalData.orderId = res.data.order_id; uni.setStorageSync('orderId', res.data.order_id); jweixin.chooseWXPay({ timestamp: res.data.payParams.timeStamp, // 支付签名时间戳 nonceStr: res.data.payParams.nonceStr, // 支付签名随机串 package: res.data.payParams.package, // 接口返回的prepay_id参数 signType: res.data.payParams.signType, // 签名方式 paySign: res.data.payParams.paySign, // 支付签名 success: function(e) { alert("支付成功"); alert(JSON.stringify(e)); // 支付成功后的回调函数 } }); } }) }); jweixin.error(function(res) { // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。 console.log("验证失败!") }); } }) }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奶凶的小熊崽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值