Mijiujs
   getCode () {
      const AppId = \'你的微信公众号appid\'
      const code = this.getQueryVariable(\'code\')
      const local = window.location.href
      if (code == null || code === \'\') {
        window.location.href = \'https://open.weixin.qq.com/connect/oauth2/authorize?appid=\' + AppId + \'&redirect_uri=\' + encodeURIComponent(local) + \'&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect\'
      } else {
        this.code = code
      }
    },
    getQueryVariable (variable) {
      let query = window.location.search.substring(1)
      let vars = query.split(\'&\')
      let index = vars.findIndex(item => {
        let pair = item.split(\'=\')
        return pair[0] === variable
      })
      if (index !== -1) {
        return vars[index].split(\'=\')[1]
      } else {
        return null
      }
    },

 实现效果

可参考:https://www.jianshu.com/p/b7e2100b56e4

分类:

技术点:

相关文章: