Authcompany.php 27 KB

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