chore(event): 更新event 默认的初始化路由等操作

This commit is contained in:
2025-12-03 15:33:33 +08:00
parent d374034694
commit ae5f56c16f
3 changed files with 189 additions and 151 deletions

View File

@@ -3,6 +3,7 @@
namespace app\event\init;
use app\model\system\Addon;
use Exception;
use think\facade\Event;
use think\facade\Cache;
@@ -14,6 +15,7 @@ class InitAddon
// 行为扩展的执行入口必须是run
public function handle()
{
log_write('InitAddon初始化插件', 'info');
if (defined('BIND_MODULE') && BIND_MODULE === 'install') return;
$this->InitEvent();
}
@@ -23,6 +25,7 @@ class InitAddon
*/
private function InitEvent()
{
try {
$cache = Cache::get("addon_event_list");
if (!defined("initroute_tag")) exit();
@@ -139,6 +142,10 @@ class InitAddon
'addon_events' => $new_addon_events,
'merged_listeners' => $new_merged_listeners
]);
} catch (\Exception $e) {
// 计划任务异常,直接返回
log_write('InitAddon初始化插件异常: ' . $e->getMessage(), 'error');
//formatThrowForDebugWithExit($th);
}
}
}

View File

@@ -13,6 +13,7 @@ class InitConfig
{
public function handle()
{
log_write('InitConfig 初始化配置信息', 'info');
// 初始化常量
$this->initConst();
//初始化配置信息

View File

@@ -1,45 +1,75 @@
<?php
namespace app\event\init;
use app\model\system\Addon;
use think\facade\Lang;
use think\facade\Event;
use think\facade\Cache;
require_once('extend/cert/pen.php');
class InitAddon {
public function handle() {
if (defined('BIND_MODULE') && BIND_MODULE === 'install') return;
$this->initEvent();
}
private function InitEvent() {
try {
$cache = Cache::get("addon_event_list");
if(empty($cache)) {
$addon_model = new Addon();
$addon_data = $addon_model->getAddonList([], 'name');
$listen_array = [];
foreach ($addon_data['data'] as $k => $v) {
if(file_exists('addon/'.$v['name'].'/config/event.php')) {
$addon_event = require_once 'addon/'.$v['name'].'/config/event.php';
$listen = isset($addon_event['listen']) ? $addon_event['listen'] : [];
if(!empty($listen)) {
$listen_array[] = $listen;
}
}
}
Cache::tag("addon")->set("addon_event_list", $listen_array);
} else {
$listen_array = $cache;
}
if(!empty($listen_array)) {
foreach ($listen_array as $k => $listen) {
if(!empty($listen)) {
Event::listenEvents($listen);
}
}
}
} catch (\Throwable $th) {
formatThrowForDebugWithExit($th);
}
namespace app\event\init;
use think\app\Service;
use think\facade\Route;
use app\model\system\Addon;
use think\facade\Cache;
use app\model\web\WebSite;
define("initroute_tag", 1);
/**
* 初始化路由规则
* @author Administrator
*
*/
class InitRoute extends Service
{
public function handle()
{
if (defined('BIND_MODULE') && BIND_MODULE === 'install')
return;
//检测当前pathinfo
$pathinfo = request()->pathinfo();
$pathinfo_array = explode('/', $pathinfo);
$url = request()->domain();
$check_model = $pathinfo_array[0];
//检测当前插件情况
$addon = in_array($check_model, $GLOBALS['system_array']) ? '' : $check_model;
if (!empty($addon)) {
$module = isset($pathinfo_array[1]) ? $pathinfo_array[1] : 'shop';
$controller = isset($pathinfo_array[2]) ? $pathinfo_array[2] : 'index';
$method = isset($pathinfo_array[3]) ? $pathinfo_array[3] : 'index';
if (SHOP_MODULE != 'shop') {
if ($module == 'shop') {
throw new \think\exception\HttpException(404, '请求异常');
} else {
if ($module == SHOP_MODULE) {
$module = 'shop';
}
$controller = str_replace(SHOP_MODULE, 'shop', $controller);
$method = str_replace(SHOP_MODULE, 'shop', $method);
}
}
request()->addon($addon);
$this->app->setNamespace("addon\\" . $addon . '\\' . $module);
$this->app->setAppPath($this->app->getRootPath() . 'addon' . DIRECTORY_SEPARATOR . $addon . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR);
} else {
$module = isset($pathinfo_array[0]) ? $pathinfo_array[0] : 'shop';
$controller = isset($pathinfo_array[1]) ? $pathinfo_array[1] : 'index';
$method = isset($pathinfo_array[2]) ? $pathinfo_array[2] : 'index';
if (SHOP_MODULE != 'shop') {
if ($module == 'shop') {
throw new \think\exception\HttpException(404, '请求异常');
} else {
if ($module == SHOP_MODULE) {
$module = 'shop';
}
$controller = str_replace(SHOP_MODULE, 'shop', $controller);
$method = str_replace(SHOP_MODULE, 'shop', $method);
}
}
$this->app->setNamespace("app\\" . $module);
$this->app->setAppPath($this->app->getRootPath() . 'app' . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR);
}
$addons = ['bundling', 'coupon', 'discount', 'freeshipping', 'manjian', 'membercancel', 'memberconsume', 'memberprice', 'membersignin', 'niusms', 'weapp', 'wechat', 'wechatpay', 'diy_default1', 'diy_default2'];
//解析路由
$pathinfo = str_replace(".html", '', $pathinfo);
$controller = str_replace(".html", '', $controller);
$method = str_replace(".html", '', $method);
request()->module($module);
Route::rule($pathinfo, $module . '/' . $controller . '/' . $method);
}
}