初始化
This commit is contained in:
525
app/admin/library/Auth.php
Normal file
525
app/admin/library/Auth.php
Normal file
@@ -0,0 +1,525 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\library;
|
||||
|
||||
use Throwable;
|
||||
use ba\Random;
|
||||
use think\facade\Db;
|
||||
use think\facade\Config;
|
||||
use app\admin\model\Admin;
|
||||
use app\common\facade\Token;
|
||||
use app\admin\model\AdminGroup;
|
||||
|
||||
/**
|
||||
* 管理员权限类
|
||||
* @property int $id 管理员ID
|
||||
* @property string $username 管理员用户名
|
||||
* @property string $nickname 管理员昵称
|
||||
* @property string $email 管理员邮箱
|
||||
* @property string $mobile 管理员手机号
|
||||
*/
|
||||
class Auth extends \ba\Auth
|
||||
{
|
||||
/**
|
||||
* 需要登录时/无需登录时的响应状态代码
|
||||
*/
|
||||
public const LOGIN_RESPONSE_CODE = 303;
|
||||
|
||||
/**
|
||||
* 需要登录标记 - 前台应清理 token、记录当前路由 path、跳转到登录页
|
||||
*/
|
||||
public const NEED_LOGIN = 'need login';
|
||||
|
||||
/**
|
||||
* 已经登录标记 - 前台应跳转到基础路由
|
||||
*/
|
||||
public const LOGGED_IN = 'logged in';
|
||||
|
||||
/**
|
||||
* token 入库 type
|
||||
*/
|
||||
public const TOKEN_TYPE = 'admin';
|
||||
|
||||
/**
|
||||
* 是否登录
|
||||
* @var bool
|
||||
*/
|
||||
protected bool $loginEd = false;
|
||||
|
||||
/**
|
||||
* 错误消息
|
||||
* @var string
|
||||
*/
|
||||
protected string $error = '';
|
||||
|
||||
/**
|
||||
* Model实例
|
||||
* @var ?Admin
|
||||
*/
|
||||
protected ?Admin $model = null;
|
||||
|
||||
/**
|
||||
* 令牌
|
||||
* @var string
|
||||
*/
|
||||
protected string $token = '';
|
||||
|
||||
/**
|
||||
* 刷新令牌
|
||||
* @var string
|
||||
*/
|
||||
protected string $refreshToken = '';
|
||||
|
||||
/**
|
||||
* 令牌默认有效期
|
||||
* 可在 config/buildadmin.php 内修改默认值
|
||||
* @var int
|
||||
*/
|
||||
protected int $keepTime = 86400;
|
||||
|
||||
/**
|
||||
* 刷新令牌有效期
|
||||
* @var int
|
||||
*/
|
||||
protected int $refreshTokenKeepTime = 2592000;
|
||||
|
||||
/**
|
||||
* 允许输出的字段
|
||||
* @var array
|
||||
*/
|
||||
protected array $allowFields = ['id', 'username', 'nickname', 'avatar', 'last_login_time'];
|
||||
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
parent::__construct($config);
|
||||
$this->setKeepTime((int)Config::get('buildadmin.admin_token_keep_time'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 魔术方法-管理员信息字段
|
||||
* @param $name
|
||||
* @return mixed 字段信息
|
||||
*/
|
||||
public function __get($name): mixed
|
||||
{
|
||||
return $this->model?->$name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
* @access public
|
||||
* @param array $options 传递到 /ba/Auth 的配置信息
|
||||
* @return Auth
|
||||
*/
|
||||
public static function instance(array $options = []): Auth
|
||||
{
|
||||
$request = request();
|
||||
if (!isset($request->adminAuth)) {
|
||||
$request->adminAuth = new static($options);
|
||||
}
|
||||
return $request->adminAuth;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据Token初始化管理员登录态
|
||||
* @param string $token
|
||||
* @return bool
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function init(string $token): bool
|
||||
{
|
||||
$tokenData = Token::get($token);
|
||||
if ($tokenData) {
|
||||
|
||||
/**
|
||||
* 过期检查,过期则抛出 @see TokenExpirationException
|
||||
*/
|
||||
Token::tokenExpirationCheck($tokenData);
|
||||
|
||||
$userId = intval($tokenData['user_id']);
|
||||
if ($tokenData['type'] == self::TOKEN_TYPE && $userId > 0) {
|
||||
$this->model = Admin::where('id', $userId)->find();
|
||||
if (!$this->model) {
|
||||
$this->setError('Account not exist');
|
||||
return false;
|
||||
}
|
||||
if ($this->model['status'] != 'enable') {
|
||||
$this->setError('Account disabled');
|
||||
return false;
|
||||
}
|
||||
$this->token = $token;
|
||||
$this->loginSuccessful();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
$this->setError('Token login failed');
|
||||
$this->reset();
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理员登录
|
||||
* @param string $username 用户名
|
||||
* @param string $password 密码
|
||||
* @param bool $keep 是否保持登录
|
||||
* @return bool
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function login(string $username, string $password, bool $keep = false): bool
|
||||
{
|
||||
$this->model = Admin::where('username', $username)->find();
|
||||
if (!$this->model) {
|
||||
$this->setError('Username is incorrect');
|
||||
return false;
|
||||
}
|
||||
if ($this->model->status == 'disable') {
|
||||
$this->setError('Account disabled');
|
||||
return false;
|
||||
}
|
||||
|
||||
// 登录失败重试检查
|
||||
$lastLoginTime = $this->model->getData('last_login_time');
|
||||
$adminLoginRetry = Config::get('buildadmin.admin_login_retry');
|
||||
if ($adminLoginRetry && $lastLoginTime) {
|
||||
// 重置失败次数
|
||||
if ($this->model->login_failure > 0 && time() - $lastLoginTime >= 86400) {
|
||||
$this->model->login_failure = 0;
|
||||
$this->model->save();
|
||||
|
||||
// 重获模型实例,避免单实例多次更新
|
||||
$this->model = Admin::where('username', $username)->find();
|
||||
}
|
||||
|
||||
if ($this->model->login_failure >= $adminLoginRetry) {
|
||||
$this->setError('Please try again after 1 day');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 密码检查
|
||||
if (!verify_password($password, $this->model->password, ['salt' => $this->model->salt])) {
|
||||
$this->loginFailed();
|
||||
$this->setError('Password is incorrect');
|
||||
return false;
|
||||
}
|
||||
|
||||
// 清理 token
|
||||
if (Config::get('buildadmin.admin_sso')) {
|
||||
Token::clear(self::TOKEN_TYPE, $this->model->id);
|
||||
Token::clear(self::TOKEN_TYPE . '-refresh', $this->model->id);
|
||||
}
|
||||
|
||||
if ($keep) {
|
||||
$this->setRefreshToken($this->refreshTokenKeepTime);
|
||||
}
|
||||
$this->loginSuccessful();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置刷新Token
|
||||
* @param int $keepTime
|
||||
*/
|
||||
public function setRefreshToken(int $keepTime = 0): void
|
||||
{
|
||||
$this->refreshToken = Random::uuid();
|
||||
Token::set($this->refreshToken, self::TOKEN_TYPE . '-refresh', $this->model->id, $keepTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理员登录成功
|
||||
* @return bool
|
||||
*/
|
||||
public function loginSuccessful(): bool
|
||||
{
|
||||
if (!$this->model) return false;
|
||||
$this->model->startTrans();
|
||||
try {
|
||||
$this->model->login_failure = 0;
|
||||
$this->model->last_login_time = time();
|
||||
$this->model->last_login_ip = request()->ip();
|
||||
$this->model->save();
|
||||
$this->loginEd = true;
|
||||
|
||||
if (!$this->token) {
|
||||
$this->token = Random::uuid();
|
||||
Token::set($this->token, self::TOKEN_TYPE, $this->model->id, $this->keepTime);
|
||||
}
|
||||
$this->model->commit();
|
||||
} catch (Throwable $e) {
|
||||
$this->model->rollback();
|
||||
$this->setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理员登录失败
|
||||
* @return bool
|
||||
*/
|
||||
public function loginFailed(): bool
|
||||
{
|
||||
if (!$this->model) return false;
|
||||
$this->model->startTrans();
|
||||
try {
|
||||
$this->model->login_failure++;
|
||||
$this->model->last_login_time = time();
|
||||
$this->model->last_login_ip = request()->ip();
|
||||
$this->model->save();
|
||||
$this->model->commit();
|
||||
} catch (Throwable $e) {
|
||||
$this->model->rollback();
|
||||
$this->setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
return $this->reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
* @return bool
|
||||
*/
|
||||
public function logout(): bool
|
||||
{
|
||||
if (!$this->loginEd) {
|
||||
$this->setError('You are not logged in');
|
||||
return false;
|
||||
}
|
||||
return $this->reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否登录
|
||||
* @return bool
|
||||
*/
|
||||
public function isLogin(): bool
|
||||
{
|
||||
return $this->loginEd;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取管理员模型
|
||||
* @return Admin
|
||||
*/
|
||||
public function getAdmin(): Admin
|
||||
{
|
||||
return $this->model;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取管理员Token
|
||||
* @return string
|
||||
*/
|
||||
public function getToken(): string
|
||||
{
|
||||
return $this->token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取管理员刷新Token
|
||||
* @return string
|
||||
*/
|
||||
public function getRefreshToken(): string
|
||||
{
|
||||
return $this->refreshToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取管理员信息 - 只输出允许输出的字段
|
||||
* @return array
|
||||
*/
|
||||
public function getInfo(): array
|
||||
{
|
||||
if (!$this->model) return [];
|
||||
$info = $this->model->toArray();
|
||||
$info = array_intersect_key($info, array_flip($this->getAllowFields()));
|
||||
$info['token'] = $this->getToken();
|
||||
$info['refresh_token'] = $this->getRefreshToken();
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取允许输出字段
|
||||
* @return array
|
||||
*/
|
||||
public function getAllowFields(): array
|
||||
{
|
||||
return $this->allowFields;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置允许输出字段
|
||||
* @param $fields
|
||||
* @return void
|
||||
*/
|
||||
public function setAllowFields($fields): void
|
||||
{
|
||||
$this->allowFields = $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置Token有效期
|
||||
* @param int $keepTime
|
||||
* @return void
|
||||
*/
|
||||
public function setKeepTime(int $keepTime = 0): void
|
||||
{
|
||||
$this->keepTime = $keepTime;
|
||||
}
|
||||
|
||||
public function check(string $name, int $uid = 0, string $relation = 'or', string $mode = 'url'): bool
|
||||
{
|
||||
return parent::check($name, $uid ?: $this->id, $relation, $mode);
|
||||
}
|
||||
|
||||
public function getGroups(int $uid = 0): array
|
||||
{
|
||||
return parent::getGroups($uid ?: $this->id);
|
||||
}
|
||||
|
||||
public function getRuleList(int $uid = 0): array
|
||||
{
|
||||
return parent::getRuleList($uid ?: $this->id);
|
||||
}
|
||||
|
||||
public function getRuleIds(int $uid = 0): array
|
||||
{
|
||||
return parent::getRuleIds($uid ?: $this->id);
|
||||
}
|
||||
|
||||
public function getMenus(int $uid = 0): array
|
||||
{
|
||||
return parent::getMenus($uid ?: $this->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是超级管理员
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function isSuperAdmin(): bool
|
||||
{
|
||||
return in_array('*', $this->getRuleIds());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取管理员所在分组的所有子级分组
|
||||
* @return array
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function getAdminChildGroups(): array
|
||||
{
|
||||
$groupIds = Db::name('admin_group_access')
|
||||
->where('uid', $this->id)
|
||||
->select();
|
||||
$children = [];
|
||||
foreach ($groupIds as $group) {
|
||||
$this->getGroupChildGroups($group['group_id'], $children);
|
||||
}
|
||||
return array_unique($children);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个分组下的子分组
|
||||
* @param int $groupId 分组ID
|
||||
* @param array $children 存放子分组的变量
|
||||
* @return void
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function getGroupChildGroups(int $groupId, array &$children): void
|
||||
{
|
||||
$childrenTemp = AdminGroup::where('pid', $groupId)
|
||||
->where('status', 1)
|
||||
->select();
|
||||
foreach ($childrenTemp as $item) {
|
||||
$children[] = $item['id'];
|
||||
$this->getGroupChildGroups($item['id'], $children);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取分组内的管理员
|
||||
* @param array $groups
|
||||
* @return array 管理员数组
|
||||
*/
|
||||
public function getGroupAdmins(array $groups): array
|
||||
{
|
||||
return Db::name('admin_group_access')
|
||||
->where('group_id', 'in', $groups)
|
||||
->column('uid');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取拥有 `所有权限` 的分组
|
||||
* @param string $dataLimit 数据权限
|
||||
* @param array $groupQueryWhere 分组查询条件(默认查询启用的分组:[['status','=',1]])
|
||||
* @return array 分组数组
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function getAllAuthGroups(string $dataLimit, array $groupQueryWhere = [['status', '=', 1]]): array
|
||||
{
|
||||
// 当前管理员拥有的权限
|
||||
$rules = $this->getRuleIds();
|
||||
$allAuthGroups = [];
|
||||
$groups = AdminGroup::where($groupQueryWhere)->select();
|
||||
foreach ($groups as $group) {
|
||||
if ($group['rules'] == '*') {
|
||||
continue;
|
||||
}
|
||||
$groupRules = explode(',', $group['rules']);
|
||||
|
||||
// 及时break, array_diff 等没有 in_array 快
|
||||
$all = true;
|
||||
foreach ($groupRules as $groupRule) {
|
||||
if (!in_array($groupRule, $rules)) {
|
||||
$all = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($all) {
|
||||
if ($dataLimit == 'allAuth' || ($dataLimit == 'allAuthAndOthers' && array_diff($rules, $groupRules))) {
|
||||
$allAuthGroups[] = $group['id'];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $allAuthGroups;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置错误消息
|
||||
* @param $error
|
||||
* @return Auth
|
||||
*/
|
||||
public function setError($error): Auth
|
||||
{
|
||||
$this->error = $error;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取错误消息
|
||||
* @return string
|
||||
*/
|
||||
public function getError(): string
|
||||
{
|
||||
return $this->error ? __($this->error) : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 属性重置(注销、登录失败、重新初始化等将单例数据销毁)
|
||||
*/
|
||||
protected function reset(bool $deleteToken = true): bool
|
||||
{
|
||||
if ($deleteToken && $this->token) {
|
||||
Token::delete($this->token);
|
||||
}
|
||||
|
||||
$this->token = '';
|
||||
$this->loginEd = false;
|
||||
$this->model = null;
|
||||
$this->refreshToken = '';
|
||||
$this->setError('');
|
||||
$this->setKeepTime((int)Config::get('buildadmin.admin_token_keep_time'));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
1299
app/admin/library/crud/Helper.php
Normal file
1299
app/admin/library/crud/Helper.php
Normal file
@@ -0,0 +1,1299 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\library\crud;
|
||||
|
||||
use Throwable;
|
||||
use ba\Filesystem;
|
||||
use think\Exception;
|
||||
use ba\TableManager;
|
||||
use think\facade\Db;
|
||||
use app\common\library\Menu;
|
||||
use app\admin\model\AdminRule;
|
||||
use app\admin\model\CrudLog;
|
||||
use ba\Exception as BaException;
|
||||
use Phinx\Db\Adapter\MysqlAdapter;
|
||||
use Phinx\Db\Adapter\AdapterInterface;
|
||||
|
||||
class Helper
|
||||
{
|
||||
/**
|
||||
* 内部保留词
|
||||
* @var array
|
||||
*/
|
||||
protected static array $reservedKeywords = [
|
||||
'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone',
|
||||
'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty',
|
||||
'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends',
|
||||
'final', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once',
|
||||
'instanceof', 'insteadof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private',
|
||||
'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try',
|
||||
'unset', 'use', 'var', 'while', 'xor', 'yield', 'match', 'readonly', 'fn',
|
||||
];
|
||||
|
||||
/**
|
||||
* 预设控制器和模型文件位置
|
||||
* @var array
|
||||
*/
|
||||
protected static array $parseNamePresets = [
|
||||
'controller' => [
|
||||
'user' => ['user', 'user'],
|
||||
'admin' => ['auth', 'admin'],
|
||||
'admin_group' => ['auth', 'group'],
|
||||
'attachment' => ['routine', 'attachment'],
|
||||
'admin_rule' => ['auth', 'rule'],
|
||||
],
|
||||
'model' => [],
|
||||
'validate' => [],
|
||||
];
|
||||
|
||||
/**
|
||||
* 子级菜单数组(权限节点)
|
||||
* @var array
|
||||
*/
|
||||
public static array $menuChildren = [
|
||||
['type' => 'button', 'title' => '查看', 'name' => '/index', 'status' => 1],
|
||||
['type' => 'button', 'title' => '添加', 'name' => '/add', 'status' => 1],
|
||||
['type' => 'button', 'title' => '编辑', 'name' => '/edit', 'status' => 1],
|
||||
['type' => 'button', 'title' => '删除', 'name' => '/del', 'status' => 1],
|
||||
['type' => 'button', 'title' => '快速排序', 'name' => '/sortable', 'status' => 1],
|
||||
];
|
||||
|
||||
/**
|
||||
* 输入框类型的识别规则
|
||||
* @var array
|
||||
*/
|
||||
protected static array $inputTypeRule = [
|
||||
// 开关组件
|
||||
[
|
||||
'type' => ['tinyint', 'int', 'enum'],
|
||||
'suffix' => ['switch', 'toggle'],
|
||||
'value' => 'switch',
|
||||
],
|
||||
[
|
||||
'column_type' => ['tinyint(1)', 'char(1)', 'tinyint(1) unsigned'],
|
||||
'suffix' => ['switch', 'toggle'],
|
||||
'value' => 'switch',
|
||||
],
|
||||
// 富文本-识别规则和textarea重合,优先识别为富文本
|
||||
[
|
||||
'type' => ['longtext', 'text', 'mediumtext', 'smalltext', 'tinytext', 'bigtext'],
|
||||
'suffix' => ['content', 'editor'],
|
||||
'value' => 'editor',
|
||||
],
|
||||
// textarea
|
||||
[
|
||||
'type' => ['varchar'],
|
||||
'suffix' => ['textarea', 'multiline', 'rows'],
|
||||
'value' => 'textarea',
|
||||
],
|
||||
// Array
|
||||
[
|
||||
'suffix' => ['array'],
|
||||
'value' => 'array',
|
||||
],
|
||||
// 时间选择器-字段类型为int同时以['time', 'datetime']结尾
|
||||
[
|
||||
'type' => ['int'],
|
||||
'suffix' => ['time', 'datetime'],
|
||||
'value' => 'timestamp',
|
||||
],
|
||||
[
|
||||
'type' => ['datetime', 'timestamp'],
|
||||
'value' => 'datetime',
|
||||
],
|
||||
[
|
||||
'type' => ['date'],
|
||||
'value' => 'date',
|
||||
],
|
||||
[
|
||||
'type' => ['year'],
|
||||
'value' => 'year',
|
||||
],
|
||||
[
|
||||
'type' => ['time'],
|
||||
'value' => 'time',
|
||||
],
|
||||
// 单选select
|
||||
[
|
||||
'suffix' => ['select', 'list', 'data'],
|
||||
'value' => 'select',
|
||||
],
|
||||
// 多选select
|
||||
[
|
||||
'suffix' => ['selects', 'multi', 'lists'],
|
||||
'value' => 'selects',
|
||||
],
|
||||
// 远程select
|
||||
[
|
||||
'suffix' => ['_id'],
|
||||
'value' => 'remoteSelect',
|
||||
],
|
||||
// 远程selects
|
||||
[
|
||||
'suffix' => ['_ids'],
|
||||
'value' => 'remoteSelects',
|
||||
],
|
||||
// 城市选择器
|
||||
[
|
||||
'suffix' => ['city'],
|
||||
'value' => 'city',
|
||||
],
|
||||
// 单图上传
|
||||
[
|
||||
'suffix' => ['image', 'avatar'],
|
||||
'value' => 'image',
|
||||
],
|
||||
// 多图上传
|
||||
[
|
||||
'suffix' => ['images', 'avatars'],
|
||||
'value' => 'images',
|
||||
],
|
||||
// 文件上传
|
||||
[
|
||||
'suffix' => ['file'],
|
||||
'value' => 'file',
|
||||
],
|
||||
// 多文件上传
|
||||
[
|
||||
'suffix' => ['files'],
|
||||
'value' => 'files',
|
||||
],
|
||||
// icon选择器
|
||||
[
|
||||
'suffix' => ['icon'],
|
||||
'value' => 'icon',
|
||||
],
|
||||
// 单选框
|
||||
[
|
||||
'column_type' => ['tinyint(1)', 'char(1)', 'tinyint(1) unsigned'],
|
||||
'suffix' => ['status', 'state', 'type'],
|
||||
'value' => 'radio',
|
||||
],
|
||||
// 数字输入框
|
||||
[
|
||||
'suffix' => ['number', 'int', 'num'],
|
||||
'value' => 'number',
|
||||
],
|
||||
[
|
||||
'type' => ['bigint', 'int', 'mediumint', 'smallint', 'tinyint', 'decimal', 'double', 'float'],
|
||||
'value' => 'number',
|
||||
],
|
||||
// 富文本-低权重
|
||||
[
|
||||
'type' => ['longtext', 'text', 'mediumtext', 'smalltext', 'tinytext', 'bigtext'],
|
||||
'value' => 'textarea',
|
||||
],
|
||||
// 单选框-低权重
|
||||
[
|
||||
'type' => ['enum'],
|
||||
'value' => 'radio',
|
||||
],
|
||||
// 多选框
|
||||
[
|
||||
'type' => ['set'],
|
||||
'value' => 'checkbox',
|
||||
],
|
||||
// 颜色选择器
|
||||
[
|
||||
'suffix' => ['color'],
|
||||
'value' => 'color',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* 预设WEB端文件位置
|
||||
* @var array
|
||||
*/
|
||||
protected static array $parseWebDirPresets = [
|
||||
'lang' => [],
|
||||
'views' => [
|
||||
'user' => ['user', 'user'],
|
||||
'admin' => ['auth', 'admin'],
|
||||
'admin_group' => ['auth', 'group'],
|
||||
'attachment' => ['routine', 'attachment'],
|
||||
'admin_rule' => ['auth', 'rule'],
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* 添加时间字段
|
||||
* @var string
|
||||
*/
|
||||
protected static string $createTimeField = 'create_time';
|
||||
|
||||
/**
|
||||
* 更新时间字段
|
||||
* @var string
|
||||
*/
|
||||
protected static string $updateTimeField = 'update_time';
|
||||
|
||||
/**
|
||||
* 属性的类型对照表
|
||||
* @var array
|
||||
*/
|
||||
protected static array $attrType = [
|
||||
'controller' => [
|
||||
'preExcludeFields' => 'array|string',
|
||||
'quickSearchField' => 'string|array',
|
||||
'withJoinTable' => 'array',
|
||||
'defaultSortField' => 'string|array',
|
||||
'weighField' => 'string',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* 获取字段字典数据
|
||||
* @param array $dict 存储字典数据的变量
|
||||
* @param array $field 字段数据
|
||||
* @param string $lang 语言
|
||||
* @param string $translationPrefix 翻译前缀
|
||||
*/
|
||||
public static function getDictData(array &$dict, array $field, string $lang, string $translationPrefix = ''): array
|
||||
{
|
||||
if (!$field['comment']) return [];
|
||||
$comment = str_replace([',', ':'], [',', ':'], $field['comment']);
|
||||
if (stripos($comment, ':') !== false && stripos($comment, ',') && stripos($comment, '=') !== false) {
|
||||
[$fieldTitle, $item] = explode(':', $comment);
|
||||
$dict[$translationPrefix . $field['name']] = $lang == 'en' ? $field['name'] : $fieldTitle;
|
||||
foreach (explode(',', $item) as $v) {
|
||||
$valArr = explode('=', $v);
|
||||
if (count($valArr) == 2) {
|
||||
[$key, $value] = $valArr;
|
||||
$dict[$translationPrefix . $field['name'] . ' ' . $key] = $lang == 'en' ? $field['name'] . ' ' . $key : $value;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$dict[$translationPrefix . $field['name']] = $lang == 'en' ? $field['name'] : $comment;
|
||||
}
|
||||
return $dict;
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录CRUD状态
|
||||
* @param array $data CRUD记录数据
|
||||
* @return int 记录ID
|
||||
*/
|
||||
public static function recordCrudStatus(array $data): int
|
||||
{
|
||||
if (isset($data['id'])) {
|
||||
CrudLog::where('id', $data['id'])
|
||||
->update([
|
||||
'status' => $data['status'],
|
||||
]);
|
||||
return $data['id'];
|
||||
}
|
||||
|
||||
$connection = $data['table']['databaseConnection'] ?: config('database.default');
|
||||
$log = CrudLog::create([
|
||||
'table_name' => $data['table']['name'],
|
||||
'comment' => $data['table']['comment'],
|
||||
'table' => $data['table'],
|
||||
'fields' => $data['fields'],
|
||||
'connection' => $connection,
|
||||
'status' => $data['status'],
|
||||
]);
|
||||
return $log->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 Phinx 的字段类型数据
|
||||
* @param string $type 字段类型
|
||||
* @param array $field 字段数据
|
||||
* @return array
|
||||
*/
|
||||
public static function getPhinxFieldType(string $type, array $field): array
|
||||
{
|
||||
if ($type == 'tinyint') {
|
||||
if (
|
||||
(isset($field['dataType']) && $field['dataType'] == 'tinyint(1)') ||
|
||||
($field['default'] == '1' && $field['defaultType'] == 'INPUT')
|
||||
) {
|
||||
$type = 'boolean';
|
||||
}
|
||||
}
|
||||
$phinxFieldTypeMap = [
|
||||
// 数字
|
||||
'tinyint' => ['type' => AdapterInterface::PHINX_TYPE_INTEGER, 'limit' => MysqlAdapter::INT_TINY],
|
||||
'smallint' => ['type' => AdapterInterface::PHINX_TYPE_INTEGER, 'limit' => MysqlAdapter::INT_SMALL],
|
||||
'mediumint' => ['type' => AdapterInterface::PHINX_TYPE_INTEGER, 'limit' => MysqlAdapter::INT_MEDIUM],
|
||||
'int' => ['type' => AdapterInterface::PHINX_TYPE_INTEGER, 'limit' => null],
|
||||
'bigint' => ['type' => AdapterInterface::PHINX_TYPE_BIG_INTEGER, 'limit' => null],
|
||||
'boolean' => ['type' => AdapterInterface::PHINX_TYPE_BOOLEAN, 'limit' => null],
|
||||
// 文本
|
||||
'varchar' => ['type' => AdapterInterface::PHINX_TYPE_STRING, 'limit' => null],
|
||||
'tinytext' => ['type' => AdapterInterface::PHINX_TYPE_TEXT, 'limit' => MysqlAdapter::TEXT_TINY],
|
||||
'mediumtext' => ['type' => AdapterInterface::PHINX_TYPE_TEXT, 'limit' => MysqlAdapter::TEXT_MEDIUM],
|
||||
'longtext' => ['type' => AdapterInterface::PHINX_TYPE_TEXT, 'limit' => MysqlAdapter::TEXT_LONG],
|
||||
'tinyblob' => ['type' => AdapterInterface::PHINX_TYPE_BLOB, 'limit' => MysqlAdapter::BLOB_TINY],
|
||||
'mediumblob' => ['type' => AdapterInterface::PHINX_TYPE_BLOB, 'limit' => MysqlAdapter::BLOB_MEDIUM],
|
||||
'longblob' => ['type' => AdapterInterface::PHINX_TYPE_BLOB, 'limit' => MysqlAdapter::BLOB_LONG],
|
||||
];
|
||||
return array_key_exists($type, $phinxFieldTypeMap) ? $phinxFieldTypeMap[$type] : ['type' => $type, 'limit' => null];
|
||||
}
|
||||
|
||||
/**
|
||||
* 分析字段limit和精度
|
||||
* @param string $type 字段类型
|
||||
* @param array $field 字段数据
|
||||
* @return array ['limit' => 10, 'precision' => null, 'scale' => null]
|
||||
*/
|
||||
public static function analyseFieldLimit(string $type, array $field): array
|
||||
{
|
||||
$fieldType = [
|
||||
'decimal' => ['decimal', 'double', 'float'],
|
||||
'values' => ['enum', 'set'],
|
||||
];
|
||||
|
||||
$dataTypeLimit = self::dataTypeLimit($field['dataType'] ?? '');
|
||||
if (in_array($type, $fieldType['decimal'])) {
|
||||
if ($dataTypeLimit) {
|
||||
return ['precision' => $dataTypeLimit[0], 'scale' => $dataTypeLimit[1] ?? 0];
|
||||
}
|
||||
$scale = isset($field['precision']) ? intval($field['precision']) : 0;
|
||||
return ['precision' => $field['length'] ?? 10, 'scale' => $scale];
|
||||
} elseif (in_array($type, $fieldType['values'])) {
|
||||
foreach ($dataTypeLimit as &$item) {
|
||||
$item = str_replace(['"', "'"], '', $item);
|
||||
}
|
||||
return ['values' => $dataTypeLimit];
|
||||
} elseif ($dataTypeLimit && $dataTypeLimit[0]) {
|
||||
return ['limit' => $dataTypeLimit[0]];
|
||||
} elseif (isset($field['length'])) {
|
||||
return ['limit' => $field['length']];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
public static function dataTypeLimit(string $dataType): array
|
||||
{
|
||||
preg_match("/\((.*?)\)/", $dataType, $matches);
|
||||
if (isset($matches[1]) && $matches[1]) {
|
||||
return explode(',', trim($matches[1], ','));
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
public static function analyseFieldDefault(array $field): mixed
|
||||
{
|
||||
return match ($field['defaultType']) {
|
||||
'EMPTY STRING' => '',
|
||||
'NULL' => null,
|
||||
default => $field['default'],
|
||||
};
|
||||
}
|
||||
|
||||
public static function searchArray($fields, callable $myFunction): array|bool
|
||||
{
|
||||
foreach ($fields as $key => $field) {
|
||||
if (call_user_func($myFunction, $field, $key)) {
|
||||
return $field;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 Phinx 格式的字段数据
|
||||
* @param array $field
|
||||
* @return array
|
||||
*/
|
||||
public static function getPhinxFieldData(array $field): array
|
||||
{
|
||||
$conciseType = self::analyseFieldType($field);
|
||||
$phinxTypeData = self::getPhinxFieldType($conciseType, $field);
|
||||
|
||||
$phinxColumnOptions = self::analyseFieldLimit($conciseType, $field);
|
||||
if (!is_null($phinxTypeData['limit'])) {
|
||||
$phinxColumnOptions['limit'] = $phinxTypeData['limit'];
|
||||
}
|
||||
|
||||
// 无默认值字段
|
||||
$noDefaultValueFields = [
|
||||
'text', 'blob', 'geometry', 'geometrycollection', 'json', 'linestring', 'longblob', 'longtext', 'mediumblob',
|
||||
'mediumtext', 'multilinestring', 'multipoint', 'multipolygon', 'point', 'polygon', 'tinyblob',
|
||||
];
|
||||
if ($field['defaultType'] != 'NONE' && !in_array($conciseType, $noDefaultValueFields)) {
|
||||
$phinxColumnOptions['default'] = self::analyseFieldDefault($field);
|
||||
}
|
||||
|
||||
$phinxColumnOptions['null'] = (bool)$field['null'];
|
||||
$phinxColumnOptions['comment'] = $field['comment'];
|
||||
$phinxColumnOptions['signed'] = !$field['unsigned'];
|
||||
$phinxColumnOptions['identity'] = $field['autoIncrement'];
|
||||
return [
|
||||
'type' => $phinxTypeData['type'],
|
||||
'options' => $phinxColumnOptions,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 表字段排序
|
||||
* @param string $tableName 表名
|
||||
* @param array $fields 字段数据
|
||||
* @param array $designChange 前端字段改变数据
|
||||
* @param ?string $connection 数据库连接标识
|
||||
* @return void
|
||||
* @throws Throwable
|
||||
*/
|
||||
public static function updateFieldOrder(string $tableName, array $fields, array $designChange, ?string $connection = null): void
|
||||
{
|
||||
if ($designChange) {
|
||||
$table = TableManager::phinxTable($tableName, [], false, $connection);
|
||||
foreach ($designChange as $item) {
|
||||
if (!$item['sync']) continue;
|
||||
|
||||
if (!empty($item['after'])) {
|
||||
|
||||
$fieldName = in_array($item['type'], ['add-field', 'change-field-name']) ? $item['newName'] : $item['oldName'];
|
||||
|
||||
$field = self::searchArray($fields, function ($field) use ($fieldName) {
|
||||
return $field['name'] == $fieldName;
|
||||
});
|
||||
|
||||
$phinxFieldData = self::getPhinxFieldData($field);
|
||||
|
||||
// 字段顺序调整
|
||||
if ($item['after'] == 'FIRST FIELD') {
|
||||
$phinxFieldData['options']['after'] = MysqlAdapter::FIRST;
|
||||
} else {
|
||||
$phinxFieldData['options']['after'] = $item['after'];
|
||||
}
|
||||
$table->changeColumn($fieldName, $phinxFieldData['type'], $phinxFieldData['options']);
|
||||
}
|
||||
}
|
||||
$table->update();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 表设计处理
|
||||
* @param array $table 表数据
|
||||
* @param array $fields 字段数据
|
||||
* @return array
|
||||
* @throws Throwable
|
||||
*/
|
||||
public static function handleTableDesign(array $table, array $fields): array
|
||||
{
|
||||
$name = TableManager::tableName($table['name'], true, $table['databaseConnection']);
|
||||
$comment = $table['comment'] ?? '';
|
||||
$designChange = $table['designChange'] ?? [];
|
||||
$adapter = TableManager::phinxAdapter(false, $table['databaseConnection']);
|
||||
|
||||
$pk = self::searchArray($fields, function ($item) {
|
||||
return $item['primaryKey'];
|
||||
});
|
||||
$pk = $pk ? $pk['name'] : '';
|
||||
|
||||
if ($adapter->hasTable($name)) {
|
||||
// 更新表
|
||||
if ($designChange) {
|
||||
$tableManager = TableManager::phinxTable($name, [], false, $table['databaseConnection']);
|
||||
$tableManager->changeComment($comment)->update();
|
||||
|
||||
// 改名和删除操作优先
|
||||
$priorityOpt = false;
|
||||
foreach ($designChange as $item) {
|
||||
|
||||
if (!$item['sync']) continue;
|
||||
|
||||
if (in_array($item['type'], ['change-field-name', 'del-field']) && !$tableManager->hasColumn($item['oldName'])) {
|
||||
// 字段不存在
|
||||
throw new BaException(__($item['type'] . ' fail not exist', [$item['oldName']]));
|
||||
}
|
||||
|
||||
if ($item['type'] == 'change-field-name') {
|
||||
$priorityOpt = true;
|
||||
$tableManager->renameColumn($item['oldName'], $item['newName']);
|
||||
} elseif ($item['type'] == 'del-field') {
|
||||
$priorityOpt = true;
|
||||
$tableManager->removeColumn($item['oldName']);
|
||||
}
|
||||
}
|
||||
|
||||
// 保存需要优先执行的操作,避免先改名再改属性时找不到字段
|
||||
if ($priorityOpt) {
|
||||
$tableManager->update();
|
||||
}
|
||||
|
||||
// 修改字段属性和添加字段操作
|
||||
foreach ($designChange as $item) {
|
||||
|
||||
if (!$item['sync']) continue;
|
||||
|
||||
if ($item['type'] == 'change-field-attr') {
|
||||
|
||||
if (!$tableManager->hasColumn($item['oldName'])) {
|
||||
// 字段不存在
|
||||
throw new BaException(__($item['type'] . ' fail not exist', [$item['oldName']]));
|
||||
}
|
||||
|
||||
$phinxFieldData = self::getPhinxFieldData(self::searchArray($fields, function ($field) use ($item) {
|
||||
return $field['name'] == $item['oldName'];
|
||||
}));
|
||||
$tableManager->changeColumn($item['oldName'], $phinxFieldData['type'], $phinxFieldData['options']);
|
||||
} elseif ($item['type'] == 'add-field') {
|
||||
|
||||
if ($tableManager->hasColumn($item['newName'])) {
|
||||
// 字段已经存在
|
||||
throw new BaException(__($item['type'] . ' fail exist', [$item['newName']]));
|
||||
}
|
||||
|
||||
$phinxFieldData = self::getPhinxFieldData(self::searchArray($fields, function ($field) use ($item) {
|
||||
return $field['name'] == $item['newName'];
|
||||
}));
|
||||
$tableManager->addColumn($item['newName'], $phinxFieldData['type'], $phinxFieldData['options']);
|
||||
}
|
||||
}
|
||||
$tableManager->update();
|
||||
|
||||
// 表更新结构完成再处理字段排序
|
||||
self::updateFieldOrder($name, $fields, $designChange, $table['databaseConnection']);
|
||||
}
|
||||
} else {
|
||||
// 创建表
|
||||
$tableManager = TableManager::phinxTable($name, [
|
||||
'id' => false,
|
||||
'comment' => $comment,
|
||||
'row_format' => 'DYNAMIC',
|
||||
'primary_key' => $pk,
|
||||
'collation' => 'utf8mb4_unicode_ci',
|
||||
], false, $table['databaseConnection']);
|
||||
foreach ($fields as $field) {
|
||||
$phinxFieldData = self::getPhinxFieldData($field);
|
||||
$tableManager->addColumn($field['name'], $phinxFieldData['type'], $phinxFieldData['options']);
|
||||
}
|
||||
$tableManager->create();
|
||||
}
|
||||
|
||||
return [$pk];
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析文件数据
|
||||
* @throws Throwable
|
||||
*/
|
||||
public static function parseNameData($app, $table, $type, $value = ''): array
|
||||
{
|
||||
$pathArr = [];
|
||||
if ($value) {
|
||||
$value = str_replace('.php', '', $value);
|
||||
$value = str_replace(['.', '/', '\\', '_'], '/', $value);
|
||||
$pathArrTemp = explode('/', $value);
|
||||
$redundantDir = [
|
||||
'app' => 0,
|
||||
$app => 1,
|
||||
$type => 2,
|
||||
];
|
||||
foreach ($pathArrTemp as $key => $item) {
|
||||
if (!array_key_exists($item, $redundantDir) || $key !== $redundantDir[$item]) {
|
||||
$pathArr[] = $item;
|
||||
}
|
||||
}
|
||||
} elseif (isset(self::$parseNamePresets[$type]) && array_key_exists($table, self::$parseNamePresets[$type])) {
|
||||
$pathArr = self::$parseNamePresets[$type][$table];
|
||||
} else {
|
||||
$table = str_replace(['.', '/', '\\', '_'], '/', $table);
|
||||
$pathArr = explode('/', $table);
|
||||
}
|
||||
$originalLastName = array_pop($pathArr);
|
||||
$pathArr = array_map('strtolower', $pathArr);
|
||||
$lastName = ucfirst($originalLastName);
|
||||
|
||||
// 类名不能为内部关键字
|
||||
if (in_array(strtolower($originalLastName), self::$reservedKeywords)) {
|
||||
throw new Exception('Unable to use internal variable:' . $lastName);
|
||||
}
|
||||
|
||||
$appDir = app()->getBasePath() . $app . DIRECTORY_SEPARATOR;
|
||||
$namespace = "app\\$app\\$type" . ($pathArr ? '\\' . implode('\\', $pathArr) : '');
|
||||
$parseFile = $appDir . $type . DIRECTORY_SEPARATOR . ($pathArr ? implode(DIRECTORY_SEPARATOR, $pathArr) . DIRECTORY_SEPARATOR : '') . $lastName . '.php';
|
||||
$rootFileName = $namespace . "/$lastName" . '.php';
|
||||
|
||||
return [
|
||||
'lastName' => $lastName,
|
||||
'originalLastName' => $originalLastName,
|
||||
'path' => $pathArr,
|
||||
'namespace' => $namespace,
|
||||
'parseFile' => Filesystem::fsFit($parseFile),
|
||||
'rootFileName' => Filesystem::fsFit($rootFileName),
|
||||
];
|
||||
}
|
||||
|
||||
public static function parseWebDirNameData($table, $type, $value = ''): array
|
||||
{
|
||||
$pathArr = [];
|
||||
if ($value) {
|
||||
$value = str_replace(['.', '/', '\\', '_'], '/', $value);
|
||||
$pathArrTemp = explode('/', $value);
|
||||
$redundantDir = [
|
||||
'web' => 0,
|
||||
'src' => 1,
|
||||
'views' => 2,
|
||||
'lang' => 2,
|
||||
'backend' => 3,
|
||||
'pages' => 3,
|
||||
'en' => 4,
|
||||
'zh-cn' => 4,
|
||||
];
|
||||
foreach ($pathArrTemp as $key => $item) {
|
||||
if (!array_key_exists($item, $redundantDir) || $key !== $redundantDir[$item]) {
|
||||
$pathArr[] = $item;
|
||||
}
|
||||
}
|
||||
} elseif (array_key_exists($table, self::$parseWebDirPresets[$type])) {
|
||||
$pathArr = self::$parseWebDirPresets[$type][$table];
|
||||
} else {
|
||||
$table = str_replace(['.', '/', '\\', '_'], '/', $table);
|
||||
$pathArr = explode('/', $table);
|
||||
}
|
||||
$originalLastName = array_pop($pathArr);
|
||||
$pathArr = array_map('strtolower', $pathArr);
|
||||
$lastName = lcfirst($originalLastName);
|
||||
|
||||
$webDir['path'] = $pathArr;
|
||||
$webDir['lastName'] = $lastName;
|
||||
$webDir['originalLastName'] = $originalLastName;
|
||||
if ($type == 'views') {
|
||||
$webDir['views'] = "web/src/views/backend" . ($pathArr ? '/' . implode('/', $pathArr) : '') . "/$lastName";
|
||||
} elseif ($type == 'lang') {
|
||||
$webDir['lang'] = array_merge($pathArr, [$lastName]);
|
||||
$langDir = ['en', 'zh-cn'];
|
||||
foreach ($langDir as $item) {
|
||||
$webDir[$item] = "web/src/lang/backend/$item" . ($pathArr ? '/' . implode('/', $pathArr) : '') . "/$lastName";
|
||||
}
|
||||
}
|
||||
foreach ($webDir as &$item) {
|
||||
if (is_string($item)) $item = Filesystem::fsFit($item);
|
||||
}
|
||||
return $webDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取菜单name、path
|
||||
* @param array $webDir
|
||||
* @return string
|
||||
*/
|
||||
public static function getMenuName(array $webDir): string
|
||||
{
|
||||
return ($webDir['path'] ? implode('/', $webDir['path']) . '/' : '') . $webDir['originalLastName'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取基础模板文件路径
|
||||
* @param string $name
|
||||
* @return string
|
||||
*/
|
||||
public static function getStubFilePath(string $name): string
|
||||
{
|
||||
return app_path() . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . 'crud' . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . Filesystem::fsFit($name) . '.stub';
|
||||
}
|
||||
|
||||
/**
|
||||
* 多维数组转字符串
|
||||
*/
|
||||
public static function arrayToString(array|string $value): string
|
||||
{
|
||||
if (!is_array($value)) {
|
||||
return $value;
|
||||
}
|
||||
foreach ($value as &$item) {
|
||||
$item = self::arrayToString($item);
|
||||
}
|
||||
return implode(PHP_EOL, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 组装模板
|
||||
* @param string $name
|
||||
* @param array $data
|
||||
* @param bool $escape
|
||||
* @return string
|
||||
*/
|
||||
public static function assembleStub(string $name, array $data, bool $escape = false): string
|
||||
{
|
||||
foreach ($data as &$datum) {
|
||||
$datum = self::arrayToString($datum);
|
||||
}
|
||||
$search = $replace = [];
|
||||
foreach ($data as $k => $v) {
|
||||
$search[] = "{%$k%}";
|
||||
$replace[] = $v;
|
||||
}
|
||||
$stubPath = self::getStubFilePath($name);
|
||||
$stubContent = file_get_contents($stubPath);
|
||||
$content = str_replace($search, $replace, $stubContent);
|
||||
return $escape ? self::escape($content) : $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取转义编码后的值
|
||||
* @param array|string $value
|
||||
* @return string
|
||||
*/
|
||||
public static function escape(array|string $value): string
|
||||
{
|
||||
if (is_array($value)) {
|
||||
$value = json_encode($value, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', false);
|
||||
}
|
||||
|
||||
public static function tab(int $num = 1): string
|
||||
{
|
||||
return str_pad('', 4 * $num);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据数据表解析字段数据
|
||||
* @throws Throwable
|
||||
*/
|
||||
public static function parseTableColumns(string $table, bool $analyseField = false, ?string $connection = null): array
|
||||
{
|
||||
$connection = TableManager::getConnection($connection);
|
||||
$connectionConfig = TableManager::getConnectionConfig($connection);
|
||||
|
||||
// 从数据库中获取表字段信息
|
||||
$sql = 'SELECT * FROM `information_schema`.`columns` '
|
||||
. 'WHERE TABLE_SCHEMA = ? AND table_name = ? '
|
||||
. 'ORDER BY ORDINAL_POSITION';
|
||||
|
||||
$columns = [];
|
||||
$tableColumn = Db::connect($connection)->query($sql, [$connectionConfig['database'], TableManager::tableName($table, true, $connection)]);
|
||||
|
||||
foreach ($tableColumn as $item) {
|
||||
$isNullAble = $item['IS_NULLABLE'] == 'YES';
|
||||
if (str_contains($item['COLUMN_TYPE'], '(')) {
|
||||
$dataType = substr_replace($item['COLUMN_TYPE'], '', stripos($item['COLUMN_TYPE'], ')') + 1);
|
||||
} else {
|
||||
$dataType = str_replace(' unsigned', '', $item['COLUMN_TYPE']);
|
||||
}
|
||||
|
||||
// 默认值和默认值类型分析
|
||||
$default = '';
|
||||
if ($isNullAble && is_null($item['COLUMN_DEFAULT'])) {
|
||||
$defaultType = 'NULL';
|
||||
} elseif ($item['COLUMN_DEFAULT'] == '' && in_array($item['DATA_TYPE'], ['varchar', 'char'])) {
|
||||
$defaultType = 'EMPTY STRING';
|
||||
} elseif (!$isNullAble && is_null($item['COLUMN_DEFAULT'])) {
|
||||
$defaultType = 'NONE';
|
||||
} else {
|
||||
$defaultType = 'INPUT';
|
||||
$default = $item['COLUMN_DEFAULT'];
|
||||
}
|
||||
|
||||
$column = [
|
||||
'name' => $item['COLUMN_NAME'],
|
||||
'type' => $item['DATA_TYPE'],
|
||||
'dataType' => $dataType,
|
||||
'default' => $default,
|
||||
'defaultType' => $defaultType,
|
||||
'null' => $isNullAble,
|
||||
'primaryKey' => $item['COLUMN_KEY'] == 'PRI',
|
||||
'unsigned' => (bool)stripos($item['COLUMN_TYPE'], 'unsigned'),
|
||||
'autoIncrement' => stripos($item['EXTRA'], 'auto_increment') !== false,
|
||||
'comment' => $item['COLUMN_COMMENT'],
|
||||
'designType' => self::getTableColumnsDataType($item),
|
||||
'table' => [],
|
||||
'form' => [],
|
||||
];
|
||||
if ($analyseField) {
|
||||
self::analyseField($column);
|
||||
} else {
|
||||
self::handleTableColumn($column);
|
||||
}
|
||||
$columns[$item['COLUMN_NAME']] = $column;
|
||||
}
|
||||
return $columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析到的表字段的额外处理
|
||||
*/
|
||||
public static function handleTableColumn(&$column): void
|
||||
{
|
||||
// 预留
|
||||
}
|
||||
|
||||
/**
|
||||
* 分析字段数据类型
|
||||
* @param array $field 字段数据
|
||||
* @return string 字段类型
|
||||
*/
|
||||
public static function analyseFieldType(array $field): string
|
||||
{
|
||||
$dataType = (isset($field['dataType']) && $field['dataType']) ? $field['dataType'] : $field['type'];
|
||||
if (stripos($dataType, '(') !== false) {
|
||||
$typeName = explode('(', $dataType);
|
||||
return trim($typeName[0]);
|
||||
}
|
||||
return trim($dataType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分析字段的完整数据类型定义
|
||||
* @param array $field 字段数据
|
||||
* @return string
|
||||
*/
|
||||
public static function analyseFieldDataType(array $field): string
|
||||
{
|
||||
if (!empty($field['dataType'])) return $field['dataType'];
|
||||
|
||||
$conciseType = self::analyseFieldType($field);
|
||||
$limit = self::analyseFieldLimit($conciseType, $field);
|
||||
|
||||
if (isset($limit['precision'])) {
|
||||
$dataType = "$conciseType({$limit['precision']}, {$limit['scale']})";
|
||||
} elseif (isset($limit['values'])) {
|
||||
$values = implode(',', $limit['values']);
|
||||
$dataType = "$conciseType($values)";
|
||||
} else {
|
||||
$dataType = "$conciseType({$limit['limit']})";
|
||||
}
|
||||
return $dataType;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分析字段
|
||||
*/
|
||||
public static function analyseField(&$field): void
|
||||
{
|
||||
$field['type'] = self::analyseFieldType($field);
|
||||
$field['originalDesignType'] = $field['designType'];
|
||||
|
||||
// 表单项类型转换对照表
|
||||
$designTypeComparison = [
|
||||
'pk' => 'string',
|
||||
'weigh' => 'number',
|
||||
'timestamp' => 'datetime',
|
||||
'float' => 'number',
|
||||
];
|
||||
if (array_key_exists($field['designType'], $designTypeComparison)) {
|
||||
$field['designType'] = $designTypeComparison[$field['designType']];
|
||||
}
|
||||
|
||||
// 是否开启了多选
|
||||
$supportMultipleComparison = ['select', 'image', 'file', 'remoteSelect'];
|
||||
if (in_array($field['designType'], $supportMultipleComparison)) {
|
||||
$multiKey = $field['designType'] == 'remoteSelect' ? 'select-multi' : $field['designType'] . '-multi';
|
||||
if (isset($field['form'][$multiKey]) && $field['form'][$multiKey]) {
|
||||
$field['designType'] = $field['designType'] . 's';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function getTableColumnsDataType($column)
|
||||
{
|
||||
if (stripos($column['COLUMN_NAME'], 'id') !== false && stripos($column['EXTRA'], 'auto_increment') !== false) {
|
||||
return 'pk';
|
||||
} elseif ($column['COLUMN_NAME'] == 'weigh') {
|
||||
return 'weigh';
|
||||
} elseif (in_array($column['COLUMN_NAME'], ['createtime', 'updatetime', 'create_time', 'update_time'])) {
|
||||
return 'timestamp';
|
||||
}
|
||||
foreach (self::$inputTypeRule as $item) {
|
||||
$typeBool = true;
|
||||
$suffixBool = true;
|
||||
$columnTypeBool = true;
|
||||
if (isset($item['type']) && $item['type'] && !in_array($column['DATA_TYPE'], $item['type'])) {
|
||||
$typeBool = false;
|
||||
}
|
||||
if (isset($item['suffix']) && $item['suffix']) {
|
||||
$suffixBool = self::isMatchSuffix($column['COLUMN_NAME'], $item['suffix']);
|
||||
}
|
||||
if (isset($item['column_type']) && $item['column_type'] && !in_array($column['COLUMN_TYPE'], $item['column_type'])) {
|
||||
$columnTypeBool = false;
|
||||
}
|
||||
if ($typeBool && $suffixBool && $columnTypeBool) {
|
||||
return $item['value'];
|
||||
}
|
||||
}
|
||||
return 'string';
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否符合指定后缀
|
||||
*
|
||||
* @param string $field 字段名称
|
||||
* @param string|array $suffixArr 后缀
|
||||
* @return bool
|
||||
*/
|
||||
protected static function isMatchSuffix(string $field, string|array $suffixArr): bool
|
||||
{
|
||||
$suffixArr = is_array($suffixArr) ? $suffixArr : explode(',', $suffixArr);
|
||||
foreach ($suffixArr as $v) {
|
||||
if (preg_match("/$v$/i", $field)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建菜单
|
||||
* @throws Throwable
|
||||
*/
|
||||
public static function createMenu($webViewsDir, $tableComment): void
|
||||
{
|
||||
$menuName = self::getMenuName($webViewsDir);
|
||||
if (AdminRule::where('name', $menuName)->value('id')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 组装权限节点数据
|
||||
$menuChildren = self::$menuChildren;
|
||||
foreach ($menuChildren as &$item) {
|
||||
$item['name'] = $menuName . $item['name'];
|
||||
}
|
||||
|
||||
// 组件路径
|
||||
$componentPath = str_replace(['\\', 'web/src'], ['/', '/src'], $webViewsDir['views'] . '/' . 'index.vue');
|
||||
|
||||
// 菜单数组
|
||||
$menus = [
|
||||
'type' => 'menu',
|
||||
'title' => $tableComment ?: $webViewsDir['originalLastName'],
|
||||
'name' => $menuName,
|
||||
'path' => $menuName,
|
||||
'menu_type' => 'tab',
|
||||
'keepalive' => 1,
|
||||
'component' => $componentPath,
|
||||
'children' => $menuChildren,
|
||||
];
|
||||
$paths = array_reverse($webViewsDir['path']);
|
||||
foreach ($paths as $path) {
|
||||
$menus = [
|
||||
'type' => 'menu_dir',
|
||||
'title' => $path,
|
||||
'name' => $path,
|
||||
'path' => $path,
|
||||
'children' => [$menus],
|
||||
];
|
||||
}
|
||||
|
||||
// 创建菜单
|
||||
Menu::create([$menus], 0, 'ignore');
|
||||
}
|
||||
|
||||
public static function writeWebLangFile($langData, $webLangDir): void
|
||||
{
|
||||
foreach ($langData as $lang => $langDatum) {
|
||||
$langTsContent = '';
|
||||
foreach ($langDatum as $key => $item) {
|
||||
$quote = self::getQuote($item);
|
||||
$keyStr = self::formatObjectKey($key);
|
||||
$langTsContent .= self::tab() . $keyStr . ": $quote$item$quote,\n";
|
||||
}
|
||||
$langTsContent = "export default {\n" . $langTsContent . "}\n";
|
||||
self::writeFile(root_path() . $webLangDir[$lang] . '.ts', $langTsContent);
|
||||
}
|
||||
}
|
||||
|
||||
public static function writeFile($path, $content): bool|int
|
||||
{
|
||||
$path = Filesystem::fsFit($path);
|
||||
if (!is_dir(dirname($path))) {
|
||||
mkdir(dirname($path), 0755, true);
|
||||
}
|
||||
return file_put_contents($path, $content);
|
||||
}
|
||||
|
||||
public static function buildModelAppend($append): string
|
||||
{
|
||||
if (!$append) return '';
|
||||
$append = self::buildFormatSimpleArray($append);
|
||||
return "\n" . self::tab() . "// 追加属性" . "\n" . self::tab() . "protected \$append = $append;\n";
|
||||
}
|
||||
|
||||
public static function buildModelFieldType(array $fieldType): string
|
||||
{
|
||||
if (!$fieldType) return '';
|
||||
$maxStrLang = 0;
|
||||
foreach ($fieldType as $key => $item) {
|
||||
$strLang = strlen($key);
|
||||
$maxStrLang = max($strLang, $maxStrLang);
|
||||
}
|
||||
|
||||
$str = '';
|
||||
foreach ($fieldType as $key => $item) {
|
||||
$str .= self::tab(2) . "'$key'" . str_pad('=>', ($maxStrLang - strlen($key) + 3), ' ', STR_PAD_LEFT) . " '$item',\n";
|
||||
}
|
||||
return "\n" . self::tab() . "// 字段类型转换" . "\n" . self::tab() . "protected \$type = [\n" . rtrim($str, "\n") . "\n" . self::tab() . "];\n";
|
||||
}
|
||||
|
||||
public static function writeModelFile(string $tablePk, array $fieldsMap, array $modelData, array $modelFile): void
|
||||
{
|
||||
if ($modelData['connection'] && $modelData['connection'] != config('database.default')) {
|
||||
$modelData['connection'] = "\n" . self::tab() . "// 数据库连接配置标识\n" . self::tab() . 'protected $connection = ' . "'{$modelData['connection']}';\n";
|
||||
} else {
|
||||
$modelData['connection'] = '';
|
||||
}
|
||||
|
||||
$modelData['pk'] = $tablePk == 'id' ? '' : "\n" . self::tab() . "// 表主键\n" . self::tab() . 'protected $pk = ' . "'$tablePk';\n";
|
||||
$modelData['autoWriteTimestamp'] = array_key_exists(self::$createTimeField, $fieldsMap) || array_key_exists(self::$updateTimeField, $fieldsMap) ? 'true' : 'false';
|
||||
if ($modelData['autoWriteTimestamp'] == 'true') {
|
||||
$modelData['createTime'] = array_key_exists(self::$createTimeField, $fieldsMap) ? '' : "\n" . self::tab() . "protected \$createTime = false;";
|
||||
$modelData['updateTime'] = array_key_exists(self::$updateTimeField, $fieldsMap) ? '' : "\n" . self::tab() . "protected \$updateTime = false;";
|
||||
}
|
||||
$modelMethodList = isset($modelData['relationMethodList']) ? array_merge($modelData['methods'], $modelData['relationMethodList']) : $modelData['methods'];
|
||||
$modelData['methods'] = $modelMethodList ? "\n" . implode("\n", $modelMethodList) : '';
|
||||
$modelData['append'] = self::buildModelAppend($modelData['append']);
|
||||
$modelData['fieldType'] = self::buildModelFieldType($modelData['fieldType']);
|
||||
|
||||
// 生成雪花ID?
|
||||
if (isset($modelData['beforeInsertMixins']['snowflake'])) {
|
||||
// beforeInsert 组装
|
||||
$modelData['beforeInsert'] = Helper::assembleStub('mixins/model/beforeInsert', [
|
||||
'setSnowFlakeIdCode' => $modelData['beforeInsertMixins']['snowflake']
|
||||
]);
|
||||
}
|
||||
if ($modelData['afterInsert'] && $modelData['beforeInsert']) {
|
||||
$modelData['afterInsert'] = "\n" . $modelData['afterInsert'];
|
||||
}
|
||||
|
||||
$modelFileContent = self::assembleStub('mixins/model/model', $modelData);
|
||||
self::writeFile($modelFile['parseFile'], $modelFileContent);
|
||||
}
|
||||
|
||||
public static function writeControllerFile(array $controllerData, array $controllerFile): void
|
||||
{
|
||||
if (isset($controllerData['relationVisibleFieldList']) && $controllerData['relationVisibleFieldList']) {
|
||||
$relationVisibleFields = '->visible([';
|
||||
foreach ($controllerData['relationVisibleFieldList'] as $cKey => $controllerDatum) {
|
||||
$relationVisibleFields .= "'$cKey' => ['" . implode("', '", $controllerDatum) . "'], ";
|
||||
}
|
||||
$relationVisibleFields = rtrim($relationVisibleFields, ', ');
|
||||
$relationVisibleFields .= '])';
|
||||
// 重写index
|
||||
$controllerData['methods']['index'] = self::assembleStub('mixins/controller/index', [
|
||||
'relationVisibleFields' => $relationVisibleFields
|
||||
]);
|
||||
$controllerData['use']['Throwable'] = "\nuse Throwable;";
|
||||
unset($controllerData['relationVisibleFieldList']);
|
||||
}
|
||||
$controllerAttr = '';
|
||||
foreach ($controllerData['attr'] as $key => $item) {
|
||||
$attrType = '';
|
||||
if (array_key_exists($key, self::$attrType['controller'])) {
|
||||
$attrType = self::$attrType['controller'][$key];
|
||||
}
|
||||
if (is_array($item)) {
|
||||
$controllerAttr .= "\n" . self::tab() . "protected $attrType \$$key = ['" . implode("', '", $item) . "'];\n";
|
||||
} elseif ($item) {
|
||||
$controllerAttr .= "\n" . self::tab() . "protected $attrType \$$key = '$item';\n";
|
||||
}
|
||||
}
|
||||
$controllerData['attr'] = $controllerAttr;
|
||||
$controllerData['initialize'] = self::assembleStub('mixins/controller/initialize', [
|
||||
'modelNamespace' => $controllerData['modelNamespace'],
|
||||
'modelName' => $controllerData['modelName'],
|
||||
'filterRule' => $controllerData['filterRule'],
|
||||
]);
|
||||
$contentFileContent = self::assembleStub('mixins/controller/controller', $controllerData);
|
||||
self::writeFile($controllerFile['parseFile'], $contentFileContent);
|
||||
}
|
||||
|
||||
public static function writeFormFile($formVueData, $webViewsDir, $fields, $webTranslate): void
|
||||
{
|
||||
$fieldHtml = "\n";
|
||||
$formVueData['bigDialog'] = $formVueData['bigDialog'] ? "\n" . self::tab(2) . 'width="70%"' : '';
|
||||
foreach ($formVueData['formFields'] as $field) {
|
||||
$fieldHtml .= self::tab(5) . "<FormItem";
|
||||
foreach ($field as $key => $attr) {
|
||||
if (is_array($attr)) {
|
||||
$fieldHtml .= ' ' . $key . '="' . self::getJsonFromArray($attr) . '"';
|
||||
} else {
|
||||
$fieldHtml .= ' ' . $key . '="' . $attr . '"';
|
||||
}
|
||||
}
|
||||
$fieldHtml .= " />\n";
|
||||
}
|
||||
$formVueData['formFields'] = rtrim($fieldHtml, "\n");
|
||||
|
||||
// 表单验证规则
|
||||
foreach ($fields as $field) {
|
||||
if (isset($field['form']['validator'])) {
|
||||
foreach ($field['form']['validator'] as $item) {
|
||||
$message = '';
|
||||
if (isset($field['form']['validatorMsg']) && $field['form']['validatorMsg']) {
|
||||
$message = ", message: '{$field['form']['validatorMsg']}'";
|
||||
}
|
||||
$formVueData['formValidatorRules'][$field['name']][] = "buildValidatorData({ name: '$item', title: t('$webTranslate{$field['name']}')$message })";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($formVueData['formValidatorRules']) {
|
||||
$formVueData['imports'][] = "import { buildValidatorData } from '/@/utils/validate'";
|
||||
}
|
||||
|
||||
$formVueData['importExpand'] = self::buildImportExpand($formVueData['imports']);
|
||||
$formVueData['formItemRules'] = self::buildFormValidatorRules($formVueData['formValidatorRules']);
|
||||
$formVueContent = self::assembleStub('html/form', $formVueData);
|
||||
self::writeFile(root_path() . $webViewsDir['views'] . '/' . 'popupForm.vue', $formVueContent);
|
||||
}
|
||||
|
||||
public static function buildImportExpand(array $imports): string
|
||||
{
|
||||
$importExpand = '';
|
||||
foreach ($imports as $import) {
|
||||
$importExpand .= "\n$import";
|
||||
}
|
||||
return $importExpand;
|
||||
}
|
||||
|
||||
public static function buildFormValidatorRules(array $formValidatorRules): string
|
||||
{
|
||||
$rulesHtml = "";
|
||||
foreach ($formValidatorRules as $key => $formItemRule) {
|
||||
$rulesArrHtml = '';
|
||||
foreach ($formItemRule as $item) {
|
||||
$rulesArrHtml .= $item . ', ';
|
||||
}
|
||||
$rulesHtml .= self::tab() . $key . ': [' . rtrim($rulesArrHtml, ', ') . "],\n";
|
||||
}
|
||||
return $rulesHtml ? "\n" . $rulesHtml : '';
|
||||
}
|
||||
|
||||
public static function writeIndexFile($indexVueData, $webViewsDir, $controllerFile): void
|
||||
{
|
||||
$indexVueData['optButtons'] = self::buildSimpleArray($indexVueData['optButtons']);
|
||||
$indexVueData['defaultItems'] = self::getJsonFromArray($indexVueData['defaultItems']);
|
||||
$indexVueData['tableColumn'] = self::buildTableColumn($indexVueData['tableColumn']);
|
||||
$indexVueData['dblClickNotEditColumn'] = self::buildSimpleArray($indexVueData['dblClickNotEditColumn']);
|
||||
$controllerFile['path'][] = $controllerFile['originalLastName'];
|
||||
$indexVueData['controllerUrl'] = '\'/admin/' . ($controllerFile['path'] ? implode('.', $controllerFile['path']) : '') . '/\'';
|
||||
$indexVueData['componentName'] = ($webViewsDir['path'] ? implode('/', $webViewsDir['path']) . '/' : '') . $webViewsDir['originalLastName'];
|
||||
$indexVueContent = self::assembleStub('html/index', $indexVueData);
|
||||
self::writeFile(root_path() . $webViewsDir['views'] . '/' . 'index.vue', $indexVueContent);
|
||||
}
|
||||
|
||||
public static function buildTableColumn($tableColumnList): string
|
||||
{
|
||||
$columnJson = '';
|
||||
$emptyUnset = ['comSearchInputAttr', 'replaceValue', 'custom'];
|
||||
foreach ($tableColumnList as $column) {
|
||||
|
||||
foreach ($emptyUnset as $unsetKey) {
|
||||
if (empty($column[$unsetKey])) {
|
||||
unset($column[$unsetKey]);
|
||||
}
|
||||
}
|
||||
|
||||
$columnJson .= self::tab(3) . '{';
|
||||
foreach ($column as $key => $item) {
|
||||
$columnJson .= self::buildTableColumnKey($key, $item);
|
||||
}
|
||||
$columnJson = rtrim($columnJson, ',');
|
||||
$columnJson .= ' }' . ",\n";
|
||||
}
|
||||
return rtrim($columnJson, "\n");
|
||||
}
|
||||
|
||||
public static function buildTableColumnKey($key, $item): string
|
||||
{
|
||||
$key = self::formatObjectKey($key);
|
||||
if (is_array($item)) {
|
||||
$itemJson = ' ' . $key . ': {';
|
||||
foreach ($item as $ik => $iItem) {
|
||||
$itemJson .= self::buildTableColumnKey($ik, $iItem);
|
||||
}
|
||||
$itemJson = rtrim($itemJson, ',');
|
||||
$itemJson .= ' },';
|
||||
} elseif ($item === 'false' || $item === 'true') {
|
||||
$itemJson = ' ' . $key . ': ' . $item . ',';
|
||||
} elseif (in_array($key, ['label', 'width', 'buttons'], true) || str_starts_with($item, "t('") || str_starts_with($item, "t(\"")) {
|
||||
$itemJson = ' ' . $key . ': ' . $item . ',';
|
||||
} else {
|
||||
$itemJson = ' ' . $key . ': \'' . $item . '\',';
|
||||
}
|
||||
return $itemJson;
|
||||
}
|
||||
|
||||
public static function formatObjectKey(string $keyName): string
|
||||
{
|
||||
if (preg_match("/^[a-zA-Z_][a-zA-Z0-9_]+$/", $keyName)) {
|
||||
return $keyName;
|
||||
} else {
|
||||
$quote = self::getQuote($keyName);
|
||||
return "$quote$keyName$quote";
|
||||
}
|
||||
}
|
||||
|
||||
public static function getQuote(string $value): string
|
||||
{
|
||||
return stripos($value, "'") === false ? "'" : '"';
|
||||
}
|
||||
|
||||
public static function buildFormatSimpleArray($arr, int $tab = 2): string
|
||||
{
|
||||
if (!$arr) return '[]';
|
||||
$str = '[' . PHP_EOL;
|
||||
foreach ($arr as $item) {
|
||||
if ($item == 'undefined' || $item == 'false' || is_numeric($item)) {
|
||||
$str .= self::tab($tab) . $item . ',' . PHP_EOL;
|
||||
} else {
|
||||
$quote = self::getQuote($item);
|
||||
$str .= self::tab($tab) . "$quote$item$quote," . PHP_EOL;
|
||||
}
|
||||
}
|
||||
return $str . self::tab($tab - 1) . ']';
|
||||
}
|
||||
|
||||
public static function buildSimpleArray($arr): string
|
||||
{
|
||||
if (!$arr) return '[]';
|
||||
$str = '';
|
||||
foreach ($arr as $item) {
|
||||
if ($item == 'undefined' || $item == 'false' || is_numeric($item)) {
|
||||
$str .= $item . ', ';
|
||||
} else {
|
||||
$quote = self::getQuote($item);
|
||||
$str .= "$quote$item$quote, ";
|
||||
}
|
||||
}
|
||||
return '[' . rtrim($str, ", ") . ']';
|
||||
}
|
||||
|
||||
public static function buildDefaultOrder(string $field, string $type): string
|
||||
{
|
||||
if ($field && $type) {
|
||||
$defaultOrderStub = [
|
||||
'prop' => $field,
|
||||
'order' => $type,
|
||||
];
|
||||
$defaultOrderStub = self::getJsonFromArray($defaultOrderStub);
|
||||
if ($defaultOrderStub) {
|
||||
return "\n" . self::tab(2) . "defaultOrder: " . $defaultOrderStub . ',';
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
public static function getJsonFromArray($arr)
|
||||
{
|
||||
if (is_array($arr)) {
|
||||
$jsonStr = '';
|
||||
foreach ($arr as $key => $item) {
|
||||
$keyStr = ' ' . self::formatObjectKey($key) . ': ';
|
||||
if (is_array($item)) {
|
||||
$jsonStr .= $keyStr . self::getJsonFromArray($item) . ',';
|
||||
} elseif ($item === 'false' || $item === 'true') {
|
||||
$jsonStr .= $keyStr . ($item === 'false' ? 'false' : 'true') . ',';
|
||||
} elseif ($item === null) {
|
||||
$jsonStr .= $keyStr . 'null,';
|
||||
} elseif (str_starts_with($item, "t('") || str_starts_with($item, "t(\"") || $item == '[]' || in_array(gettype($item), ['integer', 'double'])) {
|
||||
$jsonStr .= $keyStr . $item . ',';
|
||||
} elseif (isset($item[0]) && $item[0] == '[' && str_ends_with($item, ']')) {
|
||||
$jsonStr .= $keyStr . $item . ',';
|
||||
} else {
|
||||
$quote = self::getQuote($item);
|
||||
$jsonStr .= $keyStr . "$quote$item$quote,";
|
||||
}
|
||||
}
|
||||
return $jsonStr ? '{' . rtrim($jsonStr, ',') . ' }' : '{}';
|
||||
} else {
|
||||
return $arr;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
63
app/admin/library/crud/stubs/html/form.stub
Normal file
63
app/admin/library/crud/stubs/html/form.stub
Normal file
@@ -0,0 +1,63 @@
|
||||
<template>
|
||||
<!-- 对话框表单 -->
|
||||
<!-- 建议使用 Prettier 格式化代码 -->
|
||||
<!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
|
||||
<el-dialog
|
||||
class="ba-operate-dialog"
|
||||
:close-on-click-modal="false"
|
||||
:model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
|
||||
@close="baTable.toggleForm"{%bigDialog%}
|
||||
>
|
||||
<template #header>
|
||||
<div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
|
||||
{{ baTable.form.operate ? t(baTable.form.operate) : '' }}
|
||||
</div>
|
||||
</template>
|
||||
<el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
|
||||
<div
|
||||
class="ba-operate-form"
|
||||
:class="'ba-' + baTable.form.operate + '-form'"
|
||||
:style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
|
||||
>
|
||||
<el-form
|
||||
v-if="!baTable.form.loading"
|
||||
ref="formRef"
|
||||
@submit.prevent=""
|
||||
@keyup.enter="baTable.onSubmit(formRef)"
|
||||
:model="baTable.form.items"
|
||||
:label-position="config.layout.shrink ? 'top' : 'right'"
|
||||
:label-width="baTable.form.labelWidth + 'px'"
|
||||
:rules="rules"
|
||||
>{%formFields%}
|
||||
</el-form>
|
||||
</div>
|
||||
</el-scrollbar>
|
||||
<template #footer>
|
||||
<div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
|
||||
<el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
|
||||
<el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
|
||||
{{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
|
||||
</el-button>
|
||||
</div>
|
||||
</template>
|
||||
</el-dialog>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import type { FormItemRule } from 'element-plus'
|
||||
import { inject, reactive, useTemplateRef } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import FormItem from '/@/components/formItem/index.vue'
|
||||
import { useConfig } from '/@/stores/config'
|
||||
import type baTableClass from '/@/utils/baTable'{%importExpand%}
|
||||
|
||||
const config = useConfig()
|
||||
const formRef = useTemplateRef('formRef')
|
||||
const baTable = inject('baTable') as baTableClass
|
||||
|
||||
const { t } = useI18n()
|
||||
|
||||
const rules: Partial<Record<string, FormItemRule[]>> = reactive({{%formItemRules%}})
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss"></style>
|
||||
69
app/admin/library/crud/stubs/html/index.stub
Normal file
69
app/admin/library/crud/stubs/html/index.stub
Normal file
@@ -0,0 +1,69 @@
|
||||
<template>
|
||||
<div class="default-main ba-table-box">
|
||||
<el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />
|
||||
|
||||
<!-- 表格顶部菜单 -->
|
||||
<!-- 自定义按钮请使用插槽,甚至公共搜索也可以使用具名插槽渲染,参见文档 -->
|
||||
<TableHeader
|
||||
:buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
|
||||
:quick-search-placeholder="t('Quick search placeholder', { fields: t('{%webTranslate%}quick Search Fields') })"
|
||||
></TableHeader>
|
||||
|
||||
<!-- 表格 -->
|
||||
<!-- 表格列有多种自定义渲染方式,比如自定义组件、具名插槽等,参见文档 -->
|
||||
<!-- 要使用 el-table 组件原有的属性,直接加在 Table 标签上即可 -->
|
||||
<Table ref="tableRef"></Table>
|
||||
|
||||
<!-- 表单 -->
|
||||
<PopupForm />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { onMounted, provide, useTemplateRef } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import PopupForm from './popupForm.vue'
|
||||
import { baTableApi } from '/@/api/common'
|
||||
import { defaultOptButtons } from '/@/components/table'
|
||||
import TableHeader from '/@/components/table/header/index.vue'
|
||||
import Table from '/@/components/table/index.vue'
|
||||
import baTableClass from '/@/utils/baTable'
|
||||
|
||||
defineOptions({
|
||||
name: '{%componentName%}',
|
||||
})
|
||||
|
||||
const { t } = useI18n()
|
||||
const tableRef = useTemplateRef('tableRef')
|
||||
const optButtons: OptButton[] = defaultOptButtons({%optButtons%})
|
||||
|
||||
/**
|
||||
* baTable 内包含了表格的所有数据且数据具备响应性,然后通过 provide 注入给了后代组件
|
||||
*/
|
||||
const baTable = new baTableClass(
|
||||
new baTableApi({%controllerUrl%}),
|
||||
{
|
||||
pk: '{%tablePk%}',
|
||||
column: [
|
||||
{%tableColumn%}
|
||||
],
|
||||
dblClickNotEditColumn: {%dblClickNotEditColumn%},{%defaultOrder%}
|
||||
},
|
||||
{
|
||||
defaultItems: {%defaultItems%},
|
||||
}
|
||||
)
|
||||
|
||||
provide('baTable', baTable)
|
||||
|
||||
onMounted(() => {
|
||||
baTable.table.ref = tableRef.value
|
||||
baTable.mount()
|
||||
baTable.getData()?.then(() => {
|
||||
baTable.initSort()
|
||||
baTable.dragSort()
|
||||
})
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss"></style>
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace {%namespace%};
|
||||
{%use%}
|
||||
use app\common\controller\Backend;
|
||||
|
||||
/**
|
||||
* {%tableComment%}
|
||||
*/
|
||||
class {%className%} extends Backend
|
||||
{
|
||||
/**
|
||||
* {%modelName%}模型对象
|
||||
* @var object
|
||||
* @phpstan-var \{%modelNamespace%}\{%modelName%}
|
||||
*/
|
||||
protected object $model;
|
||||
{%attr%}{%initialize%}
|
||||
{%methods%}
|
||||
|
||||
/**
|
||||
* 若需重写查看、编辑、删除等方法,请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
|
||||
*/
|
||||
}
|
||||
32
app/admin/library/crud/stubs/mixins/controller/index.stub
Normal file
32
app/admin/library/crud/stubs/mixins/controller/index.stub
Normal file
@@ -0,0 +1,32 @@
|
||||
|
||||
/**
|
||||
* 查看
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function index(): void
|
||||
{
|
||||
// 如果是 select 则转发到 select 方法,若未重写该方法,其实还是继续执行 index
|
||||
if ($this->request->param('select')) {
|
||||
$this->select();
|
||||
}
|
||||
|
||||
/**
|
||||
* 1. withJoin 不可使用 alias 方法设置表别名,别名将自动使用关联模型名称(小写下划线命名规则)
|
||||
* 2. 以下的别名设置了主表别名,同时便于拼接查询参数等
|
||||
* 3. paginate 数据集可使用链式操作 each(function($item, $key) {}) 遍历处理
|
||||
*/
|
||||
list($where, $alias, $limit, $order) = $this->queryBuilder();
|
||||
$res = $this->model
|
||||
->withJoin($this->withJoinTable, $this->withJoinType)
|
||||
{%relationVisibleFields%}
|
||||
->alias($alias)
|
||||
->where($where)
|
||||
->order($order)
|
||||
->paginate($limit);
|
||||
|
||||
$this->success('', [
|
||||
'list' => $res->items(),
|
||||
'total' => $res->total(),
|
||||
'remark' => get_route_remark(),
|
||||
]);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
public function initialize(): void
|
||||
{
|
||||
parent::initialize();
|
||||
$this->model = new \{%modelNamespace%}\{%modelName%}();{%filterRule%}
|
||||
}
|
||||
12
app/admin/library/crud/stubs/mixins/model/afterInsert.stub
Normal file
12
app/admin/library/crud/stubs/mixins/model/afterInsert.stub
Normal file
@@ -0,0 +1,12 @@
|
||||
|
||||
protected static function onAfterInsert($model): void
|
||||
{
|
||||
if (is_null($model->{%field%})) {
|
||||
$pk = $model->getPk();
|
||||
if (strlen($model[$pk]) >= 19) {
|
||||
$model->where($pk, $model[$pk])->update(['{%field%}' => $model->count()]);
|
||||
} else {
|
||||
$model->where($pk, $model[$pk])->update(['{%field%}' => $model[$pk]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
|
||||
protected static function onBeforeInsert($model): void
|
||||
{
|
||||
{%setSnowFlakeIdCode%}
|
||||
}
|
||||
5
app/admin/library/crud/stubs/mixins/model/belongsTo.stub
Normal file
5
app/admin/library/crud/stubs/mixins/model/belongsTo.stub
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
public function {%relationMethod%}(): \think\model\relation\BelongsTo
|
||||
{
|
||||
return $this->{%relationMode%}({%relationClassName%}, '{%relationForeignKey%}', '{%relationPrimaryKey%}');
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
|
||||
public function get{%field%}Attr($value, $row): string
|
||||
{
|
||||
if ($row['{%originalFieldName%}'] === '' || $row['{%originalFieldName%}'] === null) return '';
|
||||
$cityNames = \think\facade\Db::name('area')->whereIn('id', $row['{%originalFieldName%}'])->column('name');
|
||||
return $cityNames ? implode(',', $cityNames) : '';
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
|
||||
public function get{%field%}Attr($value): ?float
|
||||
{
|
||||
return is_null($value) ? null : (float)$value;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
|
||||
public function get{%field%}Attr($value): string
|
||||
{
|
||||
return !$value ? '' : htmlspecialchars_decode($value);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
|
||||
public function get{%field%}Attr($value): array
|
||||
{
|
||||
return !$value ? [] : json_decode($value, true);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
|
||||
public function get{%field%}Attr($value, $row): array
|
||||
{
|
||||
return [
|
||||
'{%labelFieldName%}' => {%className%}::whereIn('{%primaryKey%}', $row['{%foreignKey%}'])->column('{%labelFieldName%}'),
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
|
||||
public function get{%field%}Attr($value): string
|
||||
{
|
||||
return (string)$value;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
|
||||
public function get{%field%}Attr($value): array
|
||||
{
|
||||
if ($value === '' || $value === null) return [];
|
||||
if (!is_array($value)) {
|
||||
return explode(',', $value);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
$pk = $model->getPk();
|
||||
$model->$pk = \app\common\library\SnowFlake::generateParticle();
|
||||
18
app/admin/library/crud/stubs/mixins/model/model.stub
Normal file
18
app/admin/library/crud/stubs/mixins/model/model.stub
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace {%namespace%};
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* {%className%}
|
||||
*/
|
||||
class {%className%} extends Model
|
||||
{{%connection%}{%pk%}
|
||||
// 表名
|
||||
protected $name = '{%name%}';
|
||||
|
||||
// 自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = {%autoWriteTimestamp%};{%createTime%}{%updateTime%}
|
||||
{%append%}{%fieldType%}{%beforeInsert%}{%afterInsert%}{%methods%}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
|
||||
public function set{%field%}Attr($value): string
|
||||
{
|
||||
return is_array($value) ? implode(',', $value) : $value;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
|
||||
public function set{%field%}Attr($value): ?string
|
||||
{
|
||||
return $value ? date('H:i:s', strtotime($value)) : $value;
|
||||
}
|
||||
31
app/admin/library/crud/stubs/mixins/validate/validate.stub
Normal file
31
app/admin/library/crud/stubs/mixins/validate/validate.stub
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace {%namespace%};
|
||||
|
||||
use think\Validate;
|
||||
|
||||
class {%className%} extends Validate
|
||||
{
|
||||
protected $failException = true;
|
||||
|
||||
/**
|
||||
* 验证规则
|
||||
*/
|
||||
protected $rule = [
|
||||
];
|
||||
|
||||
/**
|
||||
* 提示消息
|
||||
*/
|
||||
protected $message = [
|
||||
];
|
||||
|
||||
/**
|
||||
* 验证场景
|
||||
*/
|
||||
protected $scene = [
|
||||
'add' => [],
|
||||
'edit' => [],
|
||||
];
|
||||
|
||||
}
|
||||
971
app/admin/library/module/Manage.php
Normal file
971
app/admin/library/module/Manage.php
Normal file
@@ -0,0 +1,971 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\library\module;
|
||||
|
||||
use Throwable;
|
||||
use ba\Version;
|
||||
use ba\Depends;
|
||||
use ba\Exception;
|
||||
use ba\Filesystem;
|
||||
use FilesystemIterator;
|
||||
use think\facade\Config;
|
||||
use RecursiveDirectoryIterator;
|
||||
use RecursiveIteratorIterator;
|
||||
|
||||
/**
|
||||
* 模块管理类
|
||||
*/
|
||||
class Manage
|
||||
{
|
||||
public const UNINSTALLED = 0;
|
||||
public const INSTALLED = 1;
|
||||
public const WAIT_INSTALL = 2;
|
||||
public const CONFLICT_PENDING = 3;
|
||||
public const DEPENDENT_WAIT_INSTALL = 4;
|
||||
public const DIRECTORY_OCCUPIED = 5;
|
||||
public const DISABLE = 6;
|
||||
|
||||
/**
|
||||
* @var ?Manage 对象实例
|
||||
*/
|
||||
protected static ?Manage $instance = null;
|
||||
|
||||
/**
|
||||
* @var string 安装目录
|
||||
*/
|
||||
protected string $installDir;
|
||||
|
||||
/**
|
||||
* @var string 备份目录
|
||||
*/
|
||||
protected string $backupsDir;
|
||||
|
||||
/**
|
||||
* @var string 模板唯一标识
|
||||
*/
|
||||
protected string $uid;
|
||||
|
||||
/**
|
||||
* @var string 模板根目录
|
||||
*/
|
||||
protected string $modulesDir;
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
* @access public
|
||||
* @param string $uid
|
||||
* @return Manage
|
||||
*/
|
||||
public static function instance(string $uid = ''): Manage
|
||||
{
|
||||
if (is_null(self::$instance)) {
|
||||
self::$instance = new static($uid);
|
||||
}
|
||||
return self::$instance->setModuleUid($uid);
|
||||
}
|
||||
|
||||
public function __construct(string $uid)
|
||||
{
|
||||
$this->installDir = root_path() . 'modules' . DIRECTORY_SEPARATOR;
|
||||
$this->backupsDir = $this->installDir . 'backups' . DIRECTORY_SEPARATOR;
|
||||
if (!is_dir($this->installDir)) {
|
||||
mkdir($this->installDir, 0755, true);
|
||||
}
|
||||
if (!is_dir($this->backupsDir)) {
|
||||
mkdir($this->backupsDir, 0755, true);
|
||||
}
|
||||
|
||||
if ($uid) {
|
||||
$this->setModuleUid($uid);
|
||||
}
|
||||
}
|
||||
|
||||
public function getInstallState()
|
||||
{
|
||||
if (!is_dir($this->modulesDir)) {
|
||||
return self::UNINSTALLED;
|
||||
}
|
||||
$info = $this->getInfo();
|
||||
if ($info && isset($info['state'])) {
|
||||
return $info['state'];
|
||||
}
|
||||
|
||||
// 目录已存在,但非正常的模块
|
||||
return Filesystem::dirIsEmpty($this->modulesDir) ? self::UNINSTALLED : self::DIRECTORY_OCCUPIED;
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载模块文件
|
||||
* @return string 已下载文件路径
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function download(): string
|
||||
{
|
||||
$token = request()->param("token/s");
|
||||
$version = request()->param('version/s');
|
||||
$orderId = request()->param("orderId/d");
|
||||
|
||||
if (!$orderId) {
|
||||
throw new Exception('Order not found');
|
||||
}
|
||||
|
||||
// 下载 - 系统版本号要求、已安装模块的互斥和依赖检测
|
||||
$zipFile = Server::download($this->uid, $this->installDir, [
|
||||
'version' => $version,
|
||||
'orderId' => $orderId,
|
||||
'nuxtVersion' => Server::getNuxtVersion(),
|
||||
'sysVersion' => Config::get('buildadmin.version'),
|
||||
'installed' => Server::getInstalledIds($this->installDir),
|
||||
'ba-user-token' => $token,
|
||||
]);
|
||||
|
||||
// 删除旧版本代码
|
||||
Filesystem::delDir($this->modulesDir);
|
||||
|
||||
// 解压
|
||||
Filesystem::unzip($zipFile);
|
||||
|
||||
// 删除下载的zip
|
||||
@unlink($zipFile);
|
||||
|
||||
// 检查是否完整
|
||||
$this->checkPackage();
|
||||
|
||||
// 设置为待安装状态
|
||||
$this->setInfo([
|
||||
'state' => self::WAIT_INSTALL,
|
||||
]);
|
||||
|
||||
return $zipFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传安装
|
||||
* @param string $file 已经上传完成的文件
|
||||
* @return array 模块的基本信息
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function upload(string $token, string $file): array
|
||||
{
|
||||
$file = Filesystem::fsFit(root_path() . 'public' . $file);
|
||||
if (!is_file($file)) {
|
||||
// 包未找到
|
||||
throw new Exception('Zip file not found');
|
||||
}
|
||||
|
||||
$copyTo = $this->installDir . 'uploadTemp' . date('YmdHis') . '.zip';
|
||||
copy($file, $copyTo);
|
||||
|
||||
// 解压
|
||||
$copyToDir = Filesystem::unzip($copyTo);
|
||||
$copyToDir .= DIRECTORY_SEPARATOR;
|
||||
|
||||
// 删除zip
|
||||
@unlink($file);
|
||||
@unlink($copyTo);
|
||||
|
||||
// 读取ini
|
||||
$info = Server::getIni($copyToDir);
|
||||
if (empty($info['uid'])) {
|
||||
Filesystem::delDir($copyToDir);
|
||||
// 基本配置不完整
|
||||
throw new Exception('Basic configuration of the Module is incomplete');
|
||||
}
|
||||
|
||||
$this->setModuleUid($info['uid']);
|
||||
|
||||
$upgrade = false;
|
||||
if (is_dir($this->modulesDir)) {
|
||||
$oldInfo = $this->getInfo();
|
||||
if ($oldInfo && !empty($oldInfo['uid'])) {
|
||||
$versions = explode('.', $oldInfo['version']);
|
||||
if (isset($versions[2])) {
|
||||
$versions[2]++;
|
||||
}
|
||||
$nextVersion = implode('.', $versions);
|
||||
$upgrade = Version::compare($nextVersion, $info['version']);
|
||||
if ($upgrade) {
|
||||
// 检查模块是否已禁用
|
||||
if (!in_array($oldInfo['state'], [self::UNINSTALLED, self::WAIT_INSTALL, self::DISABLE])) {
|
||||
Filesystem::delDir($copyToDir);
|
||||
throw new Exception('Please disable the module before updating');
|
||||
}
|
||||
} else {
|
||||
Filesystem::delDir($copyToDir);
|
||||
// 模块已经存在
|
||||
throw new Exception('Module already exists');
|
||||
}
|
||||
}
|
||||
|
||||
if (!Filesystem::dirIsEmpty($this->modulesDir) && !$upgrade) {
|
||||
Filesystem::delDir($copyToDir);
|
||||
// 模块目录被占
|
||||
throw new Exception('The directory required by the module is occupied');
|
||||
}
|
||||
}
|
||||
|
||||
// 安装预检 - 系统版本号要求、已安装模块的互斥和依赖检测
|
||||
try {
|
||||
Server::installPreCheck([
|
||||
'uid' => $info['uid'],
|
||||
'version' => $info['version'],
|
||||
'sysVersion' => Config::get('buildadmin.version'),
|
||||
'nuxtVersion' => Server::getNuxtVersion(),
|
||||
'moduleVersion' => $info['version'],
|
||||
'ba-user-token' => $token,
|
||||
'installed' => Server::getInstalledIds($this->installDir),
|
||||
'server' => 1,
|
||||
]);
|
||||
} catch (Throwable $e) {
|
||||
Filesystem::delDir($copyToDir);
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$newInfo = ['state' => self::WAIT_INSTALL];
|
||||
if ($upgrade) {
|
||||
$info['update'] = 1;
|
||||
|
||||
// 清理旧版本代码
|
||||
Filesystem::delDir($this->modulesDir);
|
||||
}
|
||||
|
||||
// 放置新模块
|
||||
rename($copyToDir, $this->modulesDir);
|
||||
|
||||
// 检查新包是否完整
|
||||
$this->checkPackage();
|
||||
|
||||
// 设置为待安装状态
|
||||
$this->setInfo($newInfo);
|
||||
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* 安装模块
|
||||
* @return array 模块基本信息
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function install(bool $update): array
|
||||
{
|
||||
$state = $this->getInstallState();
|
||||
|
||||
if ($update) {
|
||||
if (!in_array($state, [self::UNINSTALLED, self::WAIT_INSTALL, self::DISABLE])) {
|
||||
throw new Exception('Please disable the module before updating');
|
||||
}
|
||||
|
||||
/**
|
||||
* self::WAIT_INSTALL=待安装
|
||||
* 即本地上传文件进行升级的安装流程,文件上传成功后将被标记为待安装,免去此处的下载
|
||||
*/
|
||||
if ($state == self::UNINSTALLED || $state != self::WAIT_INSTALL) {
|
||||
$this->download();
|
||||
}
|
||||
} else {
|
||||
if ($state == self::INSTALLED || $state == self::DIRECTORY_OCCUPIED || $state == self::DISABLE) {
|
||||
throw new Exception('Module already exists');
|
||||
}
|
||||
|
||||
if ($state == self::UNINSTALLED) {
|
||||
$this->download();
|
||||
}
|
||||
}
|
||||
|
||||
// 导入sql
|
||||
Server::importSql($this->modulesDir);
|
||||
|
||||
// 如果是更新,先执行更新脚本
|
||||
$info = $this->getInfo();
|
||||
if ($update) {
|
||||
$info['update'] = 1;
|
||||
Server::execEvent($this->uid, 'update');
|
||||
}
|
||||
|
||||
// 执行安装脚本 - 排除冲突处理时会重复提交至此的请求
|
||||
$extend = request()->post('extend/a', []);
|
||||
if (!isset($extend['conflictHandle'])) {
|
||||
Server::execEvent($this->uid, 'install');
|
||||
}
|
||||
|
||||
// 启用插件
|
||||
$this->enable('install');
|
||||
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* 卸载
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function uninstall(): void
|
||||
{
|
||||
$info = $this->getInfo();
|
||||
if ($info['state'] != self::DISABLE) {
|
||||
throw new Exception('Please disable the module first', 0, [
|
||||
'uid' => $this->uid,
|
||||
]);
|
||||
}
|
||||
|
||||
// 执行卸载脚本
|
||||
Server::execEvent($this->uid, 'uninstall');
|
||||
|
||||
Filesystem::delDir($this->modulesDir);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改模块状态
|
||||
* @param bool $state 新状态
|
||||
* @return array 模块基本信息
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function changeState(bool $state): array
|
||||
{
|
||||
$info = $this->getInfo();
|
||||
if (!$state) {
|
||||
$canDisable = [
|
||||
self::INSTALLED,
|
||||
self::CONFLICT_PENDING,
|
||||
self::DEPENDENT_WAIT_INSTALL,
|
||||
];
|
||||
if (!in_array($info['state'], $canDisable)) {
|
||||
throw new Exception('The current state of the module cannot be set to disabled', 0, [
|
||||
'uid' => $this->uid,
|
||||
'state' => $info['state'],
|
||||
]);
|
||||
}
|
||||
return $this->disable();
|
||||
}
|
||||
|
||||
if ($info['state'] != self::DISABLE) {
|
||||
throw new Exception('The current state of the module cannot be set to enabled', 0, [
|
||||
'uid' => $this->uid,
|
||||
'state' => $info['state'],
|
||||
]);
|
||||
}
|
||||
$this->setInfo([
|
||||
'state' => self::WAIT_INSTALL,
|
||||
]);
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* 启用
|
||||
* @param string $trigger 触发启用的标志,比如:install=安装
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function enable(string $trigger): void
|
||||
{
|
||||
// 安装 WebBootstrap
|
||||
Server::installWebBootstrap($this->uid, $this->modulesDir);
|
||||
|
||||
// 建立 .runtime
|
||||
Server::createRuntime($this->modulesDir);
|
||||
|
||||
// 冲突检查
|
||||
$this->conflictHandle($trigger);
|
||||
|
||||
// 执行启用脚本
|
||||
Server::execEvent($this->uid, 'enable');
|
||||
|
||||
$this->dependUpdateHandle();
|
||||
}
|
||||
|
||||
/**
|
||||
* 禁用
|
||||
* @return array 模块基本信息
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function disable(): array
|
||||
{
|
||||
$update = request()->post("update/b", false);
|
||||
$confirmConflict = request()->post("confirmConflict/b", false);
|
||||
$dependConflictSolution = request()->post("dependConflictSolution/a", []);
|
||||
|
||||
$info = $this->getInfo();
|
||||
$zipFile = $this->backupsDir . $this->uid . '-install.zip';
|
||||
$zipDir = false;
|
||||
if (is_file($zipFile)) {
|
||||
try {
|
||||
$zipDir = $this->backupsDir . $this->uid . '-install' . DIRECTORY_SEPARATOR;
|
||||
Filesystem::unzip($zipFile, $zipDir);
|
||||
} catch (Exception) {
|
||||
// skip
|
||||
}
|
||||
}
|
||||
|
||||
$conflictFile = Server::getFileList($this->modulesDir, true);
|
||||
$dependConflict = $this->disableDependCheck();
|
||||
if (($conflictFile || !self::isEmptyArray($dependConflict)) && !$confirmConflict) {
|
||||
$dependConflictTemp = [];
|
||||
foreach ($dependConflict as $env => $item) {
|
||||
foreach ($item as $depend => $v) {
|
||||
$dependConflictTemp[] = [
|
||||
'env' => $env,
|
||||
'depend' => $depend,
|
||||
'dependTitle' => $depend . ' ' . $v,
|
||||
'solution' => 'delete',
|
||||
];
|
||||
}
|
||||
}
|
||||
throw new Exception('Module file updated', -1, [
|
||||
'uid' => $this->uid,
|
||||
'conflictFile' => $conflictFile,
|
||||
'dependConflict' => $dependConflictTemp,
|
||||
]);
|
||||
}
|
||||
|
||||
// 执行禁用脚本
|
||||
Server::execEvent($this->uid, 'disable', ['update' => $update]);
|
||||
|
||||
// 是否需要备份依赖?
|
||||
$delNpmDepend = false;
|
||||
$delNuxtNpmDepend = false;
|
||||
$delComposerDepend = false;
|
||||
foreach ($dependConflictSolution as $env => $depends) {
|
||||
if (!$depends) continue;
|
||||
if ($env == 'require' || $env == 'require-dev') {
|
||||
$delComposerDepend = true;
|
||||
} elseif ($env == 'dependencies' || $env == 'devDependencies') {
|
||||
$delNpmDepend = true;
|
||||
} elseif ($env == 'nuxtDependencies' || $env == 'nuxtDevDependencies') {
|
||||
$delNuxtNpmDepend = true;
|
||||
}
|
||||
}
|
||||
|
||||
// 备份
|
||||
$dependJsonFiles = [
|
||||
'composer' => 'composer.json',
|
||||
'webPackage' => 'web' . DIRECTORY_SEPARATOR . 'package.json',
|
||||
'webNuxtPackage' => 'web-nuxt' . DIRECTORY_SEPARATOR . 'package.json',
|
||||
];
|
||||
$dependWaitInstall = [];
|
||||
if ($delComposerDepend) {
|
||||
$conflictFile[] = $dependJsonFiles['composer'];
|
||||
$dependWaitInstall[] = [
|
||||
'pm' => false,
|
||||
'command' => 'composer.update',
|
||||
'type' => 'composer_dependent_wait_install',
|
||||
];
|
||||
}
|
||||
if ($delNpmDepend) {
|
||||
$conflictFile[] = $dependJsonFiles['webPackage'];
|
||||
$dependWaitInstall[] = [
|
||||
'pm' => true,
|
||||
'command' => 'web-install',
|
||||
'type' => 'npm_dependent_wait_install',
|
||||
];
|
||||
}
|
||||
if ($delNuxtNpmDepend) {
|
||||
$conflictFile[] = $dependJsonFiles['webNuxtPackage'];
|
||||
$dependWaitInstall[] = [
|
||||
'pm' => true,
|
||||
'command' => 'nuxt-install',
|
||||
'type' => 'nuxt_npm_dependent_wait_install',
|
||||
];
|
||||
}
|
||||
if ($conflictFile) {
|
||||
// 如果是模块自带文件需要备份,加上模块目录前缀
|
||||
$overwriteDir = Server::getOverwriteDir();
|
||||
foreach ($conflictFile as $key => $item) {
|
||||
$paths = explode(DIRECTORY_SEPARATOR, $item);
|
||||
if (in_array($paths[0], $overwriteDir) || in_array($item, $dependJsonFiles)) {
|
||||
$conflictFile[$key] = $item;
|
||||
} else {
|
||||
$conflictFile[$key] = Filesystem::fsFit(str_replace(root_path(), '', $this->modulesDir . $item));
|
||||
}
|
||||
if (!is_file(root_path() . $conflictFile[$key])) {
|
||||
unset($conflictFile[$key]);
|
||||
}
|
||||
}
|
||||
$backupsZip = $this->backupsDir . $this->uid . '-disable-' . date('YmdHis') . '.zip';
|
||||
Filesystem::zip($conflictFile, $backupsZip);
|
||||
}
|
||||
|
||||
// 删除依赖
|
||||
$serverDepend = new Depends(root_path() . 'composer.json', 'composer');
|
||||
$webDep = new Depends(root_path() . 'web' . DIRECTORY_SEPARATOR . 'package.json');
|
||||
$webNuxtDep = new Depends(root_path() . 'web-nuxt' . DIRECTORY_SEPARATOR . 'package.json');
|
||||
foreach ($dependConflictSolution as $env => $depends) {
|
||||
if (!$depends) continue;
|
||||
$dev = !(stripos($env, 'dev') === false);
|
||||
if ($env == 'require' || $env == 'require-dev') {
|
||||
$serverDepend->removeDepends($depends, $dev);
|
||||
} elseif ($env == 'dependencies' || $env == 'devDependencies') {
|
||||
$webDep->removeDepends($depends, $dev);
|
||||
} elseif ($env == 'nuxtDependencies' || $env == 'nuxtDevDependencies') {
|
||||
$webNuxtDep->removeDepends($depends, $dev);
|
||||
}
|
||||
}
|
||||
|
||||
// 删除 composer.json 中的 config
|
||||
$composerConfig = Server::getConfig($this->modulesDir, 'composerConfig');
|
||||
if ($composerConfig) {
|
||||
$serverDepend->removeComposerConfig($composerConfig);
|
||||
}
|
||||
|
||||
// 配置了不删除的文件
|
||||
$protectedFiles = Server::getConfig($this->modulesDir, 'protectedFiles');
|
||||
foreach ($protectedFiles as &$protectedFile) {
|
||||
$protectedFile = Filesystem::fsFit(root_path() . $protectedFile);
|
||||
}
|
||||
// 模块文件列表
|
||||
$moduleFile = Server::getFileList($this->modulesDir);
|
||||
|
||||
// 删除模块文件
|
||||
foreach ($moduleFile as &$file) {
|
||||
// 纯净模式下,模块文件将被删除,此处直接检查模块目录中是否有该文件并恢复(不检查是否开启纯净模式,因为开关可能被调整)
|
||||
$moduleFilePath = Filesystem::fsFit($this->modulesDir . $file);
|
||||
$file = Filesystem::fsFit(root_path() . $file);
|
||||
if (!file_exists($file)) continue;
|
||||
if (!file_exists($moduleFilePath)) {
|
||||
if (!is_dir(dirname($moduleFilePath))) {
|
||||
mkdir(dirname($moduleFilePath), 0755, true);
|
||||
}
|
||||
copy($file, $moduleFilePath);
|
||||
}
|
||||
|
||||
if (in_array($file, $protectedFiles)) {
|
||||
continue;
|
||||
}
|
||||
if (file_exists($file)) {
|
||||
unlink($file);
|
||||
}
|
||||
Filesystem::delEmptyDir(dirname($file));
|
||||
}
|
||||
|
||||
// 恢复备份文件
|
||||
if ($zipDir) {
|
||||
$unrecoverableFiles = [
|
||||
Filesystem::fsFit(root_path() . 'composer.json'),
|
||||
Filesystem::fsFit(root_path() . 'web/package.json'),
|
||||
Filesystem::fsFit(root_path() . 'web-nuxt/package.json'),
|
||||
];
|
||||
foreach (
|
||||
new RecursiveIteratorIterator(
|
||||
new RecursiveDirectoryIterator($zipDir, FilesystemIterator::SKIP_DOTS),
|
||||
RecursiveIteratorIterator::SELF_FIRST
|
||||
) as $item
|
||||
) {
|
||||
$backupsFile = Filesystem::fsFit(root_path() . str_replace($zipDir, '', $item->getPathname()));
|
||||
|
||||
// 在模块包中,同时不在 $protectedFiles 列表的文件不恢复,这些文件可能是模块升级时备份的
|
||||
if (in_array($backupsFile, $moduleFile) && !in_array($backupsFile, $protectedFiles)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($item->isDir()) {
|
||||
if (!is_dir($backupsFile)) {
|
||||
mkdir($backupsFile, 0755, true);
|
||||
}
|
||||
} elseif (!in_array($backupsFile, $unrecoverableFiles)) {
|
||||
copy($item, $backupsFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 删除解压后的备份文件
|
||||
Filesystem::delDir($zipDir);
|
||||
|
||||
// 卸载 WebBootstrap
|
||||
Server::uninstallWebBootstrap($this->uid);
|
||||
|
||||
$this->setInfo([
|
||||
'state' => self::DISABLE,
|
||||
]);
|
||||
|
||||
if ($update) {
|
||||
throw new Exception('update', -3, [
|
||||
'uid' => $this->uid,
|
||||
]);
|
||||
}
|
||||
|
||||
if ($dependWaitInstall) {
|
||||
throw new Exception('dependent wait install', -2, [
|
||||
'uid' => $this->uid,
|
||||
'wait_install' => $dependWaitInstall,
|
||||
]);
|
||||
}
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理依赖和文件冲突,并完成与前端的冲突处理交互
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function conflictHandle(string $trigger): bool
|
||||
{
|
||||
$info = $this->getInfo();
|
||||
if ($info['state'] != self::WAIT_INSTALL && $info['state'] != self::CONFLICT_PENDING) {
|
||||
return false;
|
||||
}
|
||||
$fileConflict = Server::getFileList($this->modulesDir, true);// 文件冲突
|
||||
$dependConflict = Server::dependConflictCheck($this->modulesDir);// 依赖冲突
|
||||
$installFiles = Server::getFileList($this->modulesDir);// 待安装文件
|
||||
$depends = Server::getDepend($this->modulesDir);// 待安装依赖
|
||||
|
||||
$coverFiles = [];// 要覆盖的文件-备份
|
||||
$discardFiles = [];// 抛弃的文件-复制时不覆盖
|
||||
$serverDep = new Depends(root_path() . 'composer.json', 'composer');
|
||||
$webDep = new Depends(root_path() . 'web' . DIRECTORY_SEPARATOR . 'package.json');
|
||||
$webNuxtDep = new Depends(root_path() . 'web-nuxt' . DIRECTORY_SEPARATOR . 'package.json');
|
||||
if ($fileConflict || !self::isEmptyArray($dependConflict)) {
|
||||
$extend = request()->post('extend/a', []);
|
||||
if (!$extend) {
|
||||
// 发现冲突->手动处理->转换为方便前端使用的格式
|
||||
$fileConflictTemp = [];
|
||||
foreach ($fileConflict as $key => $item) {
|
||||
$fileConflictTemp[$key] = [
|
||||
'newFile' => $this->uid . DIRECTORY_SEPARATOR . $item,
|
||||
'oldFile' => $item,
|
||||
'solution' => 'cover',
|
||||
];
|
||||
}
|
||||
$dependConflictTemp = [];
|
||||
foreach ($dependConflict as $env => $item) {
|
||||
$dev = !(stripos($env, 'dev') === false);
|
||||
foreach ($item as $depend => $v) {
|
||||
$oldDepend = '';
|
||||
if (in_array($env, ['require', 'require-dev'])) {
|
||||
$oldDepend = $depend . ' ' . $serverDep->hasDepend($depend, $dev);
|
||||
} elseif (in_array($env, ['dependencies', 'devDependencies'])) {
|
||||
$oldDepend = $depend . ' ' . $webDep->hasDepend($depend, $dev);
|
||||
} elseif (in_array($env, ['nuxtDependencies', 'nuxtDevDependencies'])) {
|
||||
$oldDepend = $depend . ' ' . $webNuxtDep->hasDepend($depend, $dev);
|
||||
}
|
||||
$dependConflictTemp[] = [
|
||||
'env' => $env,
|
||||
'newDepend' => $depend . ' ' . $v,
|
||||
'oldDepend' => $oldDepend,
|
||||
'depend' => $depend,
|
||||
'solution' => 'cover',
|
||||
];
|
||||
}
|
||||
}
|
||||
$this->setInfo([
|
||||
'state' => self::CONFLICT_PENDING,
|
||||
]);
|
||||
throw new Exception('Module file conflicts', -1, [
|
||||
'fileConflict' => $fileConflictTemp,
|
||||
'dependConflict' => $dependConflictTemp,
|
||||
'uid' => $this->uid,
|
||||
'state' => self::CONFLICT_PENDING,
|
||||
]);
|
||||
}
|
||||
|
||||
// 处理冲突
|
||||
if ($fileConflict && isset($extend['fileConflict'])) {
|
||||
foreach ($installFiles as $ikey => $installFile) {
|
||||
if (isset($extend['fileConflict'][$installFile])) {
|
||||
if ($extend['fileConflict'][$installFile] == 'discard') {
|
||||
$discardFiles[] = $installFile;
|
||||
unset($installFiles[$ikey]);
|
||||
} else {
|
||||
$coverFiles[] = $installFile;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!self::isEmptyArray($dependConflict) && isset($extend['dependConflict'])) {
|
||||
foreach ($depends as $fKey => $fItem) {
|
||||
foreach ($fItem as $cKey => $cItem) {
|
||||
if (isset($extend['dependConflict'][$fKey][$cKey])) {
|
||||
if ($extend['dependConflict'][$fKey][$cKey] == 'discard') {
|
||||
unset($depends[$fKey][$cKey]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果有依赖更新,增加要备份的文件
|
||||
if ($depends) {
|
||||
foreach ($depends as $key => $item) {
|
||||
if (!$item) {
|
||||
continue;
|
||||
}
|
||||
if ($key == 'require' || $key == 'require-dev') {
|
||||
$coverFiles[] = 'composer.json';
|
||||
continue;
|
||||
}
|
||||
if ($key == 'dependencies' || $key == 'devDependencies') {
|
||||
$coverFiles[] = 'web' . DIRECTORY_SEPARATOR . 'package.json';
|
||||
}
|
||||
if ($key == 'nuxtDependencies' || $key == 'nuxtDevDependencies') {
|
||||
$coverFiles[] = 'web-nuxt' . DIRECTORY_SEPARATOR . 'package.json';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 备份将被覆盖的文件
|
||||
if ($coverFiles) {
|
||||
$backupsZip = $trigger == 'install' ? $this->backupsDir . $this->uid . '-install.zip' : $this->backupsDir . $this->uid . '-cover-' . date('YmdHis') . '.zip';
|
||||
Filesystem::zip($coverFiles, $backupsZip);
|
||||
}
|
||||
|
||||
if ($depends) {
|
||||
$npm = false;
|
||||
$composer = false;
|
||||
$nuxtNpm = false;
|
||||
|
||||
// composer config 更新
|
||||
$composerConfig = Server::getConfig($this->modulesDir, 'composerConfig');
|
||||
if ($composerConfig) {
|
||||
$serverDep->setComposerConfig($composerConfig);
|
||||
}
|
||||
|
||||
foreach ($depends as $key => $item) {
|
||||
if (!$item) {
|
||||
continue;
|
||||
}
|
||||
if ($key == 'require') {
|
||||
$composer = true;
|
||||
$serverDep->addDepends($item, false, true);
|
||||
} elseif ($key == 'require-dev') {
|
||||
$composer = true;
|
||||
$serverDep->addDepends($item, true, true);
|
||||
} elseif ($key == 'dependencies') {
|
||||
$npm = true;
|
||||
$webDep->addDepends($item, false, true);
|
||||
} elseif ($key == 'devDependencies') {
|
||||
$npm = true;
|
||||
$webDep->addDepends($item, true, true);
|
||||
} elseif ($key == 'nuxtDependencies') {
|
||||
$nuxtNpm = true;
|
||||
$webNuxtDep->addDepends($item, false, true);
|
||||
} elseif ($key == 'nuxtDevDependencies') {
|
||||
$nuxtNpm = true;
|
||||
$webNuxtDep->addDepends($item, true, true);
|
||||
}
|
||||
}
|
||||
if ($npm) {
|
||||
$info['npm_dependent_wait_install'] = 1;
|
||||
$info['state'] = self::DEPENDENT_WAIT_INSTALL;
|
||||
}
|
||||
if ($composer) {
|
||||
$info['composer_dependent_wait_install'] = 1;
|
||||
$info['state'] = self::DEPENDENT_WAIT_INSTALL;
|
||||
}
|
||||
if ($nuxtNpm) {
|
||||
$info['nuxt_npm_dependent_wait_install'] = 1;
|
||||
$info['state'] = self::DEPENDENT_WAIT_INSTALL;
|
||||
}
|
||||
if ($info['state'] != self::DEPENDENT_WAIT_INSTALL) {
|
||||
// 无冲突
|
||||
$this->setInfo([
|
||||
'state' => self::INSTALLED,
|
||||
]);
|
||||
} else {
|
||||
$this->setInfo([], $info);
|
||||
}
|
||||
} else {
|
||||
// 无冲突
|
||||
$this->setInfo([
|
||||
'state' => self::INSTALLED,
|
||||
]);
|
||||
}
|
||||
|
||||
// 复制文件
|
||||
$overwriteDir = Server::getOverwriteDir();
|
||||
foreach ($overwriteDir as $dirItem) {
|
||||
$baseDir = $this->modulesDir . $dirItem;
|
||||
$destDir = root_path() . $dirItem;
|
||||
if (!is_dir($baseDir)) {
|
||||
continue;
|
||||
}
|
||||
foreach (
|
||||
new RecursiveIteratorIterator(
|
||||
new RecursiveDirectoryIterator($baseDir, FilesystemIterator::SKIP_DOTS),
|
||||
RecursiveIteratorIterator::SELF_FIRST
|
||||
) as $item
|
||||
) {
|
||||
$destDirItem = Filesystem::fsFit($destDir . DIRECTORY_SEPARATOR . str_replace($baseDir, '', $item->getPathname()));
|
||||
if ($item->isDir()) {
|
||||
Filesystem::mkdir($destDirItem);
|
||||
} elseif (!in_array(str_replace(root_path(), '', $destDirItem), $discardFiles)) {
|
||||
Filesystem::mkdir(dirname($destDirItem));
|
||||
copy($item, $destDirItem);
|
||||
}
|
||||
}
|
||||
// 纯净模式
|
||||
if (Config::get('buildadmin.module_pure_install')) {
|
||||
Filesystem::delDir($baseDir);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 依赖升级处理
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function dependUpdateHandle(): void
|
||||
{
|
||||
$info = $this->getInfo();
|
||||
if ($info['state'] == self::DEPENDENT_WAIT_INSTALL) {
|
||||
$waitInstall = [];
|
||||
if (isset($info['composer_dependent_wait_install'])) {
|
||||
$waitInstall[] = 'composer_dependent_wait_install';
|
||||
}
|
||||
if (isset($info['npm_dependent_wait_install'])) {
|
||||
$waitInstall[] = 'npm_dependent_wait_install';
|
||||
}
|
||||
if (isset($info['nuxt_npm_dependent_wait_install'])) {
|
||||
$waitInstall[] = 'nuxt_npm_dependent_wait_install';
|
||||
}
|
||||
if ($waitInstall) {
|
||||
throw new Exception('dependent wait install', -2, [
|
||||
'uid' => $this->uid,
|
||||
'state' => self::DEPENDENT_WAIT_INSTALL,
|
||||
'wait_install' => $waitInstall,
|
||||
]);
|
||||
} else {
|
||||
$this->setInfo([
|
||||
'state' => self::INSTALLED,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 依赖安装完成标记
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function dependentInstallComplete(string $type): void
|
||||
{
|
||||
$info = $this->getInfo();
|
||||
if ($info['state'] == self::DEPENDENT_WAIT_INSTALL) {
|
||||
if ($type == 'npm') {
|
||||
unset($info['npm_dependent_wait_install']);
|
||||
}
|
||||
if ($type == 'nuxt_npm') {
|
||||
unset($info['nuxt_npm_dependent_wait_install']);
|
||||
}
|
||||
if ($type == 'composer') {
|
||||
unset($info['composer_dependent_wait_install']);
|
||||
}
|
||||
if ($type == 'all') {
|
||||
unset($info['npm_dependent_wait_install'], $info['composer_dependent_wait_install'], $info['nuxt_npm_dependent_wait_install']);
|
||||
}
|
||||
if (!isset($info['npm_dependent_wait_install']) && !isset($info['composer_dependent_wait_install']) && !isset($info['nuxt_npm_dependent_wait_install'])) {
|
||||
$info['state'] = self::INSTALLED;
|
||||
}
|
||||
$this->setInfo([], $info);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 禁用依赖检查
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function disableDependCheck(): array
|
||||
{
|
||||
// 读取模块所有依赖
|
||||
$depend = Server::getDepend($this->modulesDir);
|
||||
if (!$depend) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// 读取所有依赖中,系统上已经安装的依赖
|
||||
$serverDep = new Depends(root_path() . 'composer.json', 'composer');
|
||||
$webDep = new Depends(root_path() . 'web' . DIRECTORY_SEPARATOR . 'package.json');
|
||||
$webNuxtDep = new Depends(root_path() . 'web-nuxt' . DIRECTORY_SEPARATOR . 'package.json');
|
||||
foreach ($depend as $key => $depends) {
|
||||
$dev = !(stripos($key, 'dev') === false);
|
||||
if ($key == 'require' || $key == 'require-dev') {
|
||||
foreach ($depends as $dependKey => $dependItem) {
|
||||
if (!$serverDep->hasDepend($dependKey, $dev)) {
|
||||
unset($depends[$dependKey]);
|
||||
}
|
||||
}
|
||||
$depend[$key] = $depends;
|
||||
} elseif ($key == 'dependencies' || $key == 'devDependencies') {
|
||||
foreach ($depends as $dependKey => $dependItem) {
|
||||
if (!$webDep->hasDepend($dependKey, $dev)) {
|
||||
unset($depends[$dependKey]);
|
||||
}
|
||||
}
|
||||
$depend[$key] = $depends;
|
||||
} elseif ($key == 'nuxtDependencies' || $key == 'nuxtDevDependencies') {
|
||||
foreach ($depends as $dependKey => $dependItem) {
|
||||
if (!$webNuxtDep->hasDepend($dependKey, $dev)) {
|
||||
unset($depends[$dependKey]);
|
||||
}
|
||||
}
|
||||
$depend[$key] = $depends;
|
||||
}
|
||||
}
|
||||
return $depend;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查包是否完整
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function checkPackage(): bool
|
||||
{
|
||||
if (!is_dir($this->modulesDir)) {
|
||||
throw new Exception('Module package file does not exist');
|
||||
}
|
||||
$info = $this->getInfo();
|
||||
$infoKeys = ['uid', 'title', 'intro', 'author', 'version', 'state'];
|
||||
foreach ($infoKeys as $value) {
|
||||
if (!array_key_exists($value, $info)) {
|
||||
Filesystem::delDir($this->modulesDir);
|
||||
throw new Exception('Basic configuration of the Module is incomplete');
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模块基本信息
|
||||
*/
|
||||
public function getInfo(): array
|
||||
{
|
||||
return Server::getIni($this->modulesDir);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置模块基本信息
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function setInfo(array $kv = [], array $arr = []): bool
|
||||
{
|
||||
if ($kv) {
|
||||
$info = $this->getInfo();
|
||||
foreach ($kv as $k => $v) {
|
||||
$info[$k] = $v;
|
||||
}
|
||||
return Server::setIni($this->modulesDir, $info);
|
||||
} elseif ($arr) {
|
||||
return Server::setIni($this->modulesDir, $arr);
|
||||
}
|
||||
throw new Exception('Parameter error');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检查多维数组是否全部为空
|
||||
*/
|
||||
public static function isEmptyArray($arr): bool
|
||||
{
|
||||
foreach ($arr as $item) {
|
||||
if (is_array($item)) {
|
||||
$empty = self::isEmptyArray($item);
|
||||
if (!$empty) return false;
|
||||
} elseif ($item) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function setModuleUid(string $uid): static
|
||||
{
|
||||
$this->uid = $uid;
|
||||
$this->modulesDir = $this->installDir . $uid . DIRECTORY_SEPARATOR;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
590
app/admin/library/module/Server.php
Normal file
590
app/admin/library/module/Server.php
Normal file
@@ -0,0 +1,590 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\library\module;
|
||||
|
||||
use Throwable;
|
||||
use ba\Depends;
|
||||
use ba\Exception;
|
||||
use ba\Filesystem;
|
||||
use think\facade\Db;
|
||||
use FilesystemIterator;
|
||||
use think\facade\Config;
|
||||
use RecursiveIteratorIterator;
|
||||
use RecursiveDirectoryIterator;
|
||||
use think\db\exception\PDOException;
|
||||
use app\admin\library\crud\Helper;
|
||||
use GuzzleHttp\Exception\TransferException;
|
||||
|
||||
/**
|
||||
* 模块服务类
|
||||
*/
|
||||
class Server
|
||||
{
|
||||
private static string $apiBaseUrl = '/api/v7.store/';
|
||||
|
||||
/**
|
||||
* 下载
|
||||
* @throws Throwable
|
||||
*/
|
||||
public static function download(string $uid, string $dir, array $extend = []): string
|
||||
{
|
||||
$tmpFile = $dir . $uid . ".zip";
|
||||
try {
|
||||
$client = get_ba_client();
|
||||
$response = $client->get(self::$apiBaseUrl . 'download', ['query' => array_merge(['uid' => $uid, 'server' => 1], $extend)]);
|
||||
$body = $response->getBody();
|
||||
$content = $body->getContents();
|
||||
if ($content == '' || stripos($content, '<title>系统发生错误</title>') !== false) {
|
||||
throw new Exception('package download failed', 0);
|
||||
}
|
||||
if (str_starts_with($content, '{')) {
|
||||
$json = (array)json_decode($content, true);
|
||||
throw new Exception($json['msg'], $json['code'], $json['data'] ?? []);
|
||||
}
|
||||
} catch (TransferException $e) {
|
||||
throw new Exception('package download failed', 0, ['msg' => $e->getMessage()]);
|
||||
}
|
||||
|
||||
if ($write = fopen($tmpFile, 'w')) {
|
||||
fwrite($write, $content);
|
||||
fclose($write);
|
||||
return $tmpFile;
|
||||
}
|
||||
throw new Exception("No permission to write temporary files");
|
||||
}
|
||||
|
||||
/**
|
||||
* 安装预检
|
||||
* @throws Throwable
|
||||
*/
|
||||
public static function installPreCheck(array $query = []): bool
|
||||
{
|
||||
try {
|
||||
$client = get_ba_client();
|
||||
$response = $client->get(self::$apiBaseUrl . 'preCheck', ['query' => $query]);
|
||||
$body = $response->getBody();
|
||||
$statusCode = $response->getStatusCode();
|
||||
$content = $body->getContents();
|
||||
if ($content == '' || stripos($content, '<title>系统发生错误</title>') !== false || $statusCode != 200) {
|
||||
return true;
|
||||
}
|
||||
if (str_starts_with($content, '{')) {
|
||||
$json = json_decode($content, true);
|
||||
if ($json && $json['code'] == 0) {
|
||||
throw new Exception($json['msg'], $json['code'], $json['data'] ?? []);
|
||||
}
|
||||
}
|
||||
} catch (TransferException $e) {
|
||||
throw new Exception('package check failed', 0, ['msg' => $e->getMessage()]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function getConfig(string $dir, $key = ''): array
|
||||
{
|
||||
$configFile = $dir . 'config.json';
|
||||
if (!is_dir($dir) || !is_file($configFile)) {
|
||||
return [];
|
||||
}
|
||||
$configContent = @file_get_contents($configFile);
|
||||
$configContent = json_decode($configContent, true);
|
||||
if (!$configContent) {
|
||||
return [];
|
||||
}
|
||||
if ($key) {
|
||||
return $configContent[$key] ?? [];
|
||||
}
|
||||
return $configContent;
|
||||
}
|
||||
|
||||
public static function getDepend(string $dir, string $key = ''): array
|
||||
{
|
||||
if ($key) {
|
||||
return self::getConfig($dir, $key);
|
||||
}
|
||||
$configContent = self::getConfig($dir);
|
||||
$dependKey = ['require', 'require-dev', 'dependencies', 'devDependencies', 'nuxtDependencies', 'nuxtDevDependencies'];
|
||||
$dependArray = [];
|
||||
foreach ($dependKey as $item) {
|
||||
if (array_key_exists($item, $configContent) && $configContent[$item]) {
|
||||
$dependArray[$item] = $configContent[$item];
|
||||
}
|
||||
}
|
||||
return $dependArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* 依赖冲突检查
|
||||
* @throws Throwable
|
||||
*/
|
||||
public static function dependConflictCheck(string $dir): array
|
||||
{
|
||||
$depend = self::getDepend($dir);
|
||||
$serverDep = new Depends(root_path() . 'composer.json', 'composer');
|
||||
$webDep = new Depends(root_path() . 'web' . DIRECTORY_SEPARATOR . 'package.json');
|
||||
$webNuxtDep = new Depends(root_path() . 'web-nuxt' . DIRECTORY_SEPARATOR . 'package.json');
|
||||
$sysDepend = [
|
||||
'require' => $serverDep->getDepends(),
|
||||
'require-dev' => $serverDep->getDepends(true),
|
||||
'dependencies' => $webDep->getDepends(),
|
||||
'devDependencies' => $webDep->getDepends(true),
|
||||
'nuxtDependencies' => $webNuxtDep->getDepends(),
|
||||
'nuxtDevDependencies' => $webNuxtDep->getDepends(true),
|
||||
];
|
||||
|
||||
$conflict = [];
|
||||
foreach ($depend as $key => $item) {
|
||||
$conflict[$key] = array_uintersect_assoc($item, $sysDepend[$key], function ($a, $b) {
|
||||
return $a == $b ? -1 : 0;
|
||||
});
|
||||
}
|
||||
return $conflict;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模块[冲突]文件列表
|
||||
* @param string $dir 模块目录
|
||||
* @param bool $onlyConflict 是否只获取冲突文件
|
||||
*/
|
||||
public static function getFileList(string $dir, bool $onlyConflict = false): array
|
||||
{
|
||||
if (!is_dir($dir)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$fileList = [];
|
||||
$overwriteDir = self::getOverwriteDir();
|
||||
$moduleFileList = self::getRuntime($dir, 'files');
|
||||
|
||||
if ($moduleFileList) {
|
||||
// 有冲突的文件
|
||||
if ($onlyConflict) {
|
||||
// 排除的文件
|
||||
$excludeFile = [
|
||||
'info.ini'
|
||||
];
|
||||
foreach ($moduleFileList as $file) {
|
||||
// 如果是要安装到项目的文件,从项目根目录开始,如果不是,从模块根目录开始
|
||||
$path = Filesystem::fsFit(str_replace($dir, '', $file['path']));
|
||||
$paths = explode(DIRECTORY_SEPARATOR, $path);
|
||||
$overwriteFile = in_array($paths[0], $overwriteDir) ? root_path() . $path : $dir . $path;
|
||||
if (is_file($overwriteFile) && !in_array($path, $excludeFile) && (filesize($overwriteFile) != $file['size'] || md5_file($overwriteFile) != $file['md5'])) {
|
||||
$fileList[] = $path;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 要安装的文件
|
||||
foreach ($overwriteDir as $item) {
|
||||
$baseDir = $dir . $item;
|
||||
foreach ($moduleFileList as $file) {
|
||||
if (!str_starts_with($file['path'], $baseDir)) continue;
|
||||
$fileList[] = Filesystem::fsFit(str_replace($dir, '', $file['path']));
|
||||
}
|
||||
}
|
||||
}
|
||||
return $fileList;
|
||||
}
|
||||
|
||||
foreach ($overwriteDir as $item) {
|
||||
$baseDir = $dir . $item;
|
||||
if (!is_dir($baseDir)) {
|
||||
continue;
|
||||
}
|
||||
$files = new RecursiveIteratorIterator(
|
||||
new RecursiveDirectoryIterator($baseDir, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST
|
||||
);
|
||||
foreach ($files as $file) {
|
||||
if ($file->isFile()) {
|
||||
$filePath = $file->getPathName();
|
||||
$path = str_replace($dir, '', $filePath);
|
||||
$path = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path);
|
||||
|
||||
if ($onlyConflict) {
|
||||
$overwriteFile = root_path() . $path;
|
||||
if (is_file($overwriteFile) && (filesize($overwriteFile) != filesize($filePath) || md5_file($overwriteFile) != md5_file($filePath))) {
|
||||
$fileList[] = $path;
|
||||
}
|
||||
} else {
|
||||
$fileList[] = $path;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $fileList;
|
||||
}
|
||||
|
||||
public static function getOverwriteDir(): array
|
||||
{
|
||||
return [
|
||||
'app',
|
||||
'config',
|
||||
'database',
|
||||
'extend',
|
||||
'modules',
|
||||
'public',
|
||||
'vendor',
|
||||
'web',
|
||||
'web-nuxt',
|
||||
];
|
||||
}
|
||||
|
||||
public static function importSql(string $dir): bool
|
||||
{
|
||||
$sqlFile = $dir . 'install.sql';
|
||||
$tempLine = '';
|
||||
if (is_file($sqlFile)) {
|
||||
$lines = file($sqlFile);
|
||||
foreach ($lines as $line) {
|
||||
if (str_starts_with($line, '--') || $line == '' || str_starts_with($line, '/*')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$tempLine .= $line;
|
||||
if (str_ends_with(trim($line), ';')) {
|
||||
$tempLine = str_ireplace('__PREFIX__', Config::get('database.connections.mysql.prefix'), $tempLine);
|
||||
$tempLine = str_ireplace('INSERT INTO ', 'INSERT IGNORE INTO ', $tempLine);
|
||||
try {
|
||||
Db::execute($tempLine);
|
||||
} catch (PDOException) {
|
||||
// $e->getMessage();
|
||||
}
|
||||
$tempLine = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function installedList(string $dir): array
|
||||
{
|
||||
if (!is_dir($dir)) {
|
||||
return [];
|
||||
}
|
||||
$installedDir = scandir($dir);
|
||||
$installedList = [];
|
||||
foreach ($installedDir as $item) {
|
||||
if ($item === '.' or $item === '..' || is_file($dir . $item)) {
|
||||
continue;
|
||||
}
|
||||
$tempDir = $dir . $item . DIRECTORY_SEPARATOR;
|
||||
if (!is_dir($tempDir)) {
|
||||
continue;
|
||||
}
|
||||
$info = self::getIni($tempDir);
|
||||
if (!isset($info['uid'])) {
|
||||
continue;
|
||||
}
|
||||
$installedList[] = $info;
|
||||
}
|
||||
return $installedList;
|
||||
}
|
||||
|
||||
public static function getInstalledIds(string $dir): array
|
||||
{
|
||||
$installedIds = [];
|
||||
$installed = self::installedList($dir);
|
||||
foreach ($installed as $item) {
|
||||
$installedIds[] = $item['uid'];
|
||||
}
|
||||
return $installedIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模块ini
|
||||
* @param string $dir 模块目录路径
|
||||
*/
|
||||
public static function getIni(string $dir): array
|
||||
{
|
||||
$infoFile = $dir . 'info.ini';
|
||||
$info = [];
|
||||
if (is_file($infoFile)) {
|
||||
$info = parse_ini_file($infoFile, true, INI_SCANNER_TYPED) ?: [];
|
||||
if (!$info) return [];
|
||||
}
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置模块ini
|
||||
* @param string $dir 模块目录路径
|
||||
* @param array $arr 新的ini数据
|
||||
* @return bool
|
||||
* @throws Throwable
|
||||
*/
|
||||
public static function setIni(string $dir, array $arr): bool
|
||||
{
|
||||
$infoFile = $dir . 'info.ini';
|
||||
$ini = [];
|
||||
foreach ($arr as $key => $val) {
|
||||
if (is_array($val)) {
|
||||
$ini[] = "[$key]";
|
||||
foreach ($val as $ikey => $ival) {
|
||||
$ini[] = "$ikey = $ival";
|
||||
}
|
||||
} else {
|
||||
$ini[] = "$key = $val";
|
||||
}
|
||||
}
|
||||
if (!file_put_contents($infoFile, implode("\n", $ini) . "\n", LOCK_EX)) {
|
||||
throw new Exception("Configuration file has no write permission");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function getClass(string $uid, string $type = 'event', ?string $class = null): string
|
||||
{
|
||||
$name = parse_name($uid);
|
||||
if (!is_null($class) && strpos($class, '.')) {
|
||||
$class = explode('.', $class);
|
||||
$class[count($class) - 1] = parse_name(end($class), 1);
|
||||
$class = implode('\\', $class);
|
||||
} else {
|
||||
$class = parse_name(is_null($class) ? $name : $class, 1);
|
||||
}
|
||||
$namespace = match ($type) {
|
||||
'controller' => '\\modules\\' . $name . '\\controller\\' . $class,
|
||||
default => '\\modules\\' . $name . '\\' . $class,
|
||||
};
|
||||
return class_exists($namespace) ? $namespace : '';
|
||||
}
|
||||
|
||||
public static function execEvent(string $uid, string $event, array $params = []): void
|
||||
{
|
||||
$eventClass = self::getClass($uid);
|
||||
if (class_exists($eventClass)) {
|
||||
$handle = new $eventClass();
|
||||
if (method_exists($eventClass, $event)) {
|
||||
$handle->$event($params);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 分析 WebBootstrap 代码
|
||||
*/
|
||||
public static function analysisWebBootstrap(string $uid, string $dir): array
|
||||
{
|
||||
$bootstrapFile = $dir . 'webBootstrap.stub';
|
||||
if (!file_exists($bootstrapFile)) return [];
|
||||
$bootstrapContent = file_get_contents($bootstrapFile);
|
||||
$pregArr = [
|
||||
'mainTsImport' => '/#main.ts import code start#([\s\S]*?)#main.ts import code end#/i',
|
||||
'mainTsStart' => '/#main.ts start code start#([\s\S]*?)#main.ts start code end#/i',
|
||||
'appVueImport' => '/#App.vue import code start#([\s\S]*?)#App.vue import code end#/i',
|
||||
'appVueOnMounted' => '/#App.vue onMounted code start#([\s\S]*?)#App.vue onMounted code end#/i',
|
||||
'nuxtAppVueImport' => '/#web-nuxt\/app.vue import code start#([\s\S]*?)#web-nuxt\/app.vue import code end#/i',
|
||||
'nuxtAppVueStart' => '/#web-nuxt\/app.vue start code start#([\s\S]*?)#web-nuxt\/app.vue start code end#/i',
|
||||
];
|
||||
$codeStrArr = [];
|
||||
foreach ($pregArr as $key => $item) {
|
||||
preg_match($item, $bootstrapContent, $matches);
|
||||
if (isset($matches[1]) && $matches[1]) {
|
||||
$mainImportCodeArr = array_filter(preg_split('/\r\n|\r|\n/', $matches[1]));
|
||||
if ($mainImportCodeArr) {
|
||||
$codeStrArr[$key] = "\n";
|
||||
if (count($mainImportCodeArr) == 1) {
|
||||
foreach ($mainImportCodeArr as $codeItem) {
|
||||
$codeStrArr[$key] .= $codeItem . self::buildMarkStr('module-line-mark', $uid, $key);
|
||||
}
|
||||
} else {
|
||||
$codeStrArr[$key] .= self::buildMarkStr('module-multi-line-mark-start', $uid, $key);
|
||||
foreach ($mainImportCodeArr as $codeItem) {
|
||||
$codeStrArr[$key] .= $codeItem . "\n";
|
||||
}
|
||||
$codeStrArr[$key] .= self::buildMarkStr('module-multi-line-mark-end', $uid, $key);
|
||||
}
|
||||
}
|
||||
}
|
||||
unset($matches);
|
||||
}
|
||||
|
||||
return $codeStrArr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 安装 WebBootstrap
|
||||
*/
|
||||
public static function installWebBootstrap(string $uid, string $dir): void
|
||||
{
|
||||
$bootstrapCode = self::analysisWebBootstrap($uid, $dir);
|
||||
if (!$bootstrapCode) {
|
||||
return;
|
||||
}
|
||||
|
||||
$webPath = root_path() . 'web' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR;
|
||||
$webNuxtPath = root_path() . 'web-nuxt' . DIRECTORY_SEPARATOR;
|
||||
$filePaths = [
|
||||
'mainTsImport' => $webPath . 'main.ts',
|
||||
'mainTsStart' => $webPath . 'main.ts',
|
||||
'appVueImport' => $webPath . 'App.vue',
|
||||
'appVueOnMounted' => $webPath . 'App.vue',
|
||||
'nuxtAppVueImport' => $webNuxtPath . 'app.vue',
|
||||
'nuxtAppVueStart' => $webNuxtPath . 'app.vue',
|
||||
];
|
||||
|
||||
$marks = [
|
||||
'mainTsImport' => self::buildMarkStr('import-root-mark'),
|
||||
'mainTsStart' => self::buildMarkStr('start-root-mark'),
|
||||
'appVueImport' => self::buildMarkStr('import-root-mark'),
|
||||
'appVueOnMounted' => self::buildMarkStr('onMounted-root-mark'),
|
||||
'nuxtAppVueImport' => self::buildMarkStr('import-root-mark'),
|
||||
'nuxtAppVueStart' => self::buildMarkStr('start-root-mark'),
|
||||
];
|
||||
|
||||
foreach ($bootstrapCode as $key => $item) {
|
||||
if ($item && isset($marks[$key])) {
|
||||
$content = file_get_contents($filePaths[$key]);
|
||||
$markPos = stripos($content, $marks[$key]);
|
||||
if ($markPos && strripos($content, self::buildMarkStr('module-line-mark', $uid, $key)) === false && strripos($content, self::buildMarkStr('module-multi-line-mark-start', $uid, $key)) === false) {
|
||||
$content = substr_replace($content, $item, $markPos + strlen($marks[$key]), 0);
|
||||
file_put_contents($filePaths[$key], $content);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 卸载 WebBootstrap
|
||||
*/
|
||||
public static function uninstallWebBootstrap(string $uid): void
|
||||
{
|
||||
$webPath = root_path() . 'web' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR;
|
||||
$webNuxtPath = root_path() . 'web-nuxt' . DIRECTORY_SEPARATOR;
|
||||
$filePaths = [
|
||||
'mainTsImport' => $webPath . 'main.ts',
|
||||
'mainTsStart' => $webPath . 'main.ts',
|
||||
'appVueImport' => $webPath . 'App.vue',
|
||||
'appVueOnMounted' => $webPath . 'App.vue',
|
||||
'nuxtAppVueImport' => $webNuxtPath . 'app.vue',
|
||||
'nuxtAppVueStart' => $webNuxtPath . 'app.vue',
|
||||
];
|
||||
|
||||
$marksKey = [
|
||||
'mainTsImport',
|
||||
'mainTsStart',
|
||||
'appVueImport',
|
||||
'appVueOnMounted',
|
||||
'nuxtAppVueImport',
|
||||
'nuxtAppVueStart',
|
||||
];
|
||||
|
||||
foreach ($marksKey as $item) {
|
||||
if (!is_file($filePaths[$item])) {
|
||||
continue;
|
||||
}
|
||||
$content = file_get_contents($filePaths[$item]);
|
||||
$moduleLineMark = self::buildMarkStr('module-line-mark', $uid, $item);
|
||||
$moduleMultiLineMarkStart = self::buildMarkStr('module-multi-line-mark-start', $uid, $item);
|
||||
$moduleMultiLineMarkEnd = self::buildMarkStr('module-multi-line-mark-end', $uid, $item);
|
||||
|
||||
// 寻找标记,找到则将其中内容删除
|
||||
$moduleLineMarkPos = strripos($content, $moduleLineMark);
|
||||
if ($moduleLineMarkPos !== false) {
|
||||
$delStartTemp = explode($moduleLineMark, $content);
|
||||
$delStartPos = strripos(rtrim($delStartTemp[0], "\n"), "\n");
|
||||
$delEndPos = stripos($content, "\n", $moduleLineMarkPos);
|
||||
$content = substr_replace($content, '', $delStartPos, $delEndPos - $delStartPos);
|
||||
}
|
||||
|
||||
$moduleMultiLineMarkStartPos = stripos($content, $moduleMultiLineMarkStart);
|
||||
if ($moduleMultiLineMarkStartPos !== false) {
|
||||
$moduleMultiLineMarkStartPos--;
|
||||
$moduleMultiLineMarkEndPos = stripos($content, $moduleMultiLineMarkEnd);
|
||||
$delLang = ($moduleMultiLineMarkEndPos + strlen($moduleMultiLineMarkEnd)) - $moduleMultiLineMarkStartPos;
|
||||
$content = substr_replace($content, '', $moduleMultiLineMarkStartPos, $delLang);
|
||||
}
|
||||
|
||||
if ($moduleLineMarkPos || $moduleMultiLineMarkStartPos) {
|
||||
file_put_contents($filePaths[$item], $content);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建 WebBootstrap 需要的各种标记字符串
|
||||
* @param string $type
|
||||
* @param string $uid 模块UID
|
||||
* @param string $extend 扩展数据
|
||||
* @return string
|
||||
*/
|
||||
public static function buildMarkStr(string $type, string $uid = '', string $extend = ''): string
|
||||
{
|
||||
$nonTabKeys = ['mti', 'avi', 'navi', 'navs'];
|
||||
$extend = match ($extend) {
|
||||
'mainTsImport' => 'mti',
|
||||
'mainTsStart' => 'mts',
|
||||
'appVueImport' => 'avi',
|
||||
'appVueOnMounted' => 'avo',
|
||||
'nuxtAppVueImport' => 'navi',
|
||||
'nuxtAppVueStart' => 'navs',
|
||||
default => '',
|
||||
};
|
||||
return match ($type) {
|
||||
'import-root-mark' => '// modules import mark, Please do not remove.',
|
||||
'start-root-mark' => '// modules start mark, Please do not remove.',
|
||||
'onMounted-root-mark' => '// Modules onMounted mark, Please do not remove.',
|
||||
'module-line-mark' => ' // Code from module \'' . $uid . "'" . ($extend ? "($extend)" : ''),
|
||||
'module-multi-line-mark-start' => (in_array($extend, $nonTabKeys) ? '' : Helper::tab()) . "// Code from module '$uid' start" . ($extend ? "($extend)" : '') . "\n",
|
||||
'module-multi-line-mark-end' => (in_array($extend, $nonTabKeys) ? '' : Helper::tab()) . "// Code from module '$uid' end",
|
||||
default => '',
|
||||
};
|
||||
}
|
||||
|
||||
public static function getNuxtVersion()
|
||||
{
|
||||
$nuxtPackageJsonPath = Filesystem::fsFit(root_path() . 'web-nuxt/package.json');
|
||||
if (is_file($nuxtPackageJsonPath)) {
|
||||
$nuxtPackageJson = file_get_contents($nuxtPackageJsonPath);
|
||||
$nuxtPackageJson = json_decode($nuxtPackageJson, true);
|
||||
if ($nuxtPackageJson && isset($nuxtPackageJson['version'])) {
|
||||
return $nuxtPackageJson['version'];
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 .runtime
|
||||
*/
|
||||
public static function createRuntime(string $dir): void
|
||||
{
|
||||
$runtimeFilePath = $dir . '.runtime';
|
||||
$files = new RecursiveIteratorIterator(
|
||||
new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::LEAVES_ONLY
|
||||
);
|
||||
$filePaths = [];
|
||||
foreach ($files as $file) {
|
||||
if (!$file->isDir()) {
|
||||
$pathName = $file->getPathName();
|
||||
if ($pathName == $runtimeFilePath) continue;
|
||||
$filePaths[] = [
|
||||
'path' => Filesystem::fsFit($pathName),
|
||||
'size' => filesize($pathName),
|
||||
'md5' => md5_file($pathName),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
file_put_contents($runtimeFilePath, json_encode([
|
||||
'files' => $filePaths,
|
||||
'pure' => Config::get('buildadmin.module_pure_install'),
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取 .runtime
|
||||
*/
|
||||
public static function getRuntime(string $dir, string $key = ''): mixed
|
||||
{
|
||||
$runtimeFilePath = $dir . '.runtime';
|
||||
$runtimeContent = @file_get_contents($runtimeFilePath);
|
||||
$runtimeContentArr = json_decode($runtimeContent, true);
|
||||
if (!$runtimeContentArr) return [];
|
||||
|
||||
if ($key) {
|
||||
return $runtimeContentArr[$key] ?? [];
|
||||
} else {
|
||||
return $runtimeContentArr;
|
||||
}
|
||||
}
|
||||
}
|
||||
24
app/admin/library/stubs/backendEntrance.stub
Normal file
24
app/admin/library/stubs/backendEntrance.stub
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | BUILDADMIN
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2022-2023 https://buildadmin.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 妙码生花 <hi@buildadmin.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
// [ 应用入口文件 ]
|
||||
namespace think;
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
// 执行HTTP应用并响应
|
||||
$http = (new App())->http;
|
||||
|
||||
$response = $http->name('admin')->run();
|
||||
|
||||
$response->send();
|
||||
|
||||
$http->end($response);
|
||||
301
app/admin/library/traits/Backend.php
Normal file
301
app/admin/library/traits/Backend.php
Normal file
@@ -0,0 +1,301 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\library\traits;
|
||||
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* 后台控制器trait类
|
||||
* 已导入到 @see \app\common\controller\Backend 中
|
||||
* 若需修改此类方法:请复制方法至对应控制器后进行重写
|
||||
*/
|
||||
trait Backend
|
||||
{
|
||||
/**
|
||||
* 排除入库字段
|
||||
* @param array $params
|
||||
* @return array
|
||||
*/
|
||||
protected function excludeFields(array $params): array
|
||||
{
|
||||
if (!is_array($this->preExcludeFields)) {
|
||||
$this->preExcludeFields = explode(',', (string)$this->preExcludeFields);
|
||||
}
|
||||
|
||||
foreach ($this->preExcludeFields as $field) {
|
||||
if (array_key_exists($field, $params)) {
|
||||
unset($params[$field]);
|
||||
}
|
||||
}
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function index(): void
|
||||
{
|
||||
if ($this->request->param('select')) {
|
||||
$this->select();
|
||||
}
|
||||
|
||||
list($where, $alias, $limit, $order) = $this->queryBuilder();
|
||||
$res = $this->model
|
||||
->field($this->indexField)
|
||||
->withJoin($this->withJoinTable, $this->withJoinType)
|
||||
->alias($alias)
|
||||
->where($where)
|
||||
->order($order)
|
||||
->paginate($limit);
|
||||
|
||||
$this->success('', [
|
||||
'list' => $res->items(),
|
||||
'total' => $res->total(),
|
||||
'remark' => get_route_remark(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加
|
||||
*/
|
||||
public function add(): void
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->request->post();
|
||||
if (!$data) {
|
||||
$this->error(__('Parameter %s can not be empty', ['']));
|
||||
}
|
||||
|
||||
$data = $this->excludeFields($data);
|
||||
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
|
||||
$data[$this->dataLimitField] = $this->auth->id;
|
||||
}
|
||||
|
||||
$result = false;
|
||||
$this->model->startTrans();
|
||||
try {
|
||||
// 模型验证
|
||||
if ($this->modelValidate) {
|
||||
$validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
|
||||
if (class_exists($validate)) {
|
||||
$validate = new $validate();
|
||||
if ($this->modelSceneValidate) $validate->scene('add');
|
||||
$validate->check($data);
|
||||
}
|
||||
}
|
||||
$result = $this->model->save($data);
|
||||
$this->model->commit();
|
||||
} catch (Throwable $e) {
|
||||
$this->model->rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if ($result !== false) {
|
||||
$this->success(__('Added successfully'));
|
||||
} else {
|
||||
$this->error(__('No rows were added'));
|
||||
}
|
||||
}
|
||||
|
||||
$this->error(__('Parameter error'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function edit(): void
|
||||
{
|
||||
$pk = $this->model->getPk();
|
||||
$id = $this->request->param($pk);
|
||||
$row = $this->model->find($id);
|
||||
if (!$row) {
|
||||
$this->error(__('Record not found'));
|
||||
}
|
||||
|
||||
$dataLimitAdminIds = $this->getDataLimitAdminIds();
|
||||
if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
|
||||
$this->error(__('You have no permission'));
|
||||
}
|
||||
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->request->post();
|
||||
if (!$data) {
|
||||
$this->error(__('Parameter %s can not be empty', ['']));
|
||||
}
|
||||
|
||||
$data = $this->excludeFields($data);
|
||||
$result = false;
|
||||
$this->model->startTrans();
|
||||
try {
|
||||
// 模型验证
|
||||
if ($this->modelValidate) {
|
||||
$validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
|
||||
if (class_exists($validate)) {
|
||||
$validate = new $validate();
|
||||
if ($this->modelSceneValidate) $validate->scene('edit');
|
||||
$data[$pk] = $row[$pk];
|
||||
$validate->check($data);
|
||||
}
|
||||
}
|
||||
$result = $row->save($data);
|
||||
$this->model->commit();
|
||||
} catch (Throwable $e) {
|
||||
$this->model->rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if ($result !== false) {
|
||||
$this->success(__('Update successful'));
|
||||
} else {
|
||||
$this->error(__('No rows updated'));
|
||||
}
|
||||
}
|
||||
|
||||
$this->success('', [
|
||||
'row' => $row
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function del(): void
|
||||
{
|
||||
$where = [];
|
||||
$dataLimitAdminIds = $this->getDataLimitAdminIds();
|
||||
if ($dataLimitAdminIds) {
|
||||
$where[] = [$this->dataLimitField, 'in', $dataLimitAdminIds];
|
||||
}
|
||||
|
||||
$ids = $this->request->param('ids/a', []);
|
||||
$where[] = [$this->model->getPk(), 'in', $ids];
|
||||
$data = $this->model->where($where)->select();
|
||||
|
||||
$count = 0;
|
||||
$this->model->startTrans();
|
||||
try {
|
||||
foreach ($data as $v) {
|
||||
$count += $v->delete();
|
||||
}
|
||||
$this->model->commit();
|
||||
} catch (Throwable $e) {
|
||||
$this->model->rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if ($count) {
|
||||
$this->success(__('Deleted successfully'));
|
||||
} else {
|
||||
$this->error(__('No rows were deleted'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 排序 - 增量重排法
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function sortable(): void
|
||||
{
|
||||
$pk = $this->model->getPk();
|
||||
$move = $this->request->param('move');
|
||||
$target = $this->request->param('target');
|
||||
$order = $this->request->param("order/s") ?: $this->defaultSortField;
|
||||
$direction = $this->request->param('direction');
|
||||
|
||||
$dataLimitWhere = [];
|
||||
$dataLimitAdminIds = $this->getDataLimitAdminIds();
|
||||
if ($dataLimitAdminIds) {
|
||||
$dataLimitWhere[] = [$this->dataLimitField, 'in', $dataLimitAdminIds];
|
||||
}
|
||||
|
||||
$moveRow = $this->model->where($dataLimitWhere)->find($move);
|
||||
$targetRow = $this->model->where($dataLimitWhere)->find($target);
|
||||
|
||||
if ($move == $target || !$moveRow || !$targetRow || !$direction) {
|
||||
$this->error(__('Record not found'));
|
||||
}
|
||||
|
||||
// 当前是否以权重字段排序(只检查当前排序和默认排序字段,不检查有序保证字段)
|
||||
if ($order && is_string($order)) {
|
||||
$order = explode(',', $order);
|
||||
$order = [$order[0] => $order[1] ?? 'asc'];
|
||||
}
|
||||
if (!array_key_exists($this->weighField, $order)) {
|
||||
$this->error(__('Please use the %s field to sort before operating', [$this->weighField]));
|
||||
}
|
||||
|
||||
// 开始增量重排
|
||||
$order = $this->queryOrderBuilder();
|
||||
$weigh = $targetRow[$this->weighField];
|
||||
|
||||
// 波及行的权重值向上增加还是向下减少
|
||||
if ($order[$this->weighField] == 'desc') {
|
||||
$updateMethod = $direction == 'up' ? 'dec' : 'inc';
|
||||
} else {
|
||||
$updateMethod = $direction == 'up' ? 'inc' : 'dec';
|
||||
}
|
||||
|
||||
// 与目标行权重相同的行
|
||||
$weighRowIds = $this->model
|
||||
->where($dataLimitWhere)
|
||||
->where($this->weighField, $weigh)
|
||||
->order($order)
|
||||
->column($pk);
|
||||
$weighRowsCount = count($weighRowIds);
|
||||
|
||||
// 单个 SQL 查询中完成大于目标权重行的修改
|
||||
$this->model->where($dataLimitWhere)
|
||||
->where($this->weighField, $updateMethod == 'dec' ? '<' : '>', $weigh)
|
||||
->whereNotIn($pk, [$moveRow->$pk])
|
||||
->$updateMethod($this->weighField, $weighRowsCount)
|
||||
->save();
|
||||
|
||||
// 遍历与目标行权重相同的行,每出现一行权重值将额外 +1,保证权重相同行的顺序位置不变
|
||||
if ($direction == 'down') {
|
||||
$weighRowIds = array_reverse($weighRowIds);
|
||||
}
|
||||
|
||||
$moveComplete = 0;
|
||||
$weighRowIds = implode(',', $weighRowIds);
|
||||
$weighRows = $this->model->where($dataLimitWhere)
|
||||
->where($pk, 'in', $weighRowIds)
|
||||
->orderRaw("field($pk,$weighRowIds)")
|
||||
->select();
|
||||
|
||||
// 权重相等行
|
||||
foreach ($weighRows as $key => $weighRow) {
|
||||
// 跳过当前拖动行(相等权重数据之间的拖动时,被拖动行会出现在 $weighRows 内)
|
||||
if ($moveRow[$pk] == $weighRow[$pk]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($updateMethod == 'dec') {
|
||||
$rowWeighVal = $weighRow[$this->weighField] - $key;
|
||||
} else {
|
||||
$rowWeighVal = $weighRow[$this->weighField] + $key;
|
||||
}
|
||||
|
||||
// 找到了目标行
|
||||
if ($weighRow[$pk] == $targetRow[$pk]) {
|
||||
$moveComplete = 1;
|
||||
$moveRow[$this->weighField] = $rowWeighVal;
|
||||
$moveRow->save();
|
||||
}
|
||||
|
||||
$rowWeighVal = $updateMethod == 'dec' ? $rowWeighVal - $moveComplete : $rowWeighVal + $moveComplete;
|
||||
$weighRow[$this->weighField] = $rowWeighVal;
|
||||
$weighRow->save();
|
||||
}
|
||||
|
||||
$this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载为select(远程下拉选择框)数据,默认还是走$this->index()方法
|
||||
* 必要时请在对应控制器类中重写
|
||||
*/
|
||||
public function select(): void
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user