PublicController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. // +—————————————————————————————————————————————————————————————————————
  3. // | Created by Yunbao
  4. // +—————————————————————————————————————————————————————————————————————
  5. // | Copyright (c) 2013~2022 http://www.yunbaokj.com All rights reserved.
  6. // +—————————————————————————————————————————————————————————————————————
  7. // | Author: https://gitee.com/yunbaokeji
  8. // +—————————————————————————————————————————————————————————————————————
  9. // | Date: 2022-04-30
  10. // +—————————————————————————————————————————————————————————————————————
  11. namespace app\admin\controller;
  12. use cmf\controller\AdminBaseController;
  13. use think\Db;
  14. class PublicController extends AdminBaseController
  15. {
  16. public function initialize()
  17. {
  18. }
  19. /**
  20. * 后台登陆界面
  21. */
  22. public function login()
  23. {
  24. $loginAllowed = session("__LOGIN_BY_CMF_ADMIN_PW__");
  25. if (empty($loginAllowed)) {
  26. //$this->error('非法登录!', cmf_get_root() . '/');
  27. return redirect(cmf_get_root() . "/");
  28. }
  29. $admin_id = session('ADMIN_ID');
  30. if (!empty($admin_id)) {//已经登录
  31. return redirect(url("admin/Index/index"));
  32. } else {
  33. session("__SP_ADMIN_LOGIN_PAGE_SHOWED_SUCCESS__", true);
  34. $result = hook_one('admin_login');
  35. if (!empty($result)) {
  36. return $result;
  37. }
  38. $siteInfo = cmf_get_site_info();
  39. $this->assign('siteInfo', $siteInfo);
  40. return $this->fetch(":login");
  41. }
  42. }
  43. /**
  44. * 登录验证
  45. */
  46. public function doLogin()
  47. {
  48. if (hook_one('admin_custom_login_open')) {
  49. $this->error('您已经通过插件自定义后台登录!');
  50. }
  51. $loginAllowed = session("__LOGIN_BY_CMF_ADMIN_PW__");
  52. if (empty($loginAllowed)) {
  53. $this->error('非法登录!', cmf_get_root() . '/');
  54. }
  55. $captcha = $this->request->param('captcha');
  56. if (empty($captcha)) {
  57. $this->error(lang('CAPTCHA_REQUIRED'));
  58. }
  59. //验证码
  60. if (!cmf_captcha_check($captcha)) {
  61. $this->error(lang('CAPTCHA_NOT_RIGHT'));
  62. }
  63. $name = $this->request->param("username");
  64. if (empty($name)) {
  65. $this->error(lang('USERNAME_OR_EMAIL_EMPTY'));
  66. }
  67. $pass = $this->request->param("password");
  68. if (empty($pass)) {
  69. $this->error(lang('PASSWORD_REQUIRED'));
  70. }
  71. if (strpos($name, "@") > 0) {//邮箱登陆
  72. $where['user_email'] = $name;
  73. } else {
  74. $where['user_login'] = $name;
  75. }
  76. $result = Db::name('user')->where($where)->find();
  77. if (!empty($result) && $result['user_type'] == 1) {
  78. if (cmf_compare_password($pass, $result['user_pass'])) {
  79. $groups = Db::name('RoleUser')
  80. ->alias("a")
  81. ->join('__ROLE__ b', 'a.role_id =b.id')
  82. ->where(["user_id" => $result["id"], "status" => 1])
  83. ->value("role_id");
  84. if ($result["id"] != 1 && (empty($groups) || empty($result['user_status']))) {
  85. $this->error(lang('USE_DISABLED'));
  86. }
  87. //登入成功页面跳转
  88. session('ADMIN_ID', $result["id"]);
  89. session('name', $result["user_login"]);
  90. $result['last_login_ip'] = get_client_ip(0, true);
  91. $result['last_login_time'] = time();
  92. $token = cmf_generate_user_token($result["id"], 'web');
  93. if (!empty($token)) {
  94. session('token', $token);
  95. }
  96. Db::name('user')->update($result);
  97. setAdminLog('管理员登录');
  98. cookie("admin_username", $name, 3600 * 24 * 30);
  99. session("__LOGIN_BY_CMF_ADMIN_PW__", null);
  100. $this->success(lang('LOGIN_SUCCESS'), url("admin/Index/index"));
  101. } else {
  102. $this->error(lang('PASSWORD_NOT_RIGHT'));
  103. }
  104. } else {
  105. $this->error(lang('USERNAME_NOT_EXIST'));
  106. }
  107. }
  108. /**
  109. * 后台管理员退出
  110. */
  111. public function logout()
  112. {
  113. session('ADMIN_ID', null);
  114. return redirect(url('/', [], false, true));
  115. }
  116. }