webman迁移
This commit is contained in:
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace app\admin\controller;
|
||||
|
||||
use ba\ClickCaptcha;
|
||||
use ba\Random;
|
||||
use app\common\facade\Token;
|
||||
use app\admin\model\AdminLog;
|
||||
use app\common\controller\Backend;
|
||||
@@ -107,8 +108,14 @@ class Index extends Backend
|
||||
|
||||
$res = $this->auth->login($username, $password, (bool) $keep);
|
||||
if ($res === true) {
|
||||
$userInfo = $this->auth->getInfo();
|
||||
// 兜底:若 getInfo 未返回 token,在控制器层生成并入库
|
||||
if (empty($userInfo['token']) && $this->auth->isLogin()) {
|
||||
$userInfo['token'] = Random::uuid();
|
||||
Token::set($userInfo['token'], \app\admin\library\Auth::TOKEN_TYPE, $this->auth->id, (int) config('buildadmin.admin_token_keep_time', 86400 * 3));
|
||||
}
|
||||
return $this->success(__('Login succeeded!'), [
|
||||
'userInfo' => $this->auth->getInfo()
|
||||
'userInfo' => $userInfo
|
||||
]);
|
||||
}
|
||||
$msg = $this->auth->getError();
|
||||
|
||||
@@ -222,8 +222,19 @@ class Auth extends \ba\Auth
|
||||
}
|
||||
$info = $this->model->toArray();
|
||||
$info = array_intersect_key($info, array_flip($this->getAllowFields()));
|
||||
$info['token'] = $this->getToken();
|
||||
$info['refresh_token'] = $this->getRefreshToken();
|
||||
// 与 ThinkPHP 一致:token 为主认证令牌,refresh_token 仅 keep=true 时有值
|
||||
$token = $this->token;
|
||||
if (!$token && $this->loginEd) {
|
||||
$token = Random::uuid();
|
||||
Token::set($token, self::TOKEN_TYPE, $this->model->id, $this->keepTime);
|
||||
$this->token = $token;
|
||||
}
|
||||
$info['token'] = $token ?: '';
|
||||
$info['refresh_token'] = $this->refreshToken ?: '';
|
||||
// last_login_time 与 ThinkPHP 一致返回整数时间戳
|
||||
if (isset($info['last_login_time'])) {
|
||||
$info['last_login_time'] = (int) $info['last_login_time'];
|
||||
}
|
||||
return $info;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,6 @@
|
||||
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');
|
||||
$cityNames = \support\think\Db::name('area')->whereIn('id', $row['{%originalFieldName%}'])->column('name');
|
||||
return $cityNames ? implode(',', $cityNames) : '';
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace {%namespace%};
|
||||
|
||||
use think\Model;
|
||||
use support\think\Model;
|
||||
|
||||
/**
|
||||
* {%className%}
|
||||
|
||||
@@ -47,7 +47,7 @@ class AdminLog extends Model
|
||||
}
|
||||
|
||||
/** 设置日志内容(BuildAdmin 控制器调用) */
|
||||
public function setData(string|array $data): void
|
||||
public function setLogData(string|array $data): void
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
@@ -143,9 +143,12 @@ class Install extends Api
|
||||
}
|
||||
// php版本-end
|
||||
|
||||
// 配置文件-start
|
||||
$dbConfigFile = config_path() . self::$dbConfigFileName;
|
||||
$configIsWritable = Filesystem::pathIsWritable(config_path()) && Filesystem::pathIsWritable($dbConfigFile);
|
||||
// 配置文件-start(分别检测目录和文件,便于定位问题)
|
||||
$configDir = rtrim(config_path(), '/\\');
|
||||
$dbConfigFile = $configDir . DIRECTORY_SEPARATOR . self::$dbConfigFileName;
|
||||
$configDirWritable = Filesystem::pathIsWritable($configDir);
|
||||
$dbConfigWritable = Filesystem::pathIsWritable($dbConfigFile);
|
||||
$configIsWritable = $configDirWritable && $dbConfigWritable;
|
||||
if (!$configIsWritable) {
|
||||
$configIsWritableLink = [
|
||||
[
|
||||
@@ -241,7 +244,9 @@ class Install extends Api
|
||||
'link' => $phpVersionLink ?? [],
|
||||
],
|
||||
'config_is_writable' => [
|
||||
'describe' => self::writableStateDescribe($configIsWritable),
|
||||
'describe' => $configIsWritable
|
||||
? self::writableStateDescribe(true)
|
||||
: (self::writableStateDescribe(false) . ' [' . $configDir . ']'),
|
||||
'state' => $configIsWritable ? self::$ok : self::$fail,
|
||||
'link' => $configIsWritableLink ?? []
|
||||
],
|
||||
@@ -408,12 +413,14 @@ class Install extends Api
|
||||
return $this->error(__('The system has completed installation. If you need to reinstall, please delete the %s file first', ['public/' . self::$lockFileName]));
|
||||
}
|
||||
|
||||
$envOk = $this->commandExecutionCheck();
|
||||
$rootPath = str_replace('\\', '/', root_path());
|
||||
$envOk = $this->commandExecutionCheck();
|
||||
$rootPath = str_replace('\\', '/', root_path());
|
||||
$migrateCommand = 'php vendor/bin/phinx migrate';
|
||||
if ($request->isGet()) {
|
||||
return $this->success('', [
|
||||
'rootPath' => $rootPath,
|
||||
'executionWebCommand' => $envOk
|
||||
'executionWebCommand' => $envOk,
|
||||
'migrateCommand' => $migrateCommand,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -433,8 +440,11 @@ class Install extends Api
|
||||
}
|
||||
|
||||
// 写入数据库配置文件(thinkorm.php 使用 $env('database.xxx', 'default') 格式)
|
||||
$dbConfigFile = config_path() . self::$dbConfigFileName;
|
||||
$dbConfigFile = config_path(self::$dbConfigFileName);
|
||||
$dbConfigContent = @file_get_contents($dbConfigFile);
|
||||
if ($dbConfigContent === false || $dbConfigContent === '') {
|
||||
return $this->error(__('File has no write permission:%s', ['config/' . self::$dbConfigFileName]));
|
||||
}
|
||||
$callback = function ($matches) use ($databaseParam) {
|
||||
$key = $matches[1];
|
||||
$value = (string) ($databaseParam[$key] ?? '');
|
||||
@@ -446,7 +456,7 @@ class Install extends Api
|
||||
return $this->error(__('File has no write permission:%s', ['config/' . self::$dbConfigFileName]));
|
||||
}
|
||||
|
||||
// 写入.env-example文件
|
||||
// 写入 dafuweng-webman/.env-example
|
||||
$envFile = root_path() . '.env-example';
|
||||
$envFileContent = @file_get_contents($envFile);
|
||||
if ($envFileContent) {
|
||||
@@ -473,8 +483,11 @@ class Install extends Api
|
||||
// 设置新的Token随机密钥key
|
||||
$oldTokenKey = config('buildadmin.token.key');
|
||||
$newTokenKey = Random::build('alnum', 32);
|
||||
$buildConfigFile = config_path() . self::$buildConfigFileName;
|
||||
$buildConfigFile = config_path(self::$buildConfigFileName);
|
||||
$buildConfigContent = @file_get_contents($buildConfigFile);
|
||||
if ($buildConfigContent === false || $buildConfigContent === '') {
|
||||
return $this->error(__('File has no write permission:%s', ['config/' . self::$buildConfigFileName]));
|
||||
}
|
||||
$buildConfigContent = preg_replace("/'key'(\s+)=>(\s+)'$oldTokenKey'/", "'key'\$1=>\$2'$newTokenKey'", $buildConfigContent);
|
||||
$result = @file_put_contents($buildConfigFile, $buildConfigContent);
|
||||
if (!$result) {
|
||||
@@ -482,21 +495,22 @@ class Install extends Api
|
||||
}
|
||||
|
||||
// 建立安装锁文件
|
||||
$result = @file_put_contents(public_path() . self::$lockFileName, date('Y-m-d H:i:s'));
|
||||
$result = @file_put_contents(public_path(self::$lockFileName), date('Y-m-d H:i:s'));
|
||||
if (!$result) {
|
||||
return $this->error(__('File has no write permission:%s', ['public/' . self::$lockFileName]));
|
||||
}
|
||||
|
||||
return $this->success('', [
|
||||
'rootPath' => $rootPath,
|
||||
'executionWebCommand' => $envOk
|
||||
'executionWebCommand' => $envOk,
|
||||
'migrateCommand' => $migrateCommand,
|
||||
]);
|
||||
}
|
||||
|
||||
protected function isInstallComplete(): bool
|
||||
{
|
||||
if (is_file(public_path() . self::$lockFileName)) {
|
||||
$contents = @file_get_contents(public_path() . self::$lockFileName);
|
||||
if (is_file(public_path(self::$lockFileName))) {
|
||||
$contents = @file_get_contents(public_path(self::$lockFileName));
|
||||
if ($contents == self::$InstallationCompletionMark) {
|
||||
return true;
|
||||
}
|
||||
@@ -517,7 +531,7 @@ class Install extends Api
|
||||
|
||||
$param = $request->only(['type', 'adminname', 'adminpassword', 'sitename']);
|
||||
if ($param['type'] == 'web') {
|
||||
$result = @file_put_contents(public_path() . self::$lockFileName, self::$InstallationCompletionMark);
|
||||
$result = @file_put_contents(public_path(self::$lockFileName), self::$InstallationCompletionMark);
|
||||
if (!$result) {
|
||||
return $this->error(__('File has no write permission:%s', ['public/' . self::$lockFileName]));
|
||||
}
|
||||
@@ -628,20 +642,24 @@ class Install extends Api
|
||||
try {
|
||||
$pdo = new \PDO($dsn, $user, $pass, [
|
||||
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
|
||||
\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
|
||||
]);
|
||||
$pdo->exec("SELECT 1");
|
||||
$pdo->query("SELECT 1")->fetchAll(\PDO::FETCH_ASSOC);
|
||||
} catch (\PDOException $e) {
|
||||
$errorMsg = $e->getMessage();
|
||||
$errorMsg = mb_convert_encoding($e->getMessage() ?: 'unknown', 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');
|
||||
$template = __('Database connection failed:%s');
|
||||
return [
|
||||
'code' => 0,
|
||||
'msg' => __('Database connection failed:%s', [mb_convert_encoding($errorMsg ?: 'unknown', 'UTF-8', 'UTF-8,GBK,GB2312,BIG5')])
|
||||
'msg' => strpos($template, '%s') !== false ? sprintf($template, $errorMsg) : $template . $errorMsg,
|
||||
];
|
||||
}
|
||||
|
||||
$databases = [];
|
||||
$databasesExclude = ['information_schema', 'mysql', 'performance_schema', 'sys'];
|
||||
$stmt = $pdo->query("SHOW DATABASES");
|
||||
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
|
||||
$rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
$stmt->closeCursor();
|
||||
foreach ($rows as $row) {
|
||||
$dbName = $row['Database'] ?? $row['database'] ?? '';
|
||||
if ($dbName && !in_array($dbName, $databasesExclude)) {
|
||||
$databases[] = $dbName;
|
||||
|
||||
@@ -3,6 +3,15 @@
|
||||
* BuildAdmin Webman 公共函数
|
||||
*/
|
||||
|
||||
// mb_split 兼容:mbstring 扩展未启用时,Illuminate 的 Str::studly 会报错,用 preg_split 兜底
|
||||
if (!function_exists('mb_split')) {
|
||||
function mb_split(string $pattern, string $string, int $limit = -1): array
|
||||
{
|
||||
$result = @preg_split('#' . $pattern . '#u', $string, $limit);
|
||||
return $result !== false ? $result : [];
|
||||
}
|
||||
}
|
||||
|
||||
use support\Response;
|
||||
|
||||
if (!function_exists('env')) {
|
||||
|
||||
Reference in New Issue
Block a user