System.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * User: Mike
  4. * Email: m@9026.com
  5. * Date: 2016/7/26
  6. * Time: 15:24
  7. */
  8. namespace App\Services\Base;
  9. use App\Models\SysConfigModel;
  10. use Illuminate\Support\Facades\Cache;
  11. /**
  12. * 全局数据字典的调用
  13. *
  14. * 提供配置缓存的类型
  15. *
  16. * @author wangzhoudong <admin@zhen.pl>
  17. * @version 1.0
  18. *
  19. */
  20. class System {
  21. private $cacheKey = 'sys_config';
  22. public function __construct() {
  23. }
  24. public function getCoufig($key=null) {
  25. $config = Cache::get($this->cacheKey);
  26. if($config){
  27. if($key){
  28. $data = isset($config[$key]) ? $config[$key] : '';
  29. }else{
  30. $data = $config;
  31. }
  32. }else {
  33. $obj = new SysConfigModel();
  34. if ($key) {
  35. $data = $obj->select("value")->where("key", $key)->first();
  36. if ($data) {
  37. $data = $data->value;
  38. }
  39. } else {
  40. $data = $obj->pluck('value', 'key');
  41. }
  42. }
  43. return $data;
  44. }
  45. public function saveConfig($data) {
  46. $obj = new SysConfigModel();
  47. foreach($data as $key=>$val) {
  48. $objVal = $obj->find($key);
  49. if ($objVal){
  50. $objVal->value = $val;
  51. $objVal->save();
  52. }else{
  53. $obj->create(['key' => $key,'value'=>$val]);
  54. }
  55. }
  56. $config = $obj->lists('value', 'key')->toArray();
  57. Cache::forever($this->cacheKey, $config);
  58. return true;
  59. }
  60. }