This commit is contained in:
全栈小学生 2023-05-31 11:20:35 +08:00
parent 9b55b17b54
commit 754f840005
46 changed files with 1030 additions and 703 deletions

View File

@ -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' => [],

View File

@ -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',
],
];

26
niucloud/config/oauth.php Normal file
View File

@ -0,0 +1,26 @@
<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的saas管理平台
// +----------------------------------------------------------------------
// | 官方网址https://www.niucloud-admin.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
return [
//默认驱动
'default' => 'wechat',
//驱动厂商列表及参数-第三方授权
'drivers' => [
//微信公众号
'wechat' => [
],
//微信小程序
'weapp' => [
]
]
];

View File

@ -18,5 +18,9 @@ return [
'wechatpay' => [],
//支付宝
'alipay' => [],
//余额
// 'balance' => [
// 'driver' => 'app\service\core\paytype\CoreBalanceService', //反射类的名字
// ],
]
];

View File

@ -1,52 +0,0 @@
<?php
namespace core\addon;
class Lang extends BaseAddon
{
/**
* 加载事件
* @param array $data //传入语言类型
* @return array|mixed
*/
public function load(array $data)
{
$addons = $this->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;
}
}

View File

@ -1,22 +0,0 @@
<?php
namespace core\addon;
class Menu extends BaseAddon
{
/**
* 加载菜单
* @param array $data //传入插件,应用类型
* @return array|mixed
*/
public function load(array $data):array
{
$menu_path = $this->getAddonEnumPath($data['addon'])."menu".DIRECTORY_SEPARATOR. $data['app_type']. ".php";
if(is_file($menu_path))
{
return include $menu_path;
}
return [];
}
}

View File

@ -1,27 +0,0 @@
<?php
namespace core\addon;
class Route extends BaseAddon
{
/**
* 加载路由
* @param array $data 传入路由端口
* @return array|mixed
*/
public function load(array $data)
{
$addons = $this->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;
}
}

View File

@ -1,37 +0,0 @@
<?php
namespace core\addon;
class UniappLink extends BaseAddon
{
/**
* 系统uniapp页面链接
* @param array $data //系统
* @return array|mixed
*/
public function load(array $data)
{
$addons = $this->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;
}
}

View File

@ -1,37 +0,0 @@
<?php
namespace core\addon;
class UniappPages extends BaseAddon
{
/**
* 系统uniapp页面链接
* @param array $data //系统
* @return array|mixed
*/
public function load(array $data)
{
$addons = $this->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;
}
}

View File

@ -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();
}
}
}
}

View File

@ -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;
}
/**

View File

@ -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

View File

@ -1,9 +1,17 @@
<?php
namespace core\addon;
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的saas管理平台
// +----------------------------------------------------------------------
// | 官方网址https://www.niucloud-admin.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace core\dict;
class Event extends BaseAddon
class Event extends BaseDict
{
/**
* 加载事件

View File

@ -0,0 +1,56 @@
<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的saas管理平台
// +----------------------------------------------------------------------
// | 官方网址https://www.niucloud-admin.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace core\dict;
class Lang extends BaseDict
{
/**
* 加载事件
* @param array $data //传入语言类型
* @return array|mixed
*/
public function load(array $data)
{
$addons = $this->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;
}
}

View File

@ -0,0 +1,50 @@
<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的saas管理平台
// +----------------------------------------------------------------------
// | 官方网址https://www.niucloud-admin.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace core\dict;
class MemberAccountChangeType extends BaseDict
{
/**
* 加载账户变动方式
* @param array $data
* @return bool|mixed
*/
public function load(array $data = [])
{
$addons = $this->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;
}
}

View File

@ -0,0 +1,30 @@
<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的saas管理平台
// +----------------------------------------------------------------------
// | 官方网址https://www.niucloud-admin.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace core\dict;
class Menu extends BaseDict
{
/**
* 加载菜单
* @param array $data //传入插件,应用类型
* @return array|mixed
*/
public function load(array $data):array
{
$menu_path = $this->getAddonDictPath($data['addon'])."menu".DIRECTORY_SEPARATOR. $data['app_type']. ".php";
if(is_file($menu_path))
{
return include $menu_path;
}
return [];
}
}

View File

