Pārlūkot izejas kodu

fix: 图片窗口不支持搜索

joe 3 gadi atpakaļ
vecāks
revīzija
f16306a7fb

+ 25 - 15
app/admin/controller/widget/Images.php

@@ -6,7 +6,8 @@ use app\admin\controller\AuthController;
 use app\admin\controller\store\StoreProduct;
 use think\facade\Route as Url;
 use app\admin\model\system\{
-    SystemAttachment as SystemAttachmentModel, SystemAttachmentCategory as Category
+    SystemAttachment as SystemAttachmentModel,
+    SystemAttachmentCategory as Category
 };
 use crmeb\services\{JsonService as Json, JsonService, UtilService as Util, FormBuilder as Form};
 use crmeb\services\upload\Upload;
@@ -37,15 +38,15 @@ class Images extends AuthController
             $this->assign('pid', $res['pid']);
             $this->assign('ppid', $res['ppid']);
         }
-       
-////       //TODO 分类标题
-//       $typearray = Category::getAll();
-//       $this->assign(compact('typearray'));
-//       $this->assign(SystemAttachmentModel::getAll($pid));
+
+        ////       //TODO 分类标题
+        //       $typearray = Category::getAll();
+        //       $this->assign(compact('typearray'));
+        //       $this->assign(SystemAttachmentModel::getAll($pid));
         return $this->fetch('widget/images');
     }
 
-    /**获取图片列表
+    /**@api {get} widget.images/get_image_list/pid/:pid/page/:page/limit/:limit 获取图片列表
      *
      */
     public function get_image_list()
@@ -58,12 +59,24 @@ class Images extends AuthController
         return Json::successful(SystemAttachmentModel::getImageList($where));
     }
 
-    /**获取分类
-     * @param string $name
+    /**
+     * @api {get} /widget.images/get_image_cate/name/:name?name= 获取分类
+     * 
      */
     public function get_image_cate($name = '')
     {
-        return Json::successful(Category::getAll($name));
+        $data = $name ? Category::searchBy($name) : Category::getAll();
+        return Json::successful($data);
+    }
+
+    /**
+     * @api {get} /widget.images/search_image_cate/name/:name?name=''
+     * 
+     */
+    public function search_image_cate(string $name = '')
+    {
+        $data = $name ? Category::searchBy($name) : Category::getAll();
+        return Json::successful($data);
     }
 
     /**
@@ -165,7 +178,7 @@ class Images extends AuthController
         $pid = $this->request->param('pid', session('pid'));
         $formbuider = [];
         $formbuider[] = Form::input('url', 'url');
-        $form = Form::make_post_form('输入图片url', $formbuider, Url::buildUrl('upload_by_url', ['pid'=>$pid]));
+        $form = Form::make_post_form('输入图片url', $formbuider, Url::buildUrl('upload_by_url', ['pid' => $pid]));
         $this->assign(compact('form'));
         return $this->fetch('public/form-builder');
     }
@@ -216,7 +229,7 @@ class Images extends AuthController
     {
         $formbuider = [];
         $formbuider[] = Form::select('pid', '上级分类', (string)$id)->setOptions(function () {
-            $list = Category::getCateList(0,1);
+            $list = Category::getCateList(0, 1);
             $options = [['value' => 0, 'label' => '所有分类']];
             foreach ($list as $id => $cateName) {
                 $options[] = ['label' => $cateName['html'] . $cateName['name'], 'value' => $cateName['id']];
@@ -249,7 +262,6 @@ SCRIPT;
             Json::successful('添加成功');
         else
             Json::fail('添加失败!');
-
     }
 
     /**
@@ -308,7 +320,5 @@ SCRIPT;
             return Json::successful('删除成功!');
         else
             return Json::fail('删除失败');
-
-
     }
 }

+ 60 - 7
app/admin/model/system/SystemAttachmentCategory.php

@@ -55,18 +55,72 @@ class SystemAttachmentCategory extends BaseModel
      * @throws \think\db\exception\ModelNotFoundException
      * @throws \think\exception\DbException
      */
