diff --git a/src/Api/ApiServiceProvider.php b/src/Api/ApiServiceProvider.php index 2e008c23d363864f9763d968683dc318d77ee9fb..45623f26742f2adebb930db28ebc5eda449f7e3c 100644 --- a/src/Api/ApiServiceProvider.php +++ b/src/Api/ApiServiceProvider.php @@ -18,6 +18,7 @@ namespace Discuz\Api; +use App\Common\Utils; use Discuz\Api\Controller\AbstractSerializeController; use Discuz\Api\Events\ApiExceptionRegisterHandler; use Discuz\Api\Events\ConfigMiddleware; @@ -103,16 +104,43 @@ class ApiServiceProvider extends ServiceProvider protected function populateRoutes(RouteCollection $route) { + $reqUri = '';$uri=''; + if (isset($_SERVER['REQUEST_URI'])) { + $reqUri = $_SERVER['REQUEST_URI']; + $uri = str_replace(['/',' '], ['',''], $reqUri); + } $route->group('/api', function (RouteCollection $route) { require $this->app->basePath('routes/api.php'); }); + if (strpos($uri, 'apiv3') === 0) { + $route->group('/apiv3', function (RouteCollection $route) { + require $this->app->basePath('routes/apiv3.php'); + }); + } else if (strpos($uri, 'api') === 0) { + $route->group('/api/backAdmin', function (RouteCollection $route) { + require $this->app->basePath('routes/apiadmin.php'); + }); + } else if (strpos($uri, 'plugin') === 0 && strpos($uri, 'api')) { + $this->setPluginRoutes($route, $reqUri); + } + } - $route->group('/apiv3', function (RouteCollection $route) { - require $this->app->basePath('routes/apiv3.php'); - }); - - $route->group('/api/backAdmin', function (RouteCollection $route) { - require $this->app->basePath('routes/apiadmin.php'); - }); + private function setPluginRoutes($route, $reqUri) + { + $plugins = \Discuz\Common\Utils::getPluginList(); + preg_match("/(?<=plugin\/).*?(?=\/api)/", $reqUri, $m); + $pluginName = $m[0]; + foreach ($plugins as $plugin) { + if (strtolower($pluginName) == strtolower($plugin['name_en'])) { + $route->group('/plugin/' . $plugin['name_en'] . '/api/', function (RouteCollection $route) use ($plugin) { + $routes = $plugin['routes']; + foreach ($routes as $name => $info) { + $method = strtolower($info['method']); + $route->$method($name, $plugin['name_en'] . '.' . str_replace('/', '.', $name), $info['controller']); + } + }); + break; + } + } } } diff --git a/src/Base/DzqController.php b/src/Base/DzqController.php index 9126cfab4d5647f32fb87f62a68b6e3d55848b86..b26b110cc45659d70b68f9892e967abb5a32657f 100644 --- a/src/Base/DzqController.php +++ b/src/Base/DzqController.php @@ -47,8 +47,8 @@ abstract class DzqController implements RequestHandlerInterface protected $isDebug = true; - private $queryParams = []; - private $parseBody = []; + public $queryParams = []; + public $parseBody = []; public $providers = []; @@ -64,7 +64,6 @@ abstract class DzqController implements RequestHandlerInterface $this->user = $request->getAttribute('actor'); $this->c9IbQHXVFFWu($this->user);//添加辅助函数 $this->dzqLogInit(); - //临时处理管理端接口权限 $path = $this->request->getUri()->getPath(); if(stristr($path,'backAdmin') && !stristr($path,'backAdmin/login')){ @@ -133,8 +132,15 @@ abstract class DzqController implements RequestHandlerInterface /* * 接口入参 */ - public function inPut($name, $checkValid = true) + public function inPut($name='', $checkValid = true) { + if(empty($name)){ + if($this->parseBody instanceof \Illuminate\Support\Collection){ + return $this->parseBody->merge($this->queryParams)->all(); + }else{ + return $this->queryParams; + } + } $p = ''; if (isset($this->queryParams[$name])) { $p = $this->queryParams[$name]; @@ -145,22 +151,6 @@ abstract class DzqController implements RequestHandlerInterface return $p; } - private function specialParamsChars(&$params) - { - return true; - if (is_array($params)) { - foreach ($params as &$item) { - if (is_array($item)) { - $this->specialParamsChars($item); - } else if (is_string($item)) { - $item = htmlspecialchars($item); - } - } - } else if (is_string($params)) { - $params = htmlspecialchars($params); - } - return $params; - } /* * 接口出参 diff --git a/src/Base/DzqModel.php b/src/Base/DzqModel.php index a96f38ccbeb78227a30495401ec521e90c4fd9e9..d92c83606d2f81cb83dd5954a4afed8eda24b1e1 100644 --- a/src/Base/DzqModel.php +++ b/src/Base/DzqModel.php @@ -23,7 +23,11 @@ use Illuminate\Support\Facades\URL; abstract class DzqModel extends Model { - + public function __construct(array $attributes = []) + { + //检查数据表合法性 + parent::__construct($attributes); + } private static $instance; public static function instance() @@ -38,37 +42,18 @@ abstract class DzqModel extends Model public function save(array $options = []) { - $original = $this->original; - $attr = $this->attributes; - unset($original['updated_at']); - unset($attr['updated_at']); - if ($original != $attr) { - $this->clearCache(); - } return parent::save($options); // TODO: Change the autogenerated stub } public function delete() { - $this->clearCache(); return parent::delete(); // TODO: Change the autogenerated stub } public function update(array $attributes = [], array $options = []) { - $original = $this->original; - $attr = $this->attributes; - unset($original['updated_at']); - unset($attr['updated_at']); - if ($original != $attr) { - $this->clearCache(); - } return parent::update($attributes, $options); // TODO: Change the autogenerated stub } - protected function clearCache() - { - //todo 缓存池数据变更 - } } diff --git a/src/Base/DzqPluginMigration.php b/src/Base/DzqPluginMigration.php new file mode 100644 index 0000000000000000000000000000000000000000..40e0333122ef9c4d7de0a45df83910584b6e0ead --- /dev/null +++ b/src/Base/DzqPluginMigration.php @@ -0,0 +1,26 @@ +getHeaders(); $server = $request->getServerParams(); - if(!empty($headers['referer']) && stristr(json_encode($headers['referer']),'servicewechat.com')){ + if (!empty($headers['referer']) && stristr(json_encode($headers['referer']), 'servicewechat.com')) { return PubEnum::MinProgram; } // app('log')->info('get_request_from_for_test_' . json_encode(['headers' => $headers, 'server' => $server], 256)); @@ -141,10 +144,10 @@ class Utils } if ($code != 0) { - app('log')->info('result error:' . $code.' api:'.$request->getUri()->getPath().' msg:'.$msg); + app('log')->info('result error:' . $code . ' api:' . $request->getUri()->getPath() . ' msg:' . $msg); } - $data = [ + $ret = [ 'Code' => $code, 'Message' => $msg, 'Data' => $data, @@ -154,10 +157,10 @@ class Utils if (strpos($api, 'backAdmin') === 0) { DzqLog::inPut(DzqLog::LOG_ADMIN); - DzqLog::outPut($data, DzqLog::LOG_ADMIN); - } elseif (! empty($dzqLog['openApiLog'])) { + DzqLog::outPut($ret, DzqLog::LOG_ADMIN); + } elseif (!empty($dzqLog['openApiLog'])) { DzqLog::inPut(DzqLog::LOG_API); - DzqLog::outPut($data, DzqLog::LOG_API); + DzqLog::outPut($ret, DzqLog::LOG_API); } $crossHeaders = DiscuzResponseFactory::getCrossHeaders(); @@ -165,11 +168,63 @@ class Utils header($k . ':' . $v); } header('Content-Type:application/json; charset=utf-8', true, 200); - $t1 = DISCUZ_START; - $t2 = microtime(true); - header('Dzq-CostTime:'.(($t2 - $t1)*1000).'ms'); -// header('Dzq-DB-CostTime:'.$GLOBALS["mysql_time"].'ms'); + header('Dzq-CostTime:' . ((microtime(true) - DISCUZ_START) * 1000) . 'ms'); + exit(json_encode($ret, 256)); + } - exit(json_encode($data, 256)); + public static function getPluginList() + { + $cacheConfig = DzqCache::get(CacheKey::PLUGIN_LOCAL_CONFIG); + if ($cacheConfig) return $cacheConfig; + $pluginDir = base_path('plugin'); + $directories = scandir($pluginDir); + $plugins = []; + foreach ($directories as $dirName) { + if ($dirName == '.' || $dirName == '..') continue; + $subPlugins = scandir($pluginDir . '/' . $dirName); + $configName = ''; + $viewName = ''; + $databaseName = ''; + $consoleName = ''; + foreach ($subPlugins as $item) { + if ($dirName == '.' || $dirName == '..') continue; + switch (strtolower($item)) { + case 'config.php': + $configName = $item; + break; + case 'view': + $viewName = $item; + break; + case 'database': + $databaseName = $item; + break; + case 'console': + $consoleName = $item; + break; + } + } + if ($configName == '') { + continue; + } + $appBase = $pluginDir . '/' . $dirName . '/'; + $configPath = $appBase . $configName; + $viewPath = $viewName == '' ? null : $appBase . $viewName . '/'; + $databasePath = $databaseName == '' ? null : $appBase . $databaseName . '/'; + $consolePath = $consoleName == '' ? null : $appBase . $consoleName . '/'; + $config = require($configPath); + if ($config['status'] == DzqConst::BOOL_YES) { + $config['plugin_' . $config['app_id']] = [ + 'directory' => $appBase, + 'view' => $viewPath, + 'database' => $databasePath, + 'console' => $consolePath, + 'config' => $configPath + ]; + } + isset($config['app_id']) && $plugins[$config['app_id']] = $config; + } + DzqCache::set(CacheKey::PLUGIN_LOCAL_CONFIG, $plugins, 5 * 60); + return $plugins; } + } diff --git a/src/Database/MigrationServiceProvider.php b/src/Database/MigrationServiceProvider.php index 8d2c22647995259306fbde35692f29d168bb744b..3e421cb69c1cb0df3c4fbe630e71d0702e882855 100644 --- a/src/Database/MigrationServiceProvider.php +++ b/src/Database/MigrationServiceProvider.php @@ -44,7 +44,8 @@ class MigrationServiceProvider extends IlluminateMigrationServiceProvider RefreshCommand::class, ResetCommand::class, RollbackCommand::class, - StatusCommand::class + StatusCommand::class, + PluginCommand::class ]; public function register() @@ -60,7 +61,7 @@ class MigrationServiceProvider extends IlluminateMigrationServiceProvider /** * Register the given commands. * - * @param array $commands + * @param array $commands * @return void */ protected function registerCommands(array $commands) @@ -72,18 +73,6 @@ class MigrationServiceProvider extends IlluminateMigrationServiceProvider }); } - /** - * Register the command. - * - * @return void - */ - protected function registerMigrateCommand() - { - $this->app->singleton('command.migrate', function ($app) { - return new MigrateCommand($app['migrator']); - }); - } - protected function registerCreator() { $this->app->singleton('migration.creator', function ($app) { diff --git a/src/Database/PluginCommand.php b/src/Database/PluginCommand.php new file mode 100644 index 0000000000000000000000000000000000000000..3505be135615b1629a03b56f7ba33d03da21c137 --- /dev/null +++ b/src/Database/PluginCommand.php @@ -0,0 +1,58 @@ +input->getOption('name'); + if (empty($name)) throw new \Exception('expected one plugin name,used like [ php disco migrate:plugin --name=test ]'); + $pluginList = Utils::getPluginList(); + + $basePath = base_path().'/'; + foreach ($pluginList as $item) { + if (strtolower($item['name_en']) == strtolower($name)) { + $paths = 'plugin_' . $item['app_id']; + $databasePath = $item[$paths]['database'] . 'migrations'; + $databasePath = str_replace($basePath,'',$databasePath); + if (!file_exists($databasePath)) throw new \Exception($databasePath . ' directory in ' . $item['name_en'] . ' not exist.'); + $this->call('migrate', array_filter(['--path' => $databasePath])); + break; + } + } + } + + protected function getOptions() + { + return [ + ['name', null, InputOption::VALUE_OPTIONAL, 'The plugin app name to use'], + ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use'], + ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'], + ['path', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The path(s) to the migrations files to be executed'], + ['seeder', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder'], + ]; + } +} diff --git a/src/Http/Server.php b/src/Http/Server.php index 103ce98ce13c32165a0ff8be7fdbf5dcf444e6d7..593c5479945012a535d9332f0022ac682bd64108 100644 --- a/src/Http/Server.php +++ b/src/Http/Server.php @@ -47,6 +47,7 @@ class Server extends SiteApp $pipe = new MiddlewarePipe(); $pipe->pipe(new RequestHandler([ + '/plugin'=>'discuz.api.middleware', '/api' => 'discuz.api.middleware', '/' => 'discuz.web.middleware' ], $this->app));