CConfig.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecadmin\helpers;
  10. use Yii;
  11. use fecadmin\models\AdminConfig;
  12. use fec\helpers\CCache;
  13. /**
  14. * @author Terry Zhao <2358269014@qq.com>
  15. * @since 1.0
  16. */
  17. class CConfig extends \fec\helpers\CConfig
  18. {
  19. const ADMIN_CONFIG_CONFIX = 'admin_x_config_';
  20. public static function param($key){
  21. if($v = Yii::$app->params[$key]){
  22. return $v;
  23. }else if($v = self::getCacheConfig($key)){
  24. return $v;
  25. }else{
  26. return false;
  27. }
  28. }
  29. # 得到缓存的配置
  30. public static function getCacheConfig($key){
  31. $cache_key = self::ADMIN_CONFIG_CONFIX.$key;
  32. $v = CCache::get($cache_key);
  33. if($v){
  34. return $v;
  35. }else{
  36. $one = AdminConfig::findOne(['key' => $key]);
  37. if($one->id){
  38. self::setCacheConfig($key,$one->value);
  39. return $one->value;
  40. }
  41. return '';
  42. }
  43. }
  44. public static function setCacheConfig($key,$val){
  45. $cache_key = self::ADMIN_CONFIG_CONFIX.$key;
  46. CCache::set($cache_key,$val);
  47. }
  48. public static function flushCacheConfig(){
  49. $data = AdminConfig::find()->all();
  50. if(is_array($data) && !empty($data)){
  51. foreach($data as $one){
  52. $key = $one['key'];
  53. $value = $one['value'];
  54. $cache_key = self::ADMIN_CONFIG_CONFIX.$key;
  55. CCache::set($cache_key,$value);
  56. }
  57. }
  58. }
  59. }