request->isPost()) { return $this->save(); } $this->view->assign('row', $this->current()); $this->view->assign('pay', $this->payStatus()); return $this->view->fetch(); } /** * 保存基础配置 * * 说明:支付配置已迁到插件页,本页不再接收 group=pay 的提交(如提交则给出明确指引)。 */ protected function save() { $params = $this->request->post('row/a'); if (!is_array($params)) { $params = $this->request->post(); } $group = isset($params['group']) ? trim((string)$params['group']) : 'base'; if ($group === 'pay') { $this->error('微信支付参数已改由插件维护,请到「插件管理 → 微信支付宝整合插件 → 配置」填写'); } $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('基础配置已保存'); return null; } /** * 批量写入 fc_config(存在则更新,不存在则新增) * * @param array $values 结构:键名 => ['value' => 值, 'group_key' => 分组, 'remark' => 备注] */ 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, ]); } } } /** * 当前基础配置(缺失时回落内置默认值) */ 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', ''), ]; } /** * 支付配置状态(只读,数据来自 epay 插件配置) * * @return array 插件是否安装 / 是否就绪 / 关键项是否填写 / 回调地址 / 配置页地址 / 配置文件名 */ protected function payStatus() { $info = (array)get_addon_info('epay'); $addonConfig = (array)get_addon_config('epay'); $wx = (isset($addonConfig['wechat']) && is_array($addonConfig['wechat'])) ? $addonConfig['wechat'] : []; $mode = isset($wx['mode']) ? (string)$wx['mode'] : 'normal'; // 与用户端 Pay::epayReady() 同一判定口径 if ($mode === 'service') { $ready = !empty($wx['sub_mch_id']) && !empty($wx['key']) && !empty($wx['sub_miniapp_id']); } else { $ready = !empty($wx['miniapp_id']) && !empty($wx['mch_id']) && !empty($wx['key']); } return [ 'installed' => !empty($info), 'ready' => $ready, 'version' => isset($addonConfig['version']) ? (string)$addonConfig['version'] : '', 'mode' => $mode === 'service' ? 'service' : 'normal', 'miniappId' => isset($wx['miniapp_id']) ? (string)$wx['miniapp_id'] : '', 'mchId' => isset($wx['mch_id']) ? (string)$wx['mch_id'] : '', 'hasKey' => !empty($wx['key']), 'notifyUrl' => $this->notifyUrl(), 'configUrl' => url('addon/config', ['name' => 'epay']), 'configFile' => 'addons/epay/config.php', ]; } /** * 微信支付回调地址(与服务端 Pay::notifyUrl() 同一算法:当前域名 + 固定路径) */ protected function notifyUrl() { $base = rtrim((string)$this->request->root(true), '/'); // 去掉入口文件(后台入口 s.php / 前台 index.php),展示与真实回调地址保持一致 $base = preg_replace('#/[a-zA-Z0-9_\-]+\.php$#i', '', $base); return ($base !== '' ? $base : rtrim((string)$this->request->domain(), '/')) . '/api/v1/pay/notify'; } }