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('小程序名称不能为空'); } $logo = isset($params['logo']) ? trim((string)$params['logo']) : ''; $values = [ 'base.appName' => ['value' => $appName, 'group_key' => 'base', 'remark' => '小程序名称'], 'base.logo' => ['value' => $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' => '分享标题'], // 兼容旧 ui.* 键(用户端已优先读 base.*) 'ui.siteName' => ['value' => $appName, 'group_key' => 'ui', 'remark' => '站点名称'], 'ui.logo' => ['value' => $logo, 'group_key' => 'ui', 'remark' => '自定义LOGO'], // 提审合规模式开关(1=开启防审, 0=正式上线模式) 'pay.virtualPayAudit' => ['value' => isset($params['virtualPayAudit']) ? (int)$params['virtualPayAudit'] : 1, 'group_key' => 'pay', 'remark' => '虚拟支付提审防审模式'], ]; $this->setConfig($values); $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', ''), 'virtualPayAudit' => (int)$pick('pay.virtualPayAudit', 1), ]; } }