/** * 用户通过手机号来注册 */ public function registerAction() { $view = new ViewModel(); $request = $this->getRequest(); $user = $this->authentication()->getIdentity(); if ($user) { return $this->redirect()->toUrl('/test_qoros'); } else { //get user info if (parent::isWeixin() && !$request->isPost()) { $code = $this->getParam('code', false); if (!$code) { $redirectUrl = $this->wechat->getOauthRedirect('http://' . $_SERVER['SERVER_NAME'] . '/test_qoros/user/completeInfo'); 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->setVariables(array('openid' => $tokenData['openid'])); } } if ($request->isPost()) { $mobile = $this->getParam('mobile'); $openid = $this->getParam('openid', false); $exsit = $this->userModel->getUserByMobile($mobile); if ($exsit) { //to login $this->login($mobile, false, false); $user = $this->authentication()->getIdentity(); if (isset($openid) && $openid) { $userInfo = $this->wechat->getUserInfo($openid); if (isset($userInfo['headimgurl'])) { $user->avatar = $userInfo['headimgurl']; } $user->open_id = $openid; $this->userModel->updateUserInfo($user); } return new UnifyJsonModel(); } else { return new ErrorJsonModel(1001, '手机号码不存在'); } } return $view; }
/** * */ 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; }