优化上传组件

This commit is contained in:
2020-04-18 09:58:51 +08:00
parent 09c0c3cc59
commit 37a972592d
4 changed files with 36 additions and 18 deletions

View File

@@ -229,25 +229,25 @@ function format_bytes($size, $delimiter = '') {
* @param string $field
* @return 完整的数据 或者 指定的$field字段值
*/
function get_attach($id, $field = null) {
function get_attach($id, $field = false, $is_list = false) {
$basePath = request()->domain();
if (empty($id)) {
return $basePath . '/static/common/images/default.png';
}
if (false !== strpos($id, ",")) {
if (false !== strpos($id, ",") || $is_list) {
$map[] = ['id', 'IN', explode(",", $id)];
$picture = \think\facade\Db::name('Attach')->where($map)->column("*", "id");
$picture = \app\model\Attach::where($map)->column("*", "id");
return $picture;
}else{
$map[] = ['id', '=', $id];
$picture = \think\facade\Db::name('Attach')->where($map)->find();
$picture = \app\model\Attach::where($map)->find();
if ($field == 'path') {
if (!empty($picture['url'])) {
$picture['path'] = $picture['url'] ? $basePath . $picture['url'] : $basePath . '/static/images/default.png';
$picture['path'] = $picture['url'] ? $basePath . $picture['url'] : $basePath . '/static/common/images/default.png';
} else {
$picture['path'] = $picture['path'] ? $basePath . $picture['path'] : $basePath . '/static/images/default.png';
$picture['path'] = $picture['path'] ? $basePath . $picture['path'] : $basePath . '/static/common/images/default.png';
}
}
return empty($field) ? $picture : $picture[$field];
return (false !== $field) ? $picture[$field] : $picture;
}
}

View File

@@ -10,7 +10,7 @@ namespace app\controller;
use think\facade\Session;
use think\facade\Filesystem;
use think\facade\Db;
use app\model\Attach;
class Upload extends Base {
@@ -65,7 +65,7 @@ class Upload extends Base {
}else{
$map[] = ['type', '=', 'image'];
}
$list = Db::where($map)->name('Attach')->paginate($pageConfig);
$list = Attach::where($map)->paginate($pageConfig);
$this->data = [
'from' => $this->request->param('from'),
@@ -121,7 +121,7 @@ class Upload extends Base {
'query' => $this->request->param()
];
$map[] = ['type', '=', 'image'];
$data = Db::where($map)->name('Attach')->paginate($pageConfig)->each(function($item, $key){
$data = Attach::where($map)->paginate($pageConfig)->each(function($item, $key){
$item['thumbURL'] = $item['url'];
$item['oriURL'] = $item['url'];
return $item;
@@ -165,7 +165,8 @@ class Upload extends Base {
$data['location'] = "/uploads/";
$data['url'] = $data['location'] . $data['savepath'];
$data['create_time'] = time();
$data['id'] = Db::name('Attach')->insertGetId($data);
$attach = Attach::create($data);
$data['id'] = $attach->id;
return $data;
}
}

View File

@@ -8,16 +8,13 @@
<div id="fileList_{$name}" class="img-list">
{if $value}
{php}
$img_list = explode(',',$value);
$images = get_attach($value, false, true);
{/php}
{volist name="img_list" id="item"}
{php}
$images = get_attach($item);
{/php}
<div class="item" data-id="{$images['id']}">
{volist name="images" id="item"}
<div class="item" data-id="{$item['id']}">
<div class="thumb">
<div class="close"><i class="fa fa-close"></i></div>
<img src="{$images['url']}" alt="{$images['create_time']}">
<img src="{$item['url']}" alt="{$item['create_time']}">
</div>
</div>
{/volist}

20
app/model/Attach.php Normal file
View File

@@ -0,0 +1,20 @@
<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace app\model;
/**
* 附件模型
*/
class Attach extends \think\Model {
protected function getUrlAttr($value, $data){
return $value ? $value : $data['path'];
}
}