代码初始化
This commit is contained in:
98
application/common/model/Attachment.php
Normal file
98
application/common/model/Attachment.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
class Attachment extends Model
|
||||
{
|
||||
|
||||
// 开启自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = 'int';
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createtime';
|
||||
protected $updateTime = 'updatetime';
|
||||
// 定义字段类型
|
||||
protected $type = [
|
||||
];
|
||||
protected $append = [
|
||||
'thumb_style'
|
||||
];
|
||||
|
||||
protected static function init()
|
||||
{
|
||||
// 如果已经上传该资源,则不再记录
|
||||
self::beforeInsert(function ($model) {
|
||||
if (self::where('url', '=', $model['url'])->where('storage', $model['storage'])->find()) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
self::beforeWrite(function ($row) {
|
||||
if (isset($row['category']) && $row['category'] == 'unclassed') {
|
||||
$row['category'] = '';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function setUploadtimeAttr($value)
|
||||
{
|
||||
return is_numeric($value) ? $value : strtotime($value);
|
||||
}
|
||||
|
||||
public function getCategoryAttr($value)
|
||||
{
|
||||
return $value == '' ? 'unclassed' : $value;
|
||||
}
|
||||
|
||||
public function setCategoryAttr($value)
|
||||
{
|
||||
return $value == 'unclassed' ? '' : $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取云储存的缩略图样式字符
|
||||
*/
|
||||
public function getThumbStyleAttr($value, $data)
|
||||
{
|
||||
if (!isset($data['storage']) || $data['storage'] == 'local') {
|
||||
return '';
|
||||
} else {
|
||||
$config = get_addon_config($data['storage']);
|
||||
if ($config && isset($config['thumbstyle'])) {
|
||||
return $config['thumbstyle'];
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Mimetype列表
|
||||
* @return array
|
||||
*/
|
||||
public static function getMimetypeList()
|
||||
{
|
||||
$data = [
|
||||
"image/*" => __("Image"),
|
||||
"audio/*" => __("Audio"),
|
||||
"video/*" => __("Video"),
|
||||
"text/*" => __("Text"),
|
||||
"application/*" => __("Application"),
|
||||
"zip,rar,7z,tar" => __("Zip"),
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取定义的附件类别列表
|
||||
* @return array
|
||||
*/
|
||||
public static function getCategoryList()
|
||||
{
|
||||
$data = config('site.attachmentcategory') ?? [];
|
||||
foreach ($data as $index => &$datum) {
|
||||
$datum = __($datum);
|
||||
}
|
||||
$data['unclassed'] = __('Unclassed');
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user