/** * 用户通过手机号来注册 */ 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; }
/** * 检查用户是否登陆过 */ public function checkUserLogin($openId) { try { // if(DW_ENV_DEBUG){ //测试环境没有unionid $userOauth = $this->userModel->getByOpenId($openId); // }else{ // $userInfo = $this->wechat->getUserInfo($openId); // $userOauth = $this->userModel->getUserOauthByUnionIdAndVender($userInfo['unionid'],UserOauthEntity::VENDER_WECHAT); // } if (!$userOauth) { //提示用户 去绑定帐号 $rst = $this->wechat->textReply('您好,继续其他操作请先绑定账号:' . 'http://' . $_SERVER['SERVER_NAME'] . '/user/bind'); return false; } else { //如果用户存在,则直接去登陆一下 } } catch (\Exception $e) { $this->wechat->log($e->getMessage()); } return true; }