-    public static function getAll($name)
+    public static function getAll()
     {
         $model = new self;
-        if ($name) $model = $model->where('name', 'LIKE', "%$name%");
-        return self::tidyMenuTier($model->select(), 0);
+        $res = $model->select()->toArray();
+        // print_r($res->toArray());
+        return self::tidyMenuTier($res);
     }
 
-    public static function tidyMenuTier($menusList, $pid = 0, $navList = [])
+    public static function searchBy(string $name): array
     {
+        $model = new self;
+        if ($name) {
+            $model = $model->where('name', 'LIKE', "%$name%");
+        }
+        $res = $model->select()->toArray();
+        // print_r($res->toArray());
+        return self::tidyMenuTier2($res);
+    }
 
+    /**
+     * 本函数支持图片框二级目录搜索
+     * 
+     * 1. 分类目录最多支持两级
+     * 2. 根据 like 的检索结果,如果为根级别,则自动检索出所有子集;如果为子级别,则自动检索出上级
+     * 3. 为保证性能,like 的结果只取 3 条记录
+     * 
+     * TODO: 注意这里有一个循环 SQL
+     */
+    public static function tidyMenuTier2($menusList)
+    {
+        $navList = [];
+        $pids = [];
+        $children = [];
+        foreach ($menusList as $k => $menu) {
+            $navList[] = $menu;
+            if ($menu['pid'] == 0) {
+                $pids[] = $menu['id'];
+            } else {
+                $children[] = $menu['pid'];
+            }
+        }
+
+        // retrieve all children layer
+        if ($pids || $children) {
+            if (!$pids) {
+                $pids = [-1]; // some skills.
+            } else if (!$children) {
+                $children = [-1];
+            }
+            $attachmentCates = SystemAttachmentCategory::where('pid', 'in', implode(',', $pids))
+                ->whereOr('id', 'in', implode(',', $children))
+                ->select()->toArray();
+            // echo SystemAttachmentCategory::getLastSql();
+            foreach ($attachmentCates as $attchmentCate) {
+                $navList[] = $attchmentCate;
+            }
+        }
+
+        // print_r($navList);
+        return self::tidyMenuTier($navList);
+    }
+
+
+    public static function tidyMenuTier($menusList, $pid = 0, $navList = [])
+    {
         foreach ($menusList as $k => $menu) {
-            $menu = $menu->getData();
             if ($menu['pid'] == $pid) {
                 unset($menusList[$k]);
                 $menu['child'] = self::tidyMenuTier($menusList, $menu['id']);
@@ -108,5 +162,4 @@ class SystemAttachmentCategory extends BaseModel
         $where['att_id'] = $att_id;
         return $model->where($where)->select()->toArray()[0];
     }
-
-}
+}

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

@@ -44,7 +44,7 @@
                     <div class="layui-card-header">
                         <div class="layui-unselect layui-form-select layui-form-selected">
                             <div class="layui-select-title">
-                                <input type="text" name="title" v-model="searchTitle" placeholder="搜索分类" style="height: 24px;line-height:24px;padding-left:7px;font-size: 12px;display: inline;padding-right: 0;width: 100%;" autocomplete="off" class="layui-input layui-input-search" @keydown="search">
+                                <input type="text" name="title" v-model="searchTitle" placeholder="搜索分类" style="height: 24px;line-height:24px;padding-left:7px;font-size: 12px;display: inline;padding-right: 0;width: 100%;" autocomplete="off" class="layui-input layui-input-search" @keyup="search">
                                 <!--                                <i class="layui-icon layui-icon-search" @click="search"  style="cursor: pointer;margin:0 3px;"></i>-->
                             </div>
                         </div>
@@ -325,8 +325,14 @@
                 });
             },
             //查询分类
-            search:function(){
-//                if(!this.searchTitle) return layList.msg('请输入搜索内容!');
+            search:function(ev){
+                if(ev.keyCode !== 13) {
+                    return
+                }
+                // let that = this;
+                // layList.baseGet(that.U({a:'search_image_cate',q:{name:this.searchTitle}}),function (res) {
+                //     that.$set(that,'categoryList',res.data);
+                // })
                 this.getCategoryList();
             },
             //打开和关闭树形