From 754f840005c05160f18e6886f3f322b6b4d77e00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=A8=E6=A0=88=E5=B0=8F=E5=AD=A6=E7=94=9F?= <1518079521@qq.com> Date: Wed, 31 May 2023 11:20:35 +0800 Subject: [PATCH] 1 --- niucloud/config/cache.php | 8 +- niucloud/config/console.php | 5 +- niucloud/config/oauth.php | 26 ++++ niucloud/config/pay.php | 4 + niucloud/core/addon/Lang.php | 52 ------- niucloud/core/addon/Menu.php | 22 --- niucloud/core/addon/Route.php | 27 ---- niucloud/core/addon/UniappLink.php | 37 ----- niucloud/core/addon/UniappPages.php | 37 ----- niucloud/core/base/BaseJob.php | 38 ++--- .../BaseAddon.php => dict/BaseDict.php} | 33 +++-- .../AddonLoader.php => dict/DictLoader.php} | 14 +- niucloud/core/{addon => dict}/Event.php | 14 +- niucloud/core/dict/Lang.php | 56 ++++++++ .../core/dict/MemberAccountChangeType.php | 50 +++++++ niucloud/core/dict/Menu.php | 30 ++++ niucloud/core/{addon => dict}/Notice.php | 18 ++- niucloud/core/dict/Route.php | 35 +++++ niucloud/core/dict/Schedule.php | 47 ++++++ .../core/{addon => dict}/UniappComponent.php | 16 ++- niucloud/core/dict/UniappLink.php | 43 ++++++ niucloud/core/dict/UniappPages.php | 42 ++++++ niucloud/core/dict/UniappTemplate.php | 42 ++++++ niucloud/core/exception/ServerException.php | 17 +++ niucloud/core/job/Dispatch.php | 21 +-- niucloud/core/loader/Loader.php | 20 ++- niucloud/core/loader/Storage.php | 2 +- niucloud/core/oauth/BaseOauth.php | 50 +++++++ niucloud/core/oauth/OauthLoader.php | 37 +++++ niucloud/core/oauth/Weapp.php | 38 +++++ niucloud/core/oauth/Wechat.php | 44 ++++++ niucloud/core/pay/Alipay.php | 114 +++++++++++---- niucloud/core/pay/BasePay.php | 29 +++- niucloud/core/pay/Wechatpay.php | 92 +++++++++--- niucloud/core/sms/Aliyun.php | 26 ++-- niucloud/core/sms/BaseSms.php | 39 +++-- niucloud/core/sms/SmsLoader.php | 5 +- niucloud/core/sms/Tencent.php | 27 ++-- niucloud/core/template/BaseTemplate.php | 2 +- niucloud/core/template/Weapp.php | 11 +- niucloud/core/template/Wechat.php | 11 +- niucloud/core/util/ConfigUtil.php | 124 ---------------- niucloud/core/util/Queue.php | 135 ++++++++---------- niucloud/core/util/SqlUtil.php | 101 ------------- niucloud/core/util/Terminal.php | 88 +++++------- niucloud/core/util/TokenAuth.php | 4 +- 46 files changed, 1030 insertions(+), 703 deletions(-) create mode 100644 niucloud/config/oauth.php delete mode 100644 niucloud/core/addon/Lang.php delete mode 100644 niucloud/core/addon/Menu.php delete mode 100644 niucloud/core/addon/Route.php delete mode 100644 niucloud/core/addon/UniappLink.php delete mode 100644 niucloud/core/addon/UniappPages.php rename niucloud/core/{addon/BaseAddon.php => dict/BaseDict.php} (81%) rename niucloud/core/{addon/AddonLoader.php => dict/DictLoader.php} (76%) rename niucloud/core/{addon => dict}/Event.php (54%) create mode 100644 niucloud/core/dict/Lang.php create mode 100644 niucloud/core/dict/MemberAccountChangeType.php create mode 100644 niucloud/core/dict/Menu.php rename niucloud/core/{addon => dict}/Notice.php (55%) create mode 100644 niucloud/core/dict/Route.php create mode 100644 niucloud/core/dict/Schedule.php rename niucloud/core/{addon => dict}/UniappComponent.php (52%) create mode 100644 niucloud/core/dict/UniappLink.php create mode 100644 niucloud/core/dict/UniappPages.php create mode 100644 niucloud/core/dict/UniappTemplate.php create mode 100644 niucloud/core/exception/ServerException.php create mode 100644 niucloud/core/oauth/BaseOauth.php create mode 100644 niucloud/core/oauth/OauthLoader.php create mode 100644 niucloud/core/oauth/Weapp.php create mode 100644 niucloud/core/oauth/Wechat.php delete mode 100644 niucloud/core/util/ConfigUtil.php delete mode 100644 niucloud/core/util/SqlUtil.php diff --git a/niucloud/config/cache.php b/niucloud/config/cache.php index cba5e5325..34f9e1aa8 100644 --- a/niucloud/config/cache.php +++ b/niucloud/config/cache.php @@ -16,11 +16,11 @@ return [ // 缓存保存目录 'path' => app()->getRuntimePath() . 'cache' . DIRECTORY_SEPARATOR, // 缓存前缀 - 'prefix' => 'CACHE', + 'prefix' => '', // 缓存有效期 0表示永久缓存 'expire' => 0, // 缓存标签前缀 - 'tag_prefix' => 'CACHE:', + 'tag_prefix' => 'tag:', // 序列化机制 例如 ['serialize', 'unserialize'] 'serialize' => [], ], @@ -38,9 +38,9 @@ return [ // 缓存有效期 0表示永久缓存 'expire' => 0 , // 缓存前缀 - 'prefix' => 'CACHE', + 'prefix' => '', // 缓存标签前缀 - 'tag_prefix' => 'CACHE:', + 'tag_prefix' => 'tag:', // 数据库 0号数据库 'select' => env('redis.select', 0), 'serialize' => [], diff --git a/niucloud/config/console.php b/niucloud/config/console.php index a56c5372f..a2794549e 100644 --- a/niucloud/config/console.php +++ b/niucloud/config/console.php @@ -8,6 +8,9 @@ return [ //消息队列 自定义命令 'queue:work' => 'think\queue\command\Work', 'queue:restart' => 'think\queue\command\Restart', - 'queue:listen' => 'think\queue\command\Listen' + 'queue:listen' => 'think\queue\command\Listen', + 'addon:install' => 'app\command\Addon\Install', + 'addon:uninstall' => 'app\command\Addon\Uninstall', + 'schedule:run'=> 'app\command\Schedule', ], ]; diff --git a/niucloud/config/oauth.php b/niucloud/config/oauth.php new file mode 100644 index 000000000..3b268d4a9 --- /dev/null +++ b/niucloud/config/oauth.php @@ -0,0 +1,26 @@ + 'wechat', + //驱动厂商列表及参数-第三方授权 + 'drivers' => [ + //微信公众号 + 'wechat' => [ + + ], + //微信小程序 + 'weapp' => [ + + ] + ] +]; diff --git a/niucloud/config/pay.php b/niucloud/config/pay.php index a75c0c8ed..c3aba6c7c 100644 --- a/niucloud/config/pay.php +++ b/niucloud/config/pay.php @@ -18,5 +18,9 @@ return [ 'wechatpay' => [], //支付宝 'alipay' => [], + //余额 +// 'balance' => [ +// 'driver' => 'app\service\core\paytype\CoreBalanceService', //反射类的名字 +// ], ] ]; diff --git a/niucloud/core/addon/Lang.php b/niucloud/core/addon/Lang.php deleted file mode 100644 index f3742b835..000000000 --- a/niucloud/core/addon/Lang.php +++ /dev/null @@ -1,52 +0,0 @@ -getLocalAddons(); - $system_lang_path = $this->getAppPath()."lang". DIRECTORY_SEPARATOR. $data['lang_type']. DIRECTORY_SEPARATOR; - $lang_files = [ - $system_lang_path. "api.php", - $system_lang_path. "enum.php", - $system_lang_path. "validate.php", - ]; - - - foreach ($addons as $k => $v) - { - $lang_path = $this->getAddonAppPath($v)."lang". DIRECTORY_SEPARATOR. $data['lang_type']. DIRECTORY_SEPARATOR; - - $api_path = $lang_path."api.php"; - $enum_path = $lang_path."enum.php"; - $validate_path = $lang_path."validate.php"; - if(is_file($api_path)) - { - $lang_files[] = $api_path; - - } - if(is_file($enum_path)) - { - $lang_files[] = $enum_path; - } - if(is_file($validate_path)) - { - $lang_files[] = $validate_path; - } - } - $files_data = $this->loadFiles($lang_files); - $lang = []; - foreach ($files_data as $file_data) { - $lang = empty($lang) ? $file_data : array_merge($lang, $file_data); - } - return $lang; - } -} \ No newline at end of file diff --git a/niucloud/core/addon/Menu.php b/niucloud/core/addon/Menu.php deleted file mode 100644 index 658799f28..000000000 --- a/niucloud/core/addon/Menu.php +++ /dev/null @@ -1,22 +0,0 @@ -getAddonEnumPath($data['addon'])."menu".DIRECTORY_SEPARATOR. $data['app_type']. ".php"; - if(is_file($menu_path)) - { - return include $menu_path; - } - return []; - } -} \ No newline at end of file diff --git a/niucloud/core/addon/Route.php b/niucloud/core/addon/Route.php deleted file mode 100644 index a2e6f489a..000000000 --- a/niucloud/core/addon/Route.php +++ /dev/null @@ -1,27 +0,0 @@ -getLocalAddons(); - - foreach ($addons as $k => $v) - { - $route_path = $this->getAddonAppPath($v). DIRECTORY_SEPARATOR. $data['app_type']. DIRECTORY_SEPARATOR. "route.php"; - if(is_file($route_path)) - { - include $route_path; - } - } - return true; - } -} \ No newline at end of file diff --git a/niucloud/core/addon/UniappLink.php b/niucloud/core/addon/UniappLink.php deleted file mode 100644 index e8fab4bdb..000000000 --- a/niucloud/core/addon/UniappLink.php +++ /dev/null @@ -1,37 +0,0 @@ -getLocalAddons(); - $link_files = []; - foreach ($addons as $k => $v) - { - $link_path = $this->getAddonEnumPath($v). "diy". DIRECTORY_SEPARATOR. "links.php"; - if(is_file($link_path)) - { - $link_files[] = $link_path; - } - } - $link_files_data = $this->loadFiles($link_files); - $links = $data; - foreach ($link_files_data as $file_data) - { - if(empty($links)) - { - $links = $file_data; - }else - $links = array_merge($links, $file_data); - } - return $links; - } -} \ No newline at end of file diff --git a/niucloud/core/addon/UniappPages.php b/niucloud/core/addon/UniappPages.php deleted file mode 100644 index 48969c46f..000000000 --- a/niucloud/core/addon/UniappPages.php +++ /dev/null @@ -1,37 +0,0 @@ -getLocalAddons(); - $page_files = []; - foreach ($addons as $k => $v) - { - $page_path = $this->getAddonEnumPath($v). "diy". DIRECTORY_SEPARATOR. "pages.php"; - if(is_file($page_path)) - { - $page_files[] = $page_path; - } - } - $page_files_data = $this->loadFiles($page_files); - $pages = $data; - foreach ($page_files_data as $file_data) - { - if(empty($pages)) - { - $pages = $file_data; - }else - $pages = array_merge($pages, $file_data); - } - return $pages; - } -} \ No newline at end of file diff --git a/niucloud/core/base/BaseJob.php b/niucloud/core/base/BaseJob.php index 78e8a8953..8d702c4b8 100644 --- a/niucloud/core/base/BaseJob.php +++ b/niucloud/core/base/BaseJob.php @@ -12,16 +12,18 @@ namespace core\base; use app\model\system\Cron; +use app\service\core\schedule\CoreCronService; use core\job\Dispatch; use think\queue\Job; /** - * 队列异步调用定时任务 + * 队列 */ abstract class BaseJob extends Dispatch { /** + * 引导 * @param $name * @param $arguments */ @@ -30,52 +32,50 @@ abstract class BaseJob extends Dispatch $this->fire(...$arguments); } + /** - * 运行消息队列 + * 运行队列 * @param Job $job - * @param $data + * @param $params */ - public function fire(Job $job, $data): void + public function fire(Job $job, $params): void { try { - $action = $data['do'] ?? 'doJob';//任务名 - $infoData = $data['data'] ?? [];//数据 - $errorCount = $data['errorCount'] ?? 0;//执行任务错误的最大重试次数 - $this->runJob($action, $job, $infoData, $errorCount); - } catch (\Throwable $e) { + $action = $params['do'] ?? 'doJob';//任务名 + $data = $params['data'] ?? [];//数据 + $error_count = $params['error_count'] ?? 0;//执行任务错误的最大重试次数 + $this->runJob($action, $job, $data, $error_count); + } catch ( \Throwable $e ) { $job->delete(); } } - /** * 执行队列 * @param string $action * @param Job $job - * @param array $infoData - * @param int $errorCount + * @param array $data + * @param int $error_count */ - protected function runJob(string $action, Job $job, array $infoData, int $errorCount = 3) + protected function runJob(string $action, Job $job, array $data, int $error_count = 3) { - $action = method_exists($this, $action) ? $action : 'handle'; if (!method_exists($this, $action)) { $job->delete(); } - - if ($this->{$action}(...$infoData)) { + if ($this->{$action}(...$data)) { //删除任务 $job->delete(); } else { - if ($job->attempts() >= $errorCount && $errorCount) { + if ($job->attempts() >= $error_count && $error_count) { //删除任务 $job->delete(); } else { - //再次放入队列 + //重发任务 $job->release(); } } - } + } diff --git a/niucloud/core/addon/BaseAddon.php b/niucloud/core/dict/BaseDict.php similarity index 81% rename from niucloud/core/addon/BaseAddon.php rename to niucloud/core/dict/BaseDict.php index 600f6191f..c3e0604e6 100644 --- a/niucloud/core/addon/BaseAddon.php +++ b/niucloud/core/dict/BaseDict.php @@ -9,9 +9,8 @@ // | Author: Niucloud Team // +---------------------------------------------------------------------- -namespace core\addon; +namespace core\dict; -use core\loader\DriverConfig; use core\loader\Storage; use think\facade\Cache; use think\facade\Db; @@ -20,7 +19,7 @@ use think\facade\Db; * Class BaseAddon * @package */ -abstract class BaseAddon extends Storage +abstract class BaseDict extends Storage { //插件整体缓存标识 public static $cache_tag_name = 'addon_cash'; @@ -39,10 +38,18 @@ abstract class BaseAddon extends Storage protected function getLocalAddons() { $cache_name = "local_install_addons"; - return Cache::tag(self::$cache_tag_name)->remember($cache_name, function () { - $list = Db::name("addon")->column("key"); - return $list; - }); + return cache_remember( + $cache_name, + function () { + $list = Db::name("addon")->column("key"); + return $list; + }, + self::$cache_tag_name + ); +// return Cache::tag(self::$cache_tag_name)->remember($cache_name, function () { +// $list = Db::name("addon")->column("key"); +// return $list; +// }); } /** @@ -75,20 +82,20 @@ abstract class BaseAddon extends Storage } /** - *获取系统enum path + *获取系统dict path */ - protected function getEnumPath() + protected function getDictPath() { - return root_path(). "app". DIRECTORY_SEPARATOR. "enum". DIRECTORY_SEPARATOR;; + return root_path(). "app". DIRECTORY_SEPARATOR. "dict". DIRECTORY_SEPARATOR;; } /** - *获取插件对应的enum目录 + *获取插件对应的dict目录 * @param string $addon */ - protected function getAddonEnumPath(string $addon) + protected function getAddonDictPath(string $addon) { - return $this->getAddonPath($addon). "app". DIRECTORY_SEPARATOR. "enum". DIRECTORY_SEPARATOR; + return $this->getAddonPath($addon). "app". DIRECTORY_SEPARATOR. "dict". DIRECTORY_SEPARATOR; } /** diff --git a/niucloud/core/addon/AddonLoader.php b/niucloud/core/dict/DictLoader.php similarity index 76% rename from niucloud/core/addon/AddonLoader.php rename to niucloud/core/dict/DictLoader.php index e95634bc5..b383701d0 100644 --- a/niucloud/core/addon/AddonLoader.php +++ b/niucloud/core/dict/DictLoader.php @@ -9,25 +9,25 @@ // | Author: Niucloud Team // +---------------------------------------------------------------------- -namespace core\addon; +namespace core\dict; use core\loader\Loader; /** - * @see \core\addon\AddonLoader - * @mixin \core\addon\BaseAddon - * @method array|null load(array $data) + * @see \core\dict\DictLoader + * @mixin \core\dict\BaseDict + * @method array|null load(array $data = []) */ -class AddonLoader extends Loader +class DictLoader extends Loader { /** * 空间名 * @var string */ - protected $namespace = '\\core\\addon\\'; + protected $namespace = '\\core\\dict\\'; - protected $config_name = 'addon'; + protected $config_name = 'dict'; /** * 默认驱动 * @return mixed diff --git a/niucloud/core/addon/Event.php b/niucloud/core/dict/Event.php similarity index 54% rename from niucloud/core/addon/Event.php rename to niucloud/core/dict/Event.php index 8695e9ffa..d30846965 100644 --- a/niucloud/core/addon/Event.php +++ b/niucloud/core/dict/Event.php @@ -1,9 +1,17 @@ getLocalAddons(); + $system_lang_path = $this->getAppPath() . "lang" . DIRECTORY_SEPARATOR . $data[ 'lang_type' ] . DIRECTORY_SEPARATOR; + $lang_files = [ + $system_lang_path . "api.php", + $system_lang_path . "dict.php", + $system_lang_path . "validate.php", + ]; + + + foreach ($addons as $k => $v) { + $lang_path = $this->getAddonAppPath($v) . "lang" . DIRECTORY_SEPARATOR . $data[ 'lang_type' ] . DIRECTORY_SEPARATOR; + + $api_path = $lang_path . "api.php"; + $dict_path = $lang_path . "dict.php"; + $validate_path = $lang_path . "validate.php"; + if (is_file($api_path)) { + $lang_files[] = $api_path; + + } + if (is_file($dict_path)) { + $lang_files[] = $dict_path; + } + if (is_file($validate_path)) { + $lang_files[] = $validate_path; + } + } + $files_data = $this->loadFiles($lang_files); + $lang = []; + foreach ($files_data as $file_data) { + $lang = empty($lang) ? $file_data : array_merge2($lang, $file_data); + } + return $lang; + } +} \ No newline at end of file diff --git a/niucloud/core/dict/MemberAccountChangeType.php b/niucloud/core/dict/MemberAccountChangeType.php new file mode 100644 index 000000000..18f64a2ad --- /dev/null +++ b/niucloud/core/dict/MemberAccountChangeType.php @@ -0,0 +1,50 @@ +getLocalAddons(); + $account_change_type_files = []; + $system_change_type_file = $this->getDictPath()."member". DIRECTORY_SEPARATOR. "account_change_type.php"; + + + if(is_file($system_change_type_file)) + { + $account_change_type_files[] = $system_change_type_file; + } + foreach ($addons as $k => $v) + { + $addon_change_type_file = $this->getAddonDictPath($v)."member". DIRECTORY_SEPARATOR. "account_change_type.php"; + if(is_file($addon_change_type_file)) + { + $account_change_type_files[] = $addon_change_type_file; + } + } + + $account_change_type_datas = $this->loadFiles($account_change_type_files); + + $account_change_type_array = []; + foreach ($account_change_type_datas as $account_change_type_data) + { + $account_change_type_array = empty($account_change_type_array) ? $account_change_type_data : array_merge2($account_change_type_array, $account_change_type_data); + } + return $account_change_type_array; + } +} \ No newline at end of file diff --git a/niucloud/core/dict/Menu.php b/niucloud/core/dict/Menu.php new file mode 100644 index 000000000..60cf8d7d7 --- /dev/null +++ b/niucloud/core/dict/Menu.php @@ -0,0 +1,30 @@ +getAddonDictPath($data['addon'])."menu".DIRECTORY_SEPARATOR. $data['app_type']. ".php"; + if(is_file($menu_path)) + { + return include $menu_path; + } + return []; + } +} \ No newline at end of file diff --git a/niucloud/core/addon/Notice.php b/niucloud/core/dict/Notice.php similarity index 55% rename from niucloud/core/addon/Notice.php rename to niucloud/core/dict/Notice.php index 8b5b5e474..44506d851 100644 --- a/niucloud/core/addon/Notice.php +++ b/niucloud/core/dict/Notice.php @@ -1,9 +1,17 @@ getEnumPath(). "notice". DIRECTORY_SEPARATOR. $data['type']. ".php"; + $system_path = $this->getDictPath(). "notice". DIRECTORY_SEPARATOR. $data['type']. ".php"; if(is_file($system_path)) { $template_files[] = $system_path; @@ -21,7 +29,7 @@ class Notice extends BaseAddon $addons = $this->getLocalAddons(); foreach ($addons as $k => $v) { - $template_path = $this->getAddonEnumPath($v). "notice". DIRECTORY_SEPARATOR. $data['type']. ".php"; + $template_path = $this->getAddonDictPath($v). "notice". DIRECTORY_SEPARATOR. $data['type']. ".php"; if(is_file($template_path)) { $template_files[] = $template_path; diff --git a/niucloud/core/dict/Route.php b/niucloud/core/dict/Route.php new file mode 100644 index 000000000..bd36f074c --- /dev/null +++ b/niucloud/core/dict/Route.php @@ -0,0 +1,35 @@ +getLocalAddons(); + + foreach ($addons as $k => $v) + { + $route_path = $this->getAddonAppPath($v). DIRECTORY_SEPARATOR. $data['app_type']. DIRECTORY_SEPARATOR. "route.php"; + if(is_file($route_path)) + { + include $route_path; + } + } + return true; + } +} \ No newline at end of file diff --git a/niucloud/core/dict/Schedule.php b/niucloud/core/dict/Schedule.php new file mode 100644 index 000000000..76c896ada --- /dev/null +++ b/niucloud/core/dict/Schedule.php @@ -0,0 +1,47 @@ +getDictPath(). "schedule". DIRECTORY_SEPARATOR. "schedule.php"; + if(is_file($system_path)) + { + $schedule_files[] = $system_path; + } + $addons = $this->getLocalAddons(); + foreach ($addons as $k => $v) + { + $addon_path = $this->getAddonDictPath($v). "schedule". DIRECTORY_SEPARATOR. "schedule.php"; + if(is_file($addon_path)) + { + $schedule_files[] = $addon_path; + } + } + $schedule_files_data = $this->loadFiles($schedule_files); + + $schedule_data_array = []; + foreach ($schedule_files_data as $file_data) + { + $schedule_data_array = empty($schedule_data_array) ? $file_data : array_merge($schedule_data_array, $file_data); + } + return $schedule_data_array; + } +} \ No newline at end of file diff --git a/niucloud/core/addon/UniappComponent.php b/niucloud/core/dict/UniappComponent.php similarity index 52% rename from niucloud/core/addon/UniappComponent.php rename to niucloud/core/dict/UniappComponent.php index 56b593b5b..8f31aef92 100644 --- a/niucloud/core/addon/UniappComponent.php +++ b/niucloud/core/dict/UniappComponent.php @@ -1,9 +1,17 @@ $v) { - $components_path = $this->getAddonEnumPath($v). "diy". DIRECTORY_SEPARATOR. "components.php"; + $components_path = $this->getAddonDictPath($v). "diy". DIRECTORY_SEPARATOR. "components.php"; if(is_file($components_path)) { $components_files[] = $components_path; diff --git a/niucloud/core/dict/UniappLink.php b/niucloud/core/dict/UniappLink.php new file mode 100644 index 000000000..2d861ee6a --- /dev/null +++ b/niucloud/core/dict/UniappLink.php @@ -0,0 +1,43 @@ +getLocalAddons(); + $link_files = []; + foreach ($addons as $k => $v) { + $link_path = $this->getAddonDictPath($v) . "diy" . DIRECTORY_SEPARATOR . "links.php"; + if (is_file($link_path)) { + $link_files[] = $link_path; + } + } + $link_files_data = $this->loadFiles($link_files); + $links = $data; + + foreach ($link_files_data as $file_data) { + if (empty($links)) { + $links = $file_data; + } else { + $links = array_merge2($links, $file_data); + } + } + return $links; + } +} \ No newline at end of file diff --git a/niucloud/core/dict/UniappPages.php b/niucloud/core/dict/UniappPages.php new file mode 100644 index 000000000..536eb7455 --- /dev/null +++ b/niucloud/core/dict/UniappPages.php @@ -0,0 +1,42 @@ +getLocalAddons(); + $page_files = []; + foreach ($addons as $k => $v) { + $page_path = $this->getAddonDictPath($v) . "diy" . DIRECTORY_SEPARATOR . "pages.php"; + if (is_file($page_path)) { + $page_files[] = $page_path; + } + } + $page_files_data = $this->loadFiles($page_files); + $pages = $data; + foreach ($page_files_data as $file_data) { + if (empty($pages)) { + $pages = $file_data; + } else { + $pages = array_merge2($pages, $file_data); + } + } + return $pages; + } +} \ No newline at end of file diff --git a/niucloud/core/dict/UniappTemplate.php b/niucloud/core/dict/UniappTemplate.php new file mode 100644 index 000000000..9dd628f7d --- /dev/null +++ b/niucloud/core/dict/UniappTemplate.php @@ -0,0 +1,42 @@ +getLocalAddons(); + $page_files = []; + foreach ($addons as $k => $v) { + $page_path = $this->getAddonDictPath($v) . "diy" . DIRECTORY_SEPARATOR . "template.php"; + if (is_file($page_path)) { + $page_files[] = $page_path; + } + } + $page_files_data = $this->loadFiles($page_files); + $pages = $data; + foreach ($page_files_data as $file_data) { + if (empty($pages)) { + $pages = $file_data; + } else { + $pages = array_merge($pages, $file_data); + } + } + return $pages; + } +} \ No newline at end of file diff --git a/niucloud/core/exception/ServerException.php b/niucloud/core/exception/ServerException.php new file mode 100644 index 000000000..b0080a26b --- /dev/null +++ b/niucloud/core/exception/ServerException.php @@ -0,0 +1,17 @@ +job($class)->secs($secs); if (is_array($action)) { $queue->data(...$action); } else if (is_string($action)) { - $queue->do($action)->data(...$data); + $queue->method($action)->data(...$data); } if ($queue_name) { $queue->setQueueName($queue_name); - } else if (static::queueName()) { - $queue->setQueueName(static::queueName()); } return $queue->push(); } else { @@ -59,5 +51,4 @@ class Dispatch } } - } diff --git a/niucloud/core/loader/Loader.php b/niucloud/core/loader/Loader.php index d9959d71e..476201eea 100644 --- a/niucloud/core/loader/Loader.php +++ b/niucloud/core/loader/Loader.php @@ -36,8 +36,9 @@ abstract class Loader extends Facade * @return mixed */ abstract protected function getDefault(); + /** - * 获取实例 + * 创建实例对象 * @param string $type * @return object|\think\DbManager * @throws \Exception @@ -59,20 +60,29 @@ abstract class Loader extends Facade */ public function getClass(string $type){ $class = config($this->config_name.'.drivers.'.$type.'.driver'); - if (class_exists($class)) { + if (!empty($class) && class_exists($class)) { return $class; }else{ if ($this->namespace || str_contains($type, '\\')) { - $class = str_contains($type, '\\') ? $type : $this->namespace . ucfirst(strtolower($type)); - if (class_exists($class)) { + $class = str_contains($type, '\\') ? $type : $this->namespace . $type; + if(class_exists($class)){ return $class; + }else{ + $class = str_contains($type, '\\') ? $type : $this->namespace . Str::studly($type); + if (class_exists($class)) { + return $class; + } } } } throw new \Exception("Driver [$type] not supported."); } - + /** + * 通过装载器获取实例 + * @return object|\think\DbManager + * @throws \Exception + */ public function getLoader(){ if(empty($this->class)){ diff --git a/niucloud/core/loader/Storage.php b/niucloud/core/loader/Storage.php index ba7583eab..fb4df3273 100644 --- a/niucloud/core/loader/Storage.php +++ b/niucloud/core/loader/Storage.php @@ -42,7 +42,7 @@ abstract class Storage */ protected function setError(?string $error = null) { - $this->error = $error ?: '未知错误'; + $this->error = $error; return false; } diff --git a/niucloud/core/oauth/BaseOauth.php b/niucloud/core/oauth/BaseOauth.php new file mode 100644 index 000000000..99a7cf07f --- /dev/null +++ b/niucloud/core/oauth/BaseOauth.php @@ -0,0 +1,50 @@ +site_id)->oauth; + } + public function oauth(string $code = null, array $options = []) + { +// $this->instance()-> + // TODO: Implement oauth() method. + } +} \ No newline at end of file diff --git a/niucloud/core/pay/Alipay.php b/niucloud/core/pay/Alipay.php index 332347e3a..bfd03d8b3 100644 --- a/niucloud/core/pay/Alipay.php +++ b/niucloud/core/pay/Alipay.php @@ -8,7 +8,9 @@ namespace core\pay; -use app\enum\pay\OnlinePayEnum; +use app\dict\pay\OnlinePayDict; +use app\dict\pay\RefundDict; +use app\dict\pay\TransferDict; use core\exception\PayException; use Psr\Http\Message\MessageInterface; use Psr\Http\Message\ResponseInterface; @@ -153,17 +155,22 @@ class Alipay extends BasePay ])); if(!empty($result['msg']) && $result['msg'] != 'Success'){ throw new PayException($result['sub_msg']); - }else{ - if($result['status'] == 'SUCCESS'){ - $result = array( - 'batch_id' => $result['pay_fund_order_id'] - ); - }else if($result['status'] == 'FAIL' && !empty($result['fail_reason'])){ - throw new PayException($result['fail_reason']); + $status = $result['status']; + $status_array = array( + 'SUCCESS' => TransferDict::SUCCESS, + 'WAIT_PAY' => TransferDict::WAIT, + 'CLOSED' => TransferDict::FAIL, + 'FAIL' => TransferDict::FAIL + ); + $res = array( + 'status' => $status_array[$status], + ); + if($status == 'FAIL'){ + $res['fail_reason'] = $result['fail_reason']; } } - return $result; + return $res; } /** @@ -196,11 +203,29 @@ class Alipay extends BasePay $out_trade_no = $params['out_trade_no']; $money = $params['money']; // $total = $params['total']; -// $refund_no = $params['refund_no']; - $result = Pay::alipay()->refund([ + $refund_no = $params['refund_no']; + $result = $this->returnFormat(Pay::alipay()->refund([ 'out_trade_no' => $out_trade_no, 'refund_amount' => $money, - ]); + 'out_request_no' => $refund_no + ])); + if(!empty($result['msg']) && $result['msg'] == 'Success'){ + $fund_change = $result['fund_change'];//退款是否成功可以根据同步响应的 fund_change 参数来判断,fund_change 表示本次退款是否发生了资金变化,返回 Y 表示退款成功,返回 N 则表示本次退款未发生资金变动 。 + if($fund_change == 'Y'){ + $status = RefundDict::SUCCESS; + }else{ + $status = RefundDict::DEALING; + } + return [ + 'status' => $status, + 'refund_no' => $refund_no, + 'out_trade_no' => $out_trade_no + ]; + }else{ + //todo 这儿可以抛出错误 + return false; + } + return $result; } @@ -210,21 +235,24 @@ class Alipay extends BasePay * @param $out_trade_no * @return void */ - public function notify(Callable $callback){ + public function notify(string $action, Callable $callback){ try{ $result = Pay::alipay()->callback(); //通过返回的值 if(!empty($result)){//成功 - //todo 这儿需要具体设计 - $temp_data = array( - 'mchid' => $result['seller_id'], - 'trade_no' => $result['trade_no'], - 'result' => $result - ); - $callback_result = $callback($result['out_trade_no'], $temp_data); - if(is_bool($callback_result) && $callback_result){ - return Pay::alipay()->success(); + if($action == 'pay'){ + //todo 这儿需要具体设计 + $temp_data = array( + 'mchid' => $result['seller_id'], + 'trade_no' => $result['trade_no'], + 'result' => $result + ); + $callback_result = $callback($result['out_trade_no'], $temp_data); + if(is_bool($callback_result) && $callback_result){ + return Pay::alipay()->success(); + } } + } return $this->fail(); } catch (\Throwable $e) { @@ -247,7 +275,7 @@ class Alipay extends BasePay $result = $this->returnFormat(Pay::alipay()->find($order)); if(!empty($result['msg']) && $result['msg'] == 'Success'){ return [ - 'status' => OnlinePayEnum::getAliPayStatus($result['trade_status']) + 'status' => OnlinePayDict::getAliPayStatus($result['trade_status']) ]; }else{ if(!empty($result['sub_code']) && $result['sub_code'] == 'ACQ.ACQ.SYSTEM_ERROR'){ @@ -272,7 +300,25 @@ class Alipay extends BasePay ]; $result = $this->returnFormat(Pay::alipay()->find($order)); - return $result; + if(!empty($result['msg']) && $result['msg'] == 'Success'){ + $refund_status = $result['refund_status'] ?? ''; + if($refund_status == 'REFUND_SUCCESS'){ + $status = RefundDict::SUCCESS; + }else{ + $status = RefundDict::DEALING; + } + return [ + 'status' => $status, + 'refund_no' => $refund_no, + 'out_trade_no' => $out_trade_no + ]; + }else{ + if(!empty($result['sub_code']) && $result['sub_code'] == 'ACQ.ACQ.SYSTEM_ERROR'){ + throw new PayException($result['msg']); + }else{ + return []; + } + } } /** @@ -286,7 +332,25 @@ class Alipay extends BasePay '_type' => 'transfer' ]; $result = $this->returnFormat(Pay::alipay()->find($order)); - return $result; + if(!empty($result['msg']) && $result['msg'] == 'Success'){ + $status = $result['SUCCESS'] ?? ''; + $status_array = array( + 'SUCCESS' => TransferDict::SUCCESS, + 'WAIT_PAY' => TransferDict::WAIT, + 'CLOSED' => TransferDict::FAIL, + 'FAIL' => TransferDict::FAIL + ); + return [ + 'status' => $status_array[$status], + 'transfer_no' => $transfer_no + ]; + }else{ + if(!empty($result['sub_code']) && $result['sub_code'] == 'ACQ.ACQ.SYSTEM_ERROR'){ + throw new PayException($result['msg']); + }else{ + return []; + } + } } public function fail(){ diff --git a/niucloud/core/pay/BasePay.php b/niucloud/core/pay/BasePay.php index f30090381..9dc8a59ac 100644 --- a/niucloud/core/pay/BasePay.php +++ b/niucloud/core/pay/BasePay.php @@ -99,7 +99,7 @@ abstract class BasePay extends Storage * @param callable $callback * @return mixed */ - abstract protected function notify(Callable $callback); + abstract protected function notify(string $action, Callable $callback); /** * 查询支付订单 @@ -163,6 +163,33 @@ abstract class BasePay extends Storage } } + /** + * 解析退款返回数据并解析 + * @param $our_trade_no + * @param $refund_no + * @param $status + * @return array + */ + public function getRefundData($our_trade_no, $refund_no, $status, $success_time = 0, $reason = ''){ + return [ + 'our_trade_no' => $our_trade_no, + 'refund_no' => $refund_no, + 'status' => $status, + 'success_time' =>$success_time, + 'reason' => $reason + ]; + } + + /** + * 获取转账数据并解析 + * @param $transfer_no + * @param $status + * @param $reason + * @return void + */ + public function getTransferData($transfer_no, $status, $reason){ + + } diff --git a/niucloud/core/pay/Wechatpay.php b/niucloud/core/pay/Wechatpay.php index 3fbb1f7c5..348ad5b1a 100644 --- a/niucloud/core/pay/Wechatpay.php +++ b/niucloud/core/pay/Wechatpay.php @@ -8,7 +8,9 @@ namespace core\pay; -use app\enum\pay\OnlinePayEnum; +use app\dict\pay\OnlinePayDict; +use app\dict\pay\RefundDict; +use app\dict\pay\TransferDict; use core\exception\PayException; use EasyWeChat\Factory; use Psr\Http\Message\MessageInterface; @@ -268,7 +270,18 @@ class Wechatpay extends BasePay 'currency' => 'CNY', ], ]); - return $this->returnFormat($result); + $result = $this->returnFormat($result); + $refund_status_array = [ + 'SUCCESS' => RefundDict::SUCCESS, + 'CLOSED' => RefundDict::FAIL, + 'PROCESSING' => RefundDict::DEALING, + 'ABNORMAL' => RefundDict::FAIL, + ]; + return [ + 'status' => $refund_status_array[$result['status']], + 'refund_no' => $refund_no, + 'out_trade_no' => $out_trade_no + ]; } @@ -277,18 +290,38 @@ class Wechatpay extends BasePay * @param $out_trade_no * @return void */ - public function notify(Callable $callback){ + public function notify(string $action, Callable $callback){ try{ $result = $this->returnFormat(Pay::wechat()->callback()); - if($result['event_type'] == 'TRANSACTION.SUCCESS'){ - $pay_trade_data = $result['resource']['ciphertext']; - $temp_params = [ - 'trade_no' => $pay_trade_data['transaction_id'], - 'mch_id' => $pay_trade_data['mchid'] - ]; - $callback_result = $callback($pay_trade_data['out_trade_no'], $temp_params); - if(is_bool($callback_result) && $callback_result){ - return Pay::wechat()->success(); + if($action == 'pay') {//支付 + if ($result['event_type'] == 'TRANSACTION.SUCCESS') { + $pay_trade_data = $result['resource']['ciphertext']; + + $temp_params = [ + 'trade_no' => $pay_trade_data['transaction_id'], + 'mch_id' => $pay_trade_data['mchid'], + 'status' => OnlinePayDict::getWechatPayStatus($pay_trade_data['trade_state']) + ]; + + $callback_result = $callback($pay_trade_data['out_trade_no'], $temp_params); + if (is_bool($callback_result) && $callback_result) { + return Pay::wechat()->success(); + } + } + }else if($action == 'refund'){//退款 + if ($result['event_type'] == 'REFUND.SUCCESS') { + $refund_trade_data = $result['resource']['ciphertext']; + $temp_params = [ + 'trade_no' => $refund_trade_data['transaction_id'], + 'mch_id' => $refund_trade_data['mchid'], + 'refund_no' => $refund_trade_data['out_refund_no'], + 'status' => OnlinePayDict::getWechatPayStatus($refund_trade_data['refund_status']) + ]; + + $callback_result = $callback($refund_trade_data['out_trade_no'], $temp_params); + if (is_bool($callback_result) && $callback_result) { + return Pay::wechat()->success(); + } } } return $this->fail(); @@ -326,7 +359,7 @@ class Wechatpay extends BasePay return $result; $result = $this->returnFormat($result); return [ - 'status' => OnlinePayEnum::getWechatPayStatus($result['trade_state']), + 'status' => OnlinePayDict::getWechatPayStatus($result['trade_state']), ]; } @@ -342,7 +375,20 @@ class Wechatpay extends BasePay 'out_refund_no' => $refund_no ]; $result = Pay::wechat()->find($order); - return $this->returnFormat($result); + if(empty($result)) + return $result; + $result = $this->returnFormat($result); + $refund_status_array = [ + 'SUCCESS' => RefundDict::SUCCESS, + 'CLOSED' => RefundDict::FAIL, + 'PROCESSING' => RefundDict::DEALING, + 'ABNORMAL' => RefundDict::FAIL, + ]; + return [ + 'status' => $refund_status_array[$result['status']], + 'refund_no' => $refund_no, + 'out_trade_no' => $out_trade_no + ]; } /** @@ -351,16 +397,24 @@ class Wechatpay extends BasePay * @return void */ public function getTransfer(string $transfer_no){ - - $params = [ 'out_batch_no' => $transfer_no, ]; - $allPlugins = Pay::wechat()->mergeCommonPlugins([QueryOutBatchNoPlugin::class]); - $result = Pay::wechat()->pay($allPlugins, $params); - return $this->returnFormat($result); + $result = $this->returnFormat($result); + //微信转账状态 + $transfer_status_array = [ + 'INIT' => TransferDict::DEALING,//初始态。 系统转账校验中 + 'WAIT_PAY' => TransferDict::DEALING, + 'PROCESSING' => TransferDict::DEALING, + 'FAIL' => TransferDict::FAIL, + 'SUCCESS' => TransferDict::SUCCESS, + ]; + return [ + 'status' => $transfer_status_array[$result['status']], + 'transfer_no' => $transfer_no + ]; } diff --git a/niucloud/core/sms/Aliyun.php b/niucloud/core/sms/Aliyun.php index bb11b5b2f..af4ea98c7 100644 --- a/niucloud/core/sms/Aliyun.php +++ b/niucloud/core/sms/Aliyun.php @@ -1,5 +1,13 @@ template()->addTemplate($data['tid'], $data['kidList'], $data['sceneDesc']); } diff --git a/niucloud/core/template/Wechat.php b/niucloud/core/template/Wechat.php index 9dfac299e..ba5483d17 100644 --- a/niucloud/core/template/Wechat.php +++ b/niucloud/core/template/Wechat.php @@ -1,4 +1,13 @@ template()->addTemplate($data['shortId']); } diff --git a/niucloud/core/util/ConfigUtil.php b/niucloud/core/util/ConfigUtil.php deleted file mode 100644 index b7611a15f..000000000 --- a/niucloud/core/util/ConfigUtil.php +++ /dev/null @@ -1,124 +0,0 @@ -path = $path; - $this->config = $init; - $this->unique_key = $unique_key; - } - - /** - * 加载配置文件(多种格式) - * @access public - * @param string $file 配置文件名 - * @param string $name 一级配置名 - * @return array - */ - public function loadConfig() : array - { - $files_data = $this->loadFiles(); - if (!empty($files_data)) { - foreach ($files_data as $data) { - if ($this->unique_key) { - $this->config = $this->config + $data; - } else { - $this->config = array_merge($this->config, $data); - } - } - } - - return $this->config; - } - - /** - * 加载返回所有文件数据 - */ - public function loadFiles() - { - $this->parseFiles($this->path); - $default_sort = 100000; - $files_data = []; - if (!empty($this->files)) { - foreach ($this->files as $file) { - $config = include $file; - if (!empty($config)) { - if (isset($config[ 'file_sort' ])) { - $sort = $config[ 'file_sort' ]; - unset($config[ 'file_sort' ]); - $sort = $sort * 10; - while (array_key_exists($sort, $files_data)) { - $sort++; - } - $files_data[ $sort ] = $config; - } else { - $files_data[ $default_sort ] = $config; - $default_sort++; - } - } - } - } - ksort($files_data); - return $files_data; - } - - /** - * 整理所有文件 - * @param string $path - */ - protected function parseFiles(string $path) - { - $files = scandir($path); - //先加载系统(system),然后加载非插件,最后按照插件安装顺序进行加载 - foreach ($files as $file) { - if ($file != '.' && $file != '..') { - if (is_dir($path . DIRECTORY_SEPARATOR . $file)) { - $this->parseFiles($path . DIRECTORY_SEPARATOR . $file); - } else { - $this->files[] = $path . DIRECTORY_SEPARATOR . $file; - } - } - } - } - -} diff --git a/niucloud/core/util/Queue.php b/niucloud/core/util/Queue.php index 1c929a9f4..2357d90e8 100644 --- a/niucloud/core/util/Queue.php +++ b/niucloud/core/util/Queue.php @@ -18,12 +18,11 @@ use think\facade\Log; /** * Class Queue * @package core\util - * @method $this do(string $do) 设置任务执行方法 + * @method $this method(string $method) 设置任务执行方法 * @method $this job(string $job) 设置任务执行类名 - * @method $this errorCount(int $errorCount) 执行失败次数 + * @method $this errorCount(int $error_count) 执行失败次数 * @method $this data(...$data) 执行数据 * @method $this secs(int $secs) 延迟执行秒数 - * @method $this log($log) 记录日志 */ class Queue { @@ -33,65 +32,37 @@ class Queue * @var string */ protected $error; - /** - * 设置错误信息 - * @param string|null $error - * @return bool - */ - protected function setError(?string $error = null) - { - $this->error = $error ?: '未知错误'; - return false; - } - - /** - * 获取错误信息 - * @return string - */ - public function getError() - { - $error = $this->error; - $this->error = null; - return $error; - } - - /** - * 任务执行 + * 任务执行方法 * @var string */ - protected $do = 'doJob'; + protected $method = 'doJob'; /** * 默认任务执行方法名 * @var string */ - protected $defaultDo; - + protected $default_method; /** * 任务类名 * @var string */ protected $job; - /** - * 错误次数 + * 队列失败次数 * @var int */ - protected $errorCount = 3; - + protected $error_count = 3; /** * 数据 * @var array|string */ protected $data; - /** - * 队列名 + * 队列名称 * @var null */ - protected $queueName = null; - + protected $queue_name = null; /** * 延迟执行秒数 * @var int @@ -99,30 +70,22 @@ class Queue protected $secs = 0; /** - * 记录日志 - * @var string|callable|array - */ - protected $log; - - /** + * 允许的方法或属性 * @var array */ - protected $rules = ['do', 'data', 'errorCount', 'job', 'secs', 'log']; - + protected $allow_function = ['method', 'data', 'error_count', 'job', 'secs']; /** + * 当前实例 * @var static */ protected static $instance; - - /** - * Queue constructor. - */ protected function __construct() { - $this->defaultDo = $this->do; + $this->default_method = $this->method; } /** + * 实例化当前队列 * @return static */ public static function instance() @@ -134,24 +97,25 @@ class Queue } /** - * @param string $queueName + * 设置队列名称 + * @param string $queue_name * @return $this */ - public function setQueueName(string $queueName) + public function setQueueName(string $queue_name) { - $this->queueName = $queueName; + $this->queue_name = $queue_name; return $this; } /** - * 放入消息队列 + * 加入队列 * @param array|null $data - * @return mixed + * @return bool */ public function push(?array $data = null) { if (!$this->job) { - return $this->setError('需要执行的队列类必须存在'); + return $this->setError('JOB_NOT_EXISTS'); } $jodValue = $this->getValues($data); //todo 队列扩展策略调度, @@ -159,7 +123,7 @@ class Queue if (!$res) { $res = ThinkQueue::{$this->action()}(...$jodValue); if (!$res) { - Log::error('加入队列失败,参数:' . json_encode($this->getValues($data))); + Log::error('队列推送失败,参数:' . json_encode($jodValue)); } } $this->clean(); @@ -173,10 +137,9 @@ class Queue { $this->secs = 0; $this->data = []; - $this->log = null; - $this->queueName = null; - $this->errorCount = 3; - $this->do = $this->defaultDo; + $this->queue_name = null; + $this->error_count = 3; + $this->method = $this->default_method; } /** @@ -196,35 +159,57 @@ class Queue protected function getValues($data) { $jobData['data'] = $data ?: $this->data; - $jobData['do'] = $this->do; - $jobData['errorCount'] = $this->errorCount; - $jobData['log'] = $this->log; - if ($this->do != $this->defaultDo) { - $this->job .= '@' . Config::get('queue.prefix', 'eb_') . $this->do; + $jobData['method'] = $this->method; + $jobData['error_count'] = $this->error_count; + if ($this->method != $this->default_method) { + $this->job .= '@'.$this->method; } if ($this->secs) { - return [$this->secs, $this->job, $jobData, $this->queueName]; + return [$this->secs, $this->job, $jobData, $this->queue_name]; } else { - return [$this->job, $jobData, $this->queueName]; + return [$this->job, $jobData, $this->queue_name]; } } /** + * 不可访问时调用 * @param $name * @param $arguments * @return $this */ - public function __call($name, $arguments) + public function __call($method, $arguments) { - if (in_array($name, $this->rules)) { - if ($name === 'data') { - $this->{$name} = $arguments; + if (in_array($method, $this->allow_function)) { + if ($method === 'data') { + $this->{$method} = $arguments; } else { - $this->{$name} = $arguments[0] ?? null; + $this->{$method} = $arguments[0] ?? null; } return $this; } else { - throw new \RuntimeException('Method does not exist' . __CLASS__ . '->' . $name . '()'); + throw new \Exception('Method does not exist' . __CLASS__ . '->' . $method . '()'); } } + + /** + * 设置错误信息 + * @param string|null $error + * @return bool + */ + protected function setError(?string $error = null) + { + $this->error = $error; + return false; + } + + /** + * 获取错误信息 + * @return string + */ + public function getError() + { + $error = $this->error; + $this->error = null; + return $error; + } } diff --git a/niucloud/core/util/SqlUtil.php b/niucloud/core/util/SqlUtil.php deleted file mode 100644 index 9e0ee671b..000000000 --- a/niucloud/core/util/SqlUtil.php +++ /dev/null @@ -1,101 +0,0 @@ -getSqlQuery($sql_data); - $query_count = count($sql_query); - for ($i = 0; $i < $query_count; $i++) { - $sql = trim($sql_query[ $i ]); - $is_write = false; - if (strstr($sql, 'CREATE TABLE')) { - $match_item = preg_match('/CREATE TABLE [`]?(\\w+)[`]?/is', $sql, $match_data); - $is_write = true; - } elseif (strstr($sql, 'ALTER TABLE')) { - $match_item = preg_match('/ALTER TABLE [`]?(\\w+)[`]?/is', $sql, $match_data); - - } elseif (strstr($sql, 'INSERT INTO')) { - $match_item = preg_match('/INSERT INTO [`]?(\\w+)[`]?/is', $sql, $match_data); - } else { - $match_item = 0; - } - if ($match_item > 0) { - try { - $table_name = $match_data[ 1 ]; - $new_table_name = $dbprefix . $table_name; - $sql_item = $this->strReplaceFirst($table_name, $new_table_name, $sql); - Db::execute($sql_item); - } catch (\Exception $e) { - return $e->getMessage(); - } - } - } - } - - /** - * @param $sql_data - * @return array - */ - public function getSqlQuery($sql_data) - { - $sql_data = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/", "ENGINE=\\1 DEFAULT CHARSET=utf8", $sql_data); - - $sql_data = str_replace("\r", "\n", $sql_data); - $sql_query = []; - $num = 0; - $sql_array = explode(";\n", trim($sql_data)); - unset($sql); - foreach ($sql_array as $sql) { - $sql_query[ $num ] = ''; - $sqls = explode("\n", trim($sql)); - $sqls = array_filter($sqls); - foreach ($sqls as $query) { - $str1 = substr($query, 0, 1); - if ($str1 != '#' && $str1 != '-') - $sql_query[ $num ] .= $query; - } - $num++; - } - return $sql_query; - } - - /** - * 代码切换 - * @param $search - * @param $replace - * @param $subject - * @return string - */ - public function strReplaceFirst($search, $replace, $subject) - { - return implode($replace, explode($search, $subject, 2)); - } - -} diff --git a/niucloud/core/util/Terminal.php b/niucloud/core/util/Terminal.php index a91e67e20..b94b4d728 100644 --- a/niucloud/core/util/Terminal.php +++ b/niucloud/core/util/Terminal.php @@ -2,65 +2,55 @@ namespace core\util; +use core\exception\CommonException; + class Terminal { + /** + * 执行命令 + * @param string $cwd 要执行命令的初始工作目录 + * @param string $command 要执行的命令 + * @return \think\Response + */ + public static function execute(string $cwd, string $command){ + if (!function_exists('proc_open') || !function_exists('proc_close')) return 'Function proc_open or proc_close disabled'; - private $out_file; -// private $ - public function __construct() - { - parent::__construct(); - $this->error_path = runtime_path() . 'terminal'.DIRECTORY_SEPARATOR.date('Ym').DIRECTORY_SEPARATOR.date('d').'.log'; - } - public function execute(){ -// $command = []; - //可以使自定义指令也可以是 - $command = $this->command(); + // 设置执行时长 + set_time_limit(0); - $cwd = $command['cwd'];//程序运行的目录 - -// proc_open($command['command'], $descriptorspec, $pipes, $cwd); - //通道消息不及时 + // 执行命令,并将输出保存到变量中 $descriptorspec = array( - 0 => array('pipe', 'r'), // 标准输入,子进程从此管道中读取数据 - 1 => array('pipe', 'w'), // 标准输出,子进程向此管道中写入数据 - 2 => array('file', $this->out_file, 'a') // 标准错误,写入到一个文件 + 0 => array("pipe", "r"), // 标准输入,我们不需要 + 1 => array("pipe", "w"), // 标准输出,我们需要将其捕获 + 2 => array("pipe", "w") // 标准错误,我们也需要将其捕获 ); - //放在文件中是同步的,放在管道中可能是不及时的 -// $descriptorspec = [ -// 0 => ['pipe', 'r'], -// 1 => ['file', $this->out_file, 'w'], -// 2 => ['file', $this->out_file, 'w']]; -// $env_vars = array('some_option' => 'aeiou');//可以不启用其他的环境变量,使用和系统一致的环境变量 - $env_vars = null; - $process = proc_open($command['command'], $descriptorspec, $pipes, $cwd, $env_vars); + $process = proc_open($command, $descriptorspec, $pipes, $cwd); - if (is_resource($process)) { - // $pipes 现在看起来是这样的: - // 0 => 可以向子进程标准输入写入的句柄 - // 1 => 可以从子进程标准输出读取的句柄 - // 错误输出将被追加到文件 /tmp/error-output.txt + // 检查进程是否成功创建 + if (!is_resource($process)) { + return "Could not execute command: $command"; + } - fwrite($pipes[0], ''); - fclose($pipes[0]); + // 从管道中获取命令的输出 + $output = ''; + while (!feof($pipes[1])) { + $output .= fgets($pipes[1]); + } + while (!feof($pipes[2])) { + $output .= fgets($pipes[2]); + } - echo stream_get_contents($pipes[1]); - fclose($pipes[1]); + // 关闭管道和进程 + fclose($pipes[0]); + fclose($pipes[1]); + fclose($pipes[2]); + $status = proc_close($process); - - // 切记:在调用 proc_close 之前关闭所有的管道以避免死锁。 - $return_value = proc_close($process); - - echo "command returned $return_value\n"; + // 判断命令的执行结果 + if ($status === 0) { + return strpos($output, 'Command failed') !== false ? $output : true; + } else { + return $output; } } - - public function command($key){ - //通过健名获取详细的命令字典 - - return [ - 'command' => '', - 'cwd' => '', - ]; - } } \ No newline at end of file diff --git a/niucloud/core/util/TokenAuth.php b/niucloud/core/util/TokenAuth.php index 43beebf48..8e988699f 100644 --- a/niucloud/core/util/TokenAuth.php +++ b/niucloud/core/util/TokenAuth.php @@ -46,7 +46,7 @@ class TokenAuth ]; $params['jti'] = $id . "_" . $type; - $token = JWT::encode($params, Env::get('app.app_key', 'niushop456$%^')); + $token = JWT::encode($params, Env::get('app.app_key', 'niucloud456$%^')); $cache_token = Cache::get("token_" . $params['jti']); $cache_token_arr = $cache_token ?: []; // if(!empty($cache_token)) @@ -67,7 +67,7 @@ class TokenAuth */ public static function parseToken(string $token, string $type): array { - $payload = JWT::decode($token, Env::get('app.app_key', 'niushop456$%^'), ['HS256']); + $payload = JWT::decode($token, Env::get('app.app_key', 'niucloud456$%^'), ['HS256']); if (!empty($payload)) { $token_info = json_decode(json_encode($payload), true);