// +---------------------------------------------------------------------- namespace app\model; use think\facade\Cache; use think\Model; /** * 设置模型 */ class Config extends Model { protected $type = array( 'id' => 'integer', ); protected $auto = array('name', 'update_time', 'status' => 1); protected $insert = array('create_time'); protected function setNameAttr($value) { return strtolower($value); } protected function getTypeTextAttr($value, $data) { $config = Cache::get('system_config'); $type = $config['config_type_list']; $type_text = explode(',', $type[$data['type']]); return $type_text[0]; } public function lists() { $map = array('status' => 1); $res = $this->where($map)->field('type,name,value')->select(); $config = array(); foreach ($res->toArray() as $value) { $config[$value['name']] = $this->parse($value['type'], $value['value']); } return $config; } public function updateConfig($data) { if (empty($data['config'])) { $this->error = "无更新"; return false; } foreach ($data['config'] as $key => $value) { self::update(['value' => $value], ['name' => $key], ['value']); } Cache::set('system_config', null); return true; } /** * 根据配置类型解析配置 * @param integer $type 配置类型 * @param string $value 配置值 * @author 麦当苗儿 */ private function parse($type, $value) { switch ($type) { case 'textarea': //解析数组 $array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n")); if (strpos($value, ':')) { $value = array(); foreach ($array as $val) { $list = explode(':', $val); if (isset($list[2])) { $value[$list[0]] = $list[1] . ',' . $list[2]; } else { $value[$list[0]] = $list[1]; } } } else { $value = $array; } break; } return $value; } public function getThemesList() { $files = array(); $files['pc'] = $this->getList('pc'); $files['mobile'] = $this->getList('mobile'); return $files; } protected function getList($type) { $path = './template/'; $file = opendir($path); while (false !== ($filename = readdir($file))) { if (!in_array($filename, array('.', '..'))) { $files = $path . $filename . '/info.php'; if (is_file($files)) { $info = include $files; if (isset($info['type']) && $info['type'] == $type) { $info['id'] = $filename; $info['img'] = '/template/' . $filename . '/' . $info['img']; $list[] = $info; } else { continue; } } } } return isset($list) ? $list : array(); } }