初始化
This commit is contained in:
115
app/common/model/Attachment.php
Normal file
115
app/common/model/Attachment.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use Throwable;
|
||||
use think\Model;
|
||||
use think\facade\Event;
|
||||
use app\admin\model\Admin;
|
||||
use app\common\library\Upload;
|
||||
use think\model\relation\BelongsTo;
|
||||
|
||||
/**
|
||||
* Attachment模型
|
||||
* @property string url 文件物理路径
|
||||
* @property int quote 上传(引用)次数
|
||||
* @property int last_upload_time 最后上传时间
|
||||
*/
|
||||
class Attachment extends Model
|
||||
{
|
||||
protected $autoWriteTimestamp = true;
|
||||
protected $updateTime = false;
|
||||
|
||||
protected $append = [
|
||||
'suffix',
|
||||
'full_url'
|
||||
];
|
||||
|
||||
/**
|
||||
* 上传类实例,可以通过它调用上传文件驱动,且驱动类具有静态缓存
|
||||
*/
|
||||
protected static Upload $upload;
|
||||
|
||||
protected static function init(): void
|
||||
{
|
||||
self::$upload = new Upload();
|
||||
}
|
||||
|
||||
public function getSuffixAttr($value, $row): string
|
||||
{
|
||||
if ($row['name']) {
|
||||
$suffix = strtolower(pathinfo($row['name'], PATHINFO_EXTENSION));
|
||||
return $suffix && preg_match("/^[a-zA-Z0-9]+$/", $suffix) ? $suffix : 'file';
|
||||
}
|
||||
return 'file';
|
||||
}
|
||||
|
||||
public function getFullUrlAttr($value, $row): string
|
||||
{
|
||||
$driver = self::$upload->getDriver($row['storage'], false);
|
||||
return $driver ? $driver->url($row['url']) : full_url($row['url']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增前
|
||||
* @throws Throwable
|
||||
*/
|
||||
protected static function onBeforeInsert($model): bool
|
||||
{
|
||||
$repeat = $model->where([
|
||||
['sha1', '=', $model->sha1],
|
||||
['topic', '=', $model->topic],
|
||||
['storage', '=', $model->storage],
|
||||
])->find();
|
||||
if ($repeat) {
|
||||
$driver = self::$upload->getDriver($repeat->storage, false);
|
||||
if ($driver && !$driver->exists($repeat->url)) {
|
||||
$repeat->delete();
|
||||
return true;
|
||||
} else {
|
||||
$repeat->quote++;
|
||||
$repeat->last_upload_time = time();
|
||||
$repeat->save();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增后
|
||||
*/
|
||||
protected static function onAfterInsert($model): void
|
||||
{
|
||||
Event::trigger('AttachmentInsert', $model);
|
||||
|
||||
if (!$model->last_upload_time) {
|
||||
$model->quote = 1;
|
||||
$model->last_upload_time = time();
|
||||
$model->save();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除后
|
||||
*/
|
||||
protected static function onAfterDelete($model): void
|
||||
{
|
||||
Event::trigger('AttachmentDel', $model);
|
||||
|
||||
$driver = self::$upload->getDriver($model->storage, false);
|
||||
if ($driver && $driver->exists($model->url)) {
|
||||
$driver->delete($model->url);
|
||||
}
|
||||
}
|
||||
|
||||
public function admin(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Admin::class);
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
83
app/common/model/Config.php
Normal file
83
app/common/model/Config.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use Throwable;
|
||||
use think\Model;
|
||||
use app\admin\model\Config as adminConfigModel;
|
||||
|
||||
class Config extends Model
|
||||
{
|
||||
/**
|
||||
* 添加系统配置分组
|
||||
* @throws Throwable
|
||||
*/
|
||||
public static function addConfigGroup(string $key, string $value): bool
|
||||
{
|
||||
return self::addArrayItem('config_group', $key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除系统配置分组
|
||||
* @throws Throwable
|
||||
*/
|
||||
public static function removeConfigGroup(string $key): bool
|
||||
{
|
||||
if (adminConfigModel::where('group', $key)->find()) return false;
|
||||
return self::removeArrayItem('config_group', $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加系统快捷配置入口
|
||||
* @throws Throwable
|
||||
*/
|
||||
public static function addQuickEntrance(string $key, string $value): bool
|
||||
{
|
||||
return self::addArrayItem('config_quick_entrance', $key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除系统快捷配置入口
|
||||
* @throws Throwable
|
||||
*/
|
||||
public static function removeQuickEntrance(string $key): bool
|
||||
{
|
||||
return self::removeArrayItem('config_quick_entrance', $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 为Array类型的配置项添加元素
|
||||
* @throws Throwable
|
||||
*/
|
||||
public static function addArrayItem(string $name, string $key, string $value): bool
|
||||
{
|
||||
$configRow = adminConfigModel::where('name', $name)->find();
|
||||
foreach ($configRow->value as $item) {
|
||||
if ($item['key'] == $key) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
$configRow->value = array_merge($configRow->value, [['key' => $key, 'value' => $value]]);
|
||||
$configRow->save();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除Array类型配置项的一个元素
|
||||
* @throws Throwable
|
||||
*/
|
||||
public static function removeArrayItem(string $name, string $key): bool
|
||||
{
|
||||
$configRow = adminConfigModel::where('name', $name)->find();
|
||||
$configRowValue = $configRow->value;
|
||||
foreach ($configRowValue as $iKey => $item) {
|
||||
if ($item['key'] == $key) {
|
||||
unset($configRowValue[$iKey]);
|
||||
}
|
||||
}
|
||||
$configRow->value = $configRowValue;
|
||||
$configRow->save();
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
51
app/common/model/User.php
Normal file
51
app/common/model/User.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 会员公共模型
|
||||
* @property int $id 会员ID
|
||||
* @property string $password 密码密文
|
||||
* @property string $salt 密码盐(废弃待删)
|
||||
* @property int $login_failure 登录失败次数
|
||||
* @property string $last_login_time 上次登录时间
|
||||
* @property string $last_login_ip 上次登录IP
|
||||
* @property string $email 会员邮箱
|
||||
* @property string $mobile 会员手机号
|
||||
* @property string $status 状态:enable=启用,disable=禁用,...(string存储,可自定义其他)
|
||||
*/
|
||||
class User extends Model
|
||||
{
|
||||
protected $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 resetPassword($uid, $newPassword): int|User
|
||||
{
|
||||
return $this->where(['id' => $uid])->update(['password' => hash_password($newPassword), 'salt' => '']);
|
||||
}
|
||||
|
||||
public function getMoneyAttr($value): string
|
||||
{
|
||||
return bcdiv($value, 100, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户的余额是不可以直接进行修改的,请通过 UserMoneyLog 模型插入记录来实现自动修改余额
|
||||
* 此处定义上 money 的修改器仅为防止直接对余额的修改造成数据错乱
|
||||
*/
|
||||
public function setMoneyAttr($value): string
|
||||
{
|
||||
return bcmul($value, 100, 2);
|
||||
}
|
||||
}
|
||||
41
app/common/model/UserMoneyLog.php
Normal file
41
app/common/model/UserMoneyLog.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\model;
|
||||
|
||||
class UserMoneyLog extends model
|
||||
{
|
||||
protected $autoWriteTimestamp = true;
|
||||
protected $updateTime = false;
|
||||
|
||||
public function getMoneyAttr($value): string
|
||||
{
|
||||
return bcdiv($value, 100, 2);
|
||||
}
|
||||
|
||||
public function setMoneyAttr($value): string
|
||||
{
|
||||
return bcmul($value, 100, 2);
|
||||
}
|
||||
|
||||
public function getBeforeAttr($value): string
|
||||
{
|
||||
return bcdiv($value, 100, 2);
|
||||
}
|
||||
|
||||
public function setBeforeAttr($value): string
|
||||
{
|
||||
return bcmul($value, 100, 2);
|
||||
}
|
||||
|
||||
public function getAfterAttr($value): string
|
||||
{
|
||||
return bcdiv($value, 100, 2);
|
||||
}
|
||||
|
||||
public function setAfterAttr($value): string
|
||||
{
|
||||
return bcmul($value, 100, 2);
|
||||
}
|
||||
}
|
||||
11
app/common/model/UserScoreLog.php
Normal file
11
app/common/model/UserScoreLog.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\model;
|
||||
|
||||
class UserScoreLog extends model
|
||||
{
|
||||
protected $autoWriteTimestamp = true;
|
||||
protected $updateTime = false;
|
||||
}
|
||||
Reference in New Issue
Block a user