Wechat.class.phppublic function getOauthUserinfo( $access_token , $openid , $type = '0' ){ if ( $type ){ $token = $this ->get_token(); $url2 = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" . $token . "&openid=" . $openid . "&lang=zh_CN" ; $re2 = $this ->http_get( $url2 ); $rearr2 = json_decode( $re2 ,true); return $rearr2 ; } $result = $this ->http_get(self::API_BASE_URL_PREFIX.self::OAUTH_USERINFO_URL. 'access_token=' . $access_token . '&openid=' . $openid . "&lang=zh_CN" ); //var_dump($result); if ( $result ) { $json = json_decode( $result ,true); if (! $json || ! empty ( $json [ 'errcode' ])) { $this ->errCode = $json [ 'errcode' ]; $this ->errMsg = $json [ 'errmsg' ]; return false; } return $json ; } return false; } public function get_token(){ // add if ( $this ->getCache( 'access_tokens' )) $this ->getCache( 'access_tokens' ); $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this ->appid. "&secret=" . $this ->appsecret; $ret_json = $this ->http_get( $url ); $ret = json_decode( $ret_json ); // dump($ret); if ( $ret -> access_token){ $this ->setCache( 'access_tokens' , $ret ->access_token,7200); return $ret -> access_token; } } WechatController.class.phpstatic function do_oauth() { // 默认公众号信息 $wxinfo = model( 'Base' )->model->table( 'wechat' ) ->field( 'id, token, appid, appsecret, oauth_redirecturi, type, oauth_status' ) ->where( 'default_wx = 1 and status = 1' ) ->find(); if (! empty ( $wxinfo ) && $wxinfo [ 'type' ] == 2) { $config [ 'token' ] = $wxinfo [ 'token' ]; $config [ 'appid' ] = $wxinfo [ 'appid' ]; $config [ 'appsecret' ] = $wxinfo [ 'appsecret' ]; // 微信通验证 $weObj = new Wechat( $config ); // 微信浏览器浏览 if (self::is_wechat_browser() && ( $_SESSION [ 'user_id' ] === 0 || empty ( $_SESSION [ 'openid' ])) && ! empty ( $wxinfo [ 'oauth_status' ])) { if (! isset( $_SESSION [ 'redirect_url' ])) { session( 'redirect_url' , __HOST__ . $_SERVER [ 'REQUEST_URI' ]); } $url = $weObj ->getOauthRedirect( $wxinfo [ 'oauth_redirecturi' ], 1, 'snsapi_base' ); if (isset( $_GET [ 'code' ]) && ! empty ( $_GET [ 'code' ])) { $token = $weObj ->getOauthAccessToken(); if ( $token ) { $userinfo = $weObj ->getOauthUserinfo( $token [ 'access_token' ], $token [ 'openid' ], 'snsapi_base' ); //var_dump($userinfo); // exit; self::update_weixin_user( $userinfo , $wxinfo [ 'id' ], $weObj ); if (! empty ( $_SESSION [ 'redirect_url' ])) { $redirect_url = session( 'redirect_url' ); header( 'Location:' . $redirect_url , true, 302); exit (); } } else { header( 'Location:' . $url , true, 302); exit (); } } else { // var_dump($url); header( 'Location:' . $url ); exit (); } } } } 这里去掉 ‘snsapi_base’ 就变为原来的 snsapi_userinfo 授权页面登录。 |
|
来自: 新学习天地 > 《ecshop(ectouch)》