Vue 微信小程序 uni-app学习系列《五》 获取用户openid

代码

export default {
		 components: {},
		data() {
			return {
				 globalData: {
				  userInfo: null,
				  version: "1.7",
				  appid: '',
				  secret: ''
				}
			}
		},
		methods: {
			
		},
		onLoad() {
			 let that = this
    // 登录
    wx.login({
      success: res => {
        if (res.code) {
          // 发送 res.code 到后台换取 openId, sessionKey
          var d = that.globalData;
          var urlVal = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + d.appid + '&secret=' + d.secret + '&js_code=' + res.code + '&grant_type=authorization_code';
          wx.request({
            url: urlVal,
            data: {},
            method: 'GET',
            success: function(res) {
              var obj = {};
              obj.openid = res.data.openid;  //获取到的openid
			  console.log("*****",obj.openid);
              wx.setStorageSync('user', obj); //存储openid 
            }
          })
        } else {
          console.log('登录失败!' + res.errMsg)
        }
      }
    })
		}
	}

你可能感兴趣的:(微信小程序,Vue)