Login.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php namespace Manage\Api;
  2. use Dever;
  3. use Dever\Helper\Str;
  4. use Dever\Helper\Code;
  5. class Login
  6. {
  7. # 获取系统信息
  8. public function getSystem()
  9. {
  10. $system = Dever::input('system', 'is_string', '系统', 'platform');
  11. $system = Dever::db('system', 'manage')->find(array('key' => $system));
  12. if (!$system) {
  13. Dever::error('当前系统不存在');
  14. }
  15. $system['placeholder'] = '请输入' . $system['name'] . '号';
  16. return $system;
  17. }
  18. # 登录
  19. public function act()
  20. {
  21. //$this->checkCode();
  22. $system = $this->getSystem();
  23. $number = Dever::input('number', '', $system['name'] . '号', 'default');
  24. $info = Dever::db($system['info_table'])->find(array('number' => $number));
  25. if (!$info) {
  26. Dever::error('登录失败,当前' . $system['name'] . '号错误');
  27. }
  28. if ($system['partition'] == 'no') {
  29. # 不分库
  30. $db = Dever::db($system['user_table']);
  31. $role_db = Dever::db($system['role_table']);
  32. } else {
  33. # 分库
  34. $info['info_id'] = $info['id'];
  35. $info['partition'] = $system['partition'];
  36. $info['system_id'] = $system['id'];
  37. $partition = Dever::load('common', 'manage')->system($info);
  38. $db = Dever::db($system['user_table'], '', true, 'default', $partition);
  39. $role_db = Dever::db($system['role_table'], '', true, 'default', $partition);
  40. }
  41. $where['mobile'] = Dever::input('mobile', Dever::rule('mobile'), '手机号');
  42. $password = Dever::input('password', 'is_string', '密码');
  43. $admin = $db->find($where);
  44. if (!$admin) {
  45. $total = $db->find(1);
  46. if (!$total) {
  47. $insert['name'] = Str::hide($where['mobile']);
  48. $insert['mobile'] = $where['mobile'];
  49. $insert['role'] = 1;
  50. $insert += Dever::load('common', 'manage')->createPwd($password);
  51. $id = $db->insert($insert);
  52. $admin = $db->find($id);
  53. } else {
  54. Dever::error('登录失败');
  55. }
  56. }
  57. if (!$admin) {
  58. Dever::error('登录失败,管理员信息无效');
  59. }
  60. if ($admin['status'] == 2) {
  61. Dever::error('登录失败,账户已被封禁');
  62. }
  63. if (Dever::load('common', 'manage')->hash($password, $admin['salt']) != $admin['password']) {
  64. Dever::error('登录失败,账户密码无效');
  65. }
  66. # 根据角色获取module_id
  67. $system_user = Dever::db('system_user', 'manage')->find(array('uid' => $admin['id'], 'system_id' => $system['id'], 'info_id' => $info['id']));
  68. $module_id = $data_id = 0;
  69. if ($system_user) {
  70. $module_id = $system_user['module_id'];
  71. $data_id = $system_user['data_id'];
  72. } elseif ($admin['role']) {
  73. $module = '';
  74. $role = $role_db->select(array('id' => array('in', $admin['role'])));
  75. foreach ($role as $k => $v) {
  76. if ($v['module']) {
  77. $module .= $v['module'] . ',';
  78. }
  79. }
  80. if ($module) {
  81. $where['id'] = array('in', $module);
  82. } else {
  83. $where['system'] = $system['key'];
  84. }
  85. $module = Dever::db('system_module', 'manage')->select($where);
  86. if ($module) {
  87. $module_id = $module[0]['id'];
  88. $child = Dever::db($module[0]['data_table'])->select([]);
  89. if ($child) {
  90. if ($admin['module_data']) {
  91. foreach ($child as $k => $v) {
  92. $key = $module_id . '-' . $v['id'];
  93. if (strstr($admin['module_data'], $key)) {
  94. $data_id = $v['id'];
  95. break;
  96. }
  97. }
  98. } else {
  99. $data_id = $child[0]['id'];
  100. }
  101. }
  102. }
  103. }
  104. if (!$module_id || !$data_id) {
  105. Dever::error('登录失败,账户无效');
  106. }
  107. return Dever::load('common', 'manage')->token($admin['id'], $admin['mobile'], $system['partition'], $system['id'], $info['id'], $module_id, $data_id);
  108. }
  109. private function checkCode()
  110. {
  111. $code = Dever::input('verificationCode');
  112. if (!$code) {
  113. Dever::error('请输入验证码');
  114. }
  115. $save = Dever::session('code');
  116. if ($code != $save) {
  117. Dever::error('验证码错误');
  118. }
  119. }
  120. public function code()
  121. {
  122. echo Dever::session('code', Code::create(), 3600);die;
  123. }
  124. public function out()
  125. {
  126. return 'ok';
  127. }
  128. public function loadMenu()
  129. {
  130. return Dever::load('menu')->init();
  131. }
  132. }