AdminController.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace wechat\controllers;
  3. use Yii;
  4. use wechat\models\Admin;
  5. class AdminController extends BaseController
  6. {
  7. /**
  8. * 登录
  9. */
  10. public function actionLogin()
  11. {
  12. $username = trim(Yii::$app->request->post('username'));
  13. $password = trim(Yii::$app->request->post('password'));
  14. $login_ip = trim(Yii::$app->request->post('login_ip'));
  15. $model = Admin::find()->where(['username' => $username])->limit(1)->one();
  16. if (!$model) {
  17. return $this->outJson(0, [], '您的用户名或密码错误');
  18. }
  19. $model->login_time = time();
  20. $model->login_ip = $login_ip;
  21. $model->save();
  22. if (Admin::hash($password) != $model->password) {
  23. return $this->outJson(0, [], '您的用户名或密码错误!');
  24. }
  25. return $this->outJson(1, $model->toArray());
  26. }
  27. /**
  28. * 某个管理员
  29. */
  30. public function actionGetAdminById()
  31. {
  32. $id = (int) Yii::$app->request->post('id');
  33. if (!$id) {
  34. return $this->outJson(0, [], '参数错误');
  35. }
  36. $admin = Admin::find()->where(['id' => $id])->limit(1)->asArray()->one();
  37. if (!$admin) {
  38. return $this->outJson(0, [], '没有该管理员');
  39. }
  40. return $this->outJson(1, $admin);
  41. }
  42. }