优化上传组件

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 * @param string $field
* @return 完整的数据 或者 指定的$field字段值 * @return 完整的数据 或者 指定的$field字段值
*/ */
function get_attach($id, $field = null) { function get_attach($id, $field = false, $is_list = false) {
$basePath = request()->domain(); $basePath = request()->domain();
if (empty($id)) { if (empty($id)) {
return $basePath . '/static/common/images/default.png'; return $basePath . '/static/common/images/default.png';
} }
if (false !== strpos($id, ",")) { if (false !== strpos($id, ",") || $is_list) {
$map[] = ['id', 'IN', explode(",", $id)]; $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; return $picture;
}else{ }else{
$map[] = ['id', '=', $id]; $map[] = ['id', '=', $id];
$picture = \think\facade\Db::name('Attach')->where($map)->find(); $picture = \app\model\Attach::where($map)->find();
if ($field == 'path') { if ($field == 'path') {
if (!empty($picture['url'])) { 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 { } 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\Session;
use think\facade\Filesystem; use think\facade\Filesystem;
use think\facade\Db; use app\model\Attach;
class Upload extends Base { class Upload extends Base {
@@ -65,7 +65,7 @@ class Upload extends Base {
}else{ }else{
$map[] = ['type', '=', 'image']; $map[] = ['type', '=', 'image'];
} }
$list = Db::where($map)->name('Attach')->paginate($pageConfig); $list = Attach::where($map)->paginate($pageConfig);
$this->data = [ $this->data = [
'from' => $this->request->param('from'), 'from' => $this->request->param('from'),
@@ -121,7 +121,7 @@ class Upload extends Base {
'query' => $this->request->param() 'query' => $this->request->param()
]; ];
$map[] = ['type', '=', 'image']; $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['thumbURL'] = $item['url'];
$item['oriURL'] = $item['url']; $item['oriURL'] = $item['url'];
return $item; return $item;
@@ -165,7 +165,8 @@ class Upload extends Base {
$data['location'] = "/uploads/"; $data['location'] = "/uploads/";
$data['url'] = $data['location'] . $data['savepath']; $data['url'] = $data['location'] . $data['savepath'];
$data['create_time'] = time(); $data['create_time'] = time();
$data['id'] = Db::name('Attach')->insertGetId($data); $attach = Attach::create($data);
$data['id'] = $attach->id;
return $data; return $data;
} }
} }

View File

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