123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482 |
- <?php
- namespace company\modules\wechat\controllers;
- //use company\modules\wechat\controllers\LoginverifyController;
- use common\models\CompanyApply;
- use Yii;
- use yii\base\InvalidParamException;
- use yii\web\BadRequestHttpException;
- use yii\web\Controller;
- 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\library\WeChat;
- use common\models\User;
- use common\models\UserCompany;
- use common\models\SortMessage;
- //use common\library\CheckVerifCode;
- use yii\web\Cookie;
- use common\library\LMMessage;
- header("Content-type: text/html; charset=utf-8");
- /**
- * Site controller
- */
- class SiteController extends Controller
- {
- public $layout = 'main';
- /**
- * @inheritdoc
- */
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::className(),
- 'rules' => [
- [
- 'actions' => ['login','index','apply','error','init','wechat','verify','verifytel','captcha','forget','newpassword'],
- 'allow' => true,
- ],
- [
- 'actions' => ['logout','index','home'],
- '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 ? 'test' : null,
- 'maxLength' => 4,
- 'minLength' => 4,
- ],
- // 'captcha' => [
- // 'class' => 'yii\captcha\CaptchaAction',
- // 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
- // 'backColor'=>0x000000,//背景颜色
- // 'maxLength' => 6, //最大显示个数
- // 'minLength' => 5,//最少显示个数
- // 'padding' => 5,//间距
- // 'height'=>40,//高度
- // 'width' => 130, //宽度
- // 'foreColor'=>0xffffff, //字体颜色
- // 'offset'=>4, //设置字符偏移量 有效果
- //// 'controller'=>'login', //拥有这个动作的controller
- // ],
- ];
- }
- public function actionWechat(){
- $wechat = new WeChat();
- echo $wechat->checkToken();
- }
- /**
- * Displays homepage.
- *
- * @return mixed
- */
- public function actionIndex()
- {
- if (Yii::$app->user->isGuest) {
- return $this->redirect(['site/login']);
- //return $this->goHome();
- }
- return $this->render('index');
- }
- public function actionHome()
- {
- // $this->layout= 'iframe';
- return $this->render('home');
- }
- /**
- * Logs in a user.
- *
- * @return mixed
- */
- public function actionLogin()
- {
- if (!Yii::$app->user->isGuest) {
- return $this->redirect(['site/index']);
- //return $this->goHome();
- }
- $model = new LoginForm();
- $cookies=Yii::$app->response->cookies;
- if(Yii::$app->request->isPost){
- if ($model->load(Yii::$app->request->post()) && $model->login()) {
- $rememberMe=(Yii::$app->request->post('rememberMe'))?1:0;
- if($rememberMe==1){
- $cookietime = time()+60*60*24*30*3;
- $cookies->add(new\yii\web\Cookie([
- 'name'=>'username',
- 'value'=>Yii::$app->request->post('LoginForm')['username'],
- 'expire'=>$cookietime
- ]));
- $cookies->add(new\yii\web\Cookie([
- 'name'=>'password',
- 'value'=>Yii::$app->request->post('LoginForm')['password'],
- 'expire'=>$cookietime
- ]));
- $cookies->add(new\yii\web\Cookie([
- 'name'=>'rememberme',
- 'value'=>$rememberMe,
- 'expire'=>$cookietime
- ]));
- }
- return $this->redirect(['site/index']);
- } else {
- $this->admin_alert("账号密码错误,请重新输入!","");
- // Yii::$app->getSession()->setFlash('error', '账号密码错误,请重新输入!');
- }
- }else{
- if(!empty($cookies['username'])&&!empty($cookies['password'])){
- $model->username = $cookies->getValue('username');
- $model->password = $cookies->getValue('password');
- if( $model->login()){
- return $this->redirect(['site/index']);
- }else{
- $cookies = Yii::$app->response->cookies;
- $cookietime = time();
- $cookies->add(new\yii\web\Cookie([
- 'name'=>'username',
- 'value'=>$cookies->getValue('username'),
- 'expire'=>$cookietime
- ]));
- $cookies->add(new\yii\web\Cookie([
- 'name'=>'password',
- 'value'=>$cookies->getValue('password'),
- 'expire'=>$cookietime
- ]));
- $cookies->remove('username');
- $cookies->remove('password');
- $this->admin_alert("密码错误,请重新输入!","");
- // Yii::$app->getSession()->setFlash('error', '密码错误,请重新输入!');
- // return $this->redirect(['site/login']);
- }
- }
- }
- return $this->render('login', [
- 'model' => $model,
- ]);
- // $checkCode = new CheckVerifCode();
- // return $this->render('login', []);
- // $wechat = new WeChat();
- // $isWecha = $wechat->isWecha();//判断是否微信登陆
- // if ($isWecha) {
- // $code = Yii::$app->request->get('code');
- // if (!empty($code)) {
- // $info = $wechat->getAccess_token($code);
- // if ($info == false) {
- // echo "授权失败";
- // exit;
- // }
- // $results = $wechat->getUserInfo($info);
- // $cookies = Yii::$app->response->cookies;
- // $cookies->add(new Cookie(['name'=>'headimgurl','value'=>$results->headimgurl]));
- // $wechatdata = UserCompany::find()->where('openid=:openid', [':openid' => $info->openid])->one();
- // if ($wechatdata && Yii::$app->user->login(User::findOne($wechatdata['uid'])))//存在设为登陆状态
- // {
- // return $this->redirect(['/wechat/site/index']);
- // }else{
- // if(!Yii::$app->user->isGuest){
- // return $this->redirect(['/wechat/site/index']);
- // }
- // $model = new LoginForm();
- // $headimgurl = Yii::$app->request->cookies->getValue('headimgurl');
- // if ($model->load(Yii::$app->request->post()) && $model->login()) {
- // return $this->redirect(['/wechat/site/index'])/*$this->goBack()*/;
- // } else {
- // return $this->render('login', [
- // 'model' => $model,
- // 'headimgurl'=>$headimgurl
- // ]);
- // }
- // }
- // } else {
- // $backurl = Yii::$app->request->getHostInfo() . Yii::$app->request->url;//完整地址
- // return $this->redirect($wechat->markUrl($backurl));
- // }
- // }
- // if (!Yii::$app->user->isGuest) {
- // return $this->goHome();
- // }
- //
- // $model = new LoginForm();
- // if ($model->load(Yii::$app->request->post()) && $model->login()) {
- // return $this->redirect(['site/index'])/*$this->goBack()*/;
- // } else {
- // return $this->render('login', [
- // 'model' => $model,
- // ]);
- // }
- // return $this->render('login',['model'=>new LoginForm(),'headimgurl'=>'']);
- }
- /**
- * Logs out the current user.
- *
- * @return mixed
- */
- public function actionLogout()
- {
- Yii::$app->user->logout();
- return $this->redirect(['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 actionVerify(){
- if(!session_id()) session_start();
- $code = Yii::$app->request->post('code');
- $tel = Yii::$app->request->post('tel');
- $code1 =$_SESSION['code'] /*Yii::$app->session['code']*/;
- // $result=['sign'=>4000,'msg'=>$code1];
- // return json_encode($result);
- if($code==$code1){
- $model = SortMessage::find()->where('tel=:tell and created_at >= :time',[':tell'=>$tel,':time'=>(time()-120)])->orderBy('created_at DESC')->one();
- $result=['sign'=>1,'msg'=>"已发送"];
- 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()){
- // .Yii::$app->params["sitetitile"].
- 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'=>"手机格式错误"];
- }
- }else{
- $result=['sign'=>4000,'msg'=>'验证码不一致'];
- }
- return json_encode($result);
- }
- public function actionVerifytel(){
- $code = Yii::$app->request->post('code');
- $tel = Yii::$app->request->post('tel');
- $result=['sign'=>1,'msg'=>"已发送"];
- $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'=>"验证成功"];
- // $user = UserCompany::find()->where('tel=:tel',[':tel'=>$tel])->one();
- // $user->openid = Yii::$app->request->cookies->getValue("acc_openid");
- // $user->updated_at = time();
- // if($user->validate()&&$user->save()){
- // if( Yii::$app->user->login(User::findOne($user->uid))){
- // $result=['sign'=>1,'msg'=>"登录成功"];
- // }else{
- // $result=['sign'=>1,'msg'=>"登录失败"];
- // }
- // }else{
- // $result=['sign'=>4000,'msg'=>"绑定失败"];
- // }
- }else{
- $result=['sign'=>4000,'msg'=>"验证码错误"];
- }
- return json_encode($result);
- }
- //弹窗提示
- function admin_alert($alert,$href=''){
- if(empty($href)){
- exit("<script>alert('$alert');history.back();</script>");
- }else{
- exit("<script>alert('$alert');window.location.href='{$href}';</script>");
- }
- }
- //找回密码
- public function actionForget(){
- return $this->render('forget');
- }
- //新密码
- public function actionNewpassword(){
- $password1 = Yii::$app->request->post('password1');
- $password2 = Yii::$app->request->post('password2');
- $tel = Yii::$app->request->post('tel');
- $result=['sign'=>4000,'msg'=>$password1."、".$password2."、".$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 actionApply()
- {
- if(Yii::$app->request->isPost)
- {
- $model = new CompanyApply();
- if($model->load(Yii::$app->request->post()))
- {
- if($model->validate()&&$model->save())
- {
- Yii::$app->getSession()->setFlash('success', '申请成功');
- }else{
- Yii::$app->getSession()->setFlash('error', '申请失败');
- }
- }
- return $this->redirect(Yii::$app->request->referrer);
- }
- return $this->render('apply');
- }
- }
|