查看: 879|回复: 0

在Uniapp中开发微信公众号应用使用weixin-js-sdk的方法

[复制链接]

748

主题

39

回帖

3907

积分

管理员

总舵主

积分
3907
QQ
发表于 2025-1-1 18:27:50 | 显示全部楼层 |阅读模式
1、安装微信sdk,代码如下:

  1. npm install weixin-js-sdk --save
复制代码
2、在页面引入:
  1. import wx from "weixin-js-sdk";
复制代码
3、页面中初始化使用jsapi支付:
  1. wx.config({
  2.                                                         debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  3.                                                         appId: res.data.appId, // 必填,公众号的唯一标识
  4.                                                         timestamp: res.data.timeStamp, // 必填,生成签名的时间戳
  5.                                                         nonceStr: res.data.nonceStr, // 必填,生成签名的随机串
  6.                                                         signature: res.data.paySign, // 必填,签名
  7.                                                         jsApiList: ['chooseWXPay'] // 必填,需要使用的JS接口列表
  8.                                                 })
复制代码
3.1、如果是需要其他的接口,比如选择图片,可以使用如下代码:
  1. wx.config({
  2.     debug: false, // 开启调试模式,
  3.     appId: data.appId, //必填,企业号的唯一标识,此处企业号corpid
  4.     timestamp: data.timestamp, //必填,生成签名的时间戳
  5.     nonceStr: data.nonceStr, //必填,生成签名的随机串
  6.     signature: data.signature, //必填,签名,见附录1
  7.     jsApiList: ["chooseImage"], //必填,需要使用的JS接口列表JS接口列表见附录2
  8.     });
复制代码
config参数是通过后台获取的,其中wx.config来执行配置初始化,当debug为true时为调试模式,无论初始化是否成功都会弹窗提示类似:{"errMsg":"configk"},jsApiList是各种js接口,想起可查看微信官方jssdk1.6.0说明链接:
微信网页开发 / JS-SDK说明文档
代码参考示例:
  1. <!--
  2. * @Author: your name
  3. * @Date: 2021-01-06 10:18:31
  4. * @LastEditTime: 2021-01-18 19:23:18
  5. * @LastEditors: Please set LastEditors
  6. * @Description: In User Settings Edit
  7. * @FilePath: /my-demo/src/views/Home.vue
  8. -->
  9. <template>
  10.   <div class="home">
  11.     <img alt="Vue logo" src="../assets/logo.png" />
  12.     <h1 @click="selectImg">这是点击微信按钮</h1>
  13.     <HelloWorld msg="Welcome to Your Vue.js App" />
  14.   </div>
  15. </template>

  16. <script>
  17. // @ is an alias to /src

  18. import axios from "axios";
  19. import wx from "weixin-js-sdk";
  20. export default {
  21.   name: "Home",
  22.   components: {
  23.     'HelloWorld':()=> import("@/components/HelloWorld.vue")
  24.   },
  25.   methods: {
  26.     // 初始化
  27.     getConfig() {
  28.       axios({
  29.         method: "post",
  30.         url: "xxxxx",//请求后端接口的地址
  31.         data: {
  32.           url: location.href.split("#")[0],
  33.         }, //向服务端提供授权url参数,并且不需要#后面的部分
  34.         //注意:这个url必须是小程序进入webview的地址,并且是合法域名形式地址!这样获取的数据才能保证签名没问题
  35.       }).then((res) => {
  36.         let data = res.data.results;
  37.         wx.config({
  38.           debug: false, // 开启调试模式,
  39.           appId: data.appId, // 必填,企业号的唯一标识,此处填写企业号corpid
  40.           timestamp: data.timestamp, // 必填,生成签名的时间戳
  41.           nonceStr: data.nonceStr, // 必填,生成签名的随机串
  42.           signature: data.signature, // 必填,签名,见附录1
  43.           jsApiList: ["chooseImage"], // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  44.         });

  45.         wx.ready(function () {
  46.           alert("ready");
  47.           // 在这里调用 API
  48.           //判断当前客户端版本是否支持指定JS接口
  49.           wx.checkJsApi({
  50.             jsApiList: [
  51.               // 所有要调用的 API 都要加到这个列表中
  52.               "chooseImage"
  53.             ], // 需要检测的JS接口列表,所有JS接口列表见附录2,
  54.             success: function (res) {
  55.               console.log('checkJsApi成功=====', res);
  56.               alert('支持chooseImage');
  57.               // 以键值对的形式返回,可用的api值true,不可用为false
  58.               // 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
  59.             },
  60.             fail:function(e){
  61.               alert('不支持');
  62.             }
  63.           });
  64.         });
  65.         wx.error(function (res) {
  66.             // wx.config注册失败就会执行
  67.           alert("error");
  68.         });
  69.       });
  70.     },
  71.     //选择图片
  72.     selectImg() {
  73.       wx.chooseImage({
  74.         success: function (res) {
  75.           console.log("success=====", res);
  76.           alert("成功");
  77.         },
  78.         fail: function (e) {
  79.           console.log("=====", e);
  80.           alert("失败");
  81.         },
  82.         complete: function () {
  83.           alert("立即完成");
  84.         },
  85.       });
  86.     },
  87.   },
  88.   mounted() {
  89.     this.getConfig();
  90.   },
  91. };
  92. </script>

复制代码
以上代码原文转载自:weixin-js-sdk使用-CSDN博客
道不同,不相为谋!理想要有,万一实现了呢?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注公众号

相关侵权、举报、投诉及建议等,请发 E-mail:2513533699@qq.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.|鲁ICP备19000917号|鲁公网安备37078302000320号

在本版发帖
关注公众号
QQ客服返回顶部