用easywechat获取open_id(thinkphp5.0)


namespace app\phone\controller;
use think\Controller;
use think\Session;
use EasyWeChat\Foundation\Application;
class VipController extends Controller{

    //验证缓存openid
    private function hasOpenid($app)
    {
        if(Session::get('openid')){
            return true;
        }
        if(input("?code")){
            $user = $app->oauth->user();
            Session::set('openid',$user->id);
            return true;
        }
        return false;
    }

    //获取openid
    public function get_openid(){
$url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];     //当前页面的url
        $app = third_party($url);//这个是开放平台的配置,请用自己的配置

        if(!$this->hasOpenid($app)){
            return $response = $app->oauth->scopes(['snsapi_base'])
                          ->redirect();
        }
        $open_id = Session::get('openid');
        return $open_id;

        }
    }

}

你可能感兴趣的:(php,thinkphp5.0,微信开发)