SiteController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. namespace company\modules\manage\controllers;
  3. use common\library\LMMessage;
  4. use common\models\User;
  5. use Yii;
  6. use yii\base\InvalidParamException;
  7. use yii\web\BadRequestHttpException;
  8. use yii\web\Controller;
  9. use company\modules\manage\controllers\LoginverifyController;
  10. use yii\filters\VerbFilter;
  11. use yii\filters\AccessControl;
  12. use common\models\LoginForm;
  13. use company\models\PasswordResetRequestForm;
  14. use company\models\ResetPasswordForm;
  15. use company\models\SignupForm;
  16. use company\models\ContactForm;
  17. use common\models\SortMessage;
  18. use common\models\UserCompany;
  19. /**
  20. * Site controller
  21. */
  22. class SiteController extends LoginverifyController
  23. {
  24. public $layout = 'hplus';
  25. /**
  26. * @inheritdoc
  27. */
  28. public function behaviors()
  29. {
  30. return [
  31. 'access' => [
  32. 'class' => AccessControl::className(),
  33. 'rules' => [
  34. [
  35. 'actions' => ['login','error','init','back','updatepassword'],
  36. 'allow' => true,
  37. ],
  38. [
  39. 'actions' => ['logout','index','home','back','updatepassword','reset'],
  40. 'allow' => true,
  41. 'roles' => ['@'],
  42. ],
  43. ],
  44. ],
  45. 'verbs' => [
  46. 'class' => VerbFilter::className(),
  47. 'actions' => [
  48. 'logout' => ['post'],
  49. ],
  50. ],
  51. ];
  52. }
  53. /**
  54. * @inheritdoc
  55. */
  56. public function actions()
  57. {
  58. return [
  59. 'error' => [
  60. 'class' => 'yii\web\ErrorAction',
  61. ],
  62. 'captcha' => [
  63. 'class' => 'yii\captcha\CaptchaAction',
  64. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  65. ],
  66. ];
  67. }
  68. /**
  69. * Displays homepage.
  70. *
  71. * @return mixed
  72. */
  73. public function actionIndex()
  74. {
  75. if (Yii::$app->user->isGuest) {
  76. return $this->redirect(['site/login']);
  77. }
  78. return $this->render('index');
  79. }
  80. public function actionHome()
  81. {
  82. $this->layout= 'iframe';
  83. return $this->render('home');
  84. }
  85. /**
  86. * Logs in a user.
  87. *
  88. * @return mixed
  89. */
  90. public function actionLogin()
  91. {
  92. $this->layout= 'iframe';
  93. if (!Yii::$app->user->isGuest) {
  94. return $this->goHome();
  95. }
  96. $model = new LoginForm();
  97. if ($model->load(Yii::$app->request->post()) && $model->loginnew()) {
  98. return $this->redirect(['site/index']);
  99. } else {
  100. if(Yii::$app->request->isPost)
  101. Yii::$app->session->setFlash('error',$model->error);
  102. return $this->render('login', [
  103. 'model' => $model,
  104. ]);
  105. }
  106. }
  107. /**
  108. * Logs out the current user.
  109. *
  110. * @return mixed
  111. */
  112. public function actionLogout()
  113. {
  114. Yii::$app->user->logout();
  115. return $this->redirect(['/manage/site/login']);
  116. }
  117. /**
  118. * Displays contact page.
  119. *
  120. * @return mixed
  121. */
  122. public function actionContact()
  123. {
  124. $model = new ContactForm();
  125. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  126. if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
  127. Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
  128. } else {
  129. Yii::$app->session->setFlash('error', 'There was an error sending your message.');
  130. }
  131. return $this->refresh();
  132. } else {
  133. return $this->render('contact', [
  134. 'model' => $model,
  135. ]);
  136. }
  137. }
  138. /**
  139. * Displays about page.
  140. *
  141. * @return mixed
  142. */
  143. public function actionAbout()
  144. {
  145. return $this->render('about');
  146. }
  147. /**
  148. * Signs user up.
  149. *
  150. * @return mixed
  151. */
  152. public function actionSignup()
  153. {
  154. $this->layout= 'iframe';
  155. $model = new SignupForm();
  156. if ($model->load(Yii::$app->request->post())) {
  157. if ($user = $model->signup()) {
  158. if (Yii::$app->getUser()->login($user)) {
  159. return $this->goHome();
  160. }
  161. }
  162. }
  163. return $this->render('signup', [
  164. 'model' => $model,
  165. ]);
  166. }
  167. /**
  168. * Requests password reset.
  169. *
  170. * @return mixed
  171. */
  172. public function actionRequestPasswordReset()
  173. {
  174. $model = new PasswordResetRequestForm();
  175. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  176. if ($model->sendEmail()) {
  177. Yii::$app->session->setFlash('success', 'Check your email for further instructions.');
  178. return $this->goHome();
  179. } else {
  180. Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for the provided email address.');
  181. }
  182. }
  183. return $this->render('requestPasswordResetToken', [
  184. 'model' => $model,
  185. ]);
  186. }
  187. /**
  188. * Resets password.
  189. *
  190. * @param string $token
  191. * @return mixed
  192. * @throws BadRequestHttpException
  193. */
  194. public function actionResetPassword($token)
  195. {
  196. try {
  197. $model = new ResetPasswordForm($token);
  198. } catch (InvalidParamException $e) {
  199. throw new BadRequestHttpException($e->getMessage());
  200. }
  201. if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
  202. Yii::$app->session->setFlash('success', 'New password saved.');
  203. return $this->goHome();
  204. }
  205. return $this->render('resetPassword', [
  206. 'model' => $model,
  207. ]);
  208. }
  209. //找回密码页面
  210. public function actionBack(){
  211. $this->layout= 'iframe';
  212. return $this->render('back');
  213. }
  214. //验证码
  215. public function actionVerify(){
  216. $tel = Yii::$app->request->post('tel');
  217. if(preg_match("/^1[34578]\d{9}$/", $tel)){
  218. $user_tel = UserCompany::find()->where('tel=:tel',[':tel'=>$tel])->one();
  219. if(!empty($user_tel)){
  220. $miodel_code = rand(100000,999999);
  221. $model = new SortMessage();
  222. $model->tel = $tel;
  223. $model->code = "".$miodel_code;
  224. $model->created_at = time();
  225. if($model->validate()&&$model->save()){
  226. LMMessage::SendMessage($tel,'【'.Yii::$app->params['sitetitle'].'】验证码:'.$miodel_code);
  227. $result=['sign'=>1,'msg'=>"发送成功"];
  228. }else{
  229. $result=['sign'=>4000,'msg'=>"短信发送失败"];
  230. }
  231. }else{
  232. $result=['sign'=>4000,'msg'=>"请填写正确的手机号"];
  233. }
  234. }else{
  235. $result=['sign'=>4000,'msg'=>"手机格式错误"];
  236. }
  237. return json_encode($result);
  238. }
  239. public function actionVerifytel(){
  240. $code = Yii::$app->request->post('code');
  241. $tel = Yii::$app->request->post('tel');
  242. $model = SortMessage::find()->where('tel=:tell and created_at >= :time',[':tell'=>$tel,':time'=>(time()-1800)])->orderBy('created_at DESC')->one();
  243. if(!empty($model)&&$code==$model->code){
  244. $result=['sign'=>1,'msg'=>"验证成功"];
  245. }else{
  246. $result=['sign'=>4000,'msg'=>"验证码错误"];
  247. }
  248. return json_encode($result);
  249. }
  250. //修改密码页面
  251. public function actionUpdatepassword(){
  252. $this->layout= 'iframe';
  253. $tel = Yii::$app->request->post('tel');
  254. return $this->render('updatepassword',['tel'=>$tel]);
  255. }
  256. //新密码
  257. public function actionNewpassword(){
  258. $password1 = Yii::$app->request->post('password1');
  259. $password2 = Yii::$app->request->post('password2');
  260. $tel = Yii::$app->request->post('tel');
  261. if($password1 == $password2){
  262. $user = UserCompany::find()->where('tel=:tel',[':tel'=>$tel])->one();
  263. if(!empty($user)){
  264. $model = User::find()->where('id=:id',[':id'=>$user->uid])->one();
  265. $possword = Yii::$app->security->generatePasswordHash($password1);
  266. $model->password_hash = $possword;
  267. $model->updated_at = time();
  268. if($model->validate() && $model->save()){
  269. $result=['sign'=>1,'msg'=>"修改成功"];
  270. }else{
  271. $result=['sign'=>4000,'msg'=>"修改失败"];;
  272. }
  273. }else{
  274. $result=['sign'=>4000,'msg'=>"用户不存在"];
  275. }
  276. }else{
  277. $result=['sign'=>4000,'msg'=>"两次密码不一致"];
  278. }
  279. return json_encode($result);
  280. }
  281. /**
  282. * 重置登录账号
  283. */
  284. public function actionReset()
  285. {
  286. $token = Yii::$app->request->get('token');
  287. if(!empty($token))
  288. {
  289. $uid = UserCompany::_checkToken($token);
  290. if(!empty($uid))
  291. {
  292. $data_user = User::find()->select('username,status')->where(['id'=>$uid])->one();
  293. if($data_user->status != User::STATUS_ACTIVE)
  294. {
  295. Yii::$app->session->setFlash('error', '该账号已被关闭');
  296. return $this->redirect(['/manage/site/index']);
  297. }
  298. if(!empty($data_user)){
  299. Yii::$app->user->login(User::findByUsername($data_user->username));
  300. return $this->redirect(['/manage/site/index']);
  301. }
  302. }
  303. }
  304. }
  305. }