zoukankan      html  css  js  c++  java
  • H5 获取用户openID,并把它传给后台

    思路:openID是在公众号里识别用户的唯一标识

    通过用户关注公众号(用户关注公众号有两种授权,一种是静默授权,一种是非静默授权,此时用的是静默授权),获取用户openID,并且把它传给后端。

    
    
    import {getUrlParam} from './getUrlParam.js'
     
    getCode () {
      if (sessionStorage.getItem("openid")&&sessionStorage.getItem("openid")!="undefined") {
        return false
      }
      var code = getUrlParam('code') // 截取路径中的code,如果没有就去微信授权,如果已经获取到了就直接传code给后台获取openId
      var local = window.location.href
      var APPID = 'xxxxxxxxxx'
      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_base&state=1_128b1ff67fdf4192836b1b0f05472f77#wechat_redirect'
      } else {
        this.getOpenId(code) //把code传给后台获取用户信息
      }
    },
    getOpenId (code) {  //把code传给后台,得到openid
      console.log(code)
    },

    getUrlParam.js:

    export function getUrlParam (name) {
      //获取地址栏的参数
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)")
      var r = window.location.hash.substr(1).match(reg)
      console.log(location.hash)
      if (r != null) return unescape(r[2])
      return null
    }
  • 相关阅读:
    win10 增加一个新磁盘
    解释mysql 语句
    Impala 中invalidate metadata和refresh
    PPT 设置幻灯片母版
    python 中startswith()和endswith() 方法
    python 把函数的值赋给变量
    python 获取导入模块的文件路径
    MD5 算法
    python 删除特定字符所在行
    JS判断内容为空方法总结
  • 原文地址:https://www.cnblogs.com/yuanyuanya/p/13791040.html
Copyright © 2011-2022 走看看