瀏覽代碼

add: 支持上传/删除远程图片

joe 4 年之前
父節點
當前提交
2b0d28b5bc
共有 4 個文件被更改,包括 104 次插入27 次删除
  1. 43 0
      app/admin/common.php
  2. 43 17
      app/admin/controller/widget/Images.php
  3. 16 9
      app/admin/view/widget/images.php
  4. 2 1
      public/install/crmeb.sql

+ 43 - 0
app/admin/common.php

@@ -144,3 +144,46 @@ if (!function_exists('verify_domain')) {
             return false;
     }
 }
+
+if (!function_exists('getFileHeaders')) {
+    function getFileHeaders(string $url, $isData = true) {
+        stream_context_set_default(['ssl' => ['verify_peer' => false, 'verify_peer_name' => false]]);
+        $header['size'] = 0;
+        $header['type'] = 'image/jpeg';
+        if (!$isData) {
+            return $header;
+        }
+        try {
+            $headerArray = get_headers(str_replace('\\', '/', $url), true);
+            if (!isset($headerArray['Content-Length'])) {
+                $header['size'] = 0;
+            }
+            if (!isset($headerArray['Content-Type'])) {
+                $header['type'] = 'image/jpeg';
+            }
+            if (is_array($headerArray['Content-Length']) && count($headerArray['Content-Length']) == 2) {
+                $header['size'] = $headerArray['Content-Length'][1];
+            }
+            if (is_array($headerArray['Content-Type']) && count($headerArray['Content-Type']) == 2) {
+                $header['type'] = $headerArray['Content-Type'][1];
+            }
+        } catch (\Exception $e) {
+        }
+        return $header;
+    }
+}
+
+if (!function_exists('getUrlImgInfo')) {
+    function getUrlImgInfo($url)
+    {
+        $headers = getFileHeaders($url);
+        return [
+            'name' => basename($url),       // 所以仅支持 .jpg/.jpeg 等结尾的 url
+            'size' => $headers['size'] ?? 0,
+            'type' => $headers['type'] ?? 'image/jpeg',
+            'dir' => $url,
+            'thumb_path' => $url,
+            'time' => time(),
+        ];
+    }
+}

+ 43 - 17
app/admin/controller/widget/Images.php

@@ -89,6 +89,21 @@ class Images extends AuthController
         }
     }
 
+    public function upload_by_url()
+    {
+        $url = $this->request->param('url');
+        $pid = $this->request->param('pid', session('pid'));
+        $upload_type = 5; //$this->request->get('upload_type', sys_config('upload_type', 1));
+        $fileInfo = getUrlImgInfo($url);
+        if ($fileInfo) {
+            $ok = SystemAttachmentModel::attachmentAdd($fileInfo['name'], $fileInfo['size'], $fileInfo['type'], $fileInfo['dir'], $fileInfo['thumb_path'], $pid, $upload_type, $fileInfo['time']);
+            if ($ok) {
+                return JsonService::successful('上传成功', ['src' => $url]);
+            }
+        }
+        return JsonService::fail('上传失败');
+    }
+
     /**
      * ajax 提交删除
      */
@@ -97,7 +112,7 @@ class Images extends AuthController
         $request = app('request');
         $post = $request->post();
         if (empty($post['imageid']))
-            Json::fail('还没选择要删除的图片呢?');
+            Json::fail('还没选择要删除的图片');
         foreach ($post['imageid'] as $v) {
             if ($v) self::deleteimganddata($v);
         }
