代码

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)
        }
      }
    })
		}
	}
Logo

前往低代码交流专区

更多推荐