239 lines
8.5 KiB
PHP
Executable File
239 lines
8.5 KiB
PHP
Executable File
<?php
|
||
|
||
namespace app\admin\controller;
|
||
|
||
use app\common\server\RedisHelper;
|
||
use \think\facade\Request;
|
||
use \think\facade\View;
|
||
use think\facade\Db;
|
||
|
||
|
||
class Config extends Base
|
||
{
|
||
public function base(Request $request)
|
||
{
|
||
$config = getConfig('base');
|
||
View::assign("key", "base");
|
||
View::assign("data", $config);
|
||
return View::fetch('Config/base');
|
||
}
|
||
|
||
//微信支付
|
||
public function weixinpay(Request $request)
|
||
{
|
||
$config = getConfig('weixinpay_setting');
|
||
if (empty($config)) {
|
||
// 兼容旧数据,如果新配置为空,则尝试获取旧配置并转换格式
|
||
$old_config = getConfig('weixinpay');
|
||
if (!empty($old_config)) {
|
||
$config = [
|
||
'merchants' => [
|
||
[
|
||
'name' => '默认商户',
|
||
'mch_id' => $old_config['mch_id'] ?? '',
|
||
'keys' => $old_config['keys'] ?? '',
|
||
'order_prefix' => $old_config['order_prefix'] ?? 'MYH',
|
||
'weight' => 1
|
||
]
|
||
]
|
||
];
|
||
}
|
||
}
|
||
|
||
View::assign("key", "weixinpay_setting");
|
||
View::assign("data", $config);
|
||
return View::fetch('Config/weixinpay');
|
||
}
|
||
|
||
//签到设置
|
||
public function sign(Request $request)
|
||
{
|
||
$config = getConfig('sign');
|
||
|
||
View::assign("key", "sign");
|
||
View::assign("data", $config);
|
||
return View::fetch('Config/sign');
|
||
}
|
||
|
||
//上传设置
|
||
public function uploads(Request $request)
|
||
{
|
||
$config = getConfig('uploads');
|
||
View::assign("key", "uploads");
|
||
View::assign("data", $config);
|
||
return View::fetch('Config/uploads');
|
||
}
|
||
|
||
//微信公众号
|
||
public function wechatofficialaccount(Request $request)
|
||
{
|
||
$config = getConfig('wechatofficialaccount');
|
||
View::assign("key", "wechatofficialaccount");
|
||
View::assign("data", $config);
|
||
return View::fetch('Config/wechatofficialaccount');
|
||
}
|
||
|
||
//微信小程序
|
||
public function miniprogram(Request $request)
|
||
{
|
||
// 获取小程序配置
|
||
$config = getConfig('miniprogram_setting');
|
||
|
||
// 获取旧的微信小程序配置,用于兼容
|
||
$wechat_setting = getConfig('wechat_setting');
|
||
|
||
// 获取所有微信支付商户信息
|
||
$merchants = [];
|
||
$weixinpay_setting = getConfig('weixinpay_setting');
|
||
if (!empty($weixinpay_setting) && !empty($weixinpay_setting['merchants'])) {
|
||
foreach ($weixinpay_setting['merchants'] as $index => $merchant) {
|
||
$merchants[$index] = [
|
||
'id' => $index,
|
||
'name' => $merchant['name'],
|
||
'mch_id' => $merchant['mch_id']
|
||
];
|
||
}
|
||
}
|
||
|
||
View::assign("data", $config);
|
||
View::assign("wechat_setting", $wechat_setting);
|
||
View::assign("merchants", $merchants);
|
||
return View::fetch('Config/miniprogram');
|
||
}
|
||
|
||
//系统设置
|
||
public function systemconfig(Request $request)
|
||
{
|
||
$config = getConfig('systemconfig');
|
||
$user_config = getConfig('user_config');
|
||
$wechat_setting = getConfig('wechat_setting');
|
||
$app_setting = getConfig('app_setting');
|
||
$infinite_multiple = getConfig('infinite_multiple');
|
||
$wechatofficialaccount_setting = getConfig('wechatofficialaccount_setting');
|
||
$rank_setting = getConfig('rank_setting');
|
||
$system_test = getConfig('system_test');
|
||
|
||
View::assign("key", "systemconfig");
|
||
View::assign("data", $config);
|
||
View::assign("user_config", $user_config);
|
||
View::assign("wechat_setting", $wechat_setting);
|
||
View::assign("app_setting", $app_setting);
|
||
View::assign("wechatofficialaccount_setting", $wechatofficialaccount_setting);
|
||
View::assign("infinite_multiple", $infinite_multiple);
|
||
View::assign("rank_setting", $rank_setting);
|
||
View::assign("system_test", $system_test);
|
||
return View::fetch('Config/systemconfig');
|
||
}
|
||
|
||
//修改
|
||
public function update()
|
||
{
|
||
$data = input("post.");
|
||
$data['update_time'] = time();
|
||
|
||
// 处理微信支付商户前缀验证
|
||
if ($data['key'] == 'weixinpay_setting' && isset($data['merchants']) && is_array($data['merchants'])) {
|
||
// 检查前缀的唯一性
|
||
$prefixes = [];
|
||
foreach ($data['merchants'] as $index => $merchant) {
|
||
if (empty($merchant['order_prefix']) || strlen($merchant['order_prefix']) !== 3) {
|
||
return $this->renderError('商户前缀必须是3位字符');
|
||
}
|
||
|
||
if (in_array($merchant['order_prefix'], $prefixes)) {
|
||
return $this->renderError('商户前缀"' . $merchant['order_prefix'] . '"重复,每个商户的前缀必须唯一');
|
||
}
|
||
|
||
$prefixes[] = $merchant['order_prefix'];
|
||
}
|
||
}
|
||
|
||
// 处理微信小程序配置
|
||
if ($data['key'] == 'miniprogram_setting' && isset($data['miniprograms']) && is_array($data['miniprograms'])) {
|
||
// 检查是否有默认小程序
|
||
$hasDefault = false;
|
||
$prefixes = [];
|
||
foreach ($data['miniprograms'] as $index => $miniprogram) {
|
||
if (isset($miniprogram['is_default']) && $miniprogram['is_default'] == 1) {
|
||
$hasDefault = true;
|
||
}
|
||
|
||
// 验证订单前缀
|
||
if (!empty($miniprogram['order_prefix'])) {
|
||
if (strlen($miniprogram['order_prefix']) != 2) {
|
||
return $this->renderError('小程序"' . $miniprogram['name'] . '"的订单前缀必须是2位字符');
|
||
}
|
||
|
||
if (in_array($miniprogram['order_prefix'], $prefixes)) {
|
||
return $this->renderError('订单前缀"' . $miniprogram['order_prefix'] . '"重复,每个小程序的前缀必须唯一');
|
||
}
|
||
|
||
$prefixes[] = $miniprogram['order_prefix'];
|
||
}
|
||
}
|
||
|
||
if (!$hasDefault) {
|
||
return $this->renderError('请至少设置一个默认小程序');
|
||
}
|
||
|
||
// 清除旧的微信小程序配置缓存
|
||
$redis = new RedisHelper();
|
||
($redis->getRedis())->del('config:miniprogram_setting');
|
||
}
|
||
|
||
// 处理同步地址数据格式
|
||
if ($data['key'] == 'systemconfig') {
|
||
$syncAddresses = [];
|
||
$addressNames = isset($data['sync_address_names']) ? $data['sync_address_names'] : [];
|
||
$addressUrls = isset($data['sync_address_urls']) ? $data['sync_address_urls'] : [];
|
||
|
||
// 移除原有数组
|
||
unset($data['sync_address_names']);
|
||
unset($data['sync_address_urls']);
|
||
|
||
// 构建新的数据格式
|
||
for ($i = 0; $i < count($addressUrls); $i++) {
|
||
if (!empty($addressUrls[$i])) {
|
||
$syncAddresses[] = [
|
||
'name' => isset($addressNames[$i]) ? $addressNames[$i] : '',
|
||
'sync_address' => $addressUrls[$i]
|
||
];
|
||
}
|
||
}
|
||
|
||
// 使用新的格式设置同步地址
|
||
$data['sync_address'] = $syncAddresses;
|
||
}
|
||
if ($data['key'] == 'infinite_multiple') {
|
||
//清除redis缓存:infinite_multiple
|
||
$redis = new RedisHelper();
|
||
($redis->getRedis())->del('config:infinite_multiple');
|
||
}
|
||
|
||
if ($data['key'] == 'rank_setting') {
|
||
//清除排行榜设置缓存
|
||
\app\common\helper\ConfigHelper::clearRankSettingsCache();
|
||
}
|
||
if ($data['key'] == 'app_setting') {
|
||
//清除redis缓存:app_setting
|
||
$redis = new RedisHelper();
|
||
($redis->getRedis())->del('config:app_setting');
|
||
}
|
||
|
||
if ($data['key'] == 'system_test') {
|
||
//清除redis缓存:system_test
|
||
\app\common\helper\ConfigHelper::clearSystemTestCache();
|
||
}
|
||
|
||
$result = setConfig($data['key'], $data);
|
||
if ($result) {
|
||
return $this->renderSuccess('修改成功');
|
||
} else {
|
||
return $this->renderError('修改失败');
|
||
}
|
||
}
|
||
|
||
|
||
}
|
||
|
||
?>
|