项目初始化

This commit is contained in:
2026-03-18 15:54:43 +08:00
commit dfcd762e23
601 changed files with 57883 additions and 0 deletions

64
app/admin/model/Admin.php Normal file
View File

@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
namespace app\admin\model;
use support\think\Model;
use support\think\Db;
/**
* Admin 模型Webman 迁移版)
* @property int $id 管理员ID
* @property string $username 管理员用户名
* @property string $nickname 管理员昵称
* @property string $email 管理员邮箱
* @property string $mobile 管理员手机号
* @property string $last_login_ip 上次登录IP
* @property string $last_login_time 上次登录时间
* @property int $login_failure 登录失败次数
* @property string $password 密码密文
* @property string $salt 密码盐
* @property string $status 状态:enable=启用,disable=禁用
*/
class Admin extends Model
{
protected string $table = 'admin';
protected string $pk = 'id';
protected bool $autoWriteTimestamp = true;
protected array $append = [
'group_arr',
'group_name_arr',
];
public function getGroupArrAttr($value, $row): array
{
return Db::name('admin_group_access')
->where('uid', $row['id'])
->column('group_id');
}
public function getGroupNameArrAttr($value, $row): array
{
$groupAccess = Db::name('admin_group_access')
->where('uid', $row['id'])
->column('group_id');
return AdminGroup::whereIn('id', $groupAccess)->column('name');
}
public function getAvatarAttr($value): string
{
return full_url($value ?? '', false, config('buildadmin.default_avatar'));
}
public function setAvatarAttr($value): string
{
return $value === full_url('', false, config('buildadmin.default_avatar')) ? '' : $value;
}
public function resetPassword(int|string $uid, string $newPassword): int
{
return $this->where(['id' => $uid])->update(['password' => hash_password($newPassword), 'salt' => '']);
}
}

View File

@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace app\admin\model;
use support\think\Model;
/**
* AdminGroup 模型Webman 迁移版)
*/
class AdminGroup extends Model
{
protected string $table = 'admin_group';
protected string $pk = 'id';
protected bool $autoWriteTimestamp = true;
}

View File