@@ -109,31 +124,42 @@ class Images extends AuthController
      */
     public function deleteimganddata($att_id)
     {
-
         $attinfo = SystemAttachmentModel::get($att_id);
         if ($attinfo) {
-            try {
-                $upload = new Upload((int)$attinfo['image_type'], [
-                    'accessKey' => sys_config('accessKey'),
-                    'secretKey' => sys_config('secretKey'),
-                    'uploadUrl' => sys_config('uploadUrl'),
-                    'storageName' => sys_config('storage_name'),
-                    'storageRegion' => sys_config('storage_region'),
-                ]);
-                if ($attinfo['image_type'] == 1) {
-                    if (strpos($attinfo['att_dir'], '/') == 0) {
-                        $attinfo['att_dir'] = substr($attinfo['att_dir'], 1);
+            if ($attinfo['image_type'] > 0 && $attinfo['image_type'] < 5) {
+                try {
+                    $upload = new Upload((int)$attinfo['image_type'], [
+                        'accessKey' => sys_config('accessKey'),
+                        'secretKey' => sys_config('secretKey'),
+                        'uploadUrl' => sys_config('uploadUrl'),
+                        'storageName' => sys_config('storage_name'),
+                        'storageRegion' => sys_config('storage_region'),
+                    ]);
+                    if ($attinfo['image_type'] == 1) {
+                        if (strpos($attinfo['att_dir'], '/') == 0) {
+                            $attinfo['att_dir'] = substr($attinfo['att_dir'], 1);
+                        }
+                        $upload->delete($attinfo['att_dir']);
+                    } else {
+                        $upload->delete($attinfo['name']);
                     }
-                    $upload->delete($attinfo['att_dir']);
-                } else {
-                    $upload->delete($attinfo['name']);
+                } catch (\Throwable $e) {
                 }
-            } catch (\Throwable $e) {
             }
+
             SystemAttachmentModel::where('att_id', $att_id)->delete();
         }
     }
 
+    public function window_upload_by_url()
+    {
+        $formbuider = [];
+        $formbuider[] = Form::input('url', 'url');
+        $form = Form::make_post_form('输入图片url', $formbuider, Url::buildUrl('upload_by_url'));
+        $this->assign(compact('form'));
+        return $this->fetch('public/form-builder');
+    }
+
     /**
      * 移动图片分类显示
      */

+ 16 - 9
app/admin/view/widget/images.php

@@ -117,7 +117,8 @@
                         <div class="layadmin-homepage-pad-ver" style="text-align: left">
                             <div class="layui-btn-group">
                                 <button type="button" class="layui-btn layui-btn-normal layui-btn-sm"  @click="addCategory">添加分类</button>
-                                <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" ref="upload">上传图片</button>
+                                <button type="button" class="layui-btn layui-btn-normal layui-btn-sm"  @click="uploadImgByUrl">远程上传</button>
+                                <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" ref="upload">本地上传</button>
                                 <button type="button" class="layui-btn layui-btn-warm layui-btn-sm" :class="selectImages.length ? '':'layui-btn-disabled' " @click="moveCate">移动分类</button>
                                 <button type="button" class="layui-btn layui-btn-danger layui-btn-sm" :class="selectImages.length ? '':'layui-btn-disabled' " @click="delImage">删除图片</button>
 
@@ -196,6 +197,12 @@
                     })
                 })
             },
+
+            // 上传远程图片
+            uploadImgByUrl: function() {
+                return this.getOpenWindow('远程上传',this.U({a:'window_upload_by_url'}), {w:768, h:300});
+            },
+
             //移动图片分类
             moveCate:function(){
                 if(!this.selectImages.length) return;
@@ -407,15 +414,15 @@
             SuccessCateg:function () {
                 this.getCategoryList();
             },
-            uploal:function () {
+            upload:function () {
                 var that=this;
                 this.uploadInst=layList.upload.render({
-                    elem: this.$refs.upload
-                    ,url: this.U({a:'upload'})+'?pid='+this.pid
-                    ,multiple: true
-                    ,auto:true
-                    ,size: 2097152 //限制文件大小,单位 KB
-                    ,done: function(res){
+                    elem: this.$refs.upload,
+                    url: this.U({a:'upload'})+'?pid='+this.pid,
+                    multiple: true,
+                    auto:true,
+                    size: 2097152, //限制文件大小,单位 KB
+                    done: function(res){
                         layList.layer.msg(res.msg,{time:3000});
                         that.getImageList();
                     }
@@ -426,7 +433,7 @@
             this.getCategoryList();
             this.getImageList();
             window.SuccessCateg = this.SuccessCateg;
-            this.uploal();
+            this.upload();
         }
     })
 

+ 2 - 1
public/install/crmeb.sql

@@ -6371,7 +6371,8 @@ CREATE TABLE IF NOT EXISTS `eb_system_attachment` (
   `time` int(11) NOT NULL DEFAULT 0 COMMENT '上传时间',
   `image_type` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '图片上传类型 1本地 2七牛云 3OSS 4COS ',
   `module_type` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '图片上传模块类型 1 后台上传 2 用户生成',
-  PRIMARY KEY (`att_id`) USING BTREE
+  PRIMARY KEY (`att_id`) USING BTREE,
+  UNIQUE KEY `att_dir` (`att_dir`) USING BTREE
 ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COMMENT='附件管理表' AUTO_INCREMENT=1204 ;
 
 --