ConfigApi.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | TwoThink [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013 http://www.twothink.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 田建龙 <864491238@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\Api;
  12. class ConfigApi {
  13. /**
  14. * 获取数据库中的配置列表
  15. * @return array 配置数组
  16. */
  17. public static function lists(){
  18. $map = array('status' => 1);
  19. $data = \think\Db::name('Config')->where($map)->field('type,name,value')->select();
  20. $config = array();
  21. if($data){
  22. foreach ($data as $value) {
  23. $config[$value['name']] = self::parse($value['type'], $value['value']);
  24. }
  25. }
  26. return $config;
  27. }
  28. /**
  29. * 根据配置类型解析配置
  30. * @param integer $type 配置类型
  31. * @param string $value 配置值
  32. */
  33. private static function parse($type, $value){
  34. switch ($type) {
  35. case 3: //解析数组
  36. $array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
  37. if(strpos($value,':')){
  38. $value = array();
  39. foreach ($array as $val) {
  40. list($k, $v) = explode(':', $val);
  41. $value[$k] = $v;
  42. }
  43. }else{
  44. $value = $array;
  45. }
  46. break;
  47. }
  48. return $value;
  49. }
  50. }