插件管理优化

This commit is contained in:
技术老胡
2024-10-19 16:58:44 +08:00
parent 0473a992a8
commit 5728976e9f
10 changed files with 248 additions and 77 deletions

View File

@@ -116,20 +116,39 @@ class PayManageController extends BaseController
{
$platform = Platform::where('platform', $acc->getData('platform'))->find();
$user = User::where('pid', $acc->pid)->find();
$query = \unserialize($platform->query);
$data = [
'pid' => $user->pid,
'key' => $user->secret_key,
'aid' => $acc->id,
'platform' => $acc->getData('platform'),
'account' => $acc->account,
'password' => $acc->password,
'payclass' => $platform->class_name,
'query' => \var_export($query, \true)
];
$config = View::fetch('tpl/account_config', $data);
$name = "{$data['pid']}_{$data['aid']}";
$path = "../config/payconfig/{$name}.php";
$query = var_export(\unserialize($platform->query), \true);
$config = <<<EOF
<?php
// +----------------------------------------------------------------------
// | 支付监听配置,一个文件,一个账号
// +----------------------------------------------------------------------
return [
// 用户账号配置
'user' => [
'pid' => {$user->pid},
'key' => '$user->secret_key'
],
// 收款平台账号配置
'pay' => [
// 账号id
'aid' => $acc->id,
// 收款平台
'platform' => '{$acc->getData('platform')}',
// 插件类名
'payclass' => '{$platform->class_name}',
// 账号
'account' => '{$acc->account}',
// 密码
'password' => '{$acc->password}',
// 订单查询参数配置
'query' => {$query},
]
];
EOF;
$name = "{$user->pid}_{$acc->id}";
$path = config_path() . "/payconfig/{$name}.php";
\file_put_contents($path, $config);
}
}

View File

@@ -10,36 +10,103 @@ use think\facade\View;
class PluginController extends BaseController
{
// 插件列表
// 获取插件列表
public function getPluginList()
{
$query = $this->request->get();
$data = Platform::order('id', 'desc')->paginate(['list_rows' => $query['limit'], 'page' => $query['page']]);
if ($data) {
return json(['code' => 0, 'msg' => 'OK', 'count' => $data->total(), 'data' => $data->items()]);
$plugin_config = $this->getPluginConfig();
if ($plugin_config) {
return json(['code' => 0, 'msg' => 'OK', 'count' => \count($plugin_config), 'data' => $plugin_config]);
} else {
return json(['code' => 1, 'msg' => '无数据记录', 'count' => 0, 'data' => []]);
}
}
// 测试
public function test()
{
// $res = $this->addPlugin(['platform' => 'haopay', 'name' => '好支付', 'class_name' => 'Haopay', 'price' => 99, 'describe' => '好支付', 'website' => 'https://store.zhihuijingyingba.com/', 'state' => 1, 'query' => []]);
// $res = $this->delPlugin('haopay');
$res = $this->setPlugin('haopay', ['state' => 0]);
return $res;
}
// 添加插件
public function addPlugin(array $option = [])
{
$keys = ['platform', 'name', 'class_name', 'price', 'describe', 'website', 'state', 'query'];
$config = [];
foreach ($option as $key => $value) {
if (in_array($key, $keys)) {
$config[$key] = $value;
}
}
$plugin_config = $this->getPluginConfig();
$plugin_platform = $config['platform'] ?: '';
foreach ($plugin_config as $value) {
if ($plugin_platform == $value['platform']) {
return 1; //'插件已存在'
}
}
$plugin_config[] = $config;
$this->savePluginConfig($plugin_config, '支付插件列表');
return 0;
}
// 删除插件
public function delPlugin($plugin_name = '')
{
$plugin_config = $this->getPluginConfig();
$keys = [];
foreach ($plugin_config as $index => $value) {
if ($value['platform'] == $plugin_name) {
$keys[] = $index;
}
}
foreach ($keys as $index) {
unset($plugin_config[$index]);
}
$config = \array_values($plugin_config);
$this->savePluginConfig($config, '支付插件列表');
return 0;
}
// 修改插件
public function setPlugin($platform = '', $option = [])
{
$config = $this->getPluginConfig();
if (!$platform) {
return 1; //'请选择插件'
}
if (!$option) {
return 2; //请添加插件配置
}
foreach ($config as $index => $options) {
if ($options['platform'] == $platform) {
foreach ($options as $key => $value) {
if (\array_key_exists($key, $option)) {
$config[$index][$key] = $option[$key];
}
}
$this->savePluginConfig($config, '支付插件列表');
return 0;
}
}
}
// 插件启用
public function pluginEnable()
{
$info = $this->request->post();
$up_res = Platform::update($info);
$up_res = $this->setPlugin($info['platform'], ['state' => $info['state']]);
if ($up_res) {
return json(\backMsg(0, '成功'));
} else {
return json(\backMsg(1, '失败'));
} else {
return json(\backMsg(0, '成功'));
}
}
// 插件选项
public function pluginOption()
{
// 加载平台配置
$platform = \think\facade\Config::load("extendconfig/platform", 'extendconfig');
$config = $this->getPluginConfig();
$option = [];
foreach ($platform as $key => $value) {
$option[] = ['platform' => $key, 'name' => $value];
foreach ($config as $value) {
$option[] = ['platform' => $value['platform'], 'name' => $value['name']];
}
return json($option);
}
@@ -60,4 +127,27 @@ class PluginController extends BaseController
return \json(\backMsg(1, '创建成功'));
}
}
// 获取插件配置
private function getPluginConfig(): array
{
$payplugin_path = config_path() . '/extendconfig/payplugin.php';
if (!file_exists($payplugin_path)) {
return [];
}
// 加载插件配置
$payplugin_config = require_once $payplugin_path;
return $payplugin_config;
}
// 保存插件配置
private function savePluginConfig(array $config, string $note = '说明')
{
$payplugin_path = config_path() . '/extendconfig/payplugin.php';
$note_tpl = <<<EOF
// +----------------------------------------------------------------------
// | $note
// +----------------------------------------------------------------------
EOF;
$config_str = "<?php\n" . $note_tpl . "\n\nreturn " . var_export($config, true) . ";\n";
\file_put_contents($payplugin_path, $config_str);
}
}