@@ -0,0 +1,141 @@
<?php
declare(strict_types=1);
namespace app\admin\model;
use Throwable;
use app\admin\library\Auth;
use support\think\Model;
use Webman\Http\Request;
/**
* AdminLog 模型Webman 迁移版)
*/
class AdminLog extends Model
{
protected string $table = 'admin_log';
protected string $pk = 'id';
protected bool $autoWriteTimestamp = true;
protected bool $updateTime = false;
protected string $title = '';
/** 日志内容(勿用 $data会与 ThinkORM Model 的 data 选项冲突) */
protected string|array $logData = '';
protected array $urlIgnoreRegex = [
'/^(.*)\/(select|index|logout)$/i',
];
protected array $desensitizationRegex = [
'/(password|salt|token)/i'
];
public static function instance(?Request $request = null): self
{
$request = $request ?? (function_exists('request') ? request() : null);
if ($request !== null && isset($request->adminLog) && $request->adminLog instanceof self) {
return $request->adminLog;
}
$log = new static();
if ($request !== null) {
$request->adminLog = $log;
}
return $log;
}
public function setTitle(string $title): void
{
$this->title = $title;
}
/** 设置日志内容BuildAdmin 控制器调用) */
public function setLogData(string|array $data): void
{
$this->logData = $data;
}
public function setUrlIgnoreRegex(array|string $regex = []): void
{
$this->urlIgnoreRegex = array_merge($this->urlIgnoreRegex, is_array($regex) ? $regex : [$regex]);
}
public function setDesensitizationRegex(array|string $regex = []): void
{
$this->desensitizationRegex = array_merge($this->desensitizationRegex, is_array($regex) ? $regex : [$regex]);
}
protected function desensitization(array|string $data): array|string
{
if (!is_array($data) || !$this->desensitizationRegex) {
return $data;
}
foreach ($data as $index => &$item) {
foreach ($this->desensitizationRegex as $reg) {
if (preg_match($reg, (string) $index)) {
$item = '***';
} elseif (is_array($item)) {
$item = $this->desensitization($item);
}
}
}
return $data;
}
public function record(string $title = '', string|array|null $data = null, ?Request $request = null): void
{
$request = $request ?? (function_exists('request') ? request() : null);
if (!$request) {
return;
}
$auth = Auth::instance();
$adminId = $auth->isLogin() ? $auth->id : 0;
$username = $auth->isLogin() ? $auth->username : ($request->get('username') ?? $request->post('username') ?? __('Unknown'));
$controllerPath = get_controller_path($request) ?? '';
$pathParts = explode('/', trim($request->path(), '/'));
$action = $pathParts[array_key_last($pathParts)] ?? '';
$path = $controllerPath . ($action ? '/' . $action : '');
foreach ($this->urlIgnoreRegex as $item) {
if (preg_match($item, $path)) {
return;
}
}
$data = $data ?: $this->logData;
if (!$data) {
$data = array_merge($request->get(), $request->post());
}
$data = $this->desensitization($data);
$title = $title ?: $this->title;
if (!$title && class_exists(\app\admin\model\AdminRule::class)) {
$controllerTitle = \app\admin\model\AdminRule::where('name', $controllerPath)->value('title');
$pathTitle = \app\admin\model\AdminRule::where('name', $path)->value('title');
$title = $pathTitle ?: __('Unknown') . '(' . $action . ')';
$title = $controllerTitle ? ($controllerTitle . '-' . $title) : $title;
}
if (!$title) {
$title = __('Unknown');
}
$url = $request->url();
$url = strlen($url) > 1500 ? substr($url, 0, 1500) : $url;
$useragent = $request->header('user-agent', '');
$useragent = strlen($useragent) > 255 ? substr($useragent, 0, 255) : $useragent;
self::create([
'admin_id' => $adminId,
'username' => $username,
'url' => $url,
'title' => $title,
'data' => !is_scalar($data) ? json_encode($data, JSON_UNESCAPED_UNICODE) : $data,
'ip' => $request->getRealIp(),
'useragent' => $useragent,
]);
}
public function admin()
{
return $this->belongsTo(Admin::class);
}
}

View File

@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace app\admin\model;
use support\think\Model;
class AdminRule extends Model
{
protected string $table = 'admin_rule';
protected string $pk = 'id';
protected bool $autoWriteTimestamp = true;
public function setComponentAttr($value)
{
if ($value) $value = str_replace('\\', '/', $value);
return $value;
}
}

127
app/admin/model/Config.php Normal file
View File

