重构系统附件字段逻辑
修改各模块附件功能
This commit is contained in:
@@ -11,12 +11,22 @@ use URL;
|
||||
|
||||
class AttachmentController extends DefaultController
|
||||
{
|
||||
public $permission = ['list','view','preview','create','delete','download','show', 'uploader', 'draft', 'qrcode'];
|
||||
public $permission = [
|
||||
'list',
|
||||
'preview',
|
||||
'create',
|
||||
'upload',
|
||||
'delete',
|
||||
'download',
|
||||
'show',
|
||||
'draft',
|
||||
'qrcode'
|
||||
];
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
*/
|
||||
public function uploader()
|
||||
public function upload()
|
||||
{
|
||||
if (Request::method() == 'POST') {
|
||||
$file = Request::file('file');
|
||||
@@ -34,7 +44,8 @@ class AttachmentController extends DefaultController
|
||||
}
|
||||
|
||||
// 获取上传uri第一个目录
|
||||
$key = Request::get('key', 'default');
|
||||
$table = Request::get('table');
|
||||
$field = Request::get('field');
|
||||
$node = Request::get('path', 'default');
|
||||
$path = $node.date('/Ym/');
|
||||
|
||||
@@ -43,15 +54,16 @@ class AttachmentController extends DefaultController
|
||||
// 文件新名字
|
||||
$filename = date('dhis_').Str::random(4).'.'.$extension;
|
||||
$filename = mb_strtolower($filename);
|
||||
$filesize = $file->getSize();
|
||||
|
||||
if ($file->move($upload_path, $filename)) {
|
||||
$data = [
|
||||
'name' => mb_strtolower($file->getClientOriginalName()),
|
||||
'node' => $node,
|
||||
'table' => $table,
|
||||
'field' => $field,
|
||||
'path' => $path.$filename,
|
||||
'type' => $extension,
|
||||
'key' => $key,
|
||||
'size' => $file->getClientSize(),
|
||||
'size' => $filesize,
|
||||
];
|
||||
$insertId = DB::table('attachment')->insertGetId($data);
|
||||
$data['id'] = $insertId;
|
||||
@@ -61,68 +73,12 @@ class AttachmentController extends DefaultController
|
||||
}
|
||||
}
|
||||
$query = Request::all();
|
||||
$SERVER_URL = url("index/attachment/uploader", $query);
|
||||
$SERVER_URL = url("index/attachment/upload", $query);
|
||||
return $this->render([
|
||||
'SERVER_URL' => $SERVER_URL,
|
||||
'key' => $query['key'],
|
||||
'key' => $query['table'].'_'.$query['field'],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新建文件
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
set_time_limit(0);
|
||||
|
||||
$file = Request::file('Filedata');
|
||||
|
||||
$rules = [
|
||||
'file' => 'mimes:'.$this->setting['upload_type'],
|
||||
];
|
||||
$v = Validator::make(['file' => $file], $rules);
|
||||
|
||||
if ($file->isValid() && $v->passes()) {
|
||||
// 获取上传uri第一个目录
|
||||
$path = Request::get('path', 'main').date('/Y/m/');
|
||||
|
||||
$upload_path = upload_path().'/'.$path;
|
||||
|
||||
// 文件后缀名
|
||||
$extension = $file->getClientOriginalExtension();
|
||||
|
||||
// 文件新名字
|
||||
$filename = date('dhis_').Str::random(4).'.'.$extension;
|
||||
$filename = mb_strtolower($filename);
|
||||
|
||||
if ($file->move($upload_path, $filename)) {
|
||||
return DB::table('attachment')->insertGetId([
|
||||
'name' => mb_strtolower($file->getClientOriginalName()),
|
||||
'path' => $path.$filename,
|
||||
'type' => $extension,
|
||||
'size' => $file->getClientSize(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 二维码上传
|
||||
*/
|
||||
public function qrcode()
|
||||
{
|
||||
$key = Request::get('key');
|
||||
$path = Request::get('path');
|
||||
list($table, $field) = explode('.', $key);
|
||||
$model = DB::table('model')->where('table', $table)->first();
|
||||
$token = Request::get('x-auth-token');
|
||||
return $this->render([
|
||||
'model' => $model,
|
||||
'token' => $token,
|
||||
'key' => $key,
|
||||
], 'attachment.qrcode');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件列表
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
use Request;
|
||||
use DB;
|
||||
use Auth;
|
||||
use Str;
|
||||
|
||||
class AttachmentService
|
||||
{
|
||||
@@ -26,15 +27,16 @@ class AttachmentService
|
||||
}
|
||||
|
||||
// 文件新名字
|
||||
$name = date('dhis_').str_random(4).'.'.$extension;
|
||||
$name = date('dhis_').Str::random(4).'.'.$extension;
|
||||
$name = mb_strtolower($name);
|
||||
$size = $file->getSize();
|
||||
|
||||
if ($file->move($upload_path, $name)) {
|
||||
$res[] = DB::table('attachment')->insertGetId([
|
||||
'name' => $name,
|
||||
'path' => $path.'/'.$name,
|
||||
'type' => $extension,
|
||||
'size' => $file->getClientSize(),
|
||||
'size' => $size,
|
||||
'status' => 1,
|
||||
]);
|
||||
}
|
||||
@@ -55,7 +57,7 @@ class AttachmentService
|
||||
$res = [];
|
||||
|
||||
foreach ($images as $image) {
|
||||
$name = date('dhis_').str_random(4).'.'.$extension;
|
||||
$name = date('dhis_').Str::random(4).'.'.$extension;
|
||||
$name = mb_strtolower($name);
|
||||
|
||||
$image = base64_decode(str_replace(' ', '+', $image));
|
||||
@@ -81,8 +83,7 @@ class AttachmentService
|
||||
if (is_array($ids)) {
|
||||
return $ids;
|
||||
}
|
||||
$ids = array_filter(explode("\n", $ids));
|
||||
|
||||
$ids = array_filter(explode(",", $ids));
|
||||
return (array)$ids;
|
||||
}
|
||||
|
||||
@@ -91,7 +92,7 @@ class AttachmentService
|
||||
*/
|
||||
public static function get($ids)
|
||||
{
|
||||
$ids = self::getIds($ids);
|
||||
$ids = static::getIds($ids);
|
||||
return DB::table('attachment')
|
||||
->whereIn('id', $ids)
|
||||
->where('status', 1)->get();
|
||||
@@ -100,10 +101,13 @@ class AttachmentService
|
||||
/**
|
||||
* 获取当前ID附件和草稿
|
||||
*/
|
||||
public static function edit($ids, $key)
|
||||
public static function edit($ids, $table, $field, $path = 'default')
|
||||
{
|
||||
$res['rows'] = self::get($ids);
|
||||
$res['draft'] = self::draft($key);
|
||||
$res['path'] = $path;
|
||||
$res['table'] = $table;
|
||||
$res['field'] = $field;
|
||||
$res['rows'] = static::get($ids);
|
||||
$res['draft'] = static::draft($table, $field);
|
||||
return $res;
|
||||
}
|
||||
|
||||
@@ -112,7 +116,7 @@ class AttachmentService
|
||||
*/
|
||||
public static function show($ids)
|
||||
{
|
||||
$res['rows'] = self::get($ids);
|
||||
$res['rows'] = static::get($ids);
|
||||
return $res;
|
||||
}
|
||||
|
||||
@@ -121,7 +125,7 @@ class AttachmentService
|
||||
*/
|
||||
public static function publish($ids)
|
||||
{
|
||||
$ids = self::getIds($ids);
|
||||
$ids = static::getIds($ids);
|
||||
$rows = DB::table('attachment')
|
||||
->whereIn('id', $ids)
|
||||
->where('status', 0)
|
||||
@@ -137,9 +141,9 @@ class AttachmentService
|
||||
/**
|
||||
* 发布附件,改成状态为可用
|
||||
*/
|
||||
public static function store($key)
|
||||
public static function store($table, $field)
|
||||
{
|
||||
$rows = self::draft($key);
|
||||
$rows = static::draft($table, $field);
|
||||
foreach ($rows as $row) {
|
||||
DB::table('attachment')->where('id', $row['id'])->update([
|
||||
'status' => 1,
|
||||
@@ -151,14 +155,15 @@ class AttachmentService
|
||||
/**
|
||||
* 获取草稿文件
|
||||
*/
|
||||
public static function draft($key, $user_id = 0)
|
||||
public static function draft($table, $field, $user_id = 0)
|
||||
{
|
||||
if ($user_id == 0) {
|
||||
$user_id = auth()->id();
|
||||
}
|
||||
return DB::table('attachment')
|
||||
->where('created_id', $user_id)
|
||||
->where('key', $key)
|
||||
->where('table', $table)
|
||||
->where('field', $field)
|
||||
->where('status', '0')
|
||||
->get();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user