@ -1,9 +1,17 @@
<?php
namespace core\addon;
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的saas管理平台
// +----------------------------------------------------------------------
// | 官方网址https://www.niucloud-admin.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace core\dict;
class Notice extends BaseAddon
class Notice extends BaseDict
{
/**
* 系统uniapp页面链接
@ -13,7 +21,7 @@ class Notice extends BaseAddon
public function load(array $data)
{
$template_files = [];
$system_path = $this->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;

View File

@ -0,0 +1,35 @@
<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的saas管理平台
// +----------------------------------------------------------------------
// | 官方网址https://www.niucloud-admin.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace core\dict;
class Route extends BaseDict
{
/**
* 加载路由
* @param array $data 传入路由端口
* @return array|mixed
*/
public function load(array $data)
{
$addons = $this->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;
}
}

View File

@ -0,0 +1,47 @@
<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的saas管理平台
// +----------------------------------------------------------------------
// | 官方网址https://www.niucloud-admin.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace core\dict;
class Schedule extends BaseDict
{
/**
* 加载计划任务调度
* @param array $data
* @return array|mixed
*/
public function load(array $data = [])
{
$schedule_files = [];
$system_path = $this->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;
}
}

View File

@ -1,9 +1,17 @@
<?php
namespace core\addon;
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的saas管理平台
// +----------------------------------------------------------------------
// | 官方网址https://www.niucloud-admin.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace core\dict;
class UniappComponent extends BaseAddon
class UniappComponent extends BaseDict
{
/**
* 系统uniapp组件配置
@ -16,7 +24,7 @@ class UniappComponent extends BaseAddon
$components_files = [];
foreach ($addons as $k => $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;

View File

@ -0,0 +1,43 @@
<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的saas管理平台
// +----------------------------------------------------------------------
// | 官方网址https://www.niucloud-admin.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace core\dict;
class UniappLink extends BaseDict
{
/**
* 系统uniapp页面链接
* @param array $data //系统
* @return array|mixed
*/
public function load(array $data)
{
$addons = $this->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;
}
}

View File

@ -0,0 +1,42 @@
<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的saas管理平台
// +----------------------------------------------------------------------
// | 官方网址https://www.niucloud-admin.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace core\dict;
class UniappPages extends BaseDict
{
/**
* 系统uniapp页面
* @param array $data //系统
* @return array|mixed
*/
public function load(array $data)
{
$addons = $this->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;
}
}

View File