@@ -0,0 +1,127 @@
<?php
declare(strict_types=1);
namespace app\admin\model;
use support\think\Model;
/**
* 系统配置模型Webman 迁移版)
*/
class Config extends Model
{
public static string $cacheTag = 'sys_config';
protected string $table = 'config';
protected string $pk = 'id';
protected bool $autoWriteTimestamp = true;
protected array $append = ['value', 'content', 'extend', 'input_extend'];
protected array $jsonDecodeType = ['checkbox', 'array', 'selects'];
protected array $needContent = ['radio', 'checkbox', 'select', 'selects'];
public static function onBeforeInsert(Config $model): void
{
if (!in_array($model->getData('type'), $model->needContent)) {
$model->content = null;
} else {
$model->content = json_encode(str_attr_to_array($model->getData('content')));
}
if (is_array($model->rule)) {
$model->rule = implode(',', $model->rule);
}
if ($model->getData('extend') || $model->getData('inputExtend')) {
$extend = str_attr_to_array($model->getData('extend'));
$inputExtend = str_attr_to_array($model->getData('inputExtend'));
if ($inputExtend) {
$extend['baInputExtend'] = $inputExtend;
}
if ($extend) {
$model->extend = json_encode($extend);
}
}
$model->allow_del = 1;
}
public static function onAfterWrite(): void
{
clear_config_cache();
}
public function getValueAttr($value, $row)
{
if (!isset($row['type']) || $value == '0') return $value;
if (in_array($row['type'], $this->jsonDecodeType)) {
return empty($value) ? [] : json_decode($value, true);
}
if ($row['type'] == 'switch') {
return (bool) $value;
}
if ($row['type'] == 'editor') {
return !$value ? '' : htmlspecialchars_decode($value);
}
if (in_array($row['type'], ['city', 'remoteSelects'])) {
if (!$value) return [];
if (!is_array($value)) return explode(',', $value);
return $value;
}
return $value ?: '';
}
public function setValueAttr(mixed $value, $row): mixed
{
if (in_array($row['type'], $this->jsonDecodeType)) {
return $value ? json_encode($value) : '';
}
if ($row['type'] == 'switch') {
return $value ? '1' : '0';
}
if ($row['type'] == 'time') {
return $value ? date('H:i:s', strtotime($value)) : '';
}
if ($row['type'] == 'city') {
if ($value && is_array($value)) {
return implode(',', $value);
}
return $value ?: '';
}
if (is_array($value)) {
return implode(',', $value);
}
return $value;
}
public function getContentAttr($value, $row)
{
if (!isset($row['type'])) return '';
if (in_array($row['type'], $this->needContent)) {
$arr = json_decode($value, true);
return $arr ?: [];
}
return '';
}
public function getExtendAttr($value)
{
if ($value) {
$arr = json_decode($value, true);
if ($arr) {
unset($arr['baInputExtend']);
return $arr;
}
}
return [];
}
public function getInputExtendAttr($value, $row)
{
if ($row && $row['extend']) {
$arr = json_decode($row['extend'], true);
if ($arr && isset($arr['baInputExtend'])) {
return $arr['baInputExtend'];
}
}
return [];
}
}

View File

@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace app\admin\model;
use support\think\Model;
class CrudLog extends Model
{
protected string $table = 'crud_log';
protected string $pk = 'id';
protected bool $autoWriteTimestamp = true;
protected bool $updateTime = false;
protected array $type = [
'table' => 'array',
'fields' => 'array',
];
}

View File

@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace app\admin\model;
use support\think\Model;
class DataRecycle extends Model
{
protected string $table = 'security_data_recycle';
protected string $pk = 'id';
protected bool $autoWriteTimestamp = true;
}

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace app\admin\model;
use support\think\Model;
use think\model\relation\BelongsTo;
class DataRecycleLog extends Model
{
protected string $table = 'security_data_recycle_log';
protected string $pk = 'id';
protected bool $autoWriteTimestamp = true;
protected bool $updateTime = false;
public function recycle(): BelongsTo
{
return $this->belongsTo(DataRecycle::class, 'recycle_id');
}
public function admin(): BelongsTo
{
return $this->belongsTo(Admin::class, 'admin_id');
}
}

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace app\admin\model;
use support\think\Model;
class SensitiveData extends Model
{
protected string $table = 'security_sensitive_data';
protected string $pk = 'id';
protected bool $autoWriteTimestamp = true;
protected array $type = [
'data_fields' => 'array',
];
}

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace app\admin\model;
use support\think\Model;
use think\model\relation\BelongsTo;
class SensitiveDataLog extends Model
{
protected string $table = 'security_sensitive_data_log';
protected string $pk = 'id';
protected bool $autoWriteTimestamp = true;
protected bool $updateTime = false;
public function sensitive(): BelongsTo
{
return $this->belongsTo(SensitiveData::class, 'sensitive_id');
}
public function admin(): BelongsTo
{
return $this->belongsTo(Admin::class, 'admin_id');
}
}

45
app/admin/model/User.php Normal file
View File

