vue微信静默登录,获取opendID

在src/config里 创建 wx-getOpendId.js 

import Vue from 'vue'

// 获取getOpendID
const wxGet = () => {
  Vue.axiosGet(Vue.api.weixinPublicLoginPlugin, {code: WXcode}).then(res => {
    if (res.code === 1) {
      localStorage.setItem('WXInfo', JSON.stringify(res.data))
    } else {
      alert('获取OpendID失败')
    }
  })
}

// 微信授权登录
const getLogin = () => {
  let urlrouter = window.location.href.split('#/')[1] // 当前路由
  let url = 'http%3A%2F%2Fweb.xxxxxx.com%2F%23%2F' // 重定向返回地址
  let wxappid = 'wxappid0000000' // 微信公众号appid
  let str = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + wxappid + '&redirect_uri=' + url + urlrouter + '&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect'
  window.location.href = str
}

var WXcode
var WXInfo = localStorage.getItem('WXInfo') ? JSON.parse(localStorage.getItem('WXInfo')) : 'noLogin'
let str = window.location.href
var ua = navigator.userAgent.toLowerCase()
var isWeixin = ua.indexOf('micromessenger') !== -1 // 是否 在微信浏览器内
let isURL = window.location.href.indexOf('code=') === -1 // 是否 没有授权重定向回来

if (isWeixin && WXInfo === 'noLogin' && isURL) {
  getLogin()
}
// 重定向回来
if (!isURL) {
  let num1 = str.indexOf('code=')
  let num2 = str.indexOf('&state=')
  WXcode = str.slice(num1 + 5, num2)
  localStorage.setItem('WXcode', JSON.stringify(WXcode))
  if (WXInfo === 'noLogin' || WXInfo.openid === null) {
    wxGet()
  }
}

main.js 引入  import './config/wx-getOpenid'

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值