微信小程序获取不到openid?

<?php

public $appid = 'wx5qweqweqweqdd4d43fcb3';
public $secret = '2c8d06fsadasfgssdfhdfgsdca43e2fd43029f6';
public $sessionKey = '';
// 小程序登录
public function loginAction(){
    $code = I('get.code');
    $loginUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=".$this->appid."&secret=".$this->secret."&js_code=".$code."&grant_type=authorization_code";
    $res = file_get_contents($loginUrl);
    $wxres = json_decode($res,true);
    $user = M('user');
    $data = $user->where(array('openid'=>$wxres['openid']))->find();
    if(!$data){
        $res = $user->add(array('openid'=>$wxres['openid']));
    }
    session("wxopenid",$wxres['openid']);
    $session_id = session_id();
    $wxres['session_id'] = $session_id;
    $wxres['creattime'] = (string)time();
    $this->sessionKey = $wxres['session_key'] ;
    $this->ajaxReturn($wxres);
}
阅读 8k
3 个回答

我的openid在小程序里是这样拿的。。所以我觉得你应该需要先请求wxLogin,要把返回的code传给js_code才可以。。而不是用一个固定的字符串‘authorization_code’。。

wx.login({
    success: function (loginCode) {
      var appid = 'appid';
      var secret = 'secret ';
      
      //调用request请求api转换登录凭证  
      wx.request({
        url: 'https://api.weixin.qq.com/sns/jscode2session?appid='+appid+'&secret='+secret+'&grant_type=authorization_code&js_code=' + loginCode.code,
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          // console.log(res.data.openid) //获取openid
          that.globalData.openid = res.data.openid
        }
      })
    }
  })

appid和secret都敢贴出来

虽然过去有点久,但我也纠结的好长时间,应该是没有审核的小程序无法获取用户 openid 吧。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题