Vue+H5 活动内容调用微信分享

本文记录了使用Vue进行H5活动内容调用微信分享时遇到的难点和解决方案,包括参照微信官方手册、引入微信工具包、配置参数、获取微信签名等步骤。在实际操作中,由于Vue的hash路由问题,作者转而采用history路由,并调整了nginx配置。在处理URL时,避免了不必要的encodeURIComponent操作。
摘要由CSDN通过智能技术生成

遇到很多坑记录一下

先看一遍微信的官方手册

https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html

代码实现步骤

第一步、引入大神写的微信工具包

<dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-common</artifactId>
            <version>3.7.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-miniapp</artifactId>
            <version>3.7.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-mp</artifactId>
            <version>3.7.0</version>
        </dependency>

第二步、配置工具类的参数

我用的springboot resource里的配置就不放了

@Configuration
public class WeChatAccountConfig {
   
    @Value("${wechat2.appId}")
    private String appId;

    @Value("${wechat2.secret}")
    private String appSecret;

    @Value("${wechat2.token}")
    private String token;

    @Value("${wechat2.aesKey}")
    private String encodingAESKey;

    public String getAppId() {
   
        return appId;
    }

    public String getAppSecret() {
   
        return appSecret;
    }

    public String getToken() {
   
        return token;
    }

    public String getEncodingAESKey() {
   
        return encodingAESKey;
    }

}

@Component
public class WeChatConfig {
   
    @Autowired
    private WeChatAccountConfig weChatAccountConfig;

    @Bean<
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue H5调用微信分享,首先需要引入微信官方的JS-SDK库。在Vue项目的index.html文件中,可以在<head>标签内添加如下代码: ```html <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script> ``` 然后,在Vue组件的created生命周期钩子函数中,使用Vue的axios或者其他网络请求库向后端请求获取微信配置信息。 ```javascript created() { this.fetchWechatConfig(); }, methods: { async fetchWechatConfig() { const response = await axios.get('/api/wechat/config'); // 向后端请求微信配置信息 const { appId, timestamp, nonceStr, signature } = response.data; // 将配置信息存储到全局变量中 this.$store.commit('setWechatConfig', { appId, timestamp, nonceStr, signature }); this.initWechatSDK(); }, initWechatSDK() { const { appId, timestamp, nonceStr, signature } = this.$store.state.wechatConfig; wx.config({ appId, timestamp, nonceStr, signature, jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'], // 配置需要使用的微信API }); // 进行微信SDK的初始化 wx.ready(() => { this.shareToWechat(); }); }, shareToWechat() { const shareData = { title: '分享标题', link: '分享链接', imgUrl: '分享图片链接', desc: '分享描述', }; // 配置分享的具体内容 wx.onMenuShareTimeline(shareData); // 分享到朋友圈 wx.onMenuShareAppMessage(shareData); // 分享给好友 }, }, ``` 上述代码中,我们通过axios库向后端请求了微信配置信息,并将其存储到Vuex的全局状态中。然后,我们使用微信JS-SDK中的`wx.config`方法进行微信验证和初始化配置。一旦初始化成功,`wx.ready`回调函数将会被触发,我们在该函数中调用`shareToWechat`方法来配置分享的具体内容。 在`shareData`对象中,我们可以自定义分享的标题、链接、图片和描述等信息,然后使用`wx.onMenuShareTimeline`方法配置分享给朋友圈的内容,使用`wx.onMenuShareAppMessage`方法配置分享给好友的内容。 最后,我们可以在Vue组件中调用`shareToWechat`方法来触发微信分享。 注意:在实际开发中,需要根据微信的API文档和后端提供的接口来进行相应的调整和配置。以上代码仅为示例,具体实现方式可能会有所不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值