View.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace App\Services;
  3. use Smarty;
  4. use App\Utils;
  5. class View
  6. {
  7. public static $connection;
  8. public static $beginTime;
  9. public static function getSmarty()
  10. {
  11. $smarty = new smarty(); //实例化smarty
  12. $user = Auth::getUser();
  13. if ($user->isLogin) {
  14. $theme = $user->theme;
  15. } else {
  16. $theme = $_ENV['theme'];
  17. }
  18. $can_backtoadmin = 0;
  19. if (Utils\Cookie::get('old_uid') && Utils\Cookie::get('old_email') && Utils\Cookie::get('old_key') && Utils\Cookie::get('old_ip') && Utils\Cookie::get('old_expire_in') && Utils\Cookie::get('old_local')) {
  20. $can_backtoadmin = 1;
  21. }
  22. $smarty->settemplatedir(BASE_PATH . '/resources/views/' . $theme . '/'); //设置模板文件存放目录
  23. $smarty->setcompiledir(BASE_PATH . '/storage/framework/smarty/compile/'); //设置生成文件存放目录
  24. $smarty->setcachedir(BASE_PATH . '/storage/framework/smarty/cache/'); //设置缓存文件存放目录
  25. // add config
  26. $smarty->assign('config', Config::getPublicConfig());
  27. $smarty->assign('user', $user);
  28. $smarty->assign('can_backtoadmin', $can_backtoadmin);
  29. if (self::$connection) {
  30. $smarty->assign('queryLog', self::$connection->connection('default')->getQueryLog());
  31. $optTime = microtime(true) - self::$beginTime;
  32. $smarty->assign('optTime', $optTime * 1000);
  33. }
  34. return $smarty;
  35. }
  36. }