SystemSetting.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: liuyifan
  5. * Date: 2019/2/27
  6. * Time: 17:54
  7. */
  8. namespace app\platform\modules\system\models;
  9. use app\common\models\BaseModel;
  10. use app\common\helpers\Cache;
  11. class SystemSetting extends BaseModel
  12. {
  13. public $table = 'yz_system_setting';
  14. public $timestamps = true;
  15. protected $guarded = [''];
  16. /**
  17. * 保存数据
  18. * @param string $data
  19. * @param string $key
  20. * @param $cache_name
  21. * @return SystemSetting|bool
  22. */
  23. public static function settingSave($data = '', $key = '', $cache_name = '')
  24. {
  25. if (!$data && !$key) {
  26. return false;
  27. }
  28. $is_exists = self::where('key', $key)->first();
  29. $data = serialize($data);
  30. if (!$is_exists) {
  31. $system_setting = new self;
  32. // 添加
  33. $type = '添加 ';
  34. $result = $system_setting::create([
  35. 'key' => $key,
  36. 'value' => $data
  37. ]);
  38. } else {
  39. $type = '修改 ';
  40. // 修改
  41. $result = self::where('key', $key)->update(['value' => $data]);
  42. }
  43. \Log::info('----------系统设置----------', $type.$key.'-----设置数据-----'.json_encode($data));
  44. Cache::store()->forget($cache_name);
  45. $result ? Cache::put($cache_name, ['key' => $key, 'value' => $data] , 3600) : null;
  46. return $result;
  47. }
  48. /**
  49. * 读取数据
  50. * @param string $key
  51. * @param string $cache_name
  52. * @param bool $sign
  53. * @return SystemSetting|mixed
  54. */
  55. public static function settingLoad($key = '', $cache_name = '', $sign = false)
  56. {
  57. $result = Cache::store()->remember($cache_name, 3600, function () use ($key) {
  58. return app('SystemSetting')->get($key);
  59. });
  60. if ($result && !$sign) {
  61. $result = unserialize($result['value']);
  62. } else {
  63. return $result;
  64. }
  65. return $result;
  66. }
  67. public static function getKeyList($key)
  68. {
  69. return self::where('key', $key)->first();
  70. }
  71. }