LoginController.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace app\admin\controller;
  3. use think\exception\ValidateException;
  4. use app\model\Users;
  5. use app\model\UsersRoles;
  6. use app\model\Config;
  7. use app\model\Platform;
  8. use tidy;
  9. class LoginController extends Base
  10. {
  11. //用户登录
  12. public function index()
  13. {
  14. //$data = only('username,password,key');
  15. $data['username'] = input('post.username', '', 'trim');
  16. $data['password'] = input('post.password', '', 'trim');
  17. $data['key'] = input('post.key', '', 'trim');
  18. if (empty($data['username'])) {
  19. throw new ValidateException('请输入用户名');
  20. }
  21. if (empty($data['password'])) {
  22. throw new ValidateException('请输入密码');
  23. }
  24. $res = $this->checkLogin($data);
  25. if ($res) {
  26. $token = $this->setToken($res);
  27. $res['token'] = $token;
  28. return $this->json(['data' => $res, 'token' => $token]);
  29. }
  30. }
  31. //验证登录
  32. private function checkLogin($data)
  33. {
  34. $where['username'] = $data['username'];
  35. $info = Users::where($where)->find();
  36. if ($info) {
  37. $info = $info->toArray();
  38. $UsersRoles = UsersRoles::getinfo($info);
  39. $info['roles_title'] = $UsersRoles['title'];
  40. $info['roles_status'] = $UsersRoles['status'];
  41. $info['is_console'] = $UsersRoles['is_console'];
  42. if ($info['password'] === pass_hash($data['password'], $info["salt"])) {
  43. unset($info['password']);
  44. unset($info["salt"]);
  45. if (!($info['status']) || !($info['roles_status'])) {
  46. throw new ValidateException("该账户被禁用");
  47. }
  48. } else {
  49. throw new ValidateException("用户名或者密码不正确");
  50. }
  51. if (empty($info['lastweid']) && !empty($info['weid'])) {
  52. $info['lastweid'] = $info['weid'];
  53. Users::where('id', $info['id'])->update(['lastweid' => $info['weid']]);
  54. }
  55. }
  56. Users::where('id', $info['id'])->update(['login_time' => time(), 'login_ip' => client_ip()]);
  57. event('LoginLog', ['user' => $data['username'], 'weid' => $info['weid']]); //写入登录日志
  58. return $info;
  59. }
  60. function sitesetup()
  61. {
  62. $id = (int) input('post.i', '', 'intval');
  63. $ac = [];
  64. $res = Config::getsitesetupconfig('sitesetup');
  65. if (empty($res)) {
  66. $res = [];
  67. }
  68. if (!empty($id)) {
  69. $ac = Platform::field('title,logo,loginbgimg')->find($id);
  70. if ($ac) {
  71. $ac = $ac->toArray();
  72. }
  73. }
  74. if (empty($ac)) {
  75. $ac['title'] = $res['sys_title'];
  76. $ac['logo'] = $res['logo'];
  77. }
  78. $res['ac'] = $ac;
  79. return $this->json(['data' => $res]);
  80. }
  81. }