Authcompany.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. <?php
  2. namespace app\common\library;
  3. use app\company\model\Admin as User;
  4. use fast\Tree;
  5. use fast\Random;
  6. use think\Config;
  7. use think\Db;
  8. use think\Exception;
  9. use think\Hook;
  10. use think\Request;
  11. use think\Validate;
  12. class Authcompany extends \fast\Authpc
  13. {
  14. protected static $instance = null;
  15. protected $_error = '';
  16. protected $_logined = false;
  17. protected $_user = null;
  18. protected $_token = '';
  19. //Token默认有效时长
  20. protected $keeptime = 2592000;
  21. protected $requestUri = '';
  22. protected $rules = [];
  23. //默认配置
  24. // protected $config = [];
  25. protected $options = [];
  26. protected $allowFields = ['id','username','nickname','avatar','company_id' ];
  27. protected $breadcrumb = [];
  28. /*public function __construct($options = [])
  29. {
  30. if ($config = Config::get('company')) {
  31. $this->config = array_merge($this->config, $config);
  32. }
  33. $this->options = array_merge($this->config, $options);
  34. }*/
  35. public function __construct()
  36. {
  37. parent::__construct();
  38. }
  39. /**
  40. *
  41. * @param array $options 参数
  42. * @return Auth
  43. */
  44. public static function instance($options = [])
  45. {
  46. if (is_null(self::$instance)) {
  47. self::$instance = new static($options);
  48. }
  49. return self::$instance;
  50. }
  51. /**
  52. * 获取User模型
  53. * @return User
  54. */
  55. public function getUser()
  56. {
  57. return $this->_user;
  58. }
  59. /**
  60. * 兼容调用user模型的属性
  61. *
  62. * @param string $name
  63. * @return mixed
  64. */
  65. public function __get($name)
  66. {
  67. return $this->_user ? $this->_user->$name : null;
  68. }
  69. /**
  70. * 兼容调用user模型的属性
  71. */
  72. public function __isset($name)
  73. {
  74. return isset($this->_user) ? isset($this->_user->$name) : false;
  75. }
  76. /**
  77. * 根据Token初始化
  78. *
  79. * @param string $token Token
  80. * @return boolean
  81. */
  82. public function init($token)
  83. {
  84. if ($this->_logined) {
  85. return true;
  86. }
  87. if ($this->_error) {
  88. return false;
  89. }
  90. $data = Tokencompany::get($token);
  91. if (!$data) {
  92. return false;
  93. }
  94. $user_id = intval($data['user_id']);
  95. if ($user_id > 0) {
  96. $user = User::get($user_id);
  97. if (!$user) {
  98. $this->setError('Account not exist');
  99. return false;
  100. }
  101. if ($user['status'] != 1) {
  102. $this->setError('Account is locked');
  103. return false;
  104. }
  105. $this->_user = $user;
  106. $this->_logined = true;
  107. $this->_token = $token;
  108. //初始化成功的事件
  109. Hook::listen("company_init_successed", $this->_user);
  110. return true;
  111. } else {
  112. $this->setError('You are not logged in');
  113. return false;
  114. }
  115. }
  116. /**
  117. * 用户登录
  118. *
  119. * @param string $account 账号,用户名、邮箱、手机号
  120. * @param string $password 密码
  121. * @return boolean
  122. */
  123. public function login($account, $password)
  124. {
  125. $field = 'username';
  126. $user = User::get([$field => $account]);
  127. if (!$user) {
  128. $this->setError('Account is incorrect');
  129. return false;
  130. }
  131. if ($user->status != 1) {
  132. $this->setError('Account is locked');
  133. return false;
  134. }
  135. if (Config::get('pcadmin.login_failure_retry') && $user->loginfailure >= 10 && time() - $user->updatetime < 86400) {
  136. $this->setError('Please try again after 1 day');
  137. return false;
  138. }
  139. if ($user->password != $this->getEncryptPassword($password, $user->salt)) {
  140. $user->loginfailure++;
  141. $this->setError('Password is incorrect');
  142. return false;
  143. }
  144. //直接登录会员
  145. return $this->direct($user->id);
  146. }
  147. /**
  148. * 退出
  149. *
  150. * @return boolean
  151. */
  152. public function logout()
  153. {
  154. if (!$this->_logined) {
  155. $this->setError('You are not logged in');
  156. return false;
  157. }
  158. //设置登录标识
  159. $this->_logined = false;
  160. //删除Token
  161. Tokencompany::delete($this->_token);
  162. //退出成功的事件
  163. Hook::listen("user_logout_successed", $this->_user);
  164. return true;
  165. }
  166. /**
  167. * 修改密码
  168. * @param string $newpassword 新密码
  169. * @param string $oldpassword 旧密码
  170. * @param bool $ignoreoldpassword 忽略旧密码
  171. * @return boolean
  172. */
  173. public function changepwd($newpassword, $oldpassword = '', $ignoreoldpassword = false)
  174. {
  175. if (!$this->_logined) {
  176. $this->setError('You are not logged in');
  177. return false;
  178. }
  179. //判断旧密码是否正确
  180. if ($this->_user->password == $this->getEncryptPassword($oldpassword, $this->_user->salt) || $ignoreoldpassword) {
  181. Db::startTrans();
  182. try {
  183. $salt = Random::alnum();
  184. $newpassword = $this->getEncryptPassword($newpassword, $salt);
  185. $this->_user->save(['loginfailure' => 0, 'password' => $newpassword, 'salt' => $salt]);
  186. Tokencompany::delete($this->_token);
  187. //修改密码成功的事件
  188. Hook::listen("company_changepwd_successed", $this->_user);
  189. Db::commit();
  190. } catch (Exception $e) {
  191. Db::rollback();
  192. $this->setError($e->getMessage());
  193. return false;
  194. }
  195. return true;
  196. } else {
  197. $this->setError('Password is incorrect');
  198. return false;
  199. }
  200. }
  201. /**
  202. * 直接登录账号
  203. * @param int $user_id
  204. * @return boolean
  205. */
  206. public function direct($user_id)
  207. {
  208. $user = User::get($user_id);
  209. if ($user) {
  210. Db::startTrans();
  211. try {
  212. $ip = request()->ip();
  213. $time = time();
  214. $user->loginfailure = 0;
  215. $user->logintime = $time;
  216. $user->loginip = $ip;
  217. $user->save();
  218. $this->_user = $user;
  219. $this->_token = Random::uuid();
  220. Tokencompany::set($this->_token, $user->id, $this->keeptime);
  221. $this->_logined = true;
  222. //登录成功的事件
  223. Hook::listen("company_login_successed", $this->_user);
  224. Db::commit();
  225. } catch (Exception $e) {
  226. Db::rollback();
  227. $this->setError($e->getMessage());
  228. return false;
  229. }
  230. return true;
  231. } else {
  232. return false;
  233. }
  234. }
  235. /**
  236. * 检测是否是否有对应权限
  237. * @param string $path 控制器/方法
  238. * @param string $module 模块 默认为当前模块
  239. * @return boolean
  240. */
  241. /*public function check($path = null, $module = null)
  242. {
  243. if (!$this->_logined) {
  244. return false;
  245. }
  246. $ruleList = $this->getRuleList();
  247. $rules = [];
  248. foreach ($ruleList as $k => $v) {
  249. $rules[] = $v['name'];
  250. }
  251. $url = ($module ? $module : request()->module()) . '/' . (is_null($path) ? $this->getRequestUri() : $path);
  252. $url = strtolower(str_replace('.', '/', $url));
  253. return in_array($url, $rules) ? true : false;
  254. }*/
  255. public function check($name, $uid = '', $relation = 'or', $mode = 'url')
  256. {
  257. $uid = $uid ? $uid : $this->id;
  258. return parent::check($name, $uid, $relation, $mode);
  259. }
  260. /**
  261. * 检测当前控制器和方法是否匹配传递的数组
  262. *
  263. * @param array $arr 需要验证权限的数组
  264. * @return bool
  265. */
  266. public function match($arr = [])
  267. {
  268. $request = Request::instance();
  269. $arr = is_array($arr) ? $arr : explode(',', $arr);
  270. if (!$arr) {
  271. return false;
  272. }
  273. $arr = array_map('strtolower', $arr);
  274. // 是否存在
  275. if (in_array(strtolower($request->action()), $arr) || in_array('*', $arr)) {
  276. return true;
  277. }
  278. // 没找到匹配
  279. return false;
  280. }
  281. /**
  282. * 判断是否登录
  283. * @return boolean
  284. */
  285. public function isLogin()
  286. {
  287. if ($this->_logined) {
  288. return true;
  289. }
  290. return false;
  291. }
  292. /**
  293. * 获取当前Token
  294. * @return string
  295. */
  296. public function getToken()
  297. {
  298. return $this->_token;
  299. }
  300. /**
  301. * 获取会员基本信息
  302. */
  303. public function getUserInfo_simple()
  304. {
  305. $data = $this->_user->toArray();
  306. $allowFields = $this->getAllowFields();
  307. $userinfo = array_intersect_key($data, array_flip($allowFields));
  308. $userinfo = array_merge($userinfo, Tokencompany::get($this->_token));
  309. //追加
  310. $userinfo['avatar'] = localpath_to_netpath($userinfo['avatar']);
  311. return $userinfo;
  312. }
  313. /**
  314. * 获取会员基本信息
  315. */
  316. public function getUserInfo()
  317. {
  318. $data = $this->_user->toArray();
  319. $allowFields = $this->getAllowFields();
  320. $userinfo = array_intersect_key($data, array_flip($allowFields));
  321. $userinfo = array_merge($userinfo, Tokencompany::get($this->_token));
  322. //追加
  323. $userinfo['avatar'] = localpath_to_netpath($userinfo['avatar']);
  324. //追加公司信息
  325. $userinfo['company'] = Db::name('company')->field('id,companyname')->where('id',$userinfo['company_id'])->find();
  326. return $userinfo;
  327. }
  328. /**
  329. * 获取当前请求的URI
  330. * @return string
  331. */
  332. public function getRequestUri()
  333. {
  334. return $this->requestUri;
  335. }
  336. /**
  337. * 设置当前请求的URI
  338. * @param string $uri
  339. */
  340. public function setRequestUri($uri)
  341. {
  342. $this->requestUri = $uri;
  343. }
  344. public function getGroups($uid = null)
  345. {
  346. $uid = is_null($uid) ? $this->id : $uid;
  347. return parent::getGroups($uid);
  348. }
  349. public function getRuleList($uid = null)
  350. {
  351. $uid = is_null($uid) ? $this->id : $uid;
  352. return parent::getRuleList($uid);
  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. return in_array('*', $this->getRuleIds()) ? true : false;
  362. }
  363. /**
  364. * 获取管理员所属于的分组ID
  365. * @param int $uid
  366. * @return array
  367. */
  368. public function getGroupIds($uid = null)
  369. {
  370. $groups = $this->getGroups($uid);
  371. $groupIds = [];
  372. foreach ($groups as $K => $v) {
  373. $groupIds[] = (int)$v['group_id'];
  374. }
  375. return $groupIds;
  376. }
  377. /**
  378. * 取出当前管理员所拥有权限的分组
  379. * @param boolean $withself 是否包含当前所在的分组
  380. * @return array
  381. */
  382. public function getChildrenGroupIds($withself = false)
  383. {
  384. //取出当前管理员所有的分组
  385. $groups = $this->getGroups();
  386. $groupIds = [];
  387. foreach ($groups as $k => $v) {
  388. $groupIds[] = $v['id'];
  389. }
  390. $originGroupIds = $groupIds;
  391. foreach ($groups as $k => $v) {
  392. if (in_array($v['pid'], $originGroupIds)) {
  393. $groupIds = array_diff($groupIds, [$v['id']]);
  394. unset($groups[$k]);
  395. }
  396. }
  397. // 取出所有分组
  398. $groupList = \app\company\model\AuthGroup::where($this->isSuperAdmin() ? '1=1' : ['status' => 'normal'])->select();
  399. $objList = [];
  400. foreach ($groups as $k => $v) {
  401. if ($v['rules'] === '*') {
  402. $objList = $groupList;
  403. break;
  404. }
  405. // 取出包含自己的所有子节点
  406. $childrenList = Tree::instance()->init($groupList, 'pid')->getChildren($v['id'], true);
  407. $obj = Tree::instance()->init($childrenList, 'pid')->getTreeArray($v['pid']);
  408. $objList = array_merge($objList, Tree::instance()->getTreeList($obj));
  409. }
  410. $childrenGroupIds = [];
  411. foreach ($objList as $k => $v) {
  412. $childrenGroupIds[] = $v['id'];
  413. }
  414. if (!$withself) {
  415. $childrenGroupIds = array_diff($childrenGroupIds, $groupIds);
  416. }
  417. return $childrenGroupIds;
  418. }
  419. /**
  420. * 取出当前管理员所拥有权限的管理员
  421. * @param boolean $withself 是否包含自身
  422. * @return array
  423. */
  424. public function getChildrenAdminIds($withself = false)
  425. {
  426. $childrenAdminIds = [];
  427. if (!$this->isSuperAdmin()) {
  428. $groupIds = $this->getChildrenGroupIds(false);
  429. $authGroupList = \app\company\model\AuthGroupAccess::field('uid,group_id')
  430. ->where('group_id', 'in', $groupIds)
  431. ->select();
  432. foreach ($authGroupList as $k => $v) {
  433. $childrenAdminIds[] = $v['uid'];
  434. }
  435. } else {
  436. //超级管理员拥有所有人的权限
  437. $childrenAdminIds = User::column('id');
  438. }
  439. if ($withself) {
  440. if (!in_array($this->id, $childrenAdminIds)) {
  441. $childrenAdminIds[] = $this->id;
  442. }
  443. } else {
  444. $childrenAdminIds = array_diff($childrenAdminIds, [$this->id]);
  445. }
  446. return $childrenAdminIds;
  447. }
  448. /**
  449. * 获得面包屑导航
  450. * @param string $path
  451. * @return array
  452. */
  453. public function getBreadCrumb($path = '')
  454. {
  455. if ($this->breadcrumb || !$path) {
  456. return $this->breadcrumb;
  457. }
  458. $titleArr = [];
  459. $menuArr = [];
  460. $urlArr = explode('/', $path);
  461. foreach ($urlArr as $index => $item) {
  462. $pathArr[implode('/', array_slice($urlArr, 0, $index + 1))] = $index;
  463. }
  464. if (!$this->rules && $this->id) {
  465. $this->getRuleList();
  466. }
  467. foreach ($this->rules as $rule) {
  468. if (isset($pathArr[$rule['name']])) {
  469. $rule['title'] = __($rule['title']);
  470. $rule['url'] = url($rule['name']);
  471. $titleArr[$pathArr[$rule['name']]] = $rule['title'];
  472. $menuArr[$pathArr[$rule['name']]] = $rule;
  473. }
  474. }
  475. ksort($menuArr);
  476. $this->breadcrumb = $menuArr;
  477. return $this->breadcrumb;
  478. }
  479. /**
  480. * 获取左侧菜单栏
  481. *
  482. * @param array $params URL对应的badge数据
  483. * @param string $fixedPage 默认页
  484. * @return array
  485. */
  486. public function get_menus(){
  487. // 读取管理员当前拥有的权限节点
  488. $userRule = $this->getRuleList();
  489. // 必须将结果集转换为数组
  490. $ruleList = collection(\app\admin\model\PcAuthRule::where('status', 'normal')
  491. ->field('id,pid as parentId,name as namerule,title as name,path,component,component_name,icon')
  492. ->where('type', 'NEQ',3)
  493. ->order('weigh', 'desc')
  494. ->select())->toArray();
  495. foreach ($ruleList as $k => &$v) {
  496. if (!in_array($v['namerule'], $userRule)) {
  497. unset($ruleList[$k]);
  498. continue;
  499. }
  500. }
  501. // dump($ruleList);
  502. Tree::instance()->init($ruleList,'parentId');
  503. $menu = Tree::instance()->getTreeArray(0);
  504. // dump($menu);
  505. return $menu;
  506. }
  507. public function get_permissions(){
  508. // 读取管理员当前拥有的权限节点
  509. $userRule = $this->getRuleList();
  510. // 必须将结果集转换为数组
  511. $ruleList = collection(\app\admin\model\PcAuthRule::where('status', 'normal')
  512. ->where('type', 3)
  513. ->order('weigh', 'desc')
  514. ->select())->toArray();
  515. foreach ($ruleList as $k => &$v) {
  516. if (!in_array($v['name'], $userRule)) {
  517. unset($ruleList[$k]);
  518. continue;
  519. }
  520. }
  521. // dump($ruleList);
  522. return array_column($ruleList,'permission');
  523. }
  524. public function getSidebar($params = [], $fixedPage = 'dashboard')
  525. {
  526. // 边栏开始
  527. Hook::listen("admin_sidebar_begin", $params);
  528. $colorArr = ['red', 'green', 'yellow', 'blue', 'teal', 'orange', 'purple'];
  529. $colorNums = count($colorArr);
  530. $badgeList = [];
  531. $module = request()->module();
  532. // 生成菜单的badge
  533. foreach ($params as $k => $v) {
  534. $url = $k;
  535. if (is_array($v)) {
  536. $nums = $v[0] ?? 0;
  537. $color = $v[1] ?? $colorArr[(is_numeric($nums) ? $nums : strlen($nums)) % $colorNums];
  538. $class = $v[2] ?? 'label';
  539. } else {
  540. $nums = $v;
  541. $color = $colorArr[(is_numeric($nums) ? $nums : strlen($nums)) % $colorNums];
  542. $class = 'label';
  543. }
  544. //必须nums大于0才显示
  545. if ($nums) {
  546. $badgeList[$url] = '<small class="' . $class . ' pull-right bg-' . $color . '">' . $nums . '</small>';
  547. }
  548. }
  549. // 读取管理员当前拥有的权限节点
  550. $userRule = $this->getRuleList();
  551. $selected = $referer = [];
  552. $refererUrl = Session::get('referer');
  553. // 必须将结果集转换为数组
  554. $ruleList = collection(\app\admin\model\PcAuthRule::where('status', 'normal')
  555. ->where('ismenu', 1)
  556. ->order('weigh', 'desc')
  557. ->cache("__menu__")
  558. ->select())->toArray();
  559. $indexRuleList = \app\admin\model\PcAuthRule::where('status', 'normal')
  560. ->where('ismenu', 0)
  561. ->where('name', 'like', '%/index')
  562. ->column('name,pid');
  563. $pidArr = array_unique(array_filter(array_column($ruleList, 'pid')));
  564. foreach ($ruleList as $k => &$v) {
  565. if (!in_array($v['name'], $userRule)) {
  566. unset($ruleList[$k]);
  567. continue;
  568. }
  569. $indexRuleName = $v['name'] . '/index';
  570. if (isset($indexRuleList[$indexRuleName]) && !in_array($indexRuleName, $userRule)) {
  571. unset($ruleList[$k]);
  572. continue;
  573. }
  574. $v['icon'] = $v['icon'] . ' fa-fw';
  575. $v['url'] = isset($v['url']) && $v['url'] ? $v['url'] : '/' . $module . '/' . $v['name'];
  576. $v['badge'] = $badgeList[$v['name']] ?? '';
  577. $v['title'] = __($v['title']);
  578. $v['url'] = preg_match("/^((?:[a-z]+:)?\/\/|data:image\/)(.*)/i", $v['url']) ? $v['url'] : url($v['url']);
  579. $v['menuclass'] = in_array($v['menutype'], ['dialog', 'ajax']) ? 'btn-' . $v['menutype'] : '';
  580. $v['menutabs'] = !$v['menutype'] || in_array($v['menutype'], ['default', 'addtabs']) ? 'addtabs="' . $v['id'] . '"' : '';
  581. $selected = $v['name'] == $fixedPage ? $v : $selected;
  582. $referer = $v['url'] == $refererUrl ? $v : $referer;
  583. }
  584. $lastArr = array_unique(array_filter(array_column($ruleList, 'pid')));
  585. $pidDiffArr = array_diff($pidArr, $lastArr);
  586. foreach ($ruleList as $index => $item) {
  587. if (in_array($item['id'], $pidDiffArr)) {
  588. unset($ruleList[$index]);
  589. }
  590. }
  591. if ($selected == $referer) {
  592. $referer = [];
  593. }
  594. $select_id = $referer ? $referer['id'] : ($selected ? $selected['id'] : 0);
  595. $menu = $nav = '';
  596. $showSubmenu = config('fastadmin.show_submenu');
  597. if (Config::get('fastadmin.multiplenav')) {
  598. $topList = [];
  599. foreach ($ruleList as $index => $item) {
  600. if (!$item['pid']) {
  601. $topList[] = $item;
  602. }
  603. }
  604. $selectParentIds = [];
  605. $tree = Tree::instance();
  606. $tree->init($ruleList);
  607. if ($select_id) {
  608. $selectParentIds = $tree->getParentsIds($select_id, true);
  609. }
  610. foreach ($topList as $index => $item) {
  611. $childList = Tree::instance()->getTreeMenu(
  612. $item['id'],
  613. '<li class="@class" pid="@pid"><a @extend href="@url@addtabs" addtabs="@id" class="@menuclass" url="@url" py="@py" pinyin="@pinyin"><i class="@icon"></i> <span>@title</span> <span class="pull-right-container">@caret @badge</span></a> @childlist</li>',
  614. $select_id,
  615. '',
  616. 'ul',
  617. 'class="treeview-menu' . ($showSubmenu ? ' menu-open' : '') . '"'
  618. );
  619. $current = in_array($item['id'], $selectParentIds);
  620. $url = $childList ? 'javascript:;' : $item['url'];
  621. $addtabs = $childList || !$url ? "" : (stripos($url, "?") !== false ? "&" : "?") . "ref=" . ($item['menutype'] ? $item['menutype'] : 'addtabs');
  622. $childList = str_replace(
  623. '" pid="' . $item['id'] . '"',
  624. ' ' . ($current ? '' : 'hidden') . '" pid="' . $item['id'] . '"',
  625. $childList
  626. );
  627. $nav .= '<li class="' . ($current ? 'active' : '') . '"><a ' . $item['extend'] . ' href="' . $url . $addtabs . '" ' . $item['menutabs'] . ' class="' . $item['menuclass'] . '" url="' . $url . '" title="' . $item['title'] . '"><i class="' . $item['icon'] . '"></i> <span>' . $item['title'] . '</span> <span class="pull-right-container"> </span></a> </li>';
  628. $menu .= $childList;
  629. }
  630. } else {
  631. // 构造菜单数据
  632. Tree::instance()->init($ruleList);
  633. $menu = Tree::instance()->getTreeMenu(
  634. 0,
  635. '<li class="@class"><a @extend href="@url@addtabs" @menutabs class="@menuclass" url="@url" py="@py" pinyin="@pinyin"><i class="@icon"></i> <span>@title</span> <span class="pull-right-container">@caret @badge</span></a> @childlist</li>',
  636. $select_id,
  637. '',
  638. 'ul',
  639. 'class="treeview-menu' . ($showSubmenu ? ' menu-open' : '') . '"'
  640. );
  641. if ($selected) {
  642. $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>';
  643. }
  644. if ($referer) {
  645. $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>';
  646. }
  647. }
  648. return [$menu, $nav, $selected, $referer];
  649. }
  650. /**
  651. * 获取允许输出的字段
  652. * @return array
  653. */
  654. public function getAllowFields()
  655. {
  656. return $this->allowFields;
  657. }
  658. /**
  659. * 设置允许输出的字段
  660. * @param array $fields
  661. */
  662. public function setAllowFields($fields)
  663. {
  664. $this->allowFields = $fields;
  665. }
  666. /**
  667. * 删除一个指定会员
  668. * @param int $user_id 会员ID
  669. * @return boolean
  670. */
  671. public function delete($user_id)
  672. {
  673. $user = User::get($user_id);
  674. if (!$user) {
  675. return false;
  676. }
  677. Db::startTrans();
  678. try {
  679. // 删除会员
  680. User::destroy($user_id);
  681. // 删除会员指定的所有Token
  682. Tokencompany::clear($user_id);
  683. Hook::listen("company_delete_successed", $user);
  684. Db::commit();
  685. } catch (Exception $e) {
  686. Db::rollback();
  687. $this->setError($e->getMessage());
  688. return false;
  689. }
  690. return true;
  691. }
  692. /**
  693. * 获取密码加密后的字符串
  694. * @param string $password 密码
  695. * @param string $salt 密码盐
  696. * @return string
  697. */
  698. public function getEncryptPassword($password, $salt = '')
  699. {
  700. return md5(md5($password) . $salt);
  701. }
  702. /**
  703. * 设置会话有效时间
  704. * @param int $keeptime 默认为永久
  705. */
  706. public function keeptime($keeptime = 0)
  707. {
  708. $this->keeptime = $keeptime;
  709. }
  710. /**
  711. * 设置错误信息
  712. *
  713. * @param string $error 错误信息
  714. * @return Auth
  715. */
  716. public function setError($error)
  717. {
  718. $this->_error = $error;
  719. return $this;
  720. }
  721. /**
  722. * 获取错误信息
  723. * @return string
  724. */
  725. public function getError()
  726. {
  727. return $this->_error ? __($this->_error) : '';
  728. }
  729. }