vue 开发微信公众号,获取用户openid

login.vue页,默认就执行getWxCode (),这样一进入页面就拿到code。在登录接口就可以一起提交。也可以在点击“微信登录”的按钮的时候,执行getWxCode ()。

const APPID='XXXXXXXX';

const currentUrl = window.location.href;

import https from './../http/http-service'


getWxCode () {

  const code = https.getUrlParam('code'); // 截取路径中的code,如果没有就去微信授权,如果已经获取到了就直接传code给后台获取openId

  if (code ==null || code ==='') {

window.location.href ='https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + APPID +'&redirect_uri=' +encodeURIComponent(currentUrl) +'&response_type=code&scope=snsapi_base&state=1#wechat_redirect'

  }else {

    localStorage.setItem("wxCode", code);  //取到code值

  }

},

+++++++++++++++++++++++++++++++++++++++++++++++++++

http-service.js页面

export function getUrlParam(name){

var reg=new RegExp('(^|&)'+name+'=([^&]*)(&|$)')

var r=window.location.search.substr(1).match(reg)

if(r!=null)return unescape(r[2])

return null

}


export default {

  getUrlParam,

}

你可能感兴趣的:(vue 开发微信公众号,获取用户openid)