微信小程序获取用户openId

来自:https://www.jb51.net/article/245381.htm 侵删

// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    urlcode: "",
    srcUrl: "https://www.wyy-supor.com.cn/supor-app/#/"
  },
  onShow() {
    wx.hideHomeButton()
  },
  // 事件处理函数
  bindViewTap() {

  },
  onLoad() {
    wx.login({
      success: res => {
        if (res.code) {
          let code = res.code;
          // this.data.srcUrl = this.data.srcUrl + '?openidcode=' + code;
          //缓存
          // wx.setStorageSync('_USER_OPENID', code)
          wx.request({
            url: "https://api.weixin.qq.com/sns/jscode2session?appid=" + "wxa5cab619a05a0423" + "&secret=" + "8612ee8bb27cfd9be313456760da1805" + "&js_code=" + code + "&grant_type=authorization_code",
            success: (res) => {
              wx.setStorageSync('_USER_OPENID', res.data.openid);
              wx.setStorageSync('_USER_session_key', res.data.session_key);
            }
          })
        }
      }
    })
  }
})

 

posted @ 2022-11-22 14:41  abcByme  阅读(144)  评论(0编辑  收藏  举报