作者 赵彬吉

nav import

@@ -116,7 +116,13 @@ class NavController extends BaseController @@ -116,7 +116,13 @@ class NavController extends BaseController
116 * @author zbj 116 * @author zbj
117 * @date 2023/11/21 117 * @date 2023/11/21
118 */ 118 */
119 - public function import(){  
120 - 119 + public function import(NavLogic $navLogic){
  120 + $this->request->validate([
  121 + 'id'=>'required',
  122 + ],[
  123 + 'id.required' => 'ID不能为空',
  124 + ]);
  125 + $navLogic->importNav();
  126 + $this->response('success');
121 } 127 }
122 } 128 }
@@ -116,4 +116,79 @@ class NavLogic extends BaseLogic @@ -116,4 +116,79 @@ class NavLogic extends BaseLogic
116 } 116 }
117 return $this->success(); 117 return $this->success();
118 } 118 }
  119 +
  120 +
  121 +
  122 + /**
  123 + * 一键导入 对应分类
  124 + * @throws \App\Exceptions\AsideGlobalException
  125 + * @throws \App\Exceptions\BsideGlobalException
  126 + */
  127 + public function importNav(){
  128 + $nav = $this->getInfo($this->param['id']);
  129 + if(!in_array($nav['url'], array_keys(BNav::ableImportMap()))){
  130 + $this->fail('该菜单不支持一键导入');
  131 + }
  132 +
  133 + $class = BNav::ableImportMap($nav['url']);
  134 + $category = new $class();
  135 + $fields = ['id', 'name', 'pid', 'alias'];
  136 + if($nav['url'] == 'products'){
  137 + $fields = ['id', 'title as name', 'pid', 'route as alias'];
  138 + }
  139 + $this->addByPid($category, $nav, $fields);
  140 + return $this->success();
  141 + }
  142 +
  143 + /**
  144 + * 一级一级的加
  145 + * @author zbj
  146 + * @date 2023/11/22
  147 + */
  148 + protected function addByPid($category, $nav, $fields, $pid = 0)
  149 + {
  150 + $nav_pid = $nav['id'];
  151 + if($pid){
  152 + $p_cate = $category->where('id', $pid)->select($fields)->first();
  153 + if($p_cate){
  154 + $nav_pid = $this->model->where('import_id', $nav['id'])->where('url', $p_cate['alias'])->value('id');
  155 + }
  156 + }
  157 + $list = $category->list(['pid' => $pid], 'id', $fields, 'asc');
  158 + $data = [];
  159 + $time = date('Y-m-d H:i:s');
  160 + foreach ($list as $item) {
  161 + $exists_info = $this->model->where('import_id', $nav['id'])->where('url', $item['alias'])->first();
  162 + if($exists_info){
  163 + continue;
  164 + }
  165 + $data[] = [
  166 + 'pid' => $nav_pid,
  167 + 'name' => $item['name'],
  168 + 'url' => $item['alias'],
  169 + 'project_id' => $this->project['id'],
  170 + 'location' => $nav['location'],
  171 + 'group_id' => $nav['group_id'],
  172 + 'status' => BNav::STATUS_ACTIVE,
  173 + 'import_id' => $nav['id'],
  174 + 'created_at' => $time,
  175 + 'updated_at' => $time,
  176 + ];
  177 + }
  178 + //每500条更一次
  179 + $data_chunk = array_chunk($data,500);
  180 + foreach ($data_chunk as $chunk){
  181 + $this->model->insert($chunk);
  182 + }
  183 +
  184 + foreach ($list as $item) {
  185 + $this->addByPid($category, $nav, $fields, $item['id']);
  186 + }
  187 + }
  188 +
  189 +
  190 +
  191 +
  192 +
  193 +
119 } 194 }
@@ -35,6 +35,21 @@ class BNav extends Base @@ -35,6 +35,21 @@ class BNav extends Base
35 const STATUS_DISABLED = 0; 35 const STATUS_DISABLED = 0;
36 36
37 37
  38 + /**
  39 + * @author zbj
  40 + * @date 2023/11/22
  41 + */
  42 + public static function ableImportMap($url=''){
  43 + $map = [
  44 + 'products' => '\\App\\Models\\Product\\Category',
  45 + 'news' => '\\App\\Models\\News\\NewsCategory',
  46 + 'blog' => '\\App\\Models\\Blog\\BlogCategory',
  47 + ];
  48 + if ($url){
  49 + return $map[$url] ?:"";
  50 + }
  51 + return $map;
  52 + }
38 53
39 54
40 /** 55 /**
@@ -85,7 +100,7 @@ class BNav extends Base @@ -85,7 +100,7 @@ class BNav extends Base
85 */ 100 */
86 public function getAbleImportAttribute($value) 101 public function getAbleImportAttribute($value)
87 { 102 {
88 - if(in_array($this->url, ['products','news','blogs'])){ 103 + if(in_array($this->url, array_keys(self::ableImportMap()))){
89 return 1; 104 return 1;
90 } 105 }
91 return 0; 106 return 0;
@@ -365,6 +365,7 @@ Route::middleware(['bloginauth'])->group(function () { @@ -365,6 +365,7 @@ Route::middleware(['bloginauth'])->group(function () {
365 Route::delete('/delete', [\App\Http\Controllers\Bside\Nav\NavController::class, 'delete'])->name('nav_delete'); 365 Route::delete('/delete', [\App\Http\Controllers\Bside\Nav\NavController::class, 'delete'])->name('nav_delete');
366 Route::get('/default-urls', [\App\Http\Controllers\Bside\Nav\NavController::class, 'urls'])->name('nav_default-urls'); 366 Route::get('/default-urls', [\App\Http\Controllers\Bside\Nav\NavController::class, 'urls'])->name('nav_default-urls');
367 Route::post('/sort', [\App\Http\Controllers\Bside\Nav\NavController::class, 'sort'])->name('nav_sort'); 367 Route::post('/sort', [\App\Http\Controllers\Bside\Nav\NavController::class, 'sort'])->name('nav_sort');
  368 + Route::post('/import', [\App\Http\Controllers\Bside\Nav\NavController::class, 'import'])->name('nav_import');
368 }); 369 });
369 370
370 //排名数据 371 //排名数据