public function onDispatch(MvcEvent $e) { parent::onDispatch($e); $user = $this->identity(); if (empty($user)) { $request = $this->getRequest(); $redirect = $_SERVER['REQUEST_URI']; if (parent::isWeixin() && !$request->isPost()) { $code = $this->getParam('code', false); if (!$code) { $redirectUrl = $this->wechat->getOauthRedirect('http://' . $_SERVER['SERVER_NAME'] . '/test_qoros/user/register?redirect=' . $redirect); return $this->redirect()->toUrl($redirectUrl); } $tokenData = $this->wechat->getOauthData(); $user = $this->userModel->getByOpenId($tokenData['openid']); if ($user) { $result = $this->login($user->mobile, null, false); if ($result->getCode() == Result::SUCCESS && $user->username != $user->openid) { return $this->redirect()->toUrl('/test_qoros'); } } } } }
/** * */ public function huanyingxinAction() { //自动登陆 $request = $this->getRequest(); if (parent::isWeixin() && !$request->isPost()) { $code = $this->getParam('code', false); if (!$code) { $redirectUrl = $this->wechat->getOauthRedirect('http://' . $_SERVER['SERVER_NAME'] . '/test_qoros/index/huanyingxin'); return $this->redirect()->toUrl($redirectUrl); } $tokenData = $this->wechat->getOauthData(); $user = $this->userModel->getByOpenId($tokenData['openid']); if ($user) { $result = $this->login($user->mobile, null, false); if ($result->getCode() == Result::SUCCESS && $user->username != $user->openid) { // return $this->redirect()->toUrl('/test_qoros'); } } } $view = new ViewModel(); // 注意 URL 一定要动态获取,不能 hardcode. $protocol = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ? "https://" : "http://"; $href = "{$protocol}{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"; $jsSign = $this->wechat->getJsSign($href); $url = 'http://' . $_SERVER['HTTP_HOST'] . '/test_qoros/img/share_image.png'; $view->setVariables(array('jsSign' => $jsSign, 'url' => $url)); return $view; }