@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace app\admin\model;
use support\think\Model;
use think\model\relation\BelongsTo;
class User extends Model
{
protected string $table = 'user';
protected string $pk = 'id';
protected bool $autoWriteTimestamp = true;
public function getAvatarAttr($value): string
{
return full_url($value ?? '', false, config('buildadmin.default_avatar'));
}
public function setAvatarAttr($value): string
{
return $value === full_url('', false, config('buildadmin.default_avatar')) ? '' : $value;
}
public function getMoneyAttr($value): string
{
return bcdiv((string) $value, '100', 2);
}
public function setMoneyAttr($value): string
{
return bcmul((string) $value, '100', 2);
}
public function userGroup(): BelongsTo
{
return $this->belongsTo(UserGroup::class, 'group_id');
}
public function resetPassword(int|string $uid, string $newPassword): int
{
return $this->where(['id' => $uid])->update(['password' => hash_password($newPassword), 'salt' => '']);
}
}

View File

@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace app\admin\model;
use support\think\Model;
class UserGroup extends Model
{
protected string $table = 'user_group';
protected string $pk = 'id';
protected bool $autoWriteTimestamp = true;
}

View File

@@ -0,0 +1,69 @@
<?php
namespace app\admin\model;
use support\think\Model;
use think\model\relation\BelongsTo;
class UserMoneyLog extends Model
{
protected string $table = 'user_money_log';
protected string $pk = 'id';
protected bool $autoWriteTimestamp = true;
protected bool $updateTime = false;
public static function onBeforeInsert($model): void
{
$user = User::where('id', $model->user_id)->lock(true)->find();
if (!$user) {
throw new \Exception(__("The user can't find it"));
}
if (!$model->memo) {
throw new \Exception(__("Change note cannot be blank"));
}
$model->before = $user->money;
$user->money += $model->money;
$user->save();
$model->after = $user->money;
}
public static function onBeforeDelete(): bool
{
return false;
}
public function getMoneyAttr($value): string
{
return bcdiv((string) $value, '100', 2);
}
public function setMoneyAttr($value): string
{
return bcmul((string) $value, '100', 2);
}
public function getBeforeAttr($value): string
{
return bcdiv((string) $value, '100', 2);
}
public function setBeforeAttr($value): string
{
return bcmul((string) $value, '100', 2);
}
public function getAfterAttr($value): string
{
return bcdiv((string) $value, '100', 2);
}
public function setAfterAttr($value): string
{
return bcmul((string) $value, '100', 2);
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
}

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace app\admin\model;
use support\think\Model;
class UserRule extends Model
{
protected string $table = 'user_rule';
protected string $pk = 'id';
protected bool $autoWriteTimestamp = true;
protected static function onAfterInsert($model): void
{
$pk = $model->getPk();
$model->where($pk, $model[$pk])->update(['weigh' => $model[$pk]]);
}
public function setComponentAttr($value)
{
if ($value) $value = str_replace('\\', '/', $value);
return $value;
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace app\admin\model;
use support\think\Model;
use think\model\relation\BelongsTo;
class UserScoreLog extends Model
{
protected string $table = 'user_score_log';
protected string $pk = 'id';
protected bool $autoWriteTimestamp = true;
protected bool $updateTime = false;
public static function onBeforeInsert($model): void
{
$user = User::where('id', $model->user_id)->lock(true)->find();
if (!$user) {
throw new \Exception(__("The user can't find it"));
}
if (!$model->memo) {
throw new \Exception(__("Change note cannot be blank"));
}
$model->before = $user->score;
$user->score += $model->score;
$user->save();
$model->after = $user->score;
}
public static function onBeforeDelete(): bool
{
return false;
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace app\admin\model\mall;
use support\think\Model;
/**
* Player
*/
class Player extends Model
{
// 表名
protected $name = 'mall_player';
// 自动写入时间戳字段
protected $autoWriteTimestamp = true;
/**
* 重置密码
*/
public function resetPassword(int $id, string $newPassword): bool
{
return $this->where(['id' => $id])->update(['password' => hash_password($newPassword)]) !== false;
}
}