AppAsset.php 2.6 KB

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