123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- namespace app\common\model;
- use think\Db;
- class Config extends Base
- {
- protected $createTime = 'add_time';
- protected $updateTime = 'update_time';
- //属性修改器
- protected function setNameAttr($value){
- return strtolower($value);
- }
- public function updates(){
- $data = request()->post();
- $data['status'] = 1;
- return $this->updateInfo($data,'Config','');
- }
- /**
- * 保存配置
- *
- * @author wst
- * @date 2021/9/8 16:13
- * @return boolean
- */
- public function saveData($data=[]){
- $msg = '';
- foreach ($data as $k => $v) {
- $res = db('config')
- ->where('name', $k)
- ->update(['value' => $v, 'update_time' => time()]);
- if (!$res) {
- $msg.=$k . '保存失败';
- continue;
- }
- }
- if($msg){
- $this->error=$msg;
- return false;
- }
- return true;
- }
- // 获取数据库配置
- public function getConfig($name,$orgId=0){
- $info = \think\Db::name('config')->where('name',$name)->find();
- if($info){
- if($info['cate'] == 2){ // 项目配置
- $oinfo = \think\Db::name('config_org')->where('config_id',$info['id'])->where('org_id',$orgId)->find();
- $info['value'] = $oinfo?$oinfo['value']:'';
- }
- return $this->configParse($info['type'], $info['value']);
- }
- return false;
- }
- /**
- * 获取配置的类型
- * @param int $type 配置类型
- * @return string
- */
- function getConfigType($type=0){
- $list = $this->getConfig('config_type_list');
- return $list[$type];
- }
- /**
- * 获取配置的分组
- * @param int $group 配置分组
- * @return string
- */
- function getConfigGroup($group=0){
- $list = $this->getConfig('config_group_list');
- return $group&&isset($list[$group])?$list[$group]:'';
- }
- /**
- * 获取配置的分组
- * @param int $group 配置分组
- * @return string
- */
- function getConfigOrgGroup($group=0){
- $list = $this->getConfig('config_org_group_list');
- return $group&&isset($list[$group])?$list[$group]:'';
- }
- /**
- * 根据配置类型解析配置
- * @param $type
- * @param $value
- * @return array|false|string[]
- */
- public function configParse($type, $value){
- switch ($type) {
- case 3: //解析数组
- $array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
- if(strpos($value,':')){
- $value = array();
- foreach ($array as $val) {
- list($k, $v) = explode(':', $val);
- $value[$k] = $v;
- }
- }else{
- $value = $array;
- }
- break;
- }
- return $value;
- }
- // 分析枚举类型配置值 格式 a:名称1,b:名称2
- function parseConfigAttr($string) {
- $array = preg_split('/[,;\r\n]+/', trim($string, ",;\r\n"));
- if(strpos($string,':')){
- $value = array();
- foreach ($array as $val) {
- list($k, $v) = explode(':', $val);
- $value[$k] = $v;
- }
- }else{
- $value = $array;
- }
- return $value;
- }
- }
|