feat: 初始化雄戈商城服务端代码与Webhook自动部署配置
这个提交包含在:
@@ -0,0 +1,176 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro_aliyun_express\library;
|
||||
|
||||
use app\admin\model\AuthRule;
|
||||
use think\addons\Service;
|
||||
use think\Cache;
|
||||
use think\Db;
|
||||
|
||||
class Installer
|
||||
{
|
||||
public static function run($output = null)
|
||||
{
|
||||
$addonDir = ADDON_PATH . 'shopro_aliyun_express' . DS;
|
||||
$name = 'shopro_aliyun_express';
|
||||
|
||||
self::log($output, '复制后台文件...');
|
||||
foreach (['application', 'public'] as $dir) {
|
||||
$source = $addonDir . $dir;
|
||||
if (is_dir($source)) {
|
||||
copydirs($source, ROOT_PATH . $dir);
|
||||
}
|
||||
}
|
||||
|
||||
self::log($output, '执行 install.sql...');
|
||||
self::importSqlFile($addonDir . 'install.sql', $output);
|
||||
|
||||
self::log($output, '注册 CLI 命令...');
|
||||
self::registerCliCommand($output);
|
||||
|
||||
self::log($output, '注册菜单...');
|
||||
self::installMenu($output);
|
||||
|
||||
\app\common\library\Menu::enable('shopro_aliyun_express/config');
|
||||
\app\common\library\Menu::enable('shopro_aliyun_express/query');
|
||||
self::grantRulesToShoproGroups($output);
|
||||
self::clearMenuCache();
|
||||
self::setAddonState($name, 1, $output);
|
||||
|
||||
Service::refresh();
|
||||
self::log($output, '安装完成: /admin/shopro_aliyun_express/config');
|
||||
}
|
||||
|
||||
public static function setAddonState($name, $state, $output = null)
|
||||
{
|
||||
$file = ADDON_PATH . $name . DS . 'info.ini';
|
||||
if (!is_file($file)) {
|
||||
return false;
|
||||
}
|
||||
$content = file_get_contents($file);
|
||||
$state = intval($state);
|
||||
if (preg_match('/^state\s*=\s*\d+/m', $content)) {
|
||||
$content = preg_replace('/^state\s*=\s*\d+/m', 'state = ' . $state, $content);
|
||||
} else {
|
||||
$content = rtrim($content) . "\nstate = " . $state . "\n";
|
||||
}
|
||||
file_put_contents($file, $content, LOCK_EX);
|
||||
\think\Config::set($name, null, 'addoninfo');
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function installMenu($output = null)
|
||||
{
|
||||
$menuFile = ADDON_PATH . 'shopro_aliyun_express' . DS . 'config' . DS . 'menu.php';
|
||||
if (!is_file($menuFile)) {
|
||||
return false;
|
||||
}
|
||||
$menu = include $menuFile;
|
||||
if (!$menu) {
|
||||
return false;
|
||||
}
|
||||
$parent = self::resolveMenuParent();
|
||||
self::recreateMenu($menu, $parent, 'shopro_aliyun_express/config', ['shopro_aliyun_express']);
|
||||
\app\common\library\Menu::refresh('shopro_aliyun_express', $menu);
|
||||
return $parent;
|
||||
}
|
||||
|
||||
protected static function resolveMenuParent()
|
||||
{
|
||||
foreach (['shopro/config', 'shopro'] as $name) {
|
||||
$rule = AuthRule::getByName($name);
|
||||
if ($rule && $rule['status'] === 'normal') {
|
||||
return $name;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected static function recreateMenu(array $menu, $parent, $rootName, array $legacyNames = [])
|
||||
{
|
||||
foreach (array_merge($legacyNames, [$rootName]) as $name) {
|
||||
\app\common\library\Menu::delete($name);
|
||||
}
|
||||
\app\common\library\Menu::create($menu, $parent);
|
||||
}
|
||||
|
||||
protected static function grantRulesToShoproGroups($output = null)
|
||||
{
|
||||
$ruleIds = array_merge(
|
||||
\app\common\library\Menu::getAuthRuleIdsByName('shopro_aliyun_express/config'),
|
||||
\app\common\library\Menu::getAuthRuleIdsByName('shopro_aliyun_express/query')
|
||||
);
|
||||
if (!$ruleIds) {
|
||||
return;
|
||||
}
|
||||
$groups = Db::name('auth_group')->where('name', 'like', '%shopro%')->select();
|
||||
foreach ($groups as $group) {
|
||||
$rules = array_filter(explode(',', $group['rules']));
|
||||
$merged = array_unique(array_merge($rules, $ruleIds));
|
||||
Db::name('auth_group')->where('id', $group['id'])->update(['rules' => implode(',', $merged)]);
|
||||
}
|
||||
}
|
||||
|
||||
protected static function importSqlFile($file, $output = null, $ignoreError = false)
|
||||
{
|
||||
if (!is_file($file)) {
|
||||
return;
|
||||
}
|
||||
$prefix = config('database.prefix');
|
||||
$sql = str_replace('__PREFIX__', $prefix, file_get_contents($file));
|
||||
$statements = array_filter(array_map('trim', explode(';', $sql)));
|
||||
foreach ($statements as $statement) {
|
||||
if ($statement === '') {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
Db::execute($statement);
|
||||
} catch (\Exception $e) {
|
||||
if (!$ignoreError) {
|
||||
self::log($output, ' [warn] SQL: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected static function clearMenuCache()
|
||||
{
|
||||
try {
|
||||
Cache::rm('__menu__');
|
||||
} catch (\Exception $e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
protected static function registerCliCommand($output = null)
|
||||
{
|
||||
$commandClass = 'addons\\shopro_aliyun_express\\command\\InstallFull';
|
||||
$commandFile = ROOT_PATH . 'application' . DS . 'command.php';
|
||||
if (!is_file($commandFile)) {
|
||||
self::log($output, ' [warn] application/command.php 不存在,跳过 CLI 注册');
|
||||
return;
|
||||
}
|
||||
|
||||
$content = file_get_contents($commandFile);
|
||||
if (strpos($content, $commandClass) !== false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$entry = " '" . $commandClass . "',\n";
|
||||
if (preg_match('/\];\s*$/', $content)) {
|
||||
$content = preg_replace('/\];\s*$/', $entry . "];\n", $content);
|
||||
} else {
|
||||
$content = rtrim($content) . "\n" . $entry;
|
||||
}
|
||||
|
||||
file_put_contents($commandFile, $content, LOCK_EX);
|
||||
self::log($output, '已注册 php think shopro_aliyun_express:install');
|
||||
}
|
||||
|
||||
protected static function log($output, $message)
|
||||
{
|
||||
if ($output) {
|
||||
$output->writeln($message);
|
||||
}
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户