作者 赵彬吉

Merge branch 'master' into develop

@@ -140,7 +140,7 @@ class ProjectUpdate extends Command @@ -140,7 +140,7 @@ class ProjectUpdate extends Command
140 'seo_keywords' => $item['seo_keywords'] ?? '', 140 'seo_keywords' => $item['seo_keywords'] ?? '',
141 'seo_description' => $item['seo_description'] ?? '', 141 'seo_description' => $item['seo_description'] ?? '',
142 ]); 142 ]);
143 - $route = RouteMap::setRoute($this->get_url_route($item['url']) ?: $item['name'], RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $project_id); 143 + $route = $this->set_map($this->get_url_route($item['url']), RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $project_id);
144 $model->edit(['route' => $route], ['id' => $id]); 144 $model->edit(['route' => $route], ['id' => $id]);
145 } catch (\Exception $e) { 145 } catch (\Exception $e) {
146 echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL; 146 echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
@@ -273,7 +273,7 @@ class ProjectUpdate extends Command @@ -273,7 +273,7 @@ class ProjectUpdate extends Command
273 ]), 273 ]),
274 'status' => Product::STATUS_ON 274 'status' => Product::STATUS_ON
275 ]); 275 ]);
276 - $route = RouteMap::setRoute($this->get_url_route($item['url']) ?: $item['ttile'], RouteMap::SOURCE_PRODUCT, $id, $project_id); 276 + $route = $this->set_map($this->get_url_route($item['url']), RouteMap::SOURCE_PRODUCT, $id, $project_id);
277 $model->edit(['route' => $route], ['id' => $id]); 277 $model->edit(['route' => $route], ['id' => $id]);
278 } catch (\Exception $e) { 278 } catch (\Exception $e) {
279 echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL; 279 echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
@@ -328,7 +328,7 @@ class ProjectUpdate extends Command @@ -328,7 +328,7 @@ class ProjectUpdate extends Command
328 'image' => $item['images'][0] ?? '', 328 'image' => $item['images'][0] ?? '',
329 'status' => $api_type == 'news' ? News::STATUS_ONE : Blog::STATUS_ONE 329 'status' => $api_type == 'news' ? News::STATUS_ONE : Blog::STATUS_ONE
330 ]); 330 ]);
331 - $route = RouteMap::setRoute($this->get_url_route($item['url']) ?: $item['ttile'], $api_type == 'news' ? RouteMap::SOURCE_NEWS : RouteMap::SOURCE_BLOG, $id, $project_id); 331 + $route = $this->set_map($this->get_url_route($item['url']), $api_type == 'news' ? RouteMap::SOURCE_NEWS : RouteMap::SOURCE_BLOG, $id, $project_id);
332 $model->edit(['url' => $route], ['id' => $id]); 332 $model->edit(['url' => $route], ['id' => $id]);
333 } catch (\Exception $e) { 333 } catch (\Exception $e) {
334 echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL; 334 echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
@@ -377,7 +377,7 @@ class ProjectUpdate extends Command @@ -377,7 +377,7 @@ class ProjectUpdate extends Command
377 'html' => $item['content'] ?? '', 377 'html' => $item['content'] ?? '',
378 'status' => 1 378 'status' => 1
379 ]); 379 ]);
380 - $route = RouteMap::setRoute($this->get_url_route($item['url']) ?: $item['ttile'], RouteMap::SOURCE_PAGE, $id, $project_id); 380 + $route = $this->set_map($this->get_url_route($item['url']), RouteMap::SOURCE_PAGE, $id, $project_id);
381 $model->edit(['url' => $route], ['id' => $id]); 381 $model->edit(['url' => $route], ['id' => $id]);
382 } catch (\Exception $e) { 382 } catch (\Exception $e) {
383 echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL; 383 echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
@@ -465,7 +465,7 @@ class ProjectUpdate extends Command @@ -465,7 +465,7 @@ class ProjectUpdate extends Command
465 'keywords' => $item['keywords'] ?? '', 465 'keywords' => $item['keywords'] ?? '',
466 'describe' => $item['description'] ?? '' 466 'describe' => $item['description'] ?? ''
467 ]); 467 ]);
468 - $route = RouteMap::setRoute($this->get_url_route($item['url']) ?: $item['name'], RouteMap::SOURCE_PRODUCT_CATE, $parent_id, $project_id); 468 + $route = $this->set_map($this->get_url_route($item['url']), RouteMap::SOURCE_PRODUCT_CATE, $parent_id, $project_id);
469 $model->edit(['route' => $route], ['id' => $parent_id]); 469 $model->edit(['route' => $route], ['id' => $parent_id]);
470 } catch (\Exception $e) { 470 } catch (\Exception $e) {
471 echo 'date:' . date('Y-m-d H:i:s') . ', category_insert error: ' . $e->getMessage() . PHP_EOL; 471 echo 'date:' . date('Y-m-d H:i:s') . ', category_insert error: ' . $e->getMessage() . PHP_EOL;
@@ -516,4 +516,28 @@ class ProjectUpdate extends Command @@ -516,4 +516,28 @@ class ProjectUpdate extends Command
516 516
517 return $str; 517 return $str;
518 } 518 }
  519 +
  520 + //路由入库
  521 + protected function set_map($route, $source, $source_id, $project_id)
  522 + {
  523 + if (empty($route)) {
  524 + return '';
  525 + }
  526 +
  527 + $route_map = new RouteMap();
  528 + $route_map->project_id = $project_id;
  529 + $route_map->source = $source;
  530 + $route_map->source_id = $source_id;
  531 + $route_map->route = $route;
  532 +
  533 + if ($source == RouteMap::SOURCE_NEWS) {
  534 + $route_map->path = RouteMap::SOURCE_NEWS;
  535 + } elseif ($source == RouteMap::SOURCE_BLOG) {
  536 + $route_map->path = RouteMap::SOURCE_BLOG;
  537 + }
  538 +
  539 + $route_map->save();
  540 +
  541 + return $route;
  542 + }
519 } 543 }
@@ -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 //排名数据