116 行
4.0 KiB
PHP
116 行
4.0 KiB
PHP
<?php
|
||
|
||
namespace app\admin\controller\fc;
|
||
|
||
use app\common\controller\Backend;
|
||
use think\Db;
|
||
|
||
/**
|
||
* 同城-小程序设置
|
||
*
|
||
* 页面职责(路由 fc/setting/index):
|
||
* 维护小程序端基础信息配置:小程序名称、LOGO、客服电话、关于我们、分享标题。
|
||
* 保存后写入 fa_fc_config 表的 base.* 键,供小程序端接口统一读取。
|
||
*
|
||
* @icon fa fa-cogs
|
||
*/
|
||
class Setting extends Backend
|
||
{
|
||
/**
|
||
* 小程序基础设置首页(GET 显示表单 / POST 保存基础配置)
|
||
*
|
||
* @return string|null 渲染页面或响应重定向
|
||
*/
|
||
public function index()
|
||
{
|
||
if ($this->request->isPost()) {
|
||
return $this->save();
|
||
}
|
||
|
||
$this->view->assign('row', $this->current());
|
||
return $this->view->fetch();
|
||
}
|
||
|
||
/**
|
||
* 保存小程序基础配置
|
||
*
|
||
* @return void
|
||
*/
|
||
protected function save()
|
||
{
|
||
$params = $this->request->post('row/a');
|
||
if (!is_array($params)) {
|
||
$params = $this->request->post();
|
||
}
|
||
|
||
$appName = isset($params['appName']) ? trim((string)$params['appName']) : '';
|
||
if ($appName === '') {
|
||
$this->error('小程序名称不能为空');
|
||
}
|
||
|
||
$this->setConfig([
|
||
'base.appName' => ['value' => $appName, 'group_key' => 'base', 'remark' => '小程序名称'],
|
||
'base.logo' => ['value' => isset($params['logo']) ? trim((string)$params['logo']) : '', 'group_key' => 'base', 'remark' => 'LOGO地址'],
|
||
'base.servicePhone' => ['value' => isset($params['servicePhone']) ? trim((string)$params['servicePhone']) : '', 'group_key' => 'base', 'remark' => '客服电话'],
|
||
'base.aboutUs' => ['value' => isset($params['aboutUs']) ? trim((string)$params['aboutUs']) : '', 'group_key' => 'base', 'remark' => '关于我们'],
|
||
'base.shareTitle' => ['value' => isset($params['shareTitle']) ? trim((string)$params['shareTitle']) : '', 'group_key' => 'base', 'remark' => '分享标题'],
|
||
]);
|
||
|
||
$this->success('小程序设置已保存');
|
||
}
|
||
|
||
/**
|
||
* 批量写入 fc_config(存在则更新,不存在则新增)
|
||
*
|
||
* @param array $values 结构:键名 => ['value' => 值, 'group_key' => 分组, 'remark' => 备注]
|
||
* @return void
|
||
*/
|
||
protected function setConfig(array $values)
|
||
{
|
||
$now = time();
|
||
foreach ($values as $key => $item) {
|
||
$exist = Db::name('fc_config')->where('key', $key)->find();
|
||
if ($exist) {
|
||
Db::name('fc_config')->where('key', $key)->update([
|
||
'value' => (string)$item['value'],
|
||
'group_key' => $item['group_key'],
|
||
'updatetime' => $now,
|
||
]);
|
||
} else {
|
||
Db::name('fc_config')->insert([
|
||
'key' => $key,
|
||
'value' => (string)$item['value'],
|
||
'group_key' => $item['group_key'],
|
||
'remark' => $item['remark'],
|
||
'createtime' => $now,
|
||
'updatetime' => $now,
|
||
]);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取当前小程序基础配置(若数据库未设置则回退内置默认值)
|
||
*
|
||
* @return array 包含 appName, logo, servicePhone, aboutUs, shareTitle 的配置数组
|
||
*/
|
||
protected function current()
|
||
{
|
||
$cfg = [];
|
||
foreach (Db::name('fc_config')->select() as $row) {
|
||
$cfg[$row['key']] = $row['value'];
|
||
}
|
||
$pick = function ($key, $default) use ($cfg) {
|
||
return (isset($cfg[$key]) && $cfg[$key] !== '') ? $cfg[$key] : $default;
|
||
};
|
||
|
||
return [
|
||
'appName' => $pick('base.appName', '同城分类信息'),
|
||
'logo' => $pick('base.logo', ''),
|
||
'servicePhone' => $pick('base.servicePhone', '400-000-0000'),
|
||
'aboutUs' => $pick('base.aboutUs', ''),
|
||
'shareTitle' => $pick('base.shareTitle', ''),
|
||
];
|
||
}
|
||
}
|