PublicController.php 5.0 KB

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