SiteController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <?php
  2. namespace company\modules\wechat\controllers;
  3. //use company\modules\wechat\controllers\LoginverifyController;
  4. use common\models\CompanyApply;
  5. use Yii;
  6. use yii\base\InvalidParamException;
  7. use yii\web\BadRequestHttpException;
  8. use yii\web\Controller;
  9. use yii\filters\VerbFilter;
  10. use yii\filters\AccessControl;
  11. use common\models\LoginForm;
  12. use company\models\PasswordResetRequestForm;
  13. use company\models\ResetPasswordForm;
  14. use company\models\SignupForm;
  15. use company\models\ContactForm;
  16. use common\library\WeChat;
  17. use common\models\User;
  18. use common\models\UserCompany;
  19. use common\models\SortMessage;
  20. //use common\library\CheckVerifCode;
  21. use yii\web\Cookie;
  22. use common\library\LMMessage;
  23. header("Content-type: text/html; charset=utf-8");
  24. /**
  25. * Site controller
  26. */
  27. class SiteController extends Controller
  28. {
  29. public $layout = 'main';
  30. /**
  31. * @inheritdoc
  32. */
  33. public function behaviors()
  34. {
  35. return [
  36. 'access' => [
  37. 'class' => AccessControl::className(),
  38. 'rules' => [
  39. [
  40. 'actions' => ['login','index','apply','error','init','wechat','verify','verifytel','captcha','forget','newpassword'],
  41. 'allow' => true,
  42. ],
  43. [
  44. 'actions' => ['logout','index','home'],
  45. 'allow' => true,
  46. 'roles' => ['@'],
  47. ],
  48. ],
  49. ],
  50. 'verbs' => [
  51. 'class' => VerbFilter::className(),
  52. 'actions' => [
  53. 'logout' => ['post'],
  54. ],
  55. ],
  56. ];
  57. }
  58. /**
  59. * @inheritdoc
  60. */
  61. public function actions()
  62. {
  63. return [
  64. // 'error' => [
  65. // 'class' => 'yii\web\ErrorAction',
  66. // ],
  67. 'captcha' => [
  68. 'class' => 'yii\captcha\CaptchaAction',
  69. // 'fixedVerifyCode' => YII_ENV_TEST ? 'test' : null,
  70. 'maxLength' => 4,
  71. 'minLength' => 4,
  72. ],
  73. // 'captcha' => [
  74. // 'class' => 'yii\captcha\CaptchaAction',
  75. // 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  76. // 'backColor'=>0x000000,//背景颜色
  77. // 'maxLength' => 6, //最大显示个数
  78. // 'minLength' => 5,//最少显示个数
  79. // 'padding' => 5,//间距
  80. // 'height'=>40,//高度
  81. // 'width' => 130, //宽度
  82. // 'foreColor'=>0xffffff, //字体颜色
  83. // 'offset'=>4, //设置字符偏移量 有效果
  84. //// 'controller'=>'login', //拥有这个动作的controller
  85. // ],
  86. ];
  87. }
  88. public function actionWechat(){
  89. $wechat = new WeChat();
  90. echo $wechat->checkToken();
  91. }
  92. /**
  93. * Displays homepage.
  94. *
  95. * @return mixed
  96. */
  97. public function actionIndex()
  98. {
  99. if (Yii::$app->user->isGuest) {
  100. return $this->redirect(['site/login']);
  101. //return $this->goHome();
  102. }
  103. return $this->render('index');
  104. }
  105. public function actionHome()
  106. {
  107. // $this->layout= 'iframe';
  108. return $this->render('home');
  109. }
  110. /**
  111. * Logs in a user.
  112. *
  113. * @return mixed
  114. */
  115. public function actionLogin()
  116. {
  117. if (!Yii::$app->user->isGuest) {
  118. return $this->redirect(['site/index']);
  119. //return $this->goHome();
  120. }
  121. $model = new LoginForm();
  122. $cookies=Yii::$app->response->cookies;
  123. if(Yii::$app->request->isPost){
  124. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  125. $rememberMe=(Yii::$app->request->post('rememberMe'))?1:0;
  126. if($rememberMe==1){
  127. $cookietime = time()+60*60*24*30*3;
  128. $cookies->add(new\yii\web\Cookie([
  129. 'name'=>'username',
  130. 'value'=>Yii::$app->request->post('LoginForm')['username'],
  131. 'expire'=>$cookietime
  132. ]));
  133. $cookies->add(new\yii\web\Cookie([
  134. 'name'=>'password',
  135. 'value'=>Yii::$app->request->post('LoginForm')['password'],
  136. 'expire'=>$cookietime
  137. ]));
  138. $cookies->add(new\yii\web\Cookie([
  139. 'name'=>'rememberme',
  140. 'value'=>$rememberMe,
  141. 'expire'=>$cookietime
  142. ]));
  143. }
  144. return $this->redirect(['site/index']);
  145. } else {
  146. $this->admin_alert("账号密码错误,请重新输入!","");
  147. // Yii::$app->getSession()->setFlash('error', '账号密码错误,请重新输入!');
  148. }
  149. }else{
  150. if(!empty($cookies['username'])&&!empty($cookies['password'])){
  151. $model->username = $cookies->getValue('username');
  152. $model->password = $cookies->getValue('password');
  153. if( $model->login()){
  154. return $this->redirect(['site/index']);
  155. }else{
  156. $cookies = Yii::$app->response->cookies;
  157. $cookietime = time();
  158. $cookies->add(new\yii\web\Cookie([
  159. 'name'=>'username',
  160. 'value'=>$cookies->getValue('username'),
  161. 'expire'=>$cookietime
  162. ]));
  163. $cookies->add(new\yii\web\Cookie([
  164. 'name'=>'password',
  165. 'value'=>$cookies->getValue('password'),
  166. 'expire'=>$cookietime
  167. ]));
  168. $cookies->remove('username');
  169. $cookies->remove('password');
  170. $this->admin_alert("密码错误,请重新输入!","");
  171. // Yii::$app->getSession()->setFlash('error', '密码错误,请重新输入!');
  172. // return $this->redirect(['site/login']);
  173. }
  174. }
  175. }
  176. return $this->render('login', [
  177. 'model' => $model,
  178. ]);
  179. // $checkCode = new CheckVerifCode();
  180. // return $this->render('login', []);
  181. // $wechat = new WeChat();
  182. // $isWecha = $wechat->isWecha();//判断是否微信登陆
  183. // if ($isWecha) {
  184. // $code = Yii::$app->request->get('code');
  185. // if (!empty($code)) {
  186. // $info = $wechat->getAccess_token($code);
  187. // if ($info == false) {
  188. // echo "授权失败";
  189. // exit;
  190. // }
  191. // $results = $wechat->getUserInfo($info);
  192. // $cookies = Yii::$app->response->cookies;
  193. // $cookies->add(new Cookie(['name'=>'headimgurl','value'=>$results->headimgurl]));
  194. // $wechatdata = UserCompany::find()->where('openid=:openid', [':openid' => $info->openid])->one();
  195. // if ($wechatdata && Yii::$app->user->login(User::findOne($wechatdata['uid'])))//存在设为登陆状态
  196. // {
  197. // return $this->redirect(['/wechat/site/index']);
  198. // }else{
  199. // if(!Yii::$app->user->isGuest){
  200. // return $this->redirect(['/wechat/site/index']);
  201. // }
  202. // $model = new LoginForm();
  203. // $headimgurl = Yii::$app->request->cookies->getValue('headimgurl');
  204. // if ($model->load(Yii::$app->request->post()) && $model->login()) {
  205. // return $this->redirect(['/wechat/site/index'])/*$this->goBack()*/;
  206. // } else {
  207. // return $this->render('login', [
  208. // 'model' => $model,
  209. // 'headimgurl'=>$headimgurl
  210. // ]);
  211. // }
  212. // }
  213. // } else {
  214. // $backurl = Yii::$app->request->getHostInfo() . Yii::$app->request->url;//完整地址
  215. // return $this->redirect($wechat->markUrl($backurl));
  216. // }
  217. // }
  218. // if (!Yii::$app->user->isGuest) {
  219. // return $this->goHome();
  220. // }
  221. //
  222. // $model = new LoginForm();
  223. // if ($model->load(Yii::$app->request->post()) && $model->login()) {
  224. // return $this->redirect(['site/index'])/*$this->goBack()*/;
  225. // } else {
  226. // return $this->render('login', [
  227. // 'model' => $model,
  228. // ]);
  229. // }
  230. // return $this->render('login',['model'=>new LoginForm(),'headimgurl'=>'']);
  231. }
  232. /**
  233. * Logs out the current user.
  234. *
  235. * @return mixed
  236. */
  237. public function actionLogout()
  238. {
  239. Yii::$app->user->logout();
  240. return $this->redirect(['site/login']);
  241. }
  242. /**
  243. * Displays contact page.
  244. *
  245. * @return mixed
  246. */
  247. public function actionContact()
  248. {
  249. $model = new ContactForm();
  250. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  251. if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
  252. Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
  253. } else {
  254. Yii::$app->session->setFlash('error', 'There was an error sending your message.');
  255. }
  256. return $this->refresh();
  257. } else {
  258. return $this->render('contact', [
  259. 'model' => $model,
  260. ]);
  261. }
  262. }
  263. /**
  264. * Displays about page.
  265. *
  266. * @return mixed
  267. */
  268. public function actionAbout()
  269. {
  270. return $this->render('about');
  271. }
  272. /**
  273. * Signs user up.
  274. *
  275. * @return mixed
  276. */
  277. public function actionSignup()
  278. {
  279. $this->layout= 'iframe';
  280. $model = new SignupForm();
  281. if ($model->load(Yii::$app->request->post())) {
  282. if ($user = $model->signup()) {
  283. if (Yii::$app->getUser()->login($user)) {
  284. return $this->goHome();
  285. }
  286. }
  287. }
  288. return $this->render('signup', [
  289. 'model' => $model,
  290. ]);
  291. }
  292. /**
  293. * Requests password reset.
  294. *
  295. * @return mixed
  296. */
  297. public function actionRequestPasswordReset()
  298. {
  299. $model = new PasswordResetRequestForm();
  300. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  301. if ($model->sendEmail()) {
  302. Yii::$app->session->setFlash('success', 'Check your email for further instructions.');
  303. return $this->goHome();
  304. } else {
  305. Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for the provided email address.');
  306. }
  307. }
  308. return $this->render('requestPasswordResetToken', [
  309. 'model' => $model,
  310. ]);
  311. }
  312. /**
  313. * Resets password.
  314. *
  315. * @param string $token
  316. * @return mixed
  317. * @throws BadRequestHttpException
  318. */
  319. public function actionResetPassword($token)
  320. {
  321. try {
  322. $model = new ResetPasswordForm($token);
  323. } catch (InvalidParamException $e) {
  324. throw new BadRequestHttpException($e->getMessage());
  325. }
  326. if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
  327. Yii::$app->session->setFlash('success', 'New password saved.');
  328. return $this->goHome();
  329. }
  330. return $this->render('resetPassword', [
  331. 'model' => $model,
  332. ]);
  333. }
  334. //验证绑定图片验证码、发送图片
  335. public function actionVerify(){
  336. if(!session_id()) session_start();
  337. $code = Yii::$app->request->post('code');
  338. $tel = Yii::$app->request->post('tel');
  339. $code1 =$_SESSION['code'] /*Yii::$app->session['code']*/;
  340. // $result=['sign'=>4000,'msg'=>$code1];
  341. // return json_encode($result);
  342. if($code==$code1){
  343. $model = SortMessage::find()->where('tel=:tell and created_at >= :time',[':tell'=>$tel,':time'=>(time()-120)])->orderBy('created_at DESC')->one();
  344. $result=['sign'=>1,'msg'=>"已发送"];
  345. if(preg_match("/^1[34578]\d{9}$/", $tel)){
  346. $user_tel = UserCompany::find()->where('tel=:tel',[':tel'=>$tel])->one();
  347. if(!empty($user_tel)){
  348. $miodel_code = rand(100000,999999);
  349. $model = new SortMessage();
  350. $model->tel = $tel;
  351. $model->code = "".$miodel_code;
  352. $model->created_at = time();
  353. if($model->validate()&&$model->save()){
  354. // .Yii::$app->params["sitetitile"].
  355. LMMessage::SendMessage($tel,'【'.Yii::$app->params['sitetitle'].'】验证码:'.$miodel_code);
  356. $result=['sign'=>1,'msg'=>"发送成功"];
  357. }else{
  358. $result=['sign'=>4000,'msg'=>"短信发送失败"];
  359. }
  360. }else{
  361. $result=['sign'=>4000,'msg'=>"请填写正确的手机号"];
  362. }
  363. }else{
  364. $result=['sign'=>4000,'msg'=>"手机格式错误"];
  365. }
  366. }else{
  367. $result=['sign'=>4000,'msg'=>'验证码不一致'];
  368. }
  369. return json_encode($result);
  370. }
  371. public function actionVerifytel(){
  372. $code = Yii::$app->request->post('code');
  373. $tel = Yii::$app->request->post('tel');
  374. $result=['sign'=>1,'msg'=>"已发送"];
  375. $model = SortMessage::find()->where('tel=:tell and created_at >= :time',[':tell'=>$tel,':time'=>(time()-1800)])->orderBy('created_at DESC')->one();
  376. if(!empty($model)&&$code==$model->code){
  377. $result=['sign'=>1,'msg'=>"验证成功"];
  378. // $user = UserCompany::find()->where('tel=:tel',[':tel'=>$tel])->one();
  379. // $user->openid = Yii::$app->request->cookies->getValue("acc_openid");
  380. // $user->updated_at = time();
  381. // if($user->validate()&&$user->save()){
  382. // if( Yii::$app->user->login(User::findOne($user->uid))){
  383. // $result=['sign'=>1,'msg'=>"登录成功"];
  384. // }else{
  385. // $result=['sign'=>1,'msg'=>"登录失败"];
  386. // }
  387. // }else{
  388. // $result=['sign'=>4000,'msg'=>"绑定失败"];
  389. // }
  390. }else{
  391. $result=['sign'=>4000,'msg'=>"验证码错误"];
  392. }
  393. return json_encode($result);
  394. }
  395. //弹窗提示
  396. function admin_alert($alert,$href=''){
  397. if(empty($href)){
  398. exit("<script>alert('$alert');history.back();</script>");
  399. }else{
  400. exit("<script>alert('$alert');window.location.href='{$href}';</script>");
  401. }
  402. }
  403. //找回密码
  404. public function actionForget(){
  405. return $this->render('forget');
  406. }
  407. //新密码
  408. public function actionNewpassword(){
  409. $password1 = Yii::$app->request->post('password1');
  410. $password2 = Yii::$app->request->post('password2');
  411. $tel = Yii::$app->request->post('tel');
  412. $result=['sign'=>4000,'msg'=>$password1."、".$password2."、".$tel];
  413. if($password1 == $password2){
  414. $user = UserCompany::find()->where('tel=:tel',[':tel'=>$tel])->one();
  415. if(!empty($user)){
  416. $model = User::find()->where('id=:id',[':id'=>$user->uid])->one();
  417. $possword = Yii::$app->security->generatePasswordHash($password1);
  418. $model->password_hash = $possword;
  419. $model->updated_at = time();
  420. if($model->validate() && $model->save()){
  421. $result=['sign'=>1,'msg'=>"修改成功"];
  422. }else{
  423. $result=['sign'=>4000,'msg'=>"修改失败"];;
  424. }
  425. }else{
  426. $result=['sign'=>4000,'msg'=>"用户不存在"];
  427. }
  428. }else{
  429. $result=['sign'=>4000,'msg'=>"两次密码不一致"];
  430. }
  431. return json_encode($result);
  432. }
  433. /**
  434. * 申请装修公司
  435. */
  436. public function actionApply()
  437. {
  438. if(Yii::$app->request->isPost)
  439. {
  440. $model = new CompanyApply();
  441. if($model->load(Yii::$app->request->post()))
  442. {
  443. if($model->validate()&&$model->save())
  444. {
  445. Yii::$app->getSession()->setFlash('success', '申请成功');
  446. }else{
  447. Yii::$app->getSession()->setFlash('error', '申请失败');
  448. }
  449. }
  450. return $this->redirect(Yii::$app->request->referrer);
  451. }
  452. return $this->render('apply');
  453. }
  454. }