Config.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. class Config extends Model
  5. {
  6. // 设置字段信息
  7. protected $schema = [
  8. 'id' => 'int',
  9. 'code' => 'string',
  10. 'value' => 'string',
  11. 'remark' => 'string',
  12. ];
  13. public static function getConfigValue($code)
  14. {
  15. $res = [];
  16. if (is_array($code)) {
  17. $list = self::where('code', 'in', $code)->select();
  18. if ($list->isEmpty()) {
  19. return [];
  20. }
  21. foreach ($list as $v) {
  22. $res[$v['code']] = $v['value'];
  23. }
  24. } elseif (is_string($code)) {
  25. $info = self::where('code', $code)->find();
  26. if (empty($info)) {
  27. return '';
  28. }
  29. $res = $info['value'];
  30. }
  31. return $res;
  32. }
  33. public static function setConfigValue($code, $value = '')
  34. {
  35. if (is_array($code)) {
  36. foreach ($code as $k => $v) {
  37. self::setConfigValueSingle($k, $v);
  38. }
  39. } elseif (is_string($code)) {
  40. self::setConfigValueSingle($code, $value);
  41. }
  42. }
  43. public static function setConfigValueSingle($code, $value = '')
  44. {
  45. $info = self::where('code', $code)->find();
  46. if (empty($info)) {
  47. self::create(['code' => $code, 'value' => $value]);
  48. } else {
  49. $info->value = $value;
  50. $info->save();
  51. }
  52. }
  53. }