主题
09 开通模块
作用
商品付款后自动(或人工)创建服务实例(host),并执行 suspend/unsuspend/terminate 等管理动作。
调度中心:ModuleService
内置模块(5 个)
| name | 说明 |
|---|---|
manual | 人工开通,不调用外部 API |
demo | 模拟生成账号密码 IP |
upstream | 上游代理(可得/魔方/WHMCS) |
mf_cloud | 魔方云对接 |
qzsystem | 轻舟系统对接 |
代码:app/common/module/builtin/
extend 模块(3 个)
| name | 路径 | 说明 |
|---|---|---|
cpanel | extend/modules/Cpanel/ | WHM API |
directadmin | extend/modules/Directadmin/ | DirectAdmin API |
plesk | extend/modules/Plesk/ | Plesk XML API |
商品编辑时 module 字段选对应 name。
ModuleInterface 动作
| 方法 | 触发时机 |
|---|---|
create | 账单支付后自动开通 |
suspend / unsuspend | 后台暂停/恢复 |
terminate | 终止服务 |
changePackage | 变更套餐 |
renew | 续费 |
testConnection | 后台测试连接 |
返回:ModuleResult::ok($msg, $data) 或 ModuleResult::fail($msg)
开发 extend 模块
文件 extend/modules/MyPanel/MyPanelModule.php
php
<?php
namespace modules\MyPanel;
use app\common\module\ModuleContext;
use app\common\module\ModuleResult;
use app\common\module\ServerModuleBase;
class MyPanelModule extends ServerModuleBase
{
public function name(): string { return 'mypanel'; }
public function metadata(): array
{
return [
'title' => 'MyPanel',
'description' => '...',
'author' => 'You',
'version' => '1.0.0',
'type' => 'server',
];
}
public function serverConfigFields(): array
{
// 服务器/上游配置表单(testConnection 传入)
return [
['name' => 'api_url', 'label' => 'API', 'type' => 'text', 'required' => true],
];
}
public function productConfigFields(): array
{
// 商品 module_config 字段
return [];
}
public function create(ModuleContext $ctx): ModuleResult
{
$apiUrl = $ctx->serverVal('api_url'); // 或 $ctx->param('api_url')
// ...
return ModuleResult::ok('开通成功', [
'status' => 'active',
'username' => 'u001',
'password' => '***',
'ip' => '1.2.3.4',
]);
}
}ModuleContext 读参优先级
service(host 记录)> options > product.module_config > server辅助方法:serverVal()、serviceVal()、param()
后台 API
GET /admin/module/index— 模块列表POST /admin/module/test— 测试连接(传 name + server 配置)
下一章:10-主题与 SSR