diff --git a/application/admin/command/CarImport.php b/application/admin/command/CarImport.php new file mode 100644 index 0000000..c71dd9c --- /dev/null +++ b/application/admin/command/CarImport.php @@ -0,0 +1,66 @@ +setName('car:import') + ->setDescription('导入常见汽车品牌和系列到 brands 与 series 表'); + } + + protected function execute(Input $input, Output $output) + { + $brands = [ + '奔驰' => ['C级', 'E级', 'S级', 'GLC', 'GLE', 'GLS'], + '宝马' => ['3系', '5系', '7系', 'X1', 'X3', 'X5'], + '奥迪' => ['A3', 'A4', 'A6', 'A8', 'Q3', 'Q5', 'Q7'], + '大众' => ['朗逸', '速腾', '宝来', '迈腾', '帕萨特', '途观', '途昂'], + '丰田' => ['卡罗拉', '凯美瑞', '汉兰达', 'RAV4', '普拉多'], + '本田' => ['思域', '雅阁', 'CR-V', 'XR-V', '奥德赛'], + '日产' => ['轩逸', '天籁', '奇骏', '逍客'], + '特斯拉' => ['Model 3', 'Model Y', 'Model S', 'Model X'], + '福特' => ['福克斯', '蒙迪欧', '锐界', '探险者'], + '比亚迪' => ['汉', '唐', '宋', '秦', '元PLUS'] + ]; + + $now = date('Y-m-d H:i:s'); + $insertedBrands = 0; + $insertedSeries = 0; + + foreach ($brands as $brandName => $seriesList) { + // 避免重复插入 + $exists = Db::name('brands')->where('name', $brandName)->find(); + if ($exists) { + $brandId = $exists['id']; + } else { + $brandId = Db::name('brands')->insertGetId([ + 'name' => $brandName, + 'sort_order' => 0, + 'created_at' => $now + ]); + $insertedBrands++; + } + + foreach ($seriesList as $seriesName) { + $existsSeries = Db::name('series')->where('brand_id', $brandId)->where('name', $seriesName)->find(); + if (!$existsSeries) { + Db::name('series')->insert([ + 'brand_id' => $brandId, + 'name' => $seriesName, + 'created_at' => $now + ]); + $insertedSeries++; + } + } + } + + $output->writeln("✅ 导入完成:{$insertedBrands} 个品牌,{$insertedSeries} 个系列"); + } +} diff --git a/application/admin/common.php b/application/admin/common.php index f3d1e99..e37c95b 100755 --- a/application/admin/common.php +++ b/application/admin/common.php @@ -194,4 +194,20 @@ if (!function_exists('build_heading')) { } return $result; } + if (!function_exists('dd')) { + /** + * 检测IP是否允许 + * @param string $ip IP地址 + */ + function dd(...$vars) + { + $counter = 1; + foreach ($vars as $var) { + echo "Variable {$counter}:\n"; + dump($var); + $counter++; + } + die; + } + } } diff --git a/application/admin/controller/Series.php b/application/admin/controller/Series.php index 516b622..7f891e2 100644 --- a/application/admin/controller/Series.php +++ b/application/admin/controller/Series.php @@ -23,6 +23,12 @@ class Series extends Backend parent::_initialize(); $this->model = new \app\admin\model\Series; + $data = \app\admin\model\Brands::order('sort_order')->field('id,name')->select(); + $brands = []; + foreach ($data as $datum){ + $brands [] = $datum->toArray(); + } + $this->view->assign('brands',$brands); } @@ -33,5 +39,27 @@ class Series extends Backend * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ + protected $relationSearch = true; + + public function index() + { + //设置过滤方法 + $this->request->filter(['strip_tags', 'trim']); + if (false === $this->request->isAjax()) { + return $this->view->fetch(); + } + //如果发送的来源是 Selectpage,则转发到 Selectpage + if ($this->request->request('keyField')) { + return $this->selectpage(); + } + [$where, $sort, $order, $offset, $limit] = $this->buildparams(); + $list = $this->model + ->where($where) + ->order($sort, $order) + ->with('brand') + ->paginate($limit); + $result = ['total' => $list->total(), 'rows' => $list->items()]; + return json($result); + } } diff --git a/application/admin/lang/zh-cn/attributes.php b/application/admin/lang/zh-cn/attributes.php new file mode 100644 index 0000000..68e566a --- /dev/null +++ b/application/admin/lang/zh-cn/attributes.php @@ -0,0 +1,11 @@ + '属性名称', + 'Field_key' => '标识', + 'Options' => '配置', + 'Input_type' => '类型', + 'Is_filter' => '是否参与筛选', + 'Created_at' => '创建时间', + 'Sort_order' => '排序' +]; diff --git a/application/admin/lang/zh-cn/brands.php b/application/admin/lang/zh-cn/brands.php index ca5d8ed..3f6c7d2 100644 --- a/application/admin/lang/zh-cn/brands.php +++ b/application/admin/lang/zh-cn/brands.php @@ -1,5 +1,8 @@ '名称', + 'Logo' => 'Logo', + 'Sort_order' => '排序', + 'Created_at' => '创建时间', ]; diff --git a/application/admin/lang/zh-cn/car/attributes.php b/application/admin/lang/zh-cn/car/attributes.php deleted file mode 100644 index 91a65fc..0000000 --- a/application/admin/lang/zh-cn/car/attributes.php +++ /dev/null @@ -1,8 +0,0 @@ - '属性名称,例如 排量', - 'Field_key' => '唯一标识,例如 displacement', - 'Options' => 'JSON数组:如 [{label:..., value:...}]', - 'Is_filter' => '是否参与筛选' -]; diff --git a/application/admin/lang/zh-cn/series.php b/application/admin/lang/zh-cn/series.php index ca5d8ed..98fc716 100644 --- a/application/admin/lang/zh-cn/series.php +++ b/application/admin/lang/zh-cn/series.php @@ -1,5 +1,5 @@ '创建时间' ]; diff --git a/application/admin/model/Series.php b/application/admin/model/Series.php index 4920aa4..3bd181f 100644 --- a/application/admin/model/Series.php +++ b/application/admin/model/Series.php @@ -29,6 +29,10 @@ class Series extends Model ]; + public function brand() + { + return $this->belongsTo(Brands::class,'brand_id')->setEagerlyType(0); + } diff --git a/application/admin/view/brands/add.html b/application/admin/view/brands/add.html index 368de0a..e9b8877 100644 --- a/application/admin/view/brands/add.html +++ b/application/admin/view/brands/add.html @@ -9,7 +9,17 @@