Config.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. foreach ($list as $v) {
  19. $res[$v['code']] = $v['value'];
  20. }
  21. } elseif (is_string($code)) {
  22. $info = self::where('code', $code)->find();
  23. $res = $info['value'];
  24. }
  25. return $res;
  26. }
  27. public static function setConfigValue($code, $value = '')
  28. {
  29. if (is_array($code)) {
  30. foreach ($code as $k => $v) {
  31. self::setConfigValueSingle($k, $v);
  32. }
  33. } elseif (is_string($code)) {
  34. self::setConfigValueSingle($code, $value);
  35. }
  36. }
  37. public static function setConfigValueSingle($code, $value = '')
  38. {
  39. $info = self::where('code', $code)->find();
  40. if (empty($info)) {
  41. self::create(['code' => $code, 'value' => $value]);
  42. } else {
  43. $info->value = $value;
  44. $info->save();
  45. }
  46. }
  47. }