diff --git a/app/common.php b/app/common.php index d2d6b097..4d02d322 100755 --- a/app/common.php +++ b/app/common.php @@ -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; } } \ No newline at end of file diff --git a/app/controller/Upload.php b/app/controller/Upload.php index 2d2bc3da..cf537639 100644 --- a/app/controller/Upload.php +++ b/app/controller/Upload.php @@ -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; } } \ No newline at end of file diff --git a/app/http/form/template/images.html b/app/http/form/template/images.html index 58f8390e..85f6f3ad 100644 --- a/app/http/form/template/images.html +++ b/app/http/form/template/images.html @@ -8,16 +8,13 @@
{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} -
+ {volist name="images" id="item"} +
- {$images['create_time']} + {$item['create_time']}
{/volist} diff --git a/app/model/Attach.php b/app/model/Attach.php new file mode 100644 index 00000000..59b99ea9 --- /dev/null +++ b/app/model/Attach.php @@ -0,0 +1,20 @@ + +// +---------------------------------------------------------------------- +namespace app\model; + +/** + * 附件模型 + */ +class Attach extends \think\Model { + + protected function getUrlAttr($value, $data){ + return $value ? $value : $data['path']; + } + +} \ No newline at end of file