CSystemlog.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace fecadmin\helpers;
  3. use Yii;
  4. use fecadmin\models\AdminLog;
  5. use fecadmin\models\AdminMenu;
  6. use fec\helpers\CUrl;
  7. use fec\helpers\CCache;
  8. use fec\helpers\CConfig;
  9. class CSystemlog
  10. {
  11. const MENU_CACHE_ARR = 'menu_cache_arr';
  12. # 保存系统日志。
  13. public static function saveSystemLog(){
  14. $logConfig = CConfig::param("systemlog");
  15. //var_dump($logConfig);
  16. if(!is_array($logConfig) || !isset($logConfig['enable']) || !$logConfig['enable']){
  17. return;
  18. }
  19. $systemLog = new AdminLog();
  20. $user = Yii::$app->user->identity;
  21. if($user){
  22. $url_key = '/'.Yii::$app->controller->module->id.'/'.Yii::$app->controller->id;
  23. $username = $user['username'];
  24. $person = $user['person'];
  25. $currentData= date('Y-m-d H:i:s');
  26. $url = CUrl::getCurrentUrl();
  27. $systemLog->account = $username;
  28. $systemLog->person = $person;
  29. $systemLog->created_at = $currentData;
  30. $systemLog->url = $url;
  31. $systemLog->url_key = $url_key;
  32. $systemLog->menu = self::getMenuByUrlKey($url_key);
  33. $systemLog->save();
  34. }
  35. }
  36. public static function getMenuByUrlKey($url_key){
  37. if(!$url_key)
  38. return null;
  39. $menuArr = self::getMenuArr();
  40. return $menuArr[$url_key];
  41. }
  42. public static function getMenuArr(){
  43. if($menuArr = CCache::get(self::MENU_CACHE_ARR)){
  44. return $menuArr;
  45. }else{
  46. $menuArr = [];
  47. $data = AdminMenu::find()->select([
  48. 'name','role_key'
  49. ])->all();
  50. foreach($data as $one){
  51. $menuArr[$one['role_key']] = $one['name'];
  52. }
  53. $menuArr['/fecadmin/index'] = '主界面';
  54. $menuArr['/fecadmin/login'] = '账号登录';
  55. $menuArr['/fecadmin/logout'] = '账号退出';
  56. CCache::set(self::MENU_CACHE_ARR,$menuArr);
  57. return $menuArr;
  58. }
  59. }
  60. }