123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- <?php
- namespace company\modules\manage\controllers;
- use common\library\LMMessage;
- use common\models\User;
- use Yii;
- use yii\base\InvalidParamException;
- use yii\web\BadRequestHttpException;
- use yii\web\Controller;
- use company\modules\manage\controllers\LoginverifyController;
- use yii\filters\VerbFilter;
- use yii\filters\AccessControl;
- use common\models\LoginForm;
- use company\models\PasswordResetRequestForm;
- use company\models\ResetPasswordForm;
- use company\models\SignupForm;
- use company\models\ContactForm;
- use common\models\SortMessage;
- use common\models\UserCompany;
- /**
- * Site controller
- */
- class SiteController extends LoginverifyController
- {
- public $layout = 'hplus';
- /**
- * @inheritdoc
- */
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::className(),
- 'rules' => [
- [
- 'actions' => ['login','error','init','back','updatepassword'],
- 'allow' => true,
- ],
- [
- 'actions' => ['logout','index','home','back','updatepassword','reset'],
- 'allow' => true,
- 'roles' => ['@'],
- ],
- ],
- ],
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'logout' => ['post'],
- ],
- ],
- ];
- }
- /**
- * @inheritdoc
- */
- public function actions()
- {
- return [
- 'error' => [
- 'class' => 'yii\web\ErrorAction',
- ],
- 'captcha' => [
- 'class' => 'yii\captcha\CaptchaAction',
- 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
- ],
- ];
- }
- /**
- * Displays homepage.
- *
- * @return mixed
- */
- public function actionIndex()
- {
- if (Yii::$app->user->isGuest) {
- return $this->redirect(['site/login']);
- }
- return $this->render('index');
- }
- public function actionHome()
- {
- $this->layout= 'iframe';
- return $this->render('home');
- }
- /**
- * Logs in a user.
- *
- * @return mixed
- */
- public function actionLogin()
- {
- $this->layout= 'iframe';
- if (!Yii::$app->user->isGuest) {
- return $this->goHome();
- }
- $model = new LoginForm();
- if ($model->load(Yii::$app->request->post()) && $model->loginnew()) {
- return $this->redirect(['site/index']);
- } else {
- if(Yii::$app->request->isPost)
- Yii::$app->session->setFlash('error',$model->error);
- return $this->render('login', [
- 'model' => $model,
- ]);
- }
- }
- /**
- * Logs out the current user.
- *
- * @return mixed
- */
- public function actionLogout()
- {
- Yii::$app->user->logout();
- return $this->redirect(['/manage/site/login']);
- }
- /**
- * Displays contact page.
- *
- * @return mixed
- */
- public function actionContact()
- {
- $model = new ContactForm();
- if ($model->load(Yii::$app->request->post()) && $model->validate()) {
- if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
- Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
- } else {
- Yii::$app->session->setFlash('error', 'There was an error sending your message.');
- }
- return $this->refresh();
- } else {
- return $this->render('contact', [
- 'model' => $model,
- ]);
- }
- }
- /**
- * Displays about page.
- *
- * @return mixed
- */
- public function actionAbout()
- {
- return $this->render('about');
- }
- /**
- * Signs user up.
- *
- * @return mixed
- */
- public function actionSignup()
- {
- $this->layout= 'iframe';
- $model = new SignupForm();
- if ($model->load(Yii::$app->request->post())) {
- if ($user = $model->signup()) {
- if (Yii::$app->getUser()->login($user)) {
- return $this->goHome();
- }
- }
- }
- return $this->render('signup', [
- 'model' => $model,
- ]);
- }
- /**
- * Requests password reset.
- *
- * @return mixed
- */
- public function actionRequestPasswordReset()
- {
- $model = new PasswordResetRequestForm();
- if ($model->load(Yii::$app->request->post()) && $model->validate()) {
- if ($model->sendEmail()) {
- Yii::$app->session->setFlash('success', 'Check your email for further instructions.');
- return $this->goHome();
- } else {
- Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for the provided email address.');
- }
- }
- return $this->render('requestPasswordResetToken', [
- 'model' => $model,
- ]);
- }
- /**
- * Resets password.
- *
- * @param string $token
- * @return mixed
- * @throws BadRequestHttpException
- */
- public function actionResetPassword($token)
- {
- try {
- $model = new ResetPasswordForm($token);
- } catch (InvalidParamException $e) {
- throw new BadRequestHttpException($e->getMessage());
- }
- if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
- Yii::$app->session->setFlash('success', 'New password saved.');
- return $this->goHome();
- }
- return $this->render('resetPassword', [
- 'model' => $model,
- ]);
- }
- //找回密码页面
- public function actionBack(){
- $this->layout= 'iframe';
- return $this->render('back');
- }
- //验证码
- public function actionVerify(){
- $tel = Yii::$app->request->post('tel');
- if(preg_match("/^1[34578]\d{9}$/", $tel)){
- $user_tel = UserCompany::find()->where('tel=:tel',[':tel'=>$tel])->one();
- if(!empty($user_tel)){
- $miodel_code = rand(100000,999999);
- $model = new SortMessage();
- $model->tel = $tel;
- $model->code = "".$miodel_code;
- $model->created_at = time();
- if($model->validate()&&$model->save()){
- LMMessage::SendMessage($tel,'【'.Yii::$app->params['sitetitle'].'】验证码:'.$miodel_code);
- $result=['sign'=>1,'msg'=>"发送成功"];
- }else{
- $result=['sign'=>4000,'msg'=>"短信发送失败"];
- }
- }else{
- $result=['sign'=>4000,'msg'=>"请填写正确的手机号"];
- }
- }else{
- $result=['sign'=>4000,'msg'=>"手机格式错误"];
- }
- return json_encode($result);
- }
- public function actionVerifytel(){
- $code = Yii::$app->request->post('code');
- $tel = Yii::$app->request->post('tel');
- $model = SortMessage::find()->where('tel=:tell and created_at >= :time',[':tell'=>$tel,':time'=>(time()-1800)])->orderBy('created_at DESC')->one();
- if(!empty($model)&&$code==$model->code){
- $result=['sign'=>1,'msg'=>"验证成功"];
- }else{
- $result=['sign'=>4000,'msg'=>"验证码错误"];
- }
- return json_encode($result);
- }
- //修改密码页面
- public function actionUpdatepassword(){
- $this->layout= 'iframe';
- $tel = Yii::$app->request->post('tel');
- return $this->render('updatepassword',['tel'=>$tel]);
- }
- //新密码
- public function actionNewpassword(){
- $password1 = Yii::$app->request->post('password1');
- $password2 = Yii::$app->request->post('password2');
- $tel = Yii::$app->request->post('tel');
- if($password1 == $password2){
- $user = UserCompany::find()->where('tel=:tel',[':tel'=>$tel])->one();
- if(!empty($user)){
- $model = User::find()->where('id=:id',[':id'=>$user->uid])->one();
- $possword = Yii::$app->security->generatePasswordHash($password1);
- $model->password_hash = $possword;
- $model->updated_at = time();
- if($model->validate() && $model->save()){
- $result=['sign'=>1,'msg'=>"修改成功"];
- }else{
- $result=['sign'=>4000,'msg'=>"修改失败"];;
- }
- }else{
- $result=['sign'=>4000,'msg'=>"用户不存在"];
- }
- }else{
- $result=['sign'=>4000,'msg'=>"两次密码不一致"];
- }
- return json_encode($result);
- }
- /**
- * 重置登录账号
- */
- public function actionReset()
- {
- $token = Yii::$app->request->get('token');
- if(!empty($token))
- {
- $uid = UserCompany::_checkToken($token);
- if(!empty($uid))
- {
- $data_user = User::find()->select('username,status')->where(['id'=>$uid])->one();
- if($data_user->status != User::STATUS_ACTIVE)
- {
- Yii::$app->session->setFlash('error', '该账号已被关闭');
- return $this->redirect(['/manage/site/index']);
- }
- if(!empty($data_user)){
- Yii::$app->user->login(User::findByUsername($data_user->username));
- return $this->redirect(['/manage/site/index']);
- }
- }
- }
- }
- }
|