Config.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace app\common\model;
  3. /**
  4. * 配置模型类
  5. * @package app\common\model
  6. */
  7. class Config extends Base
  8. {
  9. const CODE_WECHAT = 'wechat';
  10. // 设置json类型字段
  11. protected $json = ['content'];
  12. /**
  13. * 获取配置对象
  14. * @param string $code
  15. * @return array|mixed|\think\Model|null
  16. * @throws \think\db\exception\DataNotFoundException
  17. * @throws \think\db\exception\DbException
  18. * @throws \think\db\exception\ModelNotFoundException
  19. */
  20. public static function getConfig($code)
  21. {
  22. $config = (new self)->where('code', $code)->cache(true, 60)->find();
  23. if (empty($config)) {
  24. $configData = [
  25. 'code' => $code,
  26. 'create_time' => getNow(),
  27. 'update_time' => getNow(),
  28. ];
  29. $config = self::create($configData);
  30. }
  31. return $config;
  32. }
  33. /**
  34. * 获取配置数据内容
  35. * @param $code
  36. * @return mixed
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public static function getConfigContent($code)
  42. {
  43. $config = self::getConfig($code);
  44. return $config->content;
  45. }
  46. /**
  47. * 获取配置数据中指定的值
  48. * @param $code
  49. * @param $content_code 如果读取的为json值才有效
  50. * @return bool|mixed
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public static function getConfigValue($code, $content_code = '')
  56. {
  57. $content = self::getConfigContent($code);
  58. return $content->$content_code;
  59. }
  60. }