AppAsset.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace backend\assets;
  3. use yii\web\AssetBundle;
  4. use Yii;
  5. use yii\helpers\ArrayHelper;
  6. /**
  7. * Main backend application asset bundle.
  8. */
  9. class AppAsset extends AssetBundle
  10. {
  11. private $cacheFiles;
  12. public $basePath = '@webroot';
  13. public $baseUrl = '@web';
  14. public $css = [
  15. // 'css/site.css',
  16. 'static/css/index.css',
  17. 'static/css/public.css',
  18. 'static/layui_2.5/layui/css/layui.css',
  19. ];
  20. public $js = [
  21. 'static/layui/csrftoken.js',
  22. 'static/layui_2.5/layui/layui.js',
  23. 'static/js/ajaxRequest.js',
  24. // 'static/js/index.js',
  25. 'static/js/cache.js',
  26. // 'static/js/main.js',
  27. ];
  28. public $depends = [
  29. 'yii\web\YiiAsset',
  30. // 'yii\bootstrap\BootstrapAsset',
  31. ];
  32. public static function addScript($view, $jsfile) {
  33. $view->registerJsFile($jsfile.static::Clear(), [AppAsset::className(), "depends" => "backend\assets\AppAsset"]);
  34. }
  35. public static function addCss($view, $jsfile)
  36. {
  37. $view->registerCssFile($jsfile);
  38. }
  39. public function registerAssetFiles($view)
  40. {
  41. $manager = $view->getAssetManager();
  42. foreach ($this->js as $js) {
  43. if (is_array($js)) {
  44. $file = array_shift($js);
  45. $options = ArrayHelper::merge($this->jsOptions, $js);
  46. $view->registerJsFile($manager->getAssetUrl($this, $file), $options);
  47. } else {
  48. if ($js !== null) {
  49. $view->registerJsFile($manager->getAssetUrl($this, $js.static::Clear()), $this->jsOptions);
  50. }
  51. }
  52. }
  53. foreach ($this->css as $css) {
  54. if (is_array($css)) {
  55. $file = array_shift($css);
  56. $options = ArrayHelper::merge($this->cssOptions, $css);
  57. $view->registerCssFile($manager->getAssetUrl($this, $file), $options);
  58. } else {
  59. if ($css !== null) {
  60. $view->registerCssFile($manager->getAssetUrl($this, $css.static::Clear()), $this->cssOptions);
  61. }
  62. }
  63. }
  64. }
  65. public static function Clear()
  66. {
  67. $cache = Yii::$app->cache;
  68. $name = Yii::$app->params['resourceCacheKey']['backend'];
  69. if($cache->get($name) != false)
  70. {
  71. return '?v='.$cache->get($name);
  72. }
  73. else
  74. {
  75. return '?v=31231243211';
  76. }
  77. }
  78. }