初始化项目:添加后端代码、ThinkPHP框架、前端资源

This commit is contained in:
amb
2026-09-03 12:42:39 +08:00
commit 482bece22e
3726 changed files with 416708 additions and 0 deletions
@@ -0,0 +1,473 @@
<?php
namespace app\admin\controller\config;
use app\common\controller\Backend;
use think\Db;
use think\Config;
use think\Cache;
use app\common\model\config\System as ConfigModel;
/**
* 系统配置
*
* @icon fa fa-circle-o
*/
class System extends Backend
{
protected $model = null;
protected $noNeedRight = ['check', 'rulelist'];
public function _initialize()
{
parent::_initialize();
//填充默认配置
\app\common\model\fill\Handle::check('config_system');
$this->model = new \app\admin\model\config\System();
}
/**
* 查看
*/
public function index()
{
$siteList = [];
$group = input('group','basic');
$name = input('name');
$where = [
'group'=>$group,
'uniacid'=>UNIACID,
'status'=>1
];
if($name){
$where['name'] = $name;
}
if($group == 'payment'){
$where['name'] = ['not in',['virtual_pay','douyinpay']];
}
// 检查并补齐分组内新增的配置项(兼容版本升级后新增配置组的场景)
\app\common\model\fill\Handle::checkByGroup($group);
$list = $this->model->where($where)->order('sort','asc')->select();
foreach ($list as $k => $v) {
$value = $v->toArray();
$value['title'] = __($value['title']);
$value['value'] = json_decode($value['value'],true);
$value['value'] = array_values(array_filter($value['value'], function($item){
return isset($item['name']) && isset($item['type']);
}));
//从默认配置里获取已有配置项中缺失的配置,并按默认定义顺序展示
$systemDefaultConfig = \app\common\model\fill\Handle::getSystemDefaultConfig($value['name']);
if($systemDefaultConfig){
$value['value'] = $this->syncConfigWithDefault($value['value'], $systemDefaultConfig);
}
if($value['name'] == 'aliyun_message'){
$value['value'] = $this->buildAliyunMessageRuntimeConfig($value['value']);
}
$siteList[] = $value;
}
$data = [
'siteList'=>$siteList,
'typeList'=>ConfigModel::getTypeList(),
'ruleList'=>ConfigModel::getRegexList(),
'groupList'=>ConfigModel::getGroupList(),
];
return $this->success("获取成功",$data);
}
/**
* 用默认配置补齐元信息和顺序,保留已保存的 value。
* @param array $currentConfig
* @param array $defaultConfig
* @return array
*/
protected function syncConfigWithDefault($currentConfig, $defaultConfig)
{
$currentMap = [];
foreach ($currentConfig as $item) {
if (isset($item['name'])) {
$currentMap[$item['name']] = $item;
}
}
$result = [];
foreach ($defaultConfig as $defaultItem) {
if (!isset($defaultItem['name'])) {
continue;
}
$item = isset($currentMap[$defaultItem['name']]) ? $currentMap[$defaultItem['name']] : $defaultItem;
foreach (['content', 'tip', 'title', 'type'] as $field) {
if (isset($defaultItem[$field])) {
$item[$field] = $defaultItem[$field];
}
}
if ($defaultItem['name'] == 'data_center' && (!isset($item['value']) || $item['value'] === '')) {
$item['value'] = 'cn-shanghai';
}
if (isset($defaultItem['type']) && $defaultItem['type'] == 'array' && isset($item['value'], $defaultItem['value'])) {
$item['value'] = $this->contrastConfigArrayValue($item['value'], $defaultItem['value']);
}
$result[] = $item;
}
return $result;
}
/**
* 判断数组类型的配置的成员差异
* @param $nowConfig
* @param $defaultConfig
* @return void
*/
public function contrastConfigArrayValue($nowConfig,$defaultConfig){
// 获取数组一中的所有 key
$array1Keys = array_column($nowConfig, 'key');
// 获取数组二中的所有 key
$array2Keys = array_column($defaultConfig, 'key');
// 计算数组一和数组二的差集,即数组一中没有但数组二中有的 key
$keysToAdd = array_diff($array2Keys, $array1Keys);
// 遍历差集,将缺失的 key 补全到数组一中
foreach ($keysToAdd as $keyToAdd) {
foreach ($defaultConfig as $item) {
if ($item['key'] === $keyToAdd) {
$nowConfig[] = $item;
break;
}
}
}
// 计算数组一和数组二的差集,即数组一中有但数组二中没有的 key
$keysToRemove = array_diff($array1Keys, $array2Keys);
// 遍历差集,从数组一中移除这些 key
foreach ($keysToRemove as $keyToRemove) {
foreach ($nowConfig as $key => $item) {
if ($item['key'] === $keyToRemove) {
unset($nowConfig[$key]);
break;
}
}
}
return array_values($nowConfig);
}
/**
* 构建阿里云互动消息运行时配置。
* @param array $config
* @return array
*/
protected function buildAliyunMessageRuntimeConfig($config)
{
return $config;
}
/**
* 持久化阿里云互动消息配置。
* @param array $config
* @return void
*/
protected function saveAliyunMessageConfig($config)
{
$this->model->where(['name' => 'aliyun_message', 'uniacid' => UNIACID])->update([
'value' => json_encode(array_values($config), JSON_UNESCAPED_UNICODE),
]);
Cache::rm(\app\common\constant\cache\Keys::SYSTEM_CONFIG('aliyun_message'));
}
/**
* 添加
*/
public function add()
{
if ($this->request->isPost()) {
$params = $this->request->post("row/a", [], 'trim');
if ($params) {
foreach ($params as $k => &$v) {
$v = is_array($v) ? implode(',', $v) : $v;
}
try {
if (in_array($params['type'], ['select', 'selects', 'checkbox', 'radio', 'array'])) {
$params['content'] = json_encode(ConfigModel::decode($params['content']), JSON_UNESCAPED_UNICODE);
} else {
$params['content'] = '';
}
$result = $this->model->create($params);
if ($result !== false) {
$this->success();
} else {
$this->error($this->model->getError());
}
} catch (\Exception $e) {
$this->error($e->getMessage());
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
return $this->view->fetch();
}
/**
* 编辑
* @param null $ids
*/
public function edit($ids = null)
{
if ($this->request->isPost()) {
$group = $this->request->post("group",'basic');
$row = $this->request->post("row/a", [], 'trim');
if ($row) {
$configList = [];
$list = $this->model->where([
'group'=>$group
])->select();
foreach ($list as $v) {
$v = $v->toArray();
if (isset($v['name']) && isset($row[$v['name']])) {
$value = $row[$v['name']];
if (is_array($value)) {
$value = $this->buildConfigValue($v['name'], $v['value'], $value);
} else {
$value = is_array($value) ? implode(',', $value) : $value;
}
$v['value'] = $value;
$configId = $v['id'];
unset($v['id']);
$this->model->where(['id'=>$configId])->update($v);
Cache::rm(\app\common\constant\cache\Keys::SYSTEM_CONFIG($v['name']));
// $configList[] = $v;
}
}
// dump($configList);
// die();
$this->model->allowField(true)->saveAll($configList);
if($group=='sms'){
// (new \app\admin\model\config\Sms())->setAddonStatus();
}
if($group=='oss'){
$config = \app\common\model\config\System::getConfig('alivod');
if($config && $config['status'] == 'open'){
\app\admin\controller\Addon::selfstate('alivod','enable');
}else{
// \app\admin\controller\Addon::selfstate('alivod','disable');
}
}
$this->success("操作成功");
}
$this->error(__('Parameter %s can not be empty', ''));
}
}
/**
* 保存配置时保留原配置项顺序,只更新各项 value。
* @param string $configName
* @param string $currentValue
* @param array $postValue
* @return string
*/
protected function buildConfigValue($configName, $currentValue, $postValue)
{
$currentConfig = json_decode($currentValue, true);
if (!is_array($currentConfig) || !$this->isConfigItemList($currentConfig)) {
return json_encode(ConfigModel::getArrayData($postValue), JSON_UNESCAPED_UNICODE);
}
$defaultConfig = \app\common\model\fill\Handle::getSystemDefaultConfig($configName);
if ($defaultConfig) {
$currentConfig = $this->syncConfigWithDefault($currentConfig, $defaultConfig);
}
$postMap = [];
if ($this->isConfigItemList($postValue)) {
foreach ($postValue as $item) {
if (isset($item['name'])) {
$postMap[$item['name']] = isset($item['value']) ? $item['value'] : '';
}
}
} else {
$postMap = $postValue;
}
foreach ($currentConfig as &$item) {
if (isset($item['name']) && array_key_exists($item['name'], $postMap)) {
$item['value'] = $postMap[$item['name']];
}
}
return json_encode(array_values($currentConfig), JSON_UNESCAPED_UNICODE);
}
/**
* 判断是否为系统配置项列表。
* @param array $config
* @return bool
*/
protected function isConfigItemList($config)
{
foreach ($config as $item) {
if (!is_array($item) || !isset($item['name'])) {
return false;
}
}
return !empty($config);
}
public function platform($type)
{
if ($this->request->isPost()) {
$this->token();
$row = $this->request->post("row/a", [], 'trim');
if ($row) {
try {
$config = $this->model->get(['name' => $type]);
$config->value = json_encode($row);
$config->save();
} catch (\Exception $e) {
$this->error($e->getMessage());
}
$this->success();
}
$this->error(__('Parameter %s can not be empty', ''));
}
$config = $this->model->where(['name' => $type])->value('value');
$config = json_decode($config, true);
if ($type === 'wxOfficialAccount') {
//动态解析微信公众号服务端Api Url地址域名
$config['url'] = request()->domain();
}
$this->assign('row', $config);
return $this->view->fetch();
}
/**
* 删除
* @param string $ids
*/
public function del($ids = "")
{
$name = $this->request->post('name');
$config = ConfigModel::getByName($name);
if ($name && $config) {
try {
$config->delete();
$this->refreshFile();
} catch (\Exception $e) {
$this->error($e->getMessage());
}
$this->success();
} else {
$this->error(__('Invalid parameters'));
}
}
/**
* 刷新配置文件
*/
protected function refreshFile()
{
$config = [];
foreach ($this->model->all() as $k => $v) {
$value = $v->toArray();
if (in_array($value['type'], ['selects', 'checkbox', 'images', 'files'])) {
$value['value'] = explode(',', $value['value']);
}
if ($value['type'] == 'array') {
$value['value'] = (array)json_decode($value['value'], true);
}
$config[$value['name']] = $value['value'];
}
file_put_contents(
APP_PATH . 'extra' . DS . 'site.php',
'<?php' . "\n\nreturn " . var_export($config, true) . ";"
);
}
/**
* 检测配置项是否存在
* @internal
*/
public function check()
{
$params = $this->request->post("row/a");
if ($params) {
$config = $this->model->get($params);
if (!$config) {
return $this->success();
} else {
return $this->error(__('Name already exist'));
}
} else {
return $this->error(__('Invalid parameters'));
}
}
/**
* 规则列表
* @internal
*/
public function rulelist()
{
//主键
$primarykey = $this->request->request("keyField");
//主键值
$keyValue = $this->request->request("keyValue", "");
$keyValueArr = array_filter(explode(',', $keyValue));
$regexList = \app\common\model\Config::getRegexList();
$list = [];
foreach ($regexList as $k => $v) {
if ($keyValueArr) {
if (in_array($k, $keyValueArr)) {
$list[] = ['id' => $k, 'name' => $v];
}
} else {
$list[] = ['id' => $k, 'name' => $v];
}
}
return json(['list' => $list]);
}
}