项目初始化
This commit is contained in:
67
app/admin/model/Admin.php
Normal file
67
app/admin/model/Admin.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\model;
|
||||
|
||||
use app\common\model\traits\TimestampInteger;
|
||||
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
|
||||
{
|
||||
use TimestampInteger;
|
||||
|
||||
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' => '']);
|
||||
}
|
||||
}
|
||||
20
app/admin/model/AdminGroup.php
Normal file
20
app/admin/model/AdminGroup.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\model;
|
||||
|
||||
use app\common\model\traits\TimestampInteger;
|
||||
use support\think\Model;
|
||||
|
||||
/**
|
||||
* AdminGroup 模型(Webman 迁移版)
|
||||
*/
|
||||
class AdminGroup extends Model
|
||||
{
|
||||
use TimestampInteger;
|
||||
|
||||
protected string $table = 'admin_group';
|
||||
protected string $pk = 'id';
|
||||
protected bool $autoWriteTimestamp = true;
|
||||
}
|
||||
144
app/admin/model/AdminLog.php
Normal file
144
app/admin/model/AdminLog.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\model;
|
||||
|
||||
use Throwable;
|
||||
use app\admin\library\Auth;
|
||||
use app\common\model\traits\TimestampInteger;
|
||||
use support\think\Model;
|
||||
use Webman\Http\Request;
|
||||
|
||||
/**
|
||||
* AdminLog 模型(Webman 迁移版)
|
||||
*/
|
||||
class AdminLog extends Model
|
||||
{
|
||||
use TimestampInteger;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
23
app/admin/model/AdminRule.php
Normal file
23
app/admin/model/AdminRule.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\model;
|
||||
|
||||
use app\common\model\traits\TimestampInteger;
|
||||
use support\think\Model;
|
||||
|
||||
class AdminRule extends Model
|
||||
{
|
||||
use TimestampInteger;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
130
app/admin/model/Config.php
Normal file
130
app/admin/model/Config.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\model;
|
||||
|
||||
use app\common\model\traits\TimestampInteger;
|
||||
use support\think\Model;
|
||||
|
||||
/**
|
||||
* 系统配置模型(Webman 迁移版)
|
||||
*/
|
||||
class Config extends Model
|
||||
{
|
||||
use TimestampInteger;
|
||||
|
||||
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 [];
|
||||
}
|
||||
}
|
||||
22
app/admin/model/CrudLog.php
Normal file
22
app/admin/model/CrudLog.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?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 = [
|
||||
'create_time' => 'integer',
|
||||
'update_time' => 'integer',
|
||||
'table' => 'array',
|
||||
'fields' => 'array',
|
||||
];
|
||||
}
|
||||
17
app/admin/model/DataRecycle.php
Normal file
17
app/admin/model/DataRecycle.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\model;
|
||||
|
||||
use app\common\model\traits\TimestampInteger;
|
||||
use support\think\Model;
|
||||
|
||||
class DataRecycle extends Model
|
||||
{
|
||||
use TimestampInteger;
|
||||
|
||||
protected string $table = 'security_data_recycle';
|
||||
protected string $pk = 'id';
|
||||
protected bool $autoWriteTimestamp = true;
|
||||
}
|
||||
29
app/admin/model/DataRecycleLog.php
Normal file
29
app/admin/model/DataRecycleLog.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\model;
|
||||
|
||||
use app\common\model\traits\TimestampInteger;
|
||||
use support\think\Model;
|
||||
use think\model\relation\BelongsTo;
|
||||
|
||||
class DataRecycleLog extends Model
|
||||
{
|
||||
use TimestampInteger;
|
||||
|
||||
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');
|
||||
}
|
||||
}
|
||||
20
app/admin/model/SensitiveData.php
Normal file
20
app/admin/model/SensitiveData.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?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 = [
|
||||
'create_time' => 'integer',
|
||||
'update_time' => 'integer',
|
||||
'data_fields' => 'array',
|
||||
];
|
||||
}
|
||||
29
app/admin/model/SensitiveDataLog.php
Normal file
29
app/admin/model/SensitiveDataLog.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\model;
|
||||
|
||||
use app\common\model\traits\TimestampInteger;
|
||||
use support\think\Model;
|
||||
use think\model\relation\BelongsTo;
|
||||
|
||||
class SensitiveDataLog extends Model
|
||||
{
|
||||
use TimestampInteger;
|
||||
|
||||
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');
|
||||
}
|
||||
}
|
||||
48
app/admin/model/User.php
Normal file
48
app/admin/model/User.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\model;
|
||||
|
||||
use app\common\model\traits\TimestampInteger;
|
||||
use support\think\Model;
|
||||
use think\model\relation\BelongsTo;
|
||||
|
||||
class User extends Model
|
||||
{
|
||||
use TimestampInteger;
|
||||
|
||||
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' => '']);
|
||||
}
|
||||
}
|
||||
17
app/admin/model/UserGroup.php
Normal file
17
app/admin/model/UserGroup.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\model;
|
||||
|
||||
use app\common\model\traits\TimestampInteger;
|
||||
use support\think\Model;
|
||||
|
||||
class UserGroup extends Model
|
||||
{
|
||||
use TimestampInteger;
|
||||
|
||||
protected string $table = 'user_group';
|
||||
protected string $pk = 'id';
|
||||
protected bool $autoWriteTimestamp = true;
|
||||
}
|
||||
72
app/admin/model/UserMoneyLog.php
Normal file
72
app/admin/model/UserMoneyLog.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\model;
|
||||
|
||||
use app\common\model\traits\TimestampInteger;
|
||||
use support\think\Model;
|
||||
use think\model\relation\BelongsTo;
|
||||
|
||||
class UserMoneyLog extends Model
|
||||
{
|
||||
use TimestampInteger;
|
||||
|
||||
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');
|
||||
}
|
||||
}
|
||||
29
app/admin/model/UserRule.php
Normal file
29
app/admin/model/UserRule.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\model;
|
||||
|
||||
use app\common\model\traits\TimestampInteger;
|
||||
use support\think\Model;
|
||||
|
||||
class UserRule extends Model
|
||||
{
|
||||
use TimestampInteger;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
42
app/admin/model/UserScoreLog.php
Normal file
42
app/admin/model/UserScoreLog.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\model;
|
||||
|
||||
use app\common\model\traits\TimestampInteger;
|
||||
use support\think\Model;
|
||||
use think\model\relation\BelongsTo;
|
||||
|
||||
class UserScoreLog extends Model
|
||||
{
|
||||
use TimestampInteger;
|
||||
|
||||
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');
|
||||
}
|
||||
}
|
||||
28
app/admin/model/mall/Player.php
Normal file
28
app/admin/model/mall/Player.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\model\mall;
|
||||
|
||||
use app\common\model\traits\TimestampInteger;
|
||||
use support\think\Model;
|
||||
|
||||
/**
|
||||
* Player
|
||||
*/
|
||||
class Player extends Model
|
||||
{
|
||||
use TimestampInteger;
|
||||
|
||||
// 表名
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user