Authcompany.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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,icon')
  495. ->where('type', 'NEQ',3)
  496. ->order('weigh', 'desc')
  497. ->select())->toArray();
  498. foreach ($ruleList as $k => &$v) {
  499. if (!in_array($v['namerule'], $userRule)) {
  500. unset($ruleList[$k]);
  501. continue;
  502. }
  503. }
  504. // dump($ruleList);
  505. Tree::instance()->init($ruleList,'parentId');
  506. $menu = Tree::instance()->getTreeArray(0);
  507. // dump($menu);
  508. return $menu;
  509. }
  510. public function get_menus_simple(){
  511. // 读取管理员当前拥有的权限节点
  512. $userRule = $this->getRuleList();
  513. // 必须将结果集转换为数组
  514. $ruleList = collection(\app\admin\model\PcAuthRule::where('status', 'normal')
  515. ->field('id,pid as parentId,name as namerule,title as name,path,component,component_name,icon')
  516. ->where('type',2)->where('pid',0)
  517. ->order('weigh', 'desc')
  518. ->select())->toArray();
  519. foreach ($ruleList as $k => &$v) {
  520. if (!in_array($v['namerule'], $userRule)) {
  521. unset($ruleList[$k]);
  522. continue;
  523. }
  524. }
  525. return $ruleList;
  526. // dump($ruleList);
  527. Tree::instance()->init($ruleList,'parentId');
  528. $menu = Tree::instance()->getTreeArray(0);
  529. // dump($menu);
  530. return $menu;
  531. }
  532. public function get_permissions(){
  533. // 读取管理员当前拥有的权限节点
  534. $userRule = $this->getRuleList();
  535. // 必须将结果集转换为数组
  536. $ruleList = collection(\app\admin\model\PcAuthRule::where('status', 'normal')
  537. ->where('type', 3)
  538. ->order('weigh', 'desc')
  539. ->select())->toArray();
  540. foreach ($ruleList as $k => &$v) {
  541. if (!in_array($v['name'], $userRule)) {
  542. unset($ruleList[$k]);
  543. continue;
  544. }
  545. }
  546. // dump($ruleList);
  547. return array_column($ruleList,'permission');
  548. }
  549. /*
  550. public function getSidebar($params = [], $fixedPage = 'dashboard')
  551. {
  552. // 边栏开始
  553. Hook::listen("admin_sidebar_begin", $params);
  554. $colorArr = ['red', 'green', 'yellow', 'blue', 'teal', 'orange', 'purple'];
  555. $colorNums = count($colorArr);
  556. $badgeList = [];
  557. $module = request()->module();
  558. // 生成菜单的badge
  559. foreach ($params as $k => $v) {
  560. $url = $k;
  561. if (is_array($v)) {
  562. $nums = $v[0] ?? 0;
  563. $color = $v[1] ?? $colorArr[(is_numeric($nums) ? $nums : strlen($nums)) % $colorNums];
  564. $class = $v[2] ?? 'label';
  565. } else {
  566. $nums = $v;
  567. $color = $colorArr[(is_numeric($nums) ? $nums : strlen($nums)) % $colorNums];
  568. $class = 'label';
  569. }
  570. //必须nums大于0才显示
  571. if ($nums) {
  572. $badgeList[$url] = '<small class="' . $class . ' pull-right bg-' . $color . '">' . $nums . '</small>';
  573. }
  574. }
  575. // 读取管理员当前拥有的权限节点
  576. $userRule = $this->getRuleList();
  577. $selected = $referer = [];
  578. $refererUrl = Session::get('referer');
  579. // 必须将结果集转换为数组
  580. $ruleList = collection(\app\admin\model\PcAuthRule::where('status', 'normal')
  581. ->where('ismenu', 1)
  582. ->order('weigh', 'desc')
  583. ->cache("__menu__")
  584. ->select())->toArray();
  585. $indexRuleList = \app\admin\model\PcAuthRule::where('status', 'normal')
  586. ->where('ismenu', 0)
  587. ->where('name', 'like', '%/index')
  588. ->column('name,pid');
  589. $pidArr = array_unique(array_filter(array_column($ruleList, 'pid')));
  590. foreach ($ruleList as $k => &$v) {
  591. if (!in_array($v['name'], $userRule)) {
  592. unset($ruleList[$k]);
  593. continue;
  594. }
  595. $indexRuleName = $v['name'] . '/index';
  596. if (isset($indexRuleList[$indexRuleName]) && !in_array($indexRuleName, $userRule)) {
  597. unset($ruleList[$k]);
  598. continue;
  599. }
  600. $v['icon'] = $v['icon'] . ' fa-fw';
  601. $v['url'] = isset($v['url']) && $v['url'] ? $v['url'] : '/' . $module . '/' . $v['name'];
  602. $v['badge'] = $badgeList[$v['name']] ?? '';
  603. $v['title'] = __($v['title']);
  604. $v['url'] = preg_match("/^((?:[a-z]+:)?\/\/|data:image\/)(.*)/i", $v['url']) ? $v['url'] : url($v['url']);
  605. $v['menuclass'] = in_array($v['menutype'], ['dialog', 'ajax']) ? 'btn-' . $v['menutype'] : '';
  606. $v['menutabs'] = !$v['menutype'] || in_array($v['menutype'], ['default', 'addtabs']) ? 'addtabs="' . $v['id'] . '"' : '';
  607. $selected = $v['name'] == $fixedPage ? $v : $selected;
  608. $referer = $v['url'] == $refererUrl ? $v : $referer;
  609. }
  610. $lastArr = array_unique(array_filter(array_column($ruleList, 'pid')));
  611. $pidDiffArr = array_diff($pidArr, $lastArr);
  612. foreach ($ruleList as $index => $item) {
  613. if (in_array($item['id'], $pidDiffArr)) {
  614. unset($ruleList[$index]);
  615. }
  616. }
  617. if ($selected == $referer) {
  618. $referer = [];
  619. }
  620. $select_id = $referer ? $referer['id'] : ($selected ? $selected['id'] : 0);
  621. $menu = $nav = '';
  622. $showSubmenu = config('fastadmin.show_submenu');
  623. if (Config::get('fastadmin.multiplenav')) {
  624. $topList = [];
  625. foreach ($ruleList as $index => $item) {
  626. if (!$item['pid']) {
  627. $topList[] = $item;
  628. }
  629. }
  630. $selectParentIds = [];
  631. $tree = Tree::instance();
  632. $tree->init($ruleList);
  633. if ($select_id) {
  634. $selectParentIds = $tree->getParentsIds($select_id, true);
  635. }
  636. foreach ($topList as $index => $item) {
  637. $childList = Tree::instance()->getTreeMenu(
  638. $item['id'],
  639. '<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>',
  640. $select_id,
  641. '',
  642. 'ul',
  643. 'class="treeview-menu' . ($showSubmenu ? ' menu-open' : '') . '"'
  644. );
  645. $current = in_array($item['id'], $selectParentIds);
  646. $url = $childList ? 'javascript:;' : $item['url'];
  647. $addtabs = $childList || !$url ? "" : (stripos($url, "?") !== false ? "&" : "?") . "ref=" . ($item['menutype'] ? $item['menutype'] : 'addtabs');
  648. $childList = str_replace(
  649. '" pid="' . $item['id'] . '"',
  650. ' ' . ($current ? '' : 'hidden') . '" pid="' . $item['id'] . '"',
  651. $childList
  652. );
  653. $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>';
  654. $menu .= $childList;
  655. }
  656. } else {
  657. // 构造菜单数据
  658. Tree::instance()->init($ruleList);
  659. $menu = Tree::instance()->getTreeMenu(
  660. 0,
  661. '<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>',
  662. $select_id,
  663. '',
  664. 'ul',
  665. 'class="treeview-menu' . ($showSubmenu ? ' menu-open' : '') . '"'
  666. );
  667. if ($selected) {
  668. $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>';
  669. }
  670. if ($referer) {
  671. $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>';
  672. }
  673. }
  674. return [$menu, $nav, $selected, $referer];
  675. }
  676. */
  677. /**
  678. * 获取允许输出的字段
  679. * @return array
  680. */
  681. public function getAllowFields()
  682. {
  683. return $this->allowFields;
  684. }
  685. /**
  686. * 设置允许输出的字段
  687. * @param array $fields
  688. */
  689. public function setAllowFields($fields)
  690. {
  691. $this->allowFields = $fields;
  692. }
  693. /**
  694. * 删除一个指定会员
  695. * @param int $user_id 会员ID
  696. * @return boolean
  697. */
  698. public function delete($user_id)
  699. {
  700. $user = User::get($user_id);
  701. if (!$user) {
  702. return false;
  703. }
  704. Db::startTrans();
  705. try {
  706. // 删除会员
  707. User::destroy($user_id);
  708. // 删除会员指定的所有Token
  709. Tokencompany::clear($user_id);
  710. Hook::listen("company_delete_successed", $user);
  711. Db::commit();
  712. } catch (Exception $e) {
  713. Db::rollback();
  714. $this->setError($e->getMessage());
  715. return false;
  716. }
  717. return true;
  718. }
  719. /**
  720. * 获取密码加密后的字符串
  721. * @param string $password 密码
  722. * @param string $salt 密码盐
  723. * @return string
  724. */
  725. public function getEncryptPassword($password, $salt = '')
  726. {
  727. return md5(md5($password) . $salt);
  728. }
  729. /**
  730. * 设置会话有效时间
  731. * @param int $keeptime 默认为永久
  732. */
  733. public function keeptime($keeptime = 0)
  734. {
  735. $this->keeptime = $keeptime;
  736. }
  737. /**
  738. * 设置错误信息
  739. *
  740. * @param string $error 错误信息
  741. * @return Auth
  742. */
  743. public function setError($error)
  744. {
  745. $this->_error = $error;
  746. return $this;
  747. }
  748. /**
  749. * 获取错误信息
  750. * @return string
  751. */
  752. public function getError()
  753. {
  754. return $this->_error ? __($this->_error) : '';
  755. }
  756. }