mirror of
https://gitee.com/zoujingli/ThinkAdmin.git
synced 2025-04-06 03:58:04 +08:00
124 lines
4.5 KiB
PHP
124 lines
4.5 KiB
PHP
<?php
|
||
|
||
// +----------------------------------------------------------------------
|
||
// | ThinkAdmin
|
||
// +----------------------------------------------------------------------
|
||
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
|
||
// +----------------------------------------------------------------------
|
||
// | 官方网站: http://demo.thinkadmin.top
|
||
// +----------------------------------------------------------------------
|
||
// | 开源协议 ( https://mit-license.org )
|
||
// +----------------------------------------------------------------------
|
||
// | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
|
||
// | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
|
||
// +----------------------------------------------------------------------
|
||
|
||
namespace think\admin\service;
|
||
|
||
use think\admin\Service;
|
||
use think\db\Query;
|
||
|
||
/**
|
||
* 系统参数管理服务
|
||
* Class SystemService
|
||
* @package think\admin\service
|
||
*/
|
||
class SystemService extends Service
|
||
{
|
||
|
||
/**
|
||
* 配置数据缓存
|
||
* @var array
|
||
*/
|
||
protected $data = [];
|
||
|
||
/**
|
||
* 设置配置数据
|
||
* @param string $name 配置名称
|
||
* @param string $value 配置内容
|
||
* @return SystemService
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function set($name, $value = '')
|
||
{
|
||
list($type, $field) = $this->parse($name);
|
||
if (is_array($value)) {
|
||
foreach ($value as $k => $v) $this->set("{$field}.{$k}", $v);
|
||
} else {
|
||
$this->data = [];
|
||
$data = ['name' => $field, 'value' => $value, 'type' => $type];
|
||
$this->save('SystemConfig', $data, 'name', ['type' => $type]);
|
||
}
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* 读取配置数据
|
||
* @param string $name
|
||
* @return array|mixed|string
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function get($name)
|
||
{
|
||
list($type, $field, $outer) = $this->parse($name);
|
||
if (empty($this->data)) foreach ($this->app->db->name('SystemConfig')->select() as $vo) {
|
||
$this->data[$vo['type']][$vo['name']] = $vo['value'];
|
||
}
|
||
if (empty($name)) {
|
||
return empty($this->data[$type]) ? [] : ($outer === 'raw' ? $this->data[$type] : array_map(function ($value) {
|
||
return htmlspecialchars($value);
|
||
}, $this->data[$type]));
|
||
} else {
|
||
if (isset($this->data[$type]) && isset($this->data[$type][$field])) {
|
||
return $outer === 'raw' ? $this->data[$type][$field] : htmlspecialchars($this->data[$type][$field]);
|
||
} else return '';
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 数据增量保存
|
||
* @param Query|string $dbQuery 数据查询对象
|
||
* @param array $data 需要保存或更新的数据
|
||
* @param string $key 条件主键限制
|
||
* @param array $where 其它的where条件
|
||
* @return bool|int|mixed|string
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function save($dbQuery, $data, $key = 'id', $where = [])
|
||
{
|
||
$db = is_string($dbQuery) ? $this->app->db->name($dbQuery) : $dbQuery;
|
||
list($table, $value) = [$db->getTable(), isset($data[$key]) ? $data[$key] : null];
|
||
$map = isset($where[$key]) ? [] : (is_string($value) ? [[$key, 'in', explode(',', $value)]] : [$key => $value]);
|
||
if (is_array($info = $this->app->db->table($table)->master()->where($where)->where($map)->find()) && !empty($info)) {
|
||
if ($this->app->db->table($table)->strict(false)->where($where)->where($map)->update($data) !== false) {
|
||
return isset($info[$key]) ? $info[$key] : true;
|
||
} else {
|
||
return false;
|
||
}
|
||
} else {
|
||
return $this->app->db->table($table)->strict(false)->insertGetId($data);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 解析缓存名称
|
||
* @param string $rule 配置名称
|
||
* @param string $type 配置类型
|
||
* @return array
|
||
*/
|
||
private function parse($rule, $type = 'base')
|
||
{
|
||
if (stripos($rule, '.') !== false) {
|
||
list($type, $rule) = explode('.', $rule);
|
||
}
|
||
list($field, $outer) = explode('|', "{$rule}|");
|
||
return [$type, $field, strtolower($outer)];
|
||
}
|
||
|
||
} |