Authcompany.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. //绝对不允许*管理员
  360. public function isSuperAdmin()
  361. {
  362. // return false;
  363. return in_array('*', $this->getRuleIds()) ? true : false;
  364. }
  365. /**
  366. * 获取管理员所属于的分组ID
  367. * @param int $uid
  368. * @return array
  369. */
  370. public function getGroupIds($uid = null)
  371. {
  372. $groups = $this->getGroups($uid);
  373. $groupIds = [];
  374. foreach ($groups as $K => $v) {
  375. $groupIds[] = (int)$v['group_id'];
  376. }
  377. return $groupIds;
  378. }
  379. /**
  380. * 取出当前管理员所拥有权限的分组
  381. * @param boolean $withself 是否包含当前所在的分组
  382. * @return array
  383. */
  384. public function getChildrenGroupIds($withself = false)
  385. {
  386. //取出当前管理员所有的分组
  387. $groups = $this->getGroups();
  388. $groupIds = [];
  389. foreach ($groups as $k => $v) {
  390. $groupIds[] = $v['id'];
  391. }
  392. $originGroupIds = $groupIds;
  393. foreach ($groups as $k => $v) {
  394. if (in_array($v['pid'], $originGroupIds)) {
  395. $groupIds = array_diff($groupIds, [$v['id']]);
  396. unset($groups[$k]);
  397. }
  398. }
  399. // 取出所有分组
  400. $groupList = \app\company\model\AuthGroup::where($this->isSuperAdmin() ? '1=1' : ['status' => 'normal'])->where('company_id',$this->company_id)->select();
  401. $objList = [];
  402. foreach ($groups as $k => $v) {
  403. if ($v['rules'] === '*') {
  404. $objList = $groupList;
  405. break;
  406. }
  407. // 取出包含自己的所有子节点
  408. $childrenList = Tree::instance()->init($groupList, 'pid')->getChildren($v['id'], true);
  409. $obj = Tree::instance()->init($childrenList, 'pid')->getTreeArray($v['pid']);
  410. $objList = array_merge($objList, Tree::instance()->getTreeList($obj));
  411. }
  412. $childrenGroupIds = [];
  413. foreach ($objList as $k => $v) {
  414. $childrenGroupIds[] = $v['id'];
  415. }
  416. if (!$withself) {
  417. $childrenGroupIds = array_diff($childrenGroupIds, $groupIds);
  418. }
  419. return $childrenGroupIds;
  420. }
  421. /**
  422. * 取出当前管理员所拥有权限的管理员
  423. * @param boolean $withself 是否包含自身
  424. * @return array
  425. */
  426. public function getChildrenAdminIds($withself = false)
  427. {
  428. $childrenAdminIds = [];
  429. if (!$this->isSuperAdmin()) {
  430. $groupIds = $this->getChildrenGroupIds(false);
  431. $authGroupList = \app\company\model\AuthGroupAccess::field('uid,group_id')
  432. ->where('group_id', 'in', $groupIds)
  433. ->select();
  434. foreach ($authGroupList as $k => $v) {
  435. $childrenAdminIds[] = $v['uid'];
  436. }
  437. } else {
  438. //超级管理员拥有所有人的权限
  439. // $childrenAdminIds = User::column('id');
  440. $childrenAdminIds = User::where('company_id',$this->company_id)->column('id');
  441. }
  442. if ($withself) {
  443. if (!in_array($this->id, $childrenAdminIds)) {
  444. $childrenAdminIds[] = $this->id;
  445. }
  446. } else {
  447. $childrenAdminIds = array_diff($childrenAdminIds, [$this->id]);
  448. }
  449. return $childrenAdminIds;
  450. }
  451. /**
  452. * 获得面包屑导航
  453. * @param string $path
  454. * @return array
  455. */
  456. public function getBreadCrumb($path = '')
  457. {
  458. if ($this->breadcrumb || !$path) {
  459. return $this->breadcrumb;
  460. }
  461. $titleArr = [];
  462. $menuArr = [];
  463. $urlArr = explode('/', $path);
  464. foreach ($urlArr as $index => $item) {
  465. $pathArr[implode('/', array_slice($urlArr, 0, $index + 1))] = $index;
  466. }
  467. if (!$this->rules && $this->id) {
  468. $this->getRuleList();
  469. }
  470. foreach ($this->rules as $rule) {
  471. if (isset($pathArr[$rule['name']])) {
  472. $rule['title'] = __($rule['title']);
  473. $rule['url'] = url($rule['name']);
  474. $titleArr[$pathArr[$rule['name']]] = $rule['title'];
  475. $menuArr[$pathArr[$rule['name']]] = $rule;
  476. }
  477. }
  478. ksort($menuArr);
  479. $this->breadcrumb = $menuArr;
  480. return $this->breadcrumb;
  481. }
  482. /**
  483. * 获取左侧菜单栏
  484. *
  485. * @param array $params URL对应的badge数据
  486. * @param string $fixedPage 默认页
  487. * @return array
  488. */
  489. public function get_menus(){
  490. // 读取管理员当前拥有的权限节点
  491. $userRule = $this->getRuleList();
  492. // 必须将结果集转换为数组
  493. $ruleList = collection(\app\admin\model\PcAuthRule::where('status', 'normal')
  494. ->field('id,pid as parentId,name as namerule,title as name,path,component,component_name as componentName,icon,status as visible')
  495. ->where('type', 'NEQ',3)
  496. ->order('weigh', 'desc')
  497. ->select())->toArray();
  498. foreach ($ruleList as $k => &$v) {
  499. $v['alwaysShow'] = true;
  500. $v['keepAlive'] = true;
  501. if (!in_array($v['namerule'], $userRule)) {
  502. unset($ruleList[$k]);
  503. continue;
  504. }
  505. }
  506. // dump($ruleList);
  507. Tree::instance()->init($ruleList,'parentId');
  508. $menu = Tree::instance()->getTreeArray_2(0);
  509. // dump($menu);
  510. return $menu;
  511. }
  512. public function get_menus_simple(){
  513. // 读取管理员当前拥有的权限节点
  514. $userRule = $this->getRuleList();
  515. // 必须将结果集转换为数组
  516. $ruleList = collection(\app\admin\model\PcAuthRule::where('status', 'normal')
  517. ->field('id,pid as parentId,name as namerule,title as name,path,component,component_name as componentName,icon')
  518. ->where('type',2)->where('pid',0)
  519. ->order('weigh', 'desc')
  520. ->select())->toArray();
  521. foreach ($ruleList as $k => &$v) {
  522. $v['alwaysShow'] = true;
  523. $v['keepAlive'] = true;
  524. $v['visible'] = true;
  525. if (!in_array($v['namerule'], $userRule)) {
  526. unset($ruleList[$k]);
  527. continue;
  528. }
  529. }
  530. return $ruleList;
  531. // dump($ruleList);
  532. Tree::instance()->init($ruleList,'parentId');
  533. $menu = Tree::instance()->getTreeArray(0);
  534. // dump($menu);
  535. return $menu;
  536. }
  537. public function get_permissions(){
  538. // 读取管理员当前拥有的权限节点
  539. $userRule = $this->getRuleList();
  540. // 必须将结果集转换为数组
  541. $ruleList = collection(\app\admin\model\PcAuthRule::where('status', 'normal')
  542. ->where('type', 3)
  543. ->order('weigh', 'desc')
  544. ->select())->toArray();
  545. foreach ($ruleList as $k => &$v) {
  546. if (!in_array($v['name'], $userRule)) {
  547. unset($ruleList[$k]);
  548. continue;
  549. }
  550. }
  551. // dump($ruleList);
  552. return array_column($ruleList,'permission');
  553. }
  554. /*
  555. public function getSidebar($params = [], $fixedPage = 'dashboard')
  556. {
  557. // 边栏开始
  558. Hook::listen("admin_sidebar_begin", $params);
  559. $colorArr = ['red', 'green', 'yellow', 'blue', 'teal', 'orange', 'purple'];
  560. $colorNums = count($colorArr);
  561. $badgeList = [];
  562. $module = request()->module();
  563. // 生成菜单的badge
  564. foreach ($params as $k => $v) {
  565. $url = $k;
  566. if (is_array($v)) {
  567. $nums = $v[0] ?? 0;
  568. $color = $v[1] ?? $colorArr[(is_numeric($nums) ? $nums : strlen($nums)) % $colorNums];
  569. $class = $v[2] ?? 'label';
  570. } else {
  571. $nums = $v;
  572. $color = $colorArr[(is_numeric($nums) ? $nums : strlen($nums)) % $colorNums];
  573. $class = 'label';
  574. }
  575. //必须nums大于0才显示
  576. if ($nums) {
  577. $badgeList[$url] = '<small class="' . $class . ' pull-right bg-' . $color . '">' . $nums . '</small>';
  578. }
  579. }
  580. // 读取管理员当前拥有的权限节点
  581. $userRule = $this->getRuleList();
  582. $selected = $referer = [];
  583. $refererUrl = Session::get('referer');
  584. // 必须将结果集转换为数组
  585. $ruleList = collection(\app\admin\model\PcAuthRule::where('status', 'normal')
  586. ->where('ismenu', 1)
  587. ->order('weigh', 'desc')
  588. ->cache("__menu__")
  589. ->select())->toArray();
  590. $indexRuleList = \app\admin\model\PcAuthRule::where('status', 'normal')
  591. ->where('ismenu', 0)
  592. ->where('name', 'like', '%/index')
  593. ->column('name,pid');
  594. $pidArr = array_unique(array_filter(array_column($ruleList, 'pid')));
  595. foreach ($ruleList as $k => &$v) {
  596. if (!in_array($v['name'], $userRule)) {
  597. unset($ruleList[$k]);
  598. continue;
  599. }
  600. $indexRuleName = $v['name'] . '/index';
  601. if (isset($indexRuleList[$indexRuleName]) && !in_array($indexRuleName, $userRule)) {
  602. unset($ruleList[$k]);
  603. continue;
  604. }
  605. $v['icon'] = $v['icon'] . ' fa-fw';
  606. $v['url'] = isset($v['url']) && $v['url'] ? $v['url'] : '/' . $module . '/' . $v['name'];
  607. $v['badge'] = $badgeList[$v['name']] ?? '';
  608. $v['title'] = __($v['title']);
  609. $v['url'] = preg_match("/^((?:[a-z]+:)?\/\/|data:image\/)(.*)/i", $v['url']) ? $v['url'] : url($v['url']);
  610. $v['menuclass'] = in_array($v['menutype'], ['dialog', 'ajax']) ? 'btn-' . $v['menutype'] : '';
  611. $v['menutabs'] = !$v['menutype'] || in_array($v['menutype'], ['default', 'addtabs']) ? 'addtabs="' . $v['id'] . '"' : '';
  612. $selected = $v['name'] == $fixedPage ? $v : $selected;
  613. $referer = $v['url'] == $refererUrl ? $v : $referer;
  614. }
  615. $lastArr = array_unique(array_filter(array_column($ruleList, 'pid')));
  616. $pidDiffArr = array_diff($pidArr, $lastArr);
  617. foreach ($ruleList as $index => $item) {
  618. if (in_array($item['id'], $pidDiffArr)) {
  619. unset($ruleList[$index]);
  620. }
  621. }
  622. if ($selected == $referer) {
  623. $referer = [];
  624. }
  625. $select_id = $referer ? $referer['id'] : ($selected ? $selected['id'] : 0);
  626. $menu = $nav = '';
  627. $showSubmenu = config('fastadmin.show_submenu');
  628. if (Config::get('fastadmin.multiplenav')) {
  629. $topList = [];
  630. foreach ($ruleList as $index => $item) {
  631. if (!$item['pid']) {
  632. $topList[] = $item;
  633. }
  634. }
  635. $selectParentIds = [];
  636. $tree = Tree::instance();
  637. $tree->init($ruleList);
  638. if ($select_id) {
  639. $selectParentIds = $tree->getParentsIds($select_id, true);
  640. }
  641. foreach ($topList as $index => $item) {
  642. $childList = Tree::instance()->getTreeMenu(
  643. $item['id'],
  644. '<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>',
  645. $select_id,
  646. '',
  647. 'ul',
  648. 'class="treeview-menu' . ($showSubmenu ? ' menu-open' : '') . '"'
  649. );
  650. $current = in_array($item['id'], $selectParentIds);
  651. $url = $childList ? 'javascript:;' : $item['url'];
  652. $addtabs = $childList || !$url ? "" : (stripos($url, "?") !== false ? "&" : "?") . "ref=" . ($item['menutype'] ? $item['menutype'] : 'addtabs');
  653. $childList = str_replace(
  654. '" pid="' . $item['id'] . '"',
  655. ' ' . ($current ? '' : 'hidden') . '" pid="' . $item['id'] . '"',
  656. $childList
  657. );
  658. $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>';
  659. $menu .= $childList;
  660. }
  661. } else {
  662. // 构造菜单数据
  663. Tree::instance()->init($ruleList);
  664. $menu = Tree::instance()->getTreeMenu(
  665. 0,
  666. '<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>',
  667. $select_id,
  668. '',
  669. 'ul',
  670. 'class="treeview-menu' . ($showSubmenu ? ' menu-open' : '') . '"'
  671. );
  672. if ($selected) {
  673. $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>';
  674. }
  675. if ($referer) {
  676. $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>';
  677. }
  678. }
  679. return [$menu, $nav, $selected, $referer];
  680. }
  681. */
  682. /**
  683. * 获取允许输出的字段
  684. * @return array
  685. */
  686. public function getAllowFields()
  687. {
  688. return $this->allowFields;
  689. }
  690. /**
  691. * 设置允许输出的字段
  692. * @param array $fields
  693. */
  694. public function setAllowFields($fields)
  695. {
  696. $this->allowFields = $fields;
  697. }
  698. /**
  699. * 删除一个指定会员
  700. * @param int $user_id 会员ID
  701. * @return boolean
  702. */
  703. public function delete($user_id)
  704. {
  705. $user = User::get($user_id);
  706. if (!$user) {
  707. return false;
  708. }
  709. Db::startTrans();
  710. try {
  711. // 删除会员
  712. User::destroy($user_id);
  713. // 删除会员指定的所有Token
  714. Tokencompany::clear($user_id);
  715. Hook::listen("company_delete_successed", $user);
  716. Db::commit();
  717. } catch (Exception $e) {
  718. Db::rollback();
  719. $this->setError($e->getMessage());
  720. return false;
  721. }
  722. return true;
  723. }
  724. /**
  725. * 获取密码加密后的字符串
  726. * @param string $password 密码
  727. * @param string $salt 密码盐
  728. * @return string
  729. */
  730. public function getEncryptPassword($password, $salt = '')
  731. {
  732. return md5(md5($password) . $salt);
  733. }
  734. /**
  735. * 设置会话有效时间
  736. * @param int $keeptime 默认为永久
  737. */
  738. public function keeptime($keeptime = 0)
  739. {
  740. $this->keeptime = $keeptime;
  741. }
  742. /**
  743. * 设置错误信息
  744. *
  745. * @param string $error 错误信息
  746. * @return Auth
  747. */
  748. public function setError($error)
  749. {
  750. $this->_error = $error;
  751. return $this;
  752. }
  753. /**
  754. * 获取错误信息
  755. * @return string
  756. */
  757. public function getError()
  758. {
  759. return $this->_error ? __($this->_error) : '';
  760. }
  761. }