@ -0,0 +1,42 @@
<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的saas管理平台
// +----------------------------------------------------------------------
// | 官方网址https://www.niucloud-admin.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace core\dict;
class UniappTemplate extends BaseDict
{
/**
* 系统uniapp页面模板
* @param array $data //系统
* @return array|mixed
*/
public function load(array $data)
{
$addons = $this->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;
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace core\exception;
use RuntimeException;
use Throwable;
/**
* 服务器异常处理类
*/
class ServerException extends RuntimeException
{
public function __construct($message = "", $code = 409, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}

View File

@ -14,38 +14,30 @@ namespace core\job;
use core\util\Queue;
/**
* 快捷加入消息队列
* 任务派遣队列
*/
class Dispatch
{
/**
* @return null
*/
protected static function queueName()
{
return null;
}
/**
* 加入队列
* @param $action
* @param array $data
* @param string|null $queueName
* @param string|null $queue_name
* @return mixed
*/
public static function invoke($action, array $data = [], int $secs = 0, string $queue_name = null, bool $is_open = true)
public static function invoke($action, array $data = [], int $secs = 0, string $queue_name = null, bool $is_async = true)
{
$class = get_called_class();//调用主调类
if ($is_open) {
if ($is_async) {
$queue = Queue::instance()->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
}
}
}

View File

@ -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)){

View File

@ -42,7 +42,7 @@ abstract class Storage
*/
protected function setError(?string $error = null)
{
$this->error = $error ?: '未知错误';
$this->error = $error;
return false;
}

View File

@ -0,0 +1,50 @@
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2023-02-17
* Time: 15:58
*/
namespace core\oauth;
use core\loader\Storage;
/**
* 第三方授权基类
* Class BaseOauth
* @package core\oauth
*/
abstract class BaseOauth extends Storage
{
protected $config;//配置
protected $site_id;
/**
* 初始化
* @param array $config
* @return mixed|void
*/
protected function initialize(array $config = [])
{
}
/**
* 获取粉丝信息
* @param string $openid
* @return mixed
*/
abstract public function getFansInfo(string $openid = null);
/**
* 授权
* @param string|null $code
* @param array $options
* @return mixed
*/
abstract public function oauth(string $code = null, array $options = []);
}

View File

@ -0,0 +1,37 @@
<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的saas管理平台
// +----------------------------------------------------------------------
// | 官方网址https://www.niucloud-admin.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace core\oauth;
use core\loader\Loader;
/**
* @see \core\oauth\OauthLoader
* @package think\facade
*/
class OauthLoader extends Loader
{
/**
* 空间名
* @var string
*/
protected $namespace = '\\core\\oauth\\';
protected $config_name = 'oauth';
/**
* 默认驱动
* @return mixed
*/
protected function getDefault()
{
return config('oauth.default');
}
}

View File

@ -0,0 +1,38 @@
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2023-02-17
* Time: 15:58
*/
namespace core\oauth;
/**
* 微信小程序授权
* Class Weapp
* @package core\oauth
*/
class Weapp extends BaseOauth
{
/**
* @param array $config
* @return mixed|void
*/
protected function initialize(array $config = [])
{
parent::initialize($config);
}
public function getFansInfo(string $openid = null)
{
// TODO: Implement getFansInfo() method.
}
public function oauth(string $code = null, array $options = [])
{
// TODO: Implement oauth() method.
}
}

View File

@ -0,0 +1,44 @@
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2023-02-17
* Time: 15:58
*/
namespace core\oauth;
use app\service\core\wechat\CoreWechatService;
/**
* 微信公众号登录
* Class Wechat
* @package core\oauth
*/
class Wechat extends BaseOauth
{
/**
* @param array $config
* @return mixed|void
*/
protected function initialize(array $config = [])
{
parent::initialize($config);
}
public function getFansInfo(string $openid = null)
{
// TODO: Implement getFansInfo() method.
}
public function instance(){
return CoreWechatService::app($this->site_id)->oauth;
}
public function oauth(string $code = null, array $options = [])
{
// $this->instance()->
// TODO: Implement oauth() method.
}
}

View File

@ -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(){

View File

@ -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){
}

View File

@ -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
];
}

View File

@ -1,5 +1,13 @@
<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的saas管理平台
// +----------------------------------------------------------------------
// | 官方网址https://www.niucloud-admin.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace core\sms;
use AlibabaCloud\Client\AlibabaCloud;
@ -68,19 +76,11 @@ class Aliyun extends BaseSms
}
}
public function open()
public function modify(string $sign = null, string $mobile, string $code)
{
}
public function modify(string $sign = null, string $phone, string $code)
{
}
public function info()
{
}
public function temps(int $page = 0, int $limit = 10, int $type = 1)
public function template(int $page = 0, int $limit = 10, int $type = 1)
{
}
@ -88,11 +88,11 @@ class Aliyun extends BaseSms
{
}
public function applys(int $tempType, int $page, int $limit)
public function localTemplate(int $type, int $page, int $limit)
{
}
public function record($record_id)
public function record($id)
{
}
}

View File

@ -31,26 +31,19 @@ abstract class BaseSms extends Storage
}
/**
* 开通服务
* 发送短信
* @param string $mobile
* @param string $template_id
* @param array $data
* @return mixed
*/
abstract public function open();
abstract public function send(string $mobile, string $template_id, array $data);
/**修改签名
/**
* 编辑签名
* @return mixed
*/
abstract public function modify(string $sign = null, string $phone, string $code);
/**用户信息
* @return mixed
*/
abstract public function info();
/**发送短信
* @return mixed
*/
abstract public function send(string $phone, string $templateId, array $data);
abstract public function modify(string $sign = null, string $mobile, string $code);
/**
* 短信模板
* @param int $page
@ -58,11 +51,11 @@ abstract class BaseSms extends Storage
* @param int $type
* @return mixed
*/
abstract public function temps(int $page, int $limit, int $type);
abstract public function template(int $page, int $limit, int $type);
/**
* 申请模板
* 申请短信
* @param string $title
* @param string $content
* @param int $type
@ -71,18 +64,20 @@ abstract class BaseSms extends Storage
abstract public function apply(string $title, string $content, int $type);
/**
* 模板记录
* @param int $tempType
* 模板列表
* @param int $type
* @param int $page
* @param int $limit
* @return mixed
*/
abstract public function applys(int $tempType, int $page, int $limit);
abstract public function localTemplate(int $type, int $page, int $limit);
/**发送记录
/**
* 记录
* @param $id
* @return mixed
*/
abstract public function record($record_id);
abstract public function record($id);
}

View File

@ -17,10 +17,7 @@ use core\loader\Loader;
* @see \core\sms\SmsLoader
* @package think\facade
* @mixin \core\sms\BaseSms
* @method string|null send(string $phone, string $templateId, array $data) 发送短信
* @method mixed open(null|string $name = null, mixed $default = null) 开启服务
* @method mixed apply(string $title, string $content, int $type) 申请模板
* @method mixed applys(int $tempType, int $page, int $limit) 模板记录
* @method string|null send(string $mobile, string $template_id, array $data) 发送短信
*/
class SmsLoader extends Loader
{

View File

@ -1,5 +1,13 @@
<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的saas管理平台
// +----------------------------------------------------------------------
// | 官方网址https://www.niucloud-admin.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace core\sms;
use core\exception\CommonException;
@ -77,19 +85,12 @@ class Tencent extends BaseSms
}
public function open()
public function modify(string $sign = null, string $mobile, string $code)
{
}
public function modify(string $sign = null, string $phone, string $code)
{
}
public function info()
{
}
public function temps(int $page = 0, int $limit = 10, int $type = 1)
public function template(int $page = 0, int $limit = 15, int $type = 1)
{
}
@ -97,11 +98,11 @@ class Tencent extends BaseSms
{
}
public function applys(int $tempType, int $page, int $limit)
public function localTemplate(int $type, int $page, int $limit)
{
}
public function record($record_id)
public function record($id)
{
}
}

View File

@ -43,7 +43,7 @@ abstract class BaseTemplate extends Storage
* @param string $shortId
* @return mixed
*/
abstract protected function add(array $data);
abstract protected function addTemplate(array $data);
/**
* 删除消息模板

View File

@ -1,4 +1,13 @@
<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的saas管理平台
// +----------------------------------------------------------------------
// | 官方网址https://www.niucloud-admin.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace core\template;
@ -52,7 +61,7 @@ class Weapp extends BaseTemplate
* @param array $data
* @return mixed|void
*/
public function add(array $data){
public function addTemplate(array $data){
return $this->template()->addTemplate($data['tid'], $data['kidList'], $data['sceneDesc']);
}

View File

@ -1,4 +1,13 @@
<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的saas管理平台
// +----------------------------------------------------------------------
// | 官方网址https://www.niucloud-admin.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace core\template;
@ -63,7 +72,7 @@ class Wechat extends BaseTemplate
* @param string $shortId
* @return mixed|void
*/
public function add(array $data){
public function addTemplate(array $data){
return $this->template()->addTemplate($data['shortId']);
}

View File

@ -1,124 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的saas管理平台
// +----------------------------------------------------------------------
// | 官方网址https://www.niucloud-admin.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace core\util;
/**
* 系统配置文件加载eventlang
* Class ConfigUtil
* @package core\util
*/
class ConfigUtil
{
/**
* config参数
* @var array
*/
public $config = [];
public $files = [];
//是否保留唯一key
public $unique_key;
/**
* 配置文件目录
* @var string
*/
protected $path;
/**
* 初始化
* ConfigUtil constructor.
* @param $path
* @param $init
*/
public function __construct(string $path, array $init = [], bool $unique_key = false)
{
$this->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;
}
}
}
}
}

View File

@ -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;
}
}

View File

@ -1,101 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的saas管理平台
// +----------------------------------------------------------------------
// | 官方网址https://www.niucloud-admin.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace core\util;
use think\db\exception\PDOException;
use think\facade\Config;
use think\facade\Db;
use think\facade\Env;
/**
* 数据库工具类
* Class SqlUtil
* @package core\util
*/
class SqlUtil
{
public function executeSql(string $sql_file): bool
{
$dbprefix = Config::get('database.connections.mysql.prefix');
$sql_data = file_get_contents($sql_file);
$sql_query = $this->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));
}
}

View File

@ -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], '<?php print_r($_ENV); ?>');
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' => '',
];
}
}

View File

@ -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);