DefaultController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 16/7/26
  6. * Time: 下午5:56
  7. */
  8. namespace common\modules\user\backend\controllers;
  9. use common\components\Controller;
  10. use common\helpers\Util;
  11. use common\modules\user\backend\models\UserSearch;
  12. use common\modules\user\models\LoginForm;
  13. use common\modules\user\models\Profile;
  14. use common\modules\user\models\QrcodeLoginForm;
  15. use common\modules\user\models\User;
  16. use common\modules\user\traits\AjaxValidationTrait;
  17. use Yii;
  18. use yii\data\ActiveDataProvider;
  19. use yii\filters\VerbFilter;
  20. use yii\helpers\Url;
  21. use yii\web\ForbiddenHttpException;
  22. use yii\web\NotFoundHttpException;
  23. class DefaultController extends Controller
  24. {
  25. // use AjaxValidationTrait;
  26. /** @inheritdoc */
  27. public function behaviors()
  28. {
  29. return [
  30. 'verbs' => [
  31. 'class' => VerbFilter::className(),
  32. 'actions' => [
  33. 'delete' => ['post'],
  34. 'confirm' => ['post'],
  35. 'block' => ['post'],
  36. ],
  37. ],
  38. ];
  39. }
  40. public function actionLogin()
  41. {
  42. $this->layout = '@common/modules/user/backend/views/default/main-login.php';
  43. if (!\Yii::$app->user->isGuest) {
  44. return $this->goHome();
  45. }
  46. $model = new LoginForm();
  47. if ($model->load(Yii::$app->request->post()) && $model->loginAdmin()) {
  48. return $this->goBack();
  49. } else {
  50. if (Yii::$app->request->isAjax) {
  51. return $this->renderAjax('login', [
  52. 'model' => $model,
  53. ]);
  54. }
  55. return $this->render('login', [
  56. 'model' => $model,
  57. ]);
  58. }
  59. }
  60. public function actionLogout()
  61. {
  62. Yii::$app->user->logout();
  63. return $this->goHome();
  64. }
  65. /**
  66. * Lists all User models.
  67. * @return mixed
  68. */
  69. public function actionIndex()
  70. {
  71. $searchModel = new UserSearch();
  72. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  73. return $this->render('index', [
  74. 'dataProvider' => $dataProvider,
  75. 'searchModel' => $searchModel,
  76. ]);
  77. }
  78. /**
  79. * Displays a single User model.
  80. * @param integer $id
  81. * @return mixed
  82. */
  83. public function actionView($id)
  84. {
  85. return $this->render('view', [
  86. 'model' => $this->findModel($id),
  87. ]);
  88. }
  89. /**
  90. * Creates a new User model.
  91. * If creation is successful, the browser will be redirected to the 'view' page.
  92. * @return mixed
  93. */
  94. public function actionCreate()
  95. {
  96. /** @var User $user */
  97. $user = \Yii::createObject([
  98. 'class' => User::className(),
  99. 'scenario' => 'create',
  100. ]);
  101. if ($user->load(\Yii::$app->request->post()) && $user->create()) {
  102. \Yii::$app->getSession()->setFlash('success', \Yii::t('user', 'User has been created'));
  103. return $this->redirect(['update', 'id' => $user->id]);
  104. }
  105. return $this->render('create', [
  106. 'user' => $user,
  107. ]);
  108. }
  109. /**
  110. * Updates an existing User model.
  111. * If update is successful, the browser will be redirected to the 'view' page.
  112. * @param integer $id
  113. * @return mixed
  114. */
  115. public function actionUpdate($id)
  116. {
  117. Url::remember('', 'actions-redirect');
  118. $user = $this->findModel($id);
  119. $user->scenario = 'update';
  120. if ($user->load(\Yii::$app->request->post()) && $user->save()) {
  121. \Yii::$app->getSession()->setFlash('success', \Yii::t('user', 'Account details have been updated'));
  122. return $this->refresh();
  123. }
  124. return $this->render('_account', [
  125. 'user' => $user,
  126. ]);
  127. }
  128. /**
  129. * Deletes an existing User model.
  130. * If deletion is successful, the browser will be redirected to the 'index' page.
  131. * @param integer $id
  132. * @return mixed
  133. */
  134. public function actionDelete($id)
  135. {
  136. $this->findModel($id)->delete();
  137. return $this->redirect(['index']);
  138. }
  139. /**
  140. * Finds the User model based on its primary key value.
  141. * If the model is not found, a 404 HTTP exception will be thrown.
  142. * @param integer $id
  143. * @return User the loaded model
  144. * @throws NotFoundHttpException if the model cannot be found
  145. */
  146. public function findModel($id)
  147. {
  148. if (($model = User::findOne($id)) !== null) {
  149. return $model;
  150. } else {
  151. throw new NotFoundHttpException('The requested page does not exist.');
  152. }
  153. }
  154. /**
  155. * 重置密码
  156. */
  157. public function actionResetPassword($id)
  158. {
  159. $model = $this->findModel($id);
  160. $model->scenario = 'resetPassword';
  161. if($model->load(Yii::$app->request->post()) && $model->save()){
  162. Yii::$app->user->logout();
  163. return $this->goHome();
  164. }
  165. return $this->render('reset-password', [
  166. 'model' => $model
  167. ]);
  168. }
  169. public function actionUpdateProfile($id)
  170. {
  171. Url::remember('', 'actions-redirect');
  172. $user = $this->findModel($id);
  173. $profile = $user->profile;
  174. if ($profile == null) {
  175. $profile = \Yii::createObject(Profile::className());
  176. $profile->link('user', $user);
  177. }
  178. if ($profile->load(\Yii::$app->request->post()) && $profile->save()) {
  179. \Yii::$app->getSession()->setFlash('success', \Yii::t('user', 'Profile details have been updated'));
  180. return $this->refresh();
  181. }
  182. return $this->render('_profile', [
  183. 'user' => $user,
  184. 'profile' => $profile,
  185. ]);
  186. }
  187. public function actionBlock($id)
  188. {
  189. if ($id == \Yii::$app->user->getId()) {
  190. if (Yii::$app->request->isAjax) {
  191. Yii::$app->response->format = 'json';
  192. return ['status' => 0, 'message' => \Yii::t('user', 'You can not block your own account')];
  193. }
  194. \Yii::$app->getSession()->setFlash('danger', \Yii::t('user', 'You can not block your own account'));
  195. } else {
  196. $user = $this->findModel($id);
  197. if ($user->isAdmin) {
  198. throw new ForbiddenHttpException('不支持封禁管理员帐号');
  199. }
  200. if ($user->getIsBlocked()) {
  201. $user->unblock();
  202. if (Yii::$app->request->isAjax) {
  203. Yii::$app->response->format = 'json';
  204. return ['message' => \Yii::t('user', 'User has been unblocked')];
  205. }
  206. \Yii::$app->getSession()->setFlash('success', \Yii::t('user', 'User has been unblocked'));
  207. } else {
  208. $user->block();
  209. if (Yii::$app->request->isAjax) {
  210. Yii::$app->response->format = 'json';
  211. return ['message' => \Yii::t('user', 'User has been blocked')];
  212. }
  213. \Yii::$app->getSession()->setFlash('success', \Yii::t('user', 'User has been blocked'));
  214. }
  215. }
  216. return $this->redirect(Url::previous('actions-redirect'));
  217. }
  218. /**
  219. * @param $id
  220. * @return \yii\web\Response
  221. * @throws NotFoundHttpException
  222. */
  223. public function actionConfirm($id)
  224. {
  225. $model = $this->findModel($id);
  226. $model->confirm();
  227. \Yii::$app->getSession()->setFlash('success', \Yii::t('user', 'User has been confirmed'));
  228. return $this->redirect(Url::previous('actions-redirect'));
  229. }
  230. public function actionAssignments($id)
  231. {
  232. Url::remember('', 'actions-redirect');
  233. $user = $this->findModel($id);
  234. return $this->render('_assignments', [
  235. 'user' => $user
  236. ]);
  237. }
  238. /**
  239. * 二维码登录
  240. * @return string|\yii\web\Response
  241. * @throws \yii\base\Exception
  242. * @throws \yii\base\InvalidConfigException
  243. * @author nodelog
  244. */
  245. public function actionQrcodeLogin()
  246. {
  247. $this->layout = '@common/modules/user/backend/views/default/main-login.php';
  248. if (!\Yii::$app->user->isGuest) {
  249. return $this->goHome();
  250. }
  251. $model = new QrcodeLoginForm();
  252. if(Yii::$app->request->isAjax){
  253. $model->access_token = request('access_token');
  254. $model->qrcode_hash = request('qrcode_hash');
  255. if ($model->login()) {
  256. return $this->renderJson(1, '登录成功', ['returnUrl' => Yii::$app->getUser()->getReturnUrl()]);
  257. } else {
  258. return $this->renderJson(0, current($model->getErrors())[0], ['errors' => $model->getErrors()]);
  259. }
  260. } else {
  261. $access_token = Yii::$app->security->generateRandomString();
  262. $qrcode = Util::qrcode(User::generateQrcodeLoginUrl($access_token));
  263. $qrcode['access_token'] = $access_token;
  264. return $this->render('qrcode-login', $qrcode);
  265. }
  266. }
  267. }