SystemUsers.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\admin\service;
  10. use think\facade\Db;
  11. use think\facade\Request;
  12. use think\facade\Session;
  13. use app\common\models\system\Users;
  14. use app\common\models\system\UsersLog;
  15. use app\common\models\system\Manage;
  16. class SystemUsers extends Service {
  17. /**
  18. * 管理员登录
  19. * @param array $data
  20. * @return array|\think\Model|null
  21. * @throws \think\db\exception\DataNotFoundException
  22. * @throws \think\db\exception\DbException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. */
  25. public static function login($data=[]){
  26. $get_ip = Request::ip();
  27. if (($user = Users::where(['username' => $data["username"]])->find()) == false) {
  28. Db::name("system_users_log")->insert([
  29. "intro" => "状态:用户 [ " . $data["username"] . " ] 用户不存在 时间:" . date("Y-m-d H:i:s") . "",
  30. "ip" => $get_ip, "time" => time()
  31. ]);
  32. throw new \Exception("用户名不存在", 0);
  33. }
  34. if(md5($data["password"]) != $user["password"]){
  35. UsersLog::create([
  36. "user_id" => $user["id"],
  37. "intro" => "状态:用户 [ " . $user['username'] . " ] 密码不正确 时间:" . date("Y-m-d H:i:s") . "",
  38. "ip" => $get_ip, "time" => time()
  39. ]);
  40. throw new \Exception("您填写的密码不正确!", 0);
  41. }
  42. if ($user['status'] == 1) {
  43. UsersLog::create(array(
  44. "user_id" => $user["id"],
  45. "intro" => "状态:用户 [ " . $user['username'] . " ] 帐号禁止使用 时间:" . date("Y-m-d H:i:s") . "",
  46. "ip" => $get_ip, "time" => time()
  47. ));
  48. throw new \Exception("您的帐号已被禁止使用!", 0);
  49. }
  50. Users::where(['id' => $user['id']])->update(['count' => ($user['count'] + 1), 'time' => time(), 'ip' => $get_ip]);
  51. if (Manage::where(["id" => $user["role_id"], "status" => 0])->count() <= 0) {
  52. throw new \Exception("您所在的权限组已被管理员禁用!", 0);
  53. }
  54. UsersLog::create([
  55. "user_id" => $user["id"],
  56. "intro" => "状态:用户 [ " . $user['username'] . " ] 登录成功 时间:" . date("Y-m-d H:i:s") . " 登录地点:" . $get_ip,
  57. "ip" => $get_ip, "time" => time()
  58. ]);
  59. Session::set("system_user_id", $user["id"]);
  60. return $user;
  61. }
  62. /**
  63. * 管理员退出登录
  64. * @return string
  65. */
  66. public static function logout(){
  67. if(Session::has("system_user_id")) {
  68. Session::delete("system_user_id");
  69. Session::delete("users");
  70. }
  71. return createUrl('login/index');
  72. }
  73. }