微信公众号授权--前端

时间:2019-08-25 18:03:26   收藏:0   阅读:133

公众号授权操作

开发微信公众号时,需要获取到用户的信息,因而不得不设计到微信公众号授权问题(本质是利用OAuth登陆)

开发模式如何进行授权操作

开发环境

技术图片

填写的域名本地服务器地址(域名 / ip)即可,不区分内网还是外网

code [vue]

getCode () {
  const code = location.href.split('?')[1]
  if (!code) return {}
  const obj = {}
  code.split('&').forEach(item => {
    const arr = item.split('=')
    obj[arr[0]] = arr[1]
  })
  return obj
},

getLogin () {
  const { code } = this.getCode()
  if (!code) {
    this.$dialog.toast({
      mes: '请授权登陆',
      timeout: 1500,
      icon: 'error'
    })
    return
  }
  const PARAMS = {
    code
  }
  // login 后台提供的接口
  this.$fetch.post(login, PARAMS).then(res => {
    /**
      * TODO: 若token没值
      */
    const { token } = res.data
    this.$store.dispatch('saveToken', token)
  }).catch(_ => {
    this.$dialog.toast({
      mes: '登陆失败',
      timeout: 1500,
      icon: 'error',
      callback: () => {
        this.$dialog.alert({ mes: '给你一次重来的机会!' })
      }
    })
  })
},

参考链接

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!