Config.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace app\admin\model;
  3. use app\BaseModel;
  4. use think\facade\Lang;
  5. class Config extends BaseModel
  6. {
  7. protected $autoWriteTimestamp = true;
  8. protected $createTime = 'created_at';
  9. protected $updateTime = 'updated_at';
  10. // protected $json = ['val'];
  11. // protected $jsonAssoc = true;
  12. public static function getSelect($language_code, $select = true) {
  13. Lang::setLangSet($language_code);
  14. $data = [
  15. 'kefu_chat_type' => [
  16. 1 => Lang::get('顺序分配')
  17. ],
  18. 'user_vip_level' => [
  19. 1 => 1,
  20. 2 => 2,
  21. 3 => 3,
  22. 4 => 4,
  23. 5 => 5,
  24. 6 => 6,
  25. 7 => 7,
  26. 8 => 8,
  27. 9 => 9,
  28. ]
  29. ];
  30. if ($select) {
  31. foreach ($data as &$item) {
  32. $item = getSelectData($item);
  33. }
  34. }
  35. return $data;
  36. }
  37. //字段val如果是json格式,转换为数组
  38. public function getValAttribute($value)
  39. {
  40. return $value ? json_decode($value, true) : '';
  41. }
  42. //获取指定配置的数据
  43. public static function getFieldValue($field)
  44. {
  45. $config = self::where('field', $field)->find();
  46. if (!empty($config['type']) && $config['type'] == 'rich_text') {
  47. return $config['val'] ? json_decode($config['val'], true) : '';
  48. }
  49. return '';
  50. }
  51. //获取配置数据
  52. public static function getConfigData($fields, $flag = 1)
  53. {
  54. if (!empty($fields)) {
  55. $list = Config::whereIn('field',$fields)->select();
  56. } else {
  57. $list = Config::where('flag',$flag)->whereIn('field',$fields)->select();
  58. }
  59. $data = [];
  60. foreach ($list as $item) {
  61. if ($item['type'] == 'rich_text') {
  62. $item['val'] = $item['val'] ? json_decode($item['val'], true) : '';
  63. }
  64. $data[$item['field']] = $item['val'];
  65. }
  66. return $data;
  67. }
  68. //获取客服接线数量的上限配置
  69. public static function getKefuChatMax()
  70. {
  71. //先从缓存中读取config表获取客服接线数量的上限配置
  72. $kefu_chat_max = cache('config_kefu_chat_max');
  73. if ($kefu_chat_max) {
  74. return $kefu_chat_max;
  75. }
  76. $config = Config::whereIn('field',['kefu_chat_max'])->column('val', 'field');
  77. cache('config_kefu_chat_max', $config['kefu_chat_max'] ?? 0);
  78. return $config['kefu_chat_max'] ?? 0;
  79. }
  80. }