uniapp获取微信小程序openid

 在小程序中获取用户的openid

 需要用到小程序appid(可以在微信开发工具中直接申请测试appid)

 申请完成后小程序的测试appid

 可以在微信公众平台中 查看secret 也就是小程序秘钥​​​​​​​

 一共需要三个参数{

                                小程序appid:‘’,

                                小程序密码:‘’,

                                code值:'', //(使用api,uni.login({}) 会返回这个值.code值有效时间为5分钟)

}

  //获取openid
   async getOpenId() {
    uni.login({
      success: res => {
        //小程序appid
        let appid = this.GLOBALVARIABLE.appid;
        //小程序secret
        let secret = this.GLOBALVARIABLE.secret;
        //wx接口路径
        let url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&js_code=' + res.code + '&grant_type=authorization_code';
        uni.request({
          url: url, // 请求路径
          method: 'GET', //请求方式
          success: result => {
            //获取到openid
            this.openId = result.data.openid;
            //校验openid
            this.openIdCheck();
          },
          fail: err => {} //失败
        });
      }
    });
   }

你可能感兴趣的:(uniapp,vue.js,前端)