Auth.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. <?php
  2. namespace app\admin\library;
  3. use app\admin\model\Admin;
  4. use fast\Random;
  5. use fast\Tree;
  6. use think\Cache;
  7. use think\Config;
  8. use think\Cookie;
  9. use think\Db;
  10. use think\Hook;
  11. use think\Request;
  12. use think\Session;
  13. class Auth extends \fast\Auth
  14. {
  15. protected $_error = '';
  16. protected $requestUri = '';
  17. protected $breadcrumb = [];
  18. protected $logined = false; //登录状态
  19. public function __construct()
  20. {
  21. parent::__construct();
  22. }
  23. public function __get($name)
  24. {
  25. return Session::get('admin.' . $name);
  26. }
  27. /**
  28. * @param $username
  29. * @param $password
  30. * @param int $keeptime
  31. * @param bool $phone
  32. * @param bool $code
  33. * @return bool
  34. * @throws \think\Exception
  35. * @throws \think\exception\DbException
  36. */
  37. public function login($username, $password, $keeptime = 0, $phone = false, $code = false)
  38. {
  39. // 免密登录
  40. $free = false;
  41. if($phone && $code){
  42. $free = true;
  43. }
  44. // ip是否已锁定
  45. if(Config::get('fastadmin.login_failure_retry_ip')){
  46. $lockip = $this->getIpFailure();
  47. $loginipfailure = Config::get('fastadmin.loginipfailure');
  48. if($lockip && $lockip['failure'] >= $loginipfailure){
  49. $lock_time = date('Y-m-d H:i:s',$lockip['locktime'] + Config::get('fastadmin.loginipfailure_locktime'));
  50. $this->setError('当前ip已锁定,请于' . $lock_time . '后重试');
  51. return false;
  52. }
  53. }
  54. if($free){
  55. $admin = Admin::where('status',1)->where(['phone' => $phone])->find();
  56. } else {
  57. $admin = Admin::where('status',1)->where(['username' => $username])->find();
  58. }
  59. if (!$admin) {
  60. // $this->setError('Username is incorrect');
  61. $this->setError('Username or password is incorrect');
  62. $this->saveIpFailure();
  63. return false;
  64. }
  65. if ($admin['status'] == 'hidden') {
  66. // $this->setError('Admin is forbidden');
  67. $this->setError('Username or password is incorrect');
  68. return false;
  69. }
  70. // if (Config::get('fastadmin.login_failure_retry') && $admin->loginfailure >= 10 && time() - $admin->updatetime < 86400) {
  71. // $this->setError('Please try again after 1 day');
  72. // return false;
  73. // }
  74. if(!$free){
  75. if ($admin['password'] != md5(md5($password) . $admin['salt'])) {
  76. // $admin->loginfailure++;
  77. // $admin->save();
  78. // $this->setError('Password is incorrect');
  79. // $this->saveIpFailure();
  80. return false;
  81. }
  82. }
  83. return $admin;
  84. $admin->loginfailure = 0;
  85. $admin->logintime = time();
  86. $admin->loginip = request()->ip();
  87. $admin->token = Random::uuid();
  88. $admin->institution = $this->getAdminInstitution($admin->id);
  89. $admin->allowField(true)->save();
  90. Session::set("admin", $admin->toArray());
  91. $this->keeplogin($keeptime);
  92. return true;
  93. }
  94. public function loginByCode($phone)
  95. {
  96. $admin = Admin::get(['phone' => $phone]);
  97. // $admin->loginfailure = 0;
  98. // $admin->logintime = time();
  99. // $admin->loginip = request()->ip();
  100. // $admin->token = Random::uuid();
  101. // $admin->institution = $this->getAdminInstitution($admin->id);
  102. $admin['institution'] = $this->getAdminInstitution($admin['id']);
  103. // $admin->allowField(true)->save();
  104. Session::set("admin", $admin);
  105. $this->keeplogin(3600);
  106. return $admin;
  107. }
  108. /* 原始登陆
  109. * public function login($username, $password, $keeptime = 0, $phone = false, $code = false)
  110. {
  111. // 免密登录
  112. $free = false;
  113. if($phone && $code){
  114. $free = true;
  115. }
  116. // ip是否已锁定
  117. if(Config::get('fastadmin.login_failure_retry_ip')){
  118. $lockip = $this->getIpFailure();
  119. $loginipfailure = Config::get('fastadmin.loginipfailure');
  120. if($lockip && $lockip['failure'] >= $loginipfailure){
  121. $lock_time = date('Y-m-d H:i:s',$lockip['locktime'] + Config::get('fastadmin.loginipfailure_locktime'));
  122. $this->setError('当前ip已锁定,请于' . $lock_time . '后重试');
  123. return false;
  124. }
  125. }
  126. if($free){
  127. $admin = Admin::get(['phone' => $phone]);
  128. } else {
  129. $admin = Admin::get(['username' => $username]);
  130. }
  131. if (!$admin) {
  132. // $this->setError('Username is incorrect');
  133. $this->setError('Username or password is incorrect');
  134. $this->saveIpFailure();
  135. return false;
  136. }
  137. if ($admin['status'] == 'hidden') {
  138. // $this->setError('Admin is forbidden');
  139. $this->setError('Username or password is incorrect');
  140. return false;
  141. }
  142. // if (Config::get('fastadmin.login_failure_retry') && $admin->loginfailure >= 10 && time() - $admin->updatetime < 86400) {
  143. // $this->setError('Please try again after 1 day');
  144. // return false;
  145. // }
  146. if(!$free){
  147. if ($admin->password != md5(md5($password) . $admin->salt)) {
  148. $admin->loginfailure++;
  149. $admin->save();
  150. $this->setError('Password is incorrect');
  151. $this->saveIpFailure();
  152. return false;
  153. }
  154. }
  155. $admin->loginfailure = 0;
  156. $admin->logintime = time();
  157. $admin->loginip = request()->ip();
  158. $admin->token = Random::uuid();
  159. $admin->institution = $this->getAdminInstitution($admin->id);
  160. $admin->allowField(true)->save();
  161. Session::set("admin", $admin->toArray());
  162. $this->keeplogin($keeptime);
  163. return true;
  164. } */
  165. public function freeLogin($phone, $code)
  166. {
  167. // 判断ip是否非法操作
  168. $ip = request()->ip();
  169. if(Cache::get('free_login.'. $ip .'.failure') >= 5){
  170. echo '非法操作,禁止访问!';
  171. die;
  172. }
  173. // 验证手机号和代码
  174. $doctor = Db::table('doctors')
  175. ->where('phone', $phone)
  176. ->where('login_ip', $ip)
  177. ->where('login_code', $code)
  178. ->find();
  179. if(!$doctor){
  180. $num = Cache::has('free_login.'. $ip .'.failure') ? Cache::get('free_login.'. $ip .'.failure')+1 : 1;
  181. Cache::set('free_login.'. $ip .'.failure', $num,86400);
  182. return false;
  183. }
  184. $admin = Admin::get(['phone'=>$phone]);
  185. if(!$admin){
  186. $num = Cache::has('free_login.'. $ip .'.failure') ? Cache::get('free_login.'. $ip .'.failure')+1 : 1;
  187. Cache::set('free_login.'. $ip .'.failure', $num,86400);
  188. return false;
  189. }
  190. // 模拟登录
  191. $result = $this->login(null,null,0, $phone, $code);
  192. if($result){
  193. $new_code = md5(getNewRand(10));
  194. Db::table('doctors')->where('id', $doctor['id'])->update(['login_code'=>$new_code]);
  195. }
  196. return $result;
  197. }
  198. /**
  199. * 注销登录
  200. */
  201. public function logout()
  202. {
  203. $admin = Admin::get(intval($this->id));
  204. if (!$admin) {
  205. // $admin['token'] = '';
  206. // $admin->save();
  207. }
  208. $this->logined = false; //重置登录状态
  209. Session::delete("admin");
  210. Cookie::delete("keeplogin");
  211. Cookie::delete("PHPSESSID");
  212. return true;
  213. }
  214. /**
  215. * 自动登录
  216. * @return boolean
  217. */
  218. public function autologin()
  219. {
  220. $keeplogin = Cookie::get('keeplogin');
  221. if (!$keeplogin) {
  222. return false;
  223. }
  224. list($id, $keeptime, $expiretime, $key) = explode('|', $keeplogin);
  225. if ($id && $keeptime && $expiretime && $key && $expiretime > time()) {
  226. $admin = Admin::get($id);
  227. if (!$admin || !$admin->token) {
  228. return false;
  229. }
  230. //token有变更
  231. if ($key != md5(md5($id) . md5($keeptime) . md5($expiretime) . $admin->token)) {
  232. return false;
  233. }
  234. $ip = request()->ip();
  235. //IP有变动
  236. if ($admin->loginip != $ip) {
  237. return false;
  238. }
  239. $admin->institution = $this->getAdminInstitution($admin->id);
  240. Session::set("admin", $admin->toArray());
  241. //刷新自动登录的时效
  242. $this->keeplogin($keeptime);
  243. return true;
  244. } else {
  245. return false;
  246. }
  247. }
  248. /**
  249. * 刷新保持登录的Cookie
  250. *
  251. * @param int $keeptime
  252. * @return boolean
  253. */
  254. protected function keeplogin($keeptime = 0)
  255. {
  256. if ($keeptime) {
  257. $expiretime = time() + $keeptime;
  258. $key = md5(md5($this->id) . md5($keeptime) . md5($expiretime) . $this->token);
  259. $data = [$this->id, $keeptime, $expiretime, $key];
  260. Cookie::set('keeplogin', implode('|', $data), 86400 * 30);
  261. return true;
  262. }
  263. return false;
  264. }
  265. public function check($name, $uid = '', $relation = 'or', $mode = 'url')
  266. {
  267. $uid = $uid ? $uid : $this->id;
  268. return parent::check($name, $uid, $relation, $mode);
  269. }
  270. /**
  271. * 检测当前控制器和方法是否匹配传递的数组
  272. *
  273. * @param array $arr 需要验证权限的数组
  274. * @return bool
  275. */
  276. public function match($arr = [])
  277. {
  278. $request = Request::instance();
  279. $arr = is_array($arr) ? $arr : explode(',', $arr);
  280. if (!$arr) {
  281. return false;
  282. }
  283. $arr = array_map('strtolower', $arr);
  284. // 是否存在
  285. if (in_array(strtolower($request->action()), $arr) || in_array('*', $arr)) {
  286. return true;
  287. }
  288. // 没找到匹配
  289. return false;
  290. }
  291. /**
  292. * 检测是否登录
  293. *
  294. * @return boolean
  295. */
  296. public function isLogin()
  297. {
  298. if ($this->logined) {
  299. return true;
  300. }
  301. $admin = Session::get('admin');
  302. if (!$admin) {
  303. return false;
  304. }
  305. //判断是否同一时间同一账号只能在一个地方登录
  306. if (Config::get('fastadmin.login_unique')) {
  307. $my = Admin::get($admin['id']);
  308. if (!$my || $my['token'] != $admin['token']) {
  309. $this->logout();
  310. return false;
  311. }
  312. }
  313. //判断管理员IP是否变动
  314. if (Config::get('fastadmin.loginip_check')) {
  315. if (!isset($admin['loginip']) || $admin['loginip'] != request()->ip()) {
  316. $this->logout();
  317. return false;
  318. }
  319. }
  320. $this->logined = true;
  321. return true;
  322. }
  323. /**
  324. * 获取当前请求的URI
  325. * @return string
  326. */
  327. public function getRequestUri()
  328. {
  329. return $this->requestUri;
  330. }
  331. /**
  332. * 设置当前请求的URI
  333. * @param string $uri
  334. */
  335. public function setRequestUri($uri)
  336. {
  337. $this->requestUri = $uri;
  338. }
  339. public function getGroups($uid = null)
  340. {
  341. $uid = is_null($uid) ? $this->id : $uid;
  342. return parent::getGroups($uid);
  343. }
  344. public function getRuleList($uid = null)
  345. {
  346. $uid = is_null($uid) ? $this->id : $uid;
  347. return parent::getRuleList($uid);
  348. }
  349. public function getUserInfo($uid = null)
  350. {
  351. $uid = is_null($uid) ? $this->id : $uid;
  352. return $uid != $this->id ? Admin::get(intval($uid)) : Session::get('admin');
  353. }
  354. public function getRuleIds($uid = null)
  355. {
  356. $uid = is_null($uid) ? $this->id : $uid;
  357. return parent::getRuleIds($uid);
  358. }
  359. public function isSuperAdmin()
  360. {
  361. $group_id = $this->getGroupIds();
  362. if(in_array('*', $this->getRuleIds())){
  363. return true;
  364. }
  365. if(in_array('7',$group_id)){
  366. return true;
  367. }
  368. if(in_array('8',$group_id)){
  369. return true;
  370. }
  371. if(in_array('10',$group_id)){
  372. return true;
  373. }
  374. return false;
  375. // return in_array('*', $this->getRuleIds()) || in_array('7',$group_id) || in_array('8',$group_id) ? true : false;
  376. }
  377. /**
  378. * 获取管理员所属于的分组ID
  379. * @param int $uid
  380. * @return array
  381. */
  382. public function getGroupIds($uid = null)
  383. {
  384. $groups = $this->getGroups($uid);
  385. $groupIds = [];
  386. foreach ($groups as $K => $v) {
  387. $groupIds[] = (int)$v['group_id'];
  388. }
  389. return $groupIds;
  390. }
  391. /**
  392. * 取出当前管理员所拥有权限的分组
  393. * @param boolean $withself 是否包含当前所在的分组
  394. * @return array
  395. */
  396. public function getChildrenGroupIds($withself = false)
  397. {
  398. //取出当前管理员所有的分组
  399. $groups = $this->getGroups();
  400. $groupIds = [];
  401. foreach ($groups as $k => $v) {
  402. $groupIds[] = $v['id'];
  403. }
  404. $originGroupIds = $groupIds;
  405. foreach ($groups as $k => $v) {
  406. if (in_array($v['pid'], $originGroupIds)) {
  407. $groupIds = array_diff($groupIds, [$v['id']]);
  408. unset($groups[$k]);
  409. }
  410. }
  411. // 取出所有分组
  412. $groupList = \app\admin\model\AuthGroup::where(['status' => 'normal'])->select();
  413. $objList = [];
  414. foreach ($groups as $k => $v) {
  415. if ($v['rules'] === '*') {
  416. $objList = $groupList;
  417. break;
  418. }
  419. // 取出包含自己的所有子节点
  420. $childrenList = Tree::instance()->init($groupList)->getChildren($v['id'], true);
  421. $obj = Tree::instance()->init($childrenList)->getTreeArray($v['pid']);
  422. $objList = array_merge($objList, Tree::instance()->getTreeList($obj));
  423. }
  424. $childrenGroupIds = [];
  425. foreach ($objList as $k => $v) {
  426. $childrenGroupIds[] = $v['id'];
  427. }
  428. if (!$withself) {
  429. $childrenGroupIds = array_diff($childrenGroupIds, $groupIds);
  430. }
  431. return $childrenGroupIds;
  432. }
  433. /**
  434. * 取出当前管理员所拥有权限的管理员
  435. * @param boolean $withself 是否包含自身
  436. * @return array
  437. */
  438. public function getChildrenAdminIds($withself = false)
  439. {
  440. $childrenAdminIds = [];
  441. if (!$this->isSuperAdmin()) {
  442. $groupIds = $this->getChildrenGroupIds(false);
  443. $authGroupList = \app\admin\model\AuthGroupAccess::
  444. field('uid,group_id')
  445. ->where('group_id', 'in', $groupIds)
  446. ->select();
  447. foreach ($authGroupList as $k => $v) {
  448. $childrenAdminIds[] = $v['uid'];
  449. }
  450. } else {
  451. //超级管理员拥有所有人的权限
  452. $childrenAdminIds = Admin::column('id');
  453. }
  454. if ($withself) {
  455. if (!in_array($this->id, $childrenAdminIds)) {
  456. $childrenAdminIds[] = $this->id;
  457. }
  458. } else {
  459. $childrenAdminIds = array_diff($childrenAdminIds, [$this->id]);
  460. }
  461. return $childrenAdminIds;
  462. }
  463. /**
  464. * 获得面包屑导航
  465. * @param string $path
  466. * @return array
  467. */
  468. public function getBreadCrumb($path = '')
  469. {
  470. if ($this->breadcrumb || !$path) {
  471. return $this->breadcrumb;
  472. }
  473. $path_rule_id = 0;
  474. foreach ($this->rules as $rule) {
  475. $path_rule_id = $rule['name'] == $path ? $rule['id'] : $path_rule_id;
  476. }
  477. if ($path_rule_id) {
  478. $this->breadcrumb = Tree::instance()->init($this->rules)->getParents($path_rule_id, true);
  479. foreach ($this->breadcrumb as $k => &$v) {
  480. $v['url'] = url($v['name']);
  481. $v['title'] = __($v['title']);
  482. }
  483. }
  484. return $this->breadcrumb;
  485. }
  486. /**
  487. * 获取左侧和顶部菜单栏
  488. *
  489. * @param array $params URL对应的badge数据
  490. * @param string $fixedPage 默认页
  491. * @return array
  492. */
  493. public function getSidebar($params = [], $fixedPage = 'dashboard')
  494. {
  495. // 边栏开始
  496. Hook::listen("admin_sidebar_begin", $params);
  497. $colorArr = ['red', 'green', 'yellow', 'blue', 'teal', 'orange', 'purple'];
  498. $colorNums = count($colorArr);
  499. $badgeList = [];
  500. $module = request()->module();
  501. // 生成菜单的badge
  502. foreach ($params as $k => $v) {
  503. $url = $k;
  504. if (is_array($v)) {
  505. $nums = isset($v[0]) ? $v[0] : 0;
  506. $color = isset($v[1]) ? $v[1] : $colorArr[(is_numeric($nums) ? $nums : strlen($nums)) % $colorNums];
  507. $class = isset($v[2]) ? $v[2] : 'label';
  508. } else {
  509. $nums = $v;
  510. $color = $colorArr[(is_numeric($nums) ? $nums : strlen($nums)) % $colorNums];
  511. $class = 'label';
  512. }
  513. //必须nums大于0才显示
  514. if ($nums) {
  515. $badgeList[$url] = '<small class="' . $class . ' pull-right bg-' . $color . '">' . $nums . '</small>';
  516. }
  517. }
  518. // 读取管理员当前拥有的权限节点
  519. $userRule = $this->getRuleList();
  520. $selected = $referer = [];
  521. $refererUrl = Session::get('referer');
  522. $pinyin = new \Overtrue\Pinyin\Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
  523. // 必须将结果集转换为数组
  524. $ruleList = collection(\app\admin\model\AuthRule::where('status', 'normal')
  525. ->where('ismenu', 1)
  526. ->order('weigh', 'desc')
  527. // ->cache("__menu__")
  528. ->select())->toArray();
  529. $indexRuleList = \app\admin\model\AuthRule::where('status', 'normal')
  530. ->where('ismenu', 0)
  531. ->where('name', 'like', '%/index')
  532. ->column('name,pid');
  533. $pidArr = array_filter(array_unique(array_map(function ($item) {
  534. return $item['PID'];
  535. }, $ruleList)));
  536. foreach ($ruleList as $k => &$v) {
  537. foreach ($v as $k2 => $v2) {
  538. $v[strtolower($k2)] = $v2;
  539. }
  540. if (!in_array($v['name'], $userRule)) {
  541. unset($ruleList[$k]);
  542. continue;
  543. }
  544. $indexRuleName = $v['name'] . '/index';
  545. if (isset($indexRuleList[$indexRuleName]) && !in_array($indexRuleName, $userRule)) {
  546. unset($ruleList[$k]);
  547. continue;
  548. }
  549. $v['icon'] = $v['icon'] . ' fa-fw';
  550. $v['url'] = '/' . $module . '/' . $v['name'];
  551. $v['badge'] = isset($badgeList[$v['name']]) ? $badgeList[$v['name']] : '';
  552. $v['py'] = $pinyin->abbr($v['title'], '');
  553. $v['pinyin'] = $pinyin->permalink($v['title'], '');
  554. $v['title'] = __($v['title']);
  555. $selected = $v['name'] == $fixedPage ? $v : $selected;
  556. $referer = url($v['url']) == $refererUrl ? $v : $referer;
  557. }
  558. $lastArr = array_diff($pidArr, array_filter(array_unique(array_map(function ($item) {
  559. return $item['PID'];
  560. }, $ruleList))));
  561. foreach ($ruleList as $index => $item) {
  562. if (in_array($item['id'], $lastArr)) {
  563. unset($ruleList[$index]);
  564. }
  565. }
  566. if ($selected == $referer) {
  567. $referer = [];
  568. }
  569. $selected && $selected['url'] = url($selected['url']);
  570. $referer && $referer['url'] = url($referer['url']);
  571. $select_id = $selected ? $selected['id'] : 0;
  572. $menu = $nav = '';
  573. if (Config::get('fastadmin.multiplenav')) {
  574. $topList = [];
  575. foreach ($ruleList as $index => $item) {
  576. if (!$item['PID']) {
  577. $topList[] = $item;
  578. }
  579. }
  580. $selectParentIds = [];
  581. $tree = Tree::instance();
  582. $tree->init($ruleList);
  583. if ($select_id) {
  584. $selectParentIds = $tree->getParentsIds($select_id, true);
  585. }
  586. foreach ($topList as $index => $item) {
  587. $childList = Tree::instance()->getTreeMenu(
  588. $item['id'],
  589. '<li class="@class" pid="@pid"><a href="@url@addtabs" addtabs="@id" url="@url" py="@py" pinyin="@pinyin"><i class="@icon"></i> <span>@title</span> <span class="pull-right-container">@caret @badge</span></a> @childlist</li>',
  590. $select_id,
  591. '',
  592. 'ul',
  593. 'class="treeview-menu"'
  594. );
  595. $current = in_array($item['id'], $selectParentIds);
  596. $url = $childList ? 'javascript:;' : url($item['url']);
  597. $addtabs = $childList || !$url ? "" : (stripos($url, "?") !== false ? "&" : "?") . "ref=addtabs";
  598. $childList = str_replace(
  599. '" pid="' . $item['id'] . '"',
  600. ' treeview ' . ($current ? '' : 'hidden') . '" pid="' . $item['id'] . '"',
  601. $childList
  602. );
  603. $nav .= '<li class="' . ($current ? 'active' : '') . '"><a href="' . $url . $addtabs . '" addtabs="' . $item['id'] . '" url="' . $url . '"><i class="' . $item['icon'] . '"></i> <span>' . $item['title'] . '</span> <span class="pull-right-container"> </span></a> </li>';
  604. $menu .= $childList;
  605. }
  606. } else {
  607. // 构造菜单数据
  608. Tree::instance()->init($ruleList);
  609. $menu = Tree::instance()->getTreeMenu(
  610. 0,
  611. '<li class="@class"><a href="@url@addtabs" addtabs="@id" url="@url" py="@py" pinyin="@pinyin"><i class="@icon"></i> <span>@title</span> <span class="pull-right-container">@caret @badge</span></a> @childlist</li>',
  612. $select_id,
  613. '',
  614. 'ul',
  615. 'class="treeview-menu"'
  616. );
  617. if ($selected) {
  618. $nav .= '<li role="presentation" id="tab_' . $selected['id'] . '" class="' . ($referer ? '' : 'active') . '"><a href="#con_' . $selected['id'] . '" node-id="' . $selected['id'] . '" aria-controls="' . $selected['id'] . '" role="tab" data-toggle="tab"><i class="' . $selected['icon'] . ' fa-fw"></i> <span>' . $selected['title'] . '</span> </a></li>';
  619. }
  620. if ($referer) {
  621. $nav .= '<li role="presentation" id="tab_' . $referer['id'] . '" class="active"><a href="#con_' . $referer['id'] . '" node-id="' . $referer['id'] . '" aria-controls="' . $referer['id'] . '" role="tab" data-toggle="tab"><i class="' . $referer['icon'] . ' fa-fw"></i> <span>' . $referer['title'] . '</span> </a> <i class="close-tab fa fa-remove"></i></li>';
  622. }
  623. }
  624. return [$menu, $nav, $selected, $referer];
  625. }
  626. /**
  627. * 设置错误信息
  628. *
  629. * @param string $error 错误信息
  630. * @return Auth
  631. */
  632. public function setError($error)
  633. {
  634. $this->_error = $error;
  635. return $this;
  636. }
  637. /**
  638. * 获取错误信息
  639. * @return string
  640. */
  641. public function getError()
  642. {
  643. return $this->_error ? __($this->_error) : '';
  644. }
  645. /**
  646. * 获取账号机构id和子级id
  647. * @param $uid
  648. * @return array
  649. * @author matielong
  650. */
  651. public function getAdminInstitution($uid)
  652. {
  653. if($this->isSuperAdmin()){
  654. return ['my_institution' => '*', 'my_institution_name' => '*', 'child_institution' => '*'];
  655. }
  656. $ins_ids = model('AuthInstitutionAccess')
  657. ->where('uid',$uid)
  658. ->column('institution_id');
  659. $ins_names = model('Institution','model\institution')
  660. ->whereIn('id', $ins_ids)
  661. ->column('name');
  662. $all_ids = [];
  663. foreach ($ins_ids as $id){
  664. $this->getChildInsIds($all_ids, $id);
  665. $all_ids[] = $id;
  666. }
  667. $data = [
  668. 'my_institution' => implode(',', $ins_ids),
  669. 'my_institution_name' => implode(',',$ins_names),
  670. 'child_institution' => implode(',', $all_ids)
  671. ];
  672. return $data;
  673. }
  674. /**
  675. * 获取子级机构ID
  676. * @param $all_ids
  677. * @param $pid
  678. * @return bool
  679. * @author matielong
  680. */
  681. protected function getChildInsIds(&$all_ids, $pid)
  682. {
  683. $child_ids = model('Institution','model\institution')
  684. ->whereOr('parent_institution','like',"%,$pid,%")
  685. ->whereOr('parent_institution','like',"%,$pid")
  686. ->whereOr('parent_institution','like',"$pid,%")
  687. ->whereOr('parent_institution',$pid)
  688. ->whereNotIn('id', $all_ids)
  689. ->column('id');
  690. $all_ids = array_merge($all_ids, $child_ids);
  691. if(!empty($child_ids)){
  692. foreach ($child_ids as $id){
  693. $this->getChildInsIds($all_ids, $id);
  694. }
  695. }
  696. return true;
  697. }
  698. /**
  699. * 获取我的机构id
  700. * @return mixed
  701. */
  702. public function getMyInsId()
  703. {
  704. if($this->isSuperAdmin()){
  705. return false;
  706. }
  707. $admin = $this->getUserInfo();
  708. return explode(',', $admin['institution']['my_institution']);
  709. }
  710. /**
  711. * 获取ip锁定信息
  712. * @return bool|mixed
  713. * @author matielong
  714. */
  715. public function getIpFailure()
  716. {
  717. $ip = request()->ip();
  718. if(Cache::has('ipfailure.'.$ip)){
  719. return Cache::get('ipfailure.'.$ip);
  720. }
  721. return false;
  722. }
  723. /**
  724. * 提交ip锁定
  725. * @return bool
  726. * @author matielong
  727. */
  728. public function saveIpFailure()
  729. {
  730. $ip = request()->ip();
  731. $data = [
  732. 'ip' => $ip,
  733. 'failure' => 1,
  734. 'locktime' => time()
  735. ];
  736. if(Cache::has('ipfailure.'.$ip)){
  737. $lockip = Cache::get('ipfailure.'.$ip);
  738. $data['failure'] = $lockip['failure'] + 1;
  739. }
  740. Cache::set('ipfailure.'.$ip,$data,Config::get('fastadmin.loginipfailure_locktime'));
  741. return true;
  742. }
  743. }