LoginController.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\Account;
  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. } else {
  30. throw new ValidateException('用户名或密码错误');
  31. }
  32. }
  33. //验证登录
  34. private function checkLogin($data)
  35. {
  36. $where['username'] = $data['username'];
  37. $info = Users::where($where)->find();
  38. if ($info) {
  39. $info = $info->toArray();
  40. $UsersRoles = UsersRoles::getinfo($info);
  41. $info['roles_title'] = $UsersRoles['title'];
  42. $info['roles_status'] = $UsersRoles['status'];
  43. $info['is_console'] = $UsersRoles['is_console'];
  44. if ($info['password'] === pass_hash($data['password'], $info["salt"])) {
  45. unset($info['password']);
  46. unset($info["salt"]);
  47. if (!($info['status']) || !($info['roles_status'])) {
  48. throw new ValidateException("该账户被禁用");
  49. }
  50. } else {
  51. throw new ValidateException("用户名或者密码不正确");
  52. }
  53. if (empty($info['lastweid']) && !empty($info['weid'])) {
  54. $info['lastweid'] = $info['weid'];
  55. Users::where('id', $info['id'])->update(['lastweid' => $info['weid']]);
  56. }
  57. }
  58. Users::where('id', $info['id'])->update(['login_time' => time(), 'login_ip' => client_ip()]);
  59. event('LoginLog', ['user' => $data['username'], 'weid' => $info['weid']]); //写入登录日志
  60. return $info;
  61. }
  62. function sitesetup()
  63. {
  64. $id = (int) input('post.i', '', 'intval');
  65. $ac = [];
  66. $res = Config::getsitesetupconfig('sitesetup');
  67. if (empty($res)) {
  68. $res = [];
  69. }
  70. if (!empty($id)) {
  71. $ac = Account::field('title,logo,loginbgimg')->find($id);
  72. if ($ac) {
  73. $ac = $ac->toArray();
  74. }
  75. }
  76. if (empty($ac)) {
  77. $ac['title'] = $res['sys_title'];
  78. $ac['logo'] = $res['logo'];
  79. }
  80. $res['ac'] = $ac;
  81. return $this->json(['data' => $res]);
  82. }
  83. }