Publics.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. namespace app\user\admin;
  10. use app\common\controller\Common;
  11. use app\user\model\User as UserModel;
  12. use app\user\model\Role as RoleModel;
  13. use app\admin\model\Menu as MenuModel;
  14. use think\facade\Hook;
  15. /**
  16. * 用户公开控制器,不经过权限认证
  17. * @package app\user\admin
  18. */
  19. class Publics extends Common
  20. {
  21. /**
  22. * 用户登录
  23. * @author 蔡伟明 <314013107@qq.com>
  24. * @return mixed
  25. */
  26. public function signin()
  27. {
  28. if ($this->request->isPost()) {
  29. // 获取post数据
  30. $data = $this->request->post();
  31. $rememberme = isset($data['remember-me']) ? true : false;
  32. // 登录钩子
  33. $hook_result = Hook::listen('signin', $data);
  34. if (!empty($hook_result) && true !== $hook_result[0]) {
  35. $this->error($hook_result[0]);
  36. }
  37. // 验证数据
  38. $result = $this->validate($data, 'User.signin');
  39. if(true !== $result){
  40. // 验证失败 输出错误信息
  41. $this->error($result);
  42. }
  43. // 验证码
  44. if (config('captcha_signin')) {
  45. $captcha = $this->request->post('captcha', '');
  46. $captcha == '' && $this->error('请输入验证码');
  47. if(!captcha_check($captcha, '')){
  48. //验证失败
  49. $this->error('验证码错误或失效');
  50. };
  51. }
  52. // 登录
  53. $UserModel = new UserModel;
  54. $uid = $UserModel->login($data['username'], $data['password'], $rememberme);
  55. if ($uid) {
  56. // 记录行为
  57. action_log('user_signin', 'admin_user', $uid, $uid);
  58. $this->jumpUrl();
  59. } else {
  60. $this->error($UserModel->getError());
  61. }
  62. } else {
  63. $hook_result = Hook::listen('signin_sso');
  64. if (!empty($hook_result) && true !== $hook_result[0]) {
  65. if (isset($hook_result[0]['url'])) {
  66. $this->redirect($hook_result[0]['url']);
  67. }
  68. if (isset($hook_result[0]['error'])) {
  69. $this->error($hook_result[0]['error']);
  70. }
  71. }
  72. if (is_signin()) {
  73. $this->jumpUrl();
  74. } else {
  75. return $this->fetch();
  76. }
  77. }
  78. }
  79. /**
  80. * 跳转到第一个有权限访问的url
  81. * @author 蔡伟明 <314013107@qq.com>
  82. * @return mixed|string
  83. */
  84. private function jumpUrl()
  85. {
  86. if (session('user_auth.role') == 1) {
  87. $this->success('登录成功', url('admin/index/index'));
  88. }
  89. $default_module = RoleModel::where('id', session('user_auth.role'))->value('default_module');
  90. $menu = MenuModel::get($default_module);
  91. if (!$menu) {
  92. $this->error('当前角色未指定默认跳转模块!');
  93. }
  94. if ($menu['url_type'] == 'link') {
  95. $this->success('登录成功', $menu['url_value']);
  96. }
  97. $menu_url = explode('/', $menu['url_value']);
  98. role_auth();
  99. $menus = MenuModel::getSidebarMenu($default_module, $menu['module'], $menu_url[1]);
  100. $url = '';
  101. foreach ($menus as $key => $menu) {
  102. if (!empty($menu['url_value'])) {
  103. $url = $menu['url_value'];
  104. break;
  105. }
  106. if (!empty($menu['child'])) {
  107. $url = $menu['child'][0]['url_value'];
  108. break;
  109. }
  110. }
  111. if ($url == '') {
  112. $this->error('权限不足');
  113. } else {
  114. $this->success('登录成功', $url);
  115. }
  116. }
  117. /**
  118. * 退出登录
  119. * @author 蔡伟明 <314013107@qq.com>
  120. */
  121. public function signout()
  122. {
  123. $hook_result = Hook::listen('signout_sso');
  124. if (!empty($hook_result) && true !== $hook_result[0]) {
  125. if (isset($hook_result[0]['url'])) {
  126. $this->redirect($hook_result[0]['url']);
  127. }
  128. if (isset($hook_result[0]['error'])) {
  129. $this->error($hook_result[0]['error']);
  130. }
  131. }
  132. session(null);
  133. cookie('uid', null);
  134. cookie('signin_token', null);
  135. $this->redirect('signin');
  136. }
  137. }