field('id,name,type,num,img,unit,price,min_price,status,cost_price,wholesale')->select(); $this->success('请求成功', $list); } /** * 获取产品分类列表 */ public function getProducttypeList() { $name = input('name'); $where=[]; if ($name) { $where['name'] = ['like', "%{$name}%"]; } $list = Producttype::where($where)->field('id,name')->select(); $this->success('请求成功', $list); } /** * 获取产品配置列表 */ public function getPartList() { $product_id = input('product_id'); $name = input('name'); $where = []; if ($name) { $where['name'] = ['like', "%{$name}%"]; } $where['product_id'] = $product_id; $list = ProductPart::where($where)->field('id,name,img,description')->select(); $this->success('请求成功', $list); } /** * 获取产品详情 */ public function getProductDetail() { $id = input('id'); $product = ProductModel::where(['id' => $id])->find(); if (empty($product)) { $this->error('产品不存在'); } preg_match_all("/()/is", $product['description'], $matchpic); foreach ($matchpic[2] as $url) { $img = cdnurl($url, true); $product['description'] = str_replace($url, $img, $product['description']); } $this->success('请求成功', $product); } /** * 添加产品 */ public function addProduct() { $params = $this->request->post(); if ($params) { $parts = $params['parts'] ?? []; $model = new ProductModel(); $model->allowField(true)->save($params); $lastid = $model->getLastInsID(); $partModel = new ProductPart(); foreach ($parts as &$v) { $v['product_id'] = $lastid; } $partModel->allowField(true)->saveAll($parts); $this->success('添加产品成功'); } } /** * 添加产品配件 */ public function addProductPart() { $params = $this->request->post(); if ($params) { $partModel = new ProductPart(); $partModel->save($params); $this->success('添加产品配置成功'); } } /** * 修改产品信息 */ public function editProduct() { $params = $this->request->post(); if ($params) { $model = new ProductModel(); $row = $model->get($params['id']); if (empty($row)) { $this->error('产品不存在'); } $row->allowField(true)->save($params); $this->success('添加产品成功'); } } /** * 修改产品配件 */ public function editProductPart() { $params = $this->request->post(); if ($params) { $partModel = new ProductPart(); $row = $partModel->get($params['id']); if (empty($row)) { $this->error('产品配置不存在'); } $row->allowField(true)->save($params); $this->success('修改产品配置成功'); } } }