在vue项目中使用微信js-sdk ,解析返回url,获取微信code,

直接上干货,如有不懂欢迎留言,如有错误,欢迎指出

vue组件

<script>
  import {authPageBaseUri, homePage, wechatAppId} from 'config'
  import {extractQueryParams} from 'utils'
  export default {
    name: 'home',
    data () {
      return {
        config: {
          appid: wechatAppId,
          responseType: 'code',
          scope: 'snsapi_base',
          state: ''
        }
      }
    },
    created(){
      this.getAuthCode()
    },
    methods: {
      async getAuthCode(){
        await this.redirectToAuthPageUrl()
        let queryParams = extractQueryParams(window.location.href)
        let code = queryParams.code
        let callbackUrl = queryParams.callbackUrl
        if (code) {
          window.location.href = homePage + 'service_list?code=' + code
        }
      },
      redirectToAuthPageUrl(){
        let authParams = `?appid=${this.config.appid}&redirect_uri=${homePage}&response_type=${this.config.responseType}&state=${this.config.state}&scope=${this.config.scope}#wechat_redirect`
        window.location.href = authPageBaseUri + authParams
      },
    }
  }
</script>

<style scoped>

</style>

config文件

export const homePage = `http://tst.service.com/#/`
export const authPageBaseUri = 'https://open.weixin.qq.com/connect/oauth2/authorize'
export const wechatAppId = 'ww17cf217249330864'

utils文件

export function extractQueryParams (url) {
  let queryParams = {}
  if (url.includes('?')) {
    let queryString = url.substr(url.indexOf('?') + 1)
    let pairs = queryString.split('&')
    for(let pair of pairs) {
      let splitArray = pair.split('=')
      let key = splitArray[0]
      let value = splitArray[1]
      if (value.indexOf('#') > -1) {
        value = value.substring(0, value.indexOf('#'))
      }

      queryParams[key] = value
    }
  }
  return queryParams
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在 Vue 使用微信 JS-SDK 实现分享功能,需要完成以下步骤: 1. 获取微信公众号的 appid 和 appsecret,以及当前页面的 url。 2. 在服务器端配置好微信 JS-SDK 的权限验证,获取到 signature、timestamp 和 noncestr 等参数。 3. 在 Vue 引入微信 JS-SDKSDK 文件,并初始化。 4. 调用微信 JS-SDK 的接口,完成分享功能。 下面是一个简单的示例代码: ```html <template> <div class="share"> <button @click="shareToWechat">分享到微信</button> </div> </template> <script> import wx from 'weixin-js-sdk' export default { data() { return { appId: '', timestamp: '', nonceStr: '', signature: '', imgUrl: '', shareUrl: '' } }, created() { // 获取微信 JS-SDK 配置参数 this.getWechatConfig() }, methods: { // 获取微信 JS-SDK 配置参数 async getWechatConfig() { const url = window.location.href.split('#')[0] const res = await this.$api.getWechatConfig({ url }) if (res.code === 0) { const data = res.data this.appId = data.appId this.timestamp = data.timestamp this.nonceStr = data.nonceStr this.signature = data.signature this.imgUrl = data.imgUrl this.shareUrl = data.shareUrl this.initWechatSDK() } }, // 初始化微信 SDK initWechatSDK() { wx.config({ debug: false, appId: this.appId, timestamp: this.timestamp, nonceStr: this.nonceStr, signature: this.signature, jsApiList: [ 'updateAppMessageShareData', 'updateTimelineShareData' ] }) wx.ready(() => { // 配置分享信息 wx.updateAppMessageShareData({ title: '分享标题', desc: '分享描述', link: this.shareUrl, imgUrl: this.imgUrl, success() { // 分享成功回调 } }) wx.updateTimelineShareData({ title: '分享标题', link: this.shareUrl, imgUrl: this.imgUrl, success() { // 分享成功回调 } }) }) }, // 分享到微信 shareToWechat() { wx.chooseWXPay({ // ... }) } } } </script> ``` 在上面的代码,我们首先通过 `getWechatConfig` 方法获取微信 JS-SDK 的配置参数,然后在 `initWechatSDK` 方法初始化微信 SDK,并配置分享信息。最后,在 `shareToWechat` 方法调用微信 JS-SDK 的 `chooseWXPay` 接口,实现分享功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值