Esempio n. 1
0
 public function run()
 {
     if (Yii::app()->user->isGuest) {
         $this->controller->bodyCss = 'access';
         if (!Yii::app()->session['wechat']) {
             $this->controller->redirect('/wechat/wechatconnect');
         }
     } else {
         $this->controller->redirect(Assist::getAccessURL());
     }
 }
Esempio n. 2
0
 public function run()
 {
     $this->controller->pageTitle = 'Login';
     if (Yii::app()->user->isGuest) {
         if (Yii::app()->request->isAjaxRequest) {
             $mobile = Yii::app()->request->getParam('mobile');
             $code = Yii::app()->request->getParam('code');
             $email = Yii::app()->request->getParam('email');
             $password = Yii::app()->request->getParam('password');
             $bind = Yii::app()->request->getParam('bind');
             $_identity = new UserIdentity();
             if ($mobile) {
                 Yii::log($code, CLogger::LEVEL_ERROR);
                 $_identity->authMobile($mobile, $code, $bind);
                 if ($_identity->errorCode === UserIdentity::ERROR_NONE) {
                     $duration = 86400;
                     Yii::app()->user->login($_identity, $duration);
                     echo CJSON::encode(array('code' => 200, 'message' => 'success'));
                     //$this->controller->redirect(Yii::app()->user->getReturnUrl(Assist::getDefaultURL()));
                 } elseif ($_identity->errorCode === UserIdentity::ERROR_CODE_INVALID) {
                     echo CJSON::encode(array('code' => 500, 'message' => '验证码不正确'));
                 } elseif ($_identity->errorCode === UserIdentity::ERROR_MOBILE_INVALID) {
                     echo CJSON::encode(array('code' => 500, 'message' => '手机号未注册'));
                 } else {
                     echo CJSON::encode(array('code' => 500, 'message' => '登录错误'));
                 }
             } elseif ($email) {
                 $_identity->authMail($email, $password, $bind);
                 if ($_identity->errorCode === UserIdentity::ERROR_NONE) {
                     $duration = 86400;
                     Yii::app()->user->login($_identity, $duration);
                     echo CJSON::encode(array('code' => 200, 'message' => 'success'));
                     //$this->controller->redirect(Assist::getDefaultURL());
                     //$this->controller->redirect(Yii::app()->user->getReturnUrl(Assist::getDefaultURL()));
                 } elseif ($_identity->errorCode === UserIdentity::ERROR_MAIL_INVALID) {
                     echo CJSON::encode(array('code' => 500, 'message' => '邮箱未注册'));
                 } elseif ($_identity->errorCode === UserIdentity::ERROR_PASSWORD_INVALID) {
                     echo CJSON::encode(array('code' => 500, 'message' => '密码错误'));
                 } else {
                     echo CJSON::encode(array('code' => 500, 'message' => '登录错误'));
                 }
             } else {
                 throw new CHttpException('405', '参数错误');
             }
         } else {
             $this->controller->render('login');
         }
     } else {
         $this->controller->redirect(Assist::getAccessURL());
     }
 }
Esempio n. 3
0
 public function run()
 {
     if (Yii::app()->user->isGuest) {
         $this->controller->bodyCss = 'access';
         // if(!empty(Yii::app()->session['user'])){
         // 	$this->controller->redirect('/registered/productlist');
         // }else{
         $this->controller->redirect('/wechat/wechatconnect');
         // }
     } else {
         $this->controller->redirect(Assist::getAccessURL());
     }
     // $this->controller->render('index');
 }
 public function run()
 {
     if (Yii::app()->user->isGuest) {
         if (Assist::isWeixin()) {
             $code = Yii::app()->request->getParam('code');
             $state = Yii::app()->request->getParam('state');
             if (empty($code)) {
                 throw new CException(Yii::t('yii', '授权码缺失'));
             } else {
                 $wechat = Yii::app()->params['partner']['wechat'];
                 $params = array('appid' => $wechat['appid'], 'secret' => $wechat['appsecret'], 'code' => $code, 'grant_type' => 'authorization_code');
                 try {
                     $output = Yii::app()->curl->get($wechat['oauth2']['token'], $params);
                     $value = json_decode($output, true);
                     if (Yii::app()->curl->getStatus() == 200 && isset($value['access_token'])) {
                         $userinfo = json_decode(Yii::app()->curl->get($wechat['oauth2']['userinfo'], array('access_token' => $value['access_token'], 'openid' => $value['openid'], 'lang' => 'zh_CN')), true);
                         if (Yii::app()->curl->getStatus() == 200 && isset($userinfo['unionid'])) {
                             $_identity = new UserIdentity();
                             $_identity->authWechat($userinfo, $value['openid']);
                             if ($_identity->errorCode === UserIdentity::ERROR_NONE) {
                                 $duration = 86400;
                                 Yii::app()->user->login($_identity, $duration);
                                 $this->controller->redirect(Assist::getAccessURL());
                                 // $this->controller->redirect(Yii::app()->user->getReturnUrl(Assist::getDefaultURL()));
                             } elseif ($_identity->errorCode === UserIdentity::ERROR_NO_BIND) {
                                 Yii::app()->session['wechat'] = null;
                                 $this->controller->render('index');
                             }
                         } else {
                             throw new CException(Yii::t('yii', '密钥错误或丢失。'));
                         }
                     } else {
                         Yii::log(print_r($value, true), CLogger::LEVEL_ERROR, 'user.wx.auth');
                         throw new CException(Yii::t('yii', '获取有效凭证失败'));
                     }
                 } catch (Exception $e) {
                     $this->controller->render('index');
                     // throw new CException(Yii::t('yii', $e->getMessage() ?: '内部服务错误'));
                 }
             }
         } else {
             throw new CHttpException(403, Yii::t('yii', '仅支持微信访问'));
         }
     } else {
         $this->controller->redirect(Assist::getDefaultURL());
     }
 }