zoukankan      html  css  js  c++  java
  • 微信小程序获取用户信息

    App({
      appData: {
        userInfo:{
          user_portraitUrl: "",
          user_nick: "",
          user_gender: 0,     // 1:男; 2:女; 0:未知
          user_city: "",
          user_province: "",
          country: ""
        },
        callBlack:{
          openid:"",
          session_key:"",
          unionid:""
        }
      },
      onLaunch:function(){
        var obj = this;   // 获取
        wx.login({
          success: function (res) {
            // 判断是否登录成功
            if (res.code) {
              // 登录成功后获取回调结果(含openID)
              wx.request({
                url: 'https://api.weixin.qq.com/sns/jscode2session',
                data:{
                  appid: "appid",          // 这里填写自己的appid
                  secret: "secret",        // 这里填写自己的secret
                  js_code: res.code,
                  grant_type: "authorization_code"
                },
                success:function(res){
                  obj.appData.callBlack.openid = res.data.openid;
                  obj.appData.callBlack.session_key = res.data.session_key;
                }
              });
              wx.getUserInfo({
                success: function (success_res) {
                  // 登陆成功后对应取值
                  obj.appData.userInfo.user_portraitUrl = success_res.userInfo.avatarUrl;
                  obj.appData.userInfo.user_nick = success_res.userInfo.nickName;
                  obj.appData.userInfo.user_gender = success_res.userInfo.gender;
                  obj.appData.userInfo.user_city = success_res.userInfo.city;
                  obj.appData.userInfo.user_province = success_res.userInfo.province;
                  obj.appData.userInfo.country = success_res.userInfo.country;
                }
              })
            } else {
              console.log('获取用户登录态失败!' + res.errMsg)
            }
          }
        })
      }
    })
     
    wx.request获取回调信息的url地址请填写自己服务器的地址;
    wx.getUserInfo 登录小程序并获取 code , 在 success 回调中获取userInfo;
  • 相关阅读:
    鲜牛奶与纯牛奶的区别 All In One
    Rough Notation Animation All In One
    java基础编程String及相关
    JDBC1
    java基础数据类型
    java基础编程
    JDBC3
    MYSQL1
    JDBC2
    JSP和Servlet的相同点和不同点,他们之间的联系
  • 原文地址:https://www.cnblogs.com/MirageFox/p/7932343.html
Copyright © 2011-2022 走看看