PublicController.php 4.5 KB

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