Config.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class Config extends Base
  5. {
  6. protected $createTime = 'add_time';
  7. protected $updateTime = 'update_time';
  8. //属性修改器
  9. protected function setNameAttr($value){
  10. return strtolower($value);
  11. }
  12. public function updates(){
  13. $data = request()->post();
  14. $data['status'] = 1;
  15. return $this->updateInfo($data,'Config','');
  16. }
  17. /**
  18. * 保存配置
  19. *
  20. * @author wst
  21. * @date 2021/9/8 16:13
  22. * @return boolean
  23. */
  24. public function saveData($data=[]){
  25. $msg = '';
  26. foreach ($data as $k => $v) {
  27. $res = db('config')
  28. ->where('name', $k)
  29. ->update(['value' => $v, 'update_time' => time()]);
  30. if (!$res) {
  31. $msg.=$k . '保存失败';
  32. continue;
  33. }
  34. }
  35. if($msg){
  36. $this->error=$msg;
  37. return false;
  38. }
  39. return true;
  40. }
  41. // 获取数据库配置
  42. public function getConfig($name,$orgId=0){
  43. $info = \think\Db::name('config')->where('name',$name)->find();
  44. if($info){
  45. if($info['cate'] == 2){ // 项目配置
  46. $oinfo = \think\Db::name('config_org')->where('config_id',$info['id'])->where('org_id',$orgId)->find();
  47. $info['value'] = $oinfo?$oinfo['value']:'';
  48. }
  49. return $this->configParse($info['type'], $info['value']);
  50. }
  51. return false;
  52. }
  53. /**
  54. * 获取配置的类型
  55. * @param int $type 配置类型
  56. * @return string
  57. */
  58. function getConfigType($type=0){
  59. $list = $this->getConfig('config_type_list');
  60. return $list[$type];
  61. }
  62. /**
  63. * 获取配置的分组
  64. * @param int $group 配置分组
  65. * @return string
  66. */
  67. function getConfigGroup($group=0){
  68. $list = $this->getConfig('config_group_list');
  69. return $group&&isset($list[$group])?$list[$group]:'';
  70. }
  71. /**
  72. * 获取配置的分组
  73. * @param int $group 配置分组
  74. * @return string
  75. */
  76. function getConfigOrgGroup($group=0){
  77. $list = $this->getConfig('config_org_group_list');
  78. return $group&&isset($list[$group])?$list[$group]:'';
  79. }
  80. /**
  81. * 根据配置类型解析配置
  82. * @param $type
  83. * @param $value
  84. * @return array|false|string[]
  85. */
  86. public function configParse($type, $value){
  87. switch ($type) {
  88. case 3: //解析数组
  89. $array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
  90. if(strpos($value,':')){
  91. $value = array();
  92. foreach ($array as $val) {
  93. list($k, $v) = explode(':', $val);
  94. $value[$k] = $v;
  95. }
  96. }else{
  97. $value = $array;
  98. }
  99. break;
  100. }
  101. return $value;
  102. }
  103. // 分析枚举类型配置值 格式 a:名称1,b:名称2
  104. function parseConfigAttr($string) {
  105. $array = preg_split('/[,;\r\n]+/', trim($string, ",;\r\n"));
  106. if(strpos($string,':')){
  107. $value = array();
  108. foreach ($array as $val) {
  109. list($k, $v) = explode(':', $val);
  110. $value[$k] = $v;
  111. }
  112. }else{
  113. $value = $array;
  114. }
  115. return $value;
  116. }
  117. }