mErrcode = 0; } public function enter($origin_url) { if(defined('SERVER_TYPE') && SERVER_TYPE == 'panda') { $back_url = 'https://passport.lrlz.com/mobile/wxauthor.php'; } elseif(defined('SERVER_TYPE') && SERVER_TYPE == 'car') { $back_url = 'http://cardev.lrlz.com/mobile/wxauthor.php'; } else { Log::record("找不到微信OpenWxPayConfig密钥",Log::ERR); } $params = [ 'appid' => self::appid, 'redirect_uri' => $back_url, 'response_type' => 'code', 'scope' => 'snsapi_userinfo', 'state' => BASE_SITE_URL]; $ref_url = util::http_add_params(self::authorize_url,$params); $ref_url .= '#wechat_redirect'; wechat_helper::set_origin_url($origin_url); Log::record("originurl=" . wechat_helper::get_origin_url(),Log::DEBUG); return $ref_url; } public function callback($code) { if($this->access_token($code) == false) { Log::record("wxauthor access token error code:{$this->mErrcode} msg:{$this->mErrmsg}"); return false; } $user_info = $this->user_info(); if($user_info == false) { Log::record("wxauthor user_info error code:{$this->mErrcode} msg:{$this->mErrmsg}"); return false; } else { $_SESSION['wxauthor_time'] = time(); return $user_info; } } private function access_token($code) { $params = ['appid' => self::appid,'secret' => self::appsecret,'code' => $code,'grant_type' => 'authorization_code']; $res = http_request(self::access_token_url,$params); if($res == false) return false; /* * 正确返回 * { "access_token":"ACCESS_TOKEN","expires_in":7200, "refresh_token":"REFRESH_TOKEN","openid":"OPENID","scope":"SCOPE" } * 错误返回 * {"errcode":40029,"errmsg":"invalid code"} */ $info = json_decode($res, true); $this->mErrcode = intval($info['errcode']); if($this->mErrcode > 0) { $this->mErrmsg = $info['errmsg']; return false; } else { $this->mAccessToken = $info['access_token']; $this->mOpenID = $info['openid']; return true; } } private function user_info() { $params = ['access_token' => $this->mAccessToken,'openid' => $this->mOpenID,'lang' => 'zh_CN']; $res = http_request(self::userinfo_url,$params); if($res == false) return false; $info = json_decode($res, true); $this->mErrcode = intval($info['errcode']); if($this->mErrcode > 0) { $this->mErrmsg = $info['errmsg']; return false; } else { Log::record("user_info={$res}",Log::DEBUG); return $